Difference between revisions of "User:Saul/eslint"

From Organic Design wiki
m
m (Update ruleset and packages.)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Linting is a way to ensure your code follows convention.<br>
 
Linting is a way to ensure your code follows convention.<br>
There is a package for the text editor atom (atom.io) that intergrates linting, the package is called: "linter-eslint"<br>
 
 
The linting config file is stored as ".eslintrc.js"<br>
 
The linting config file is stored as ".eslintrc.js"<br>
Here are the dependencies needed for linting vue:
 
 
<source lang="bash">
 
<source lang="bash">
npm i -D babel-eslint eslint eslint-config-standard eslint-plugin-html eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard
+
npm i --save-dev eslint
 +
 
 +
# If using Vue
 +
npm i --save-dev eslint-plugin-html
 +
 
 +
# If using TypeScript
 +
npm i --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser
 
</source>
 
</source>
 
Here is the rule set I like to use:
 
Here is the rule set I like to use:
 
<source lang="javascript">
 
<source lang="javascript">
 
module.exports = {
 
module.exports = {
root: true,
+
// If using typescript:
parser: "babel-eslint",
+
//parser: "@typescript-eslint/parser",
 +
 
 
parserOptions: {
 
parserOptions: {
sourceType: "module"
+
ecmaVersion: "latest"
 
},
 
},
 
env: {
 
env: {
 +
// For nodejs
 +
commonjs: true,
 +
es2021: true,
 +
node: true
 +
 +
// For browser:
 
browser: true,
 
browser: true,
 
},
 
},
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
+
extends: "eslint:recommended",
extends: "standard",
 
 
// required to lint *.vue files
 
// required to lint *.vue files
plugins: [
+
// plugins: [ "html" ],
"html"
 
],
 
 
rules: {
 
rules: {
 +
curly: [1, "all"],
 
// disallow single quotes
 
// disallow single quotes
quotes: ["error", "double"],
+
quotes: [1, "double", { allowTemplateLiterals: true }],
 
// force semi-colons
 
// force semi-colons
semi: ["error", "always"],
+
semi: 1,
 
// allow tabs
 
// allow tabs
no-tabs: [0],
+
"no-tabs": [0],
 
// use tab indentation
 
// use tab indentation
indent: ["error", "tab"],
+
indent: [1, "tab", {
 +
SwitchCase: 1
 +
}],
 +
// prevent commar dangles
 +
"comma-dangle": [1, "never"],
 
// allow paren-less arrow functions
 
// allow paren-less arrow functions
arrow-parens: 0,
+
"arrow-parens": 0,
 
// allow async-await
 
// allow async-await
generator-star-spacing: 0,
+
"generator-star-spacing": 0,
 +
"no-unused-vars": [1, { args: "after-used", vars: "local" }],
 +
"no-constant-condition": 0,
 
// allow debugger during development
 
// allow debugger during development
no-debugger: process.env.NODE_ENV === "production" ? 2 : 0
+
"no-debugger": process.env.NODE_ENV === "production" ? 2 : 0
 
}
 
}
 
};
 
};
 
</source>
 
</source>

Latest revision as of 02:40, 26 August 2022

Linting is a way to ensure your code follows convention.
The linting config file is stored as ".eslintrc.js"

npm i --save-dev eslint

# If using Vue
npm i --save-dev eslint-plugin-html

# If using TypeScript
npm i --save-dev @typescript-eslint/eslint-plugin @typescript-eslint/parser

Here is the rule set I like to use:

module.exports = {
	// If using typescript:
	//parser: "@typescript-eslint/parser",

	parserOptions: {
		ecmaVersion: "latest"
	},
	env: {
		// For nodejs
		commonjs: true,
		es2021: true,
		node: true

		// For browser:
		browser: true,
	},
	extends: "eslint:recommended",
	// required to lint *.vue files
	// plugins: [ "html" ],
	rules: {
		curly: [1, "all"],
		// disallow single quotes
		quotes: [1, "double", { allowTemplateLiterals: true }],
		// force semi-colons
		semi: 1,
		// allow tabs
		"no-tabs": [0],
		// use tab indentation
		indent: [1, "tab", {
			SwitchCase: 1
		}],
		// prevent commar dangles
		"comma-dangle": [1, "never"],
		// allow paren-less arrow functions
		"arrow-parens": 0,
		// allow async-await
		"generator-star-spacing": 0,
		"no-unused-vars": [1, { args: "after-used", vars: "local" }],
		"no-constant-condition": 0,
		// allow debugger during development
		"no-debugger": process.env.NODE_ENV === "production" ? 2 : 0
	}
};