Monday, 29 July 2019

lint for typescript

with my below settings, you can have eslint for typescript and integration with VSCode auto fix on save

my package json is:

"@azure/functions": "^1.0.2-beta2",
"@types/jest": "^24.0.15",
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.13.0",
"eslint": "^6.1.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-prettier": "^3.1.0",
"jest": "^24.8.0",
"nock": "^10.0.6",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"typescript": "^3.3.3"

i use eslint and prettier to check format and auto fix.

my settings.json    for VS code

"azureFunctions.deploySubpath": ".",
"azureFunctions.projectLanguage": "TypeScript",
"azureFunctions.projectRuntime": "~2",
"debug.internalConsoleOptions": "neverOpen",
"azureFunctions.preDeployTask": "npm prune",
"eslint.autoFixOnSave": true,
"eslint.validate": [
{"language": "typescript", "autoFix": true },
"editor.formatOnSave": false,

it do auto fix on save.

my .eslintrc.js:

module.exports = {
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
extends: [
parserOptions: {
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: 'module' // Allows for the use of imports
rules: {
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-explicit-any" : "off",
"@typescript-eslint/interface-name-prefix" : "off"

my .prettierrc.js

module.exports = {
semi: true,
trailingComma: 'none',
singleQuote: true,
printWidth: 120,
tabWidth: 4

i found eslint and prettier conflict on some settings, e.g. trailingcomma.  the right place to config it is prettierrs.js.

i also found prettier doesn't support all eslint checks,  for those checks, then i add them into eslintrs.js e.g. interface-name-prefix    typescript doesn't like I prefix for interfaces.

run eslint :

./node_modules/.bin/eslint ./ --ext .ts 

No comments:

Post a comment