@darksheep/eslint 4.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +737 -0
- package/LICENSE +24 -0
- package/README.md +34 -0
- package/package.json +62 -0
- package/src/bin/eslint.cjs +11 -0
- package/src/configs/eslint-base.js +124 -0
- package/src/configs/eslint-complexity.js +28 -0
- package/src/configs/eslint-ignores.js +51 -0
- package/src/configs/eslint-recommended.js +9 -0
- package/src/configs/eslint-style.js +23 -0
- package/src/custom/index.js +11 -0
- package/src/custom/instance-of-array.js +72 -0
- package/src/custom/loose-types.js +204 -0
- package/src/custom/no-useless-expression.js +28 -0
- package/src/custom/sequence-expression.js +21 -0
- package/src/index.js +86 -0
- package/src/plugins/eslint-comments.js +27 -0
- package/src/plugins/import.js +139 -0
- package/src/plugins/jest.js +124 -0
- package/src/plugins/jsdoc.js +70 -0
- package/src/plugins/json.js +37 -0
- package/src/plugins/jsx-a11y.js +100 -0
- package/src/plugins/node.js +129 -0
- package/src/plugins/promise.js +16 -0
- package/src/plugins/react.js +128 -0
- package/src/plugins/regexp.js +13 -0
- package/src/plugins/sca.js +41 -0
- package/src/plugins/security.js +15 -0
- package/src/plugins/sonarjs.js +28 -0
- package/src/plugins/style.js +249 -0
- package/src/plugins/typescript.js +87 -0
- package/src/plugins/unicorn.js +58 -0
- package/src/plugins/unused-imports.js +52 -0
- package/src/types.d.ts +118 -0
- package/src/utilities/editorconfig.js +210 -0
- package/src/utilities/eslint-files.js +65 -0
- package/src/utilities/expand-glob.js +49 -0
- package/src/utilities/filesystem.js +73 -0
- package/src/utilities/make-compat.js +17 -0
- package/src/utilities/package.js +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
|
4
|
+
distribute this software, either in source code form or as a compiled
|
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
|
6
|
+
means.
|
|
7
|
+
|
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
|
9
|
+
of this software dedicate any and all copyright interest in the
|
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
|
11
|
+
of the public at large and to the detriment of our heirs and
|
|
12
|
+
successors. We intend this dedication to be an overt act of
|
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
|
14
|
+
software under copyright law.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
For more information, please refer to <https://unlicense.org>
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# eslint
|
|
2
|
+
|
|
3
|
+
### Creating your `eslint.config.js` file
|
|
4
|
+
|
|
5
|
+
If you're using `"type": "module"` in your `package.json` you create a file like this:
|
|
6
|
+
```js
|
|
7
|
+
import { createConfig } from '@darksheep/eslint';
|
|
8
|
+
export default await createConfig(import.meta.url);
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
If you're using `"type": "commonjs"` or you have not defined `type` in your `package.json` you create a file like this:
|
|
12
|
+
```js
|
|
13
|
+
module.exports = (async () => {
|
|
14
|
+
const { createConfig } = await import('@darksheep/eslint');
|
|
15
|
+
return createConfig(__dirname);
|
|
16
|
+
})();
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
In your terminal you can run:
|
|
20
|
+
```shell
|
|
21
|
+
cat << EOF > eslint.config.js
|
|
22
|
+
import { createConfig } from '@darksheep/eslint';
|
|
23
|
+
export default await createConfig(import.meta.url);
|
|
24
|
+
EOF
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```shell
|
|
28
|
+
cat << EOF > eslint.config.js
|
|
29
|
+
module.exports = (async () => {
|
|
30
|
+
const { createConfig } = await import('@darksheep/eslint');
|
|
31
|
+
return createConfig(__dirname);
|
|
32
|
+
})();
|
|
33
|
+
EOF
|
|
34
|
+
```
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@darksheep/eslint",
|
|
3
|
+
"version": "4.1.3",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"packageManager": "yarn@4.1.0",
|
|
6
|
+
"bin": "./src/bin/eslint.cjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": "^20 || ^21"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/pickles-and-beans/eslint.git"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"githooks": "git config core.hooksPath .githooks"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@types/eslint": "~8.56.0",
|
|
25
|
+
"@types/eslint__eslintrc": "~2.1.1",
|
|
26
|
+
"@types/eslint__js": "~8.42.3",
|
|
27
|
+
"@types/estree": "~1.0.2",
|
|
28
|
+
"@types/node": "~20.11.0",
|
|
29
|
+
"type-fest": "~4.10.0",
|
|
30
|
+
"typescript": "~5.3.2"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@eslint-community/eslint-plugin-eslint-comments": "~4.1.0",
|
|
34
|
+
"@eslint/eslintrc": "~3.0.0",
|
|
35
|
+
"@eslint/js": "~8.56.0",
|
|
36
|
+
"@stylistic/eslint-plugin": "~1.6.0",
|
|
37
|
+
"@typescript-eslint/eslint-plugin": "~7.0.0",
|
|
38
|
+
"@typescript-eslint/parser": "~7.0.0",
|
|
39
|
+
"editorconfig": "~2.0.0",
|
|
40
|
+
"eslint": "~8.56.0",
|
|
41
|
+
"eslint-import-resolver-typescript": "~3.6.1",
|
|
42
|
+
"eslint-plugin-import": "~2.29.0",
|
|
43
|
+
"eslint-plugin-jest": "~27.6.0",
|
|
44
|
+
"eslint-plugin-jsdoc": "~48.0.0",
|
|
45
|
+
"eslint-plugin-jsonc": "~2.13.0",
|
|
46
|
+
"eslint-plugin-jsx-a11y": "~6.8.0",
|
|
47
|
+
"eslint-plugin-n": "~17.0.0-0",
|
|
48
|
+
"eslint-plugin-promise": "~6.1.1",
|
|
49
|
+
"eslint-plugin-react": "~7.33.2",
|
|
50
|
+
"eslint-plugin-regexp": "~2.2.0",
|
|
51
|
+
"eslint-plugin-security": "~2.1.0",
|
|
52
|
+
"eslint-plugin-sonarjs": "~0.24.0",
|
|
53
|
+
"eslint-plugin-unicorn": "~51.0.0",
|
|
54
|
+
"eslint-plugin-unused-imports": "~3.0.0",
|
|
55
|
+
"jsonc-eslint-parser": "~2.4.0"
|
|
56
|
+
},
|
|
57
|
+
"peerDependenciesMeta": {
|
|
58
|
+
"typescript": {
|
|
59
|
+
"optional": true
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/** @type {import('eslint').Linter.RulesRecord} */
|
|
2
|
+
const rules = {
|
|
3
|
+
'no-else-return': 'error',
|
|
4
|
+
|
|
5
|
+
'radix': 'error',
|
|
6
|
+
'prefer-exponentiation-operator': 'error',
|
|
7
|
+
|
|
8
|
+
'class-methods-use-this': 'off',
|
|
9
|
+
'no-eval': 'error',
|
|
10
|
+
'no-extend-native': 'error',
|
|
11
|
+
'no-implicit-coercion': 'error',
|
|
12
|
+
'no-implied-eval': 'error',
|
|
13
|
+
'no-invalid-this': 'error',
|
|
14
|
+
'no-new-func': 'error',
|
|
15
|
+
'no-process-env': 'off',
|
|
16
|
+
'no-proto': 'error',
|
|
17
|
+
'no-script-url': 'error',
|
|
18
|
+
'no-unexpected-multiline': 'error',
|
|
19
|
+
'no-void': 'error',
|
|
20
|
+
|
|
21
|
+
'no-caller': 'error',
|
|
22
|
+
'no-empty': [ 'error', { allowEmptyCatch: true } ],
|
|
23
|
+
'no-empty-function': [ 'error', { allow: [ 'arrowFunctions' ] } ],
|
|
24
|
+
'no-labels': 'error',
|
|
25
|
+
'no-lone-blocks': 'error',
|
|
26
|
+
'no-loop-func': 'error',
|
|
27
|
+
'no-misleading-character-class': 'error',
|
|
28
|
+
'no-new-wrappers': 'error',
|
|
29
|
+
'no-new': 'error',
|
|
30
|
+
'no-undef': [ 'error', { typeof: true } ],
|
|
31
|
+
'no-use-before-define': 'off',
|
|
32
|
+
|
|
33
|
+
'arrow-body-style': [ 'error', 'as-needed' ],
|
|
34
|
+
'constructor-super': 'error',
|
|
35
|
+
'no-class-assign': 'error',
|
|
36
|
+
'no-const-assign': 'error',
|
|
37
|
+
'no-dupe-class-members': 'error',
|
|
38
|
+
'no-duplicate-imports': 'error',
|
|
39
|
+
'no-new-symbol': 'error',
|
|
40
|
+
'no-restricted-imports': 'error',
|
|
41
|
+
'no-this-before-super': 'error',
|
|
42
|
+
'no-useless-computed-key': 'error',
|
|
43
|
+
'no-useless-constructor': 'error',
|
|
44
|
+
'no-useless-rename': 'error',
|
|
45
|
+
'no-var': 'error',
|
|
46
|
+
'object-shorthand': [ 'error', 'consistent-as-needed' ],
|
|
47
|
+
'prefer-arrow-callback': [
|
|
48
|
+
'error',
|
|
49
|
+
{
|
|
50
|
+
allowNamedFunctions: false,
|
|
51
|
+
allowUnboundThis: true,
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
'prefer-const': 'error',
|
|
55
|
+
'prefer-destructuring': [
|
|
56
|
+
'error',
|
|
57
|
+
{ array: false, object: true },
|
|
58
|
+
{ enforceForRenamedProperties: false },
|
|
59
|
+
],
|
|
60
|
+
'prefer-numeric-literals': 'error',
|
|
61
|
+
'prefer-rest-params': 'error',
|
|
62
|
+
'prefer-spread': 'error',
|
|
63
|
+
'prefer-template': 'error',
|
|
64
|
+
'require-yield': 'error',
|
|
65
|
+
'sort-imports': 'off',
|
|
66
|
+
'symbol-description': 'error',
|
|
67
|
+
|
|
68
|
+
'eqeqeq': [ 'error', 'always', { null: 'ignore' } ],
|
|
69
|
+
'no-extra-bind': 'error',
|
|
70
|
+
'no-useless-call': 'error',
|
|
71
|
+
'no-useless-catch': 'error',
|
|
72
|
+
'no-useless-concat': 'error',
|
|
73
|
+
'no-useless-escape': 'error',
|
|
74
|
+
'no-useless-return': 'error',
|
|
75
|
+
'no-multi-str': 'error',
|
|
76
|
+
'no-octal': 'error',
|
|
77
|
+
'no-octal-escape': 'error',
|
|
78
|
+
'no-return-assign': 'error',
|
|
79
|
+
'no-return-await': 'error',
|
|
80
|
+
'prefer-promise-reject-errors': 'error',
|
|
81
|
+
'require-await': 'off',
|
|
82
|
+
|
|
83
|
+
'no-shadow-restricted-names': 'error',
|
|
84
|
+
'no-undefined': 'off',
|
|
85
|
+
|
|
86
|
+
'func-name-matching': [ 'error', 'never' ],
|
|
87
|
+
'func-style': [
|
|
88
|
+
'error',
|
|
89
|
+
'declaration',
|
|
90
|
+
{ allowArrowFunctions: true },
|
|
91
|
+
],
|
|
92
|
+
'consistent-this': [ 'error', 'that' ],
|
|
93
|
+
'no-array-constructor': 'error',
|
|
94
|
+
'no-lonely-if': 'error',
|
|
95
|
+
'no-negated-condition': 'error',
|
|
96
|
+
'no-new-object': 'error',
|
|
97
|
+
'no-nested-ternary': 'warn',
|
|
98
|
+
'no-unneeded-ternary': 'warn',
|
|
99
|
+
|
|
100
|
+
'one-var': [
|
|
101
|
+
'error',
|
|
102
|
+
'never',
|
|
103
|
+
],
|
|
104
|
+
'operator-assignment': [ 'error', 'always' ],
|
|
105
|
+
|
|
106
|
+
'yoda': 'error',
|
|
107
|
+
|
|
108
|
+
'prefer-object-spread': 'error',
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Get the ESLint base config
|
|
113
|
+
* @returns {Promise<import('eslint').Linter.FlatConfig[]>}
|
|
114
|
+
*/
|
|
115
|
+
export async function createEslintBaseConfig() {
|
|
116
|
+
return [ {
|
|
117
|
+
linterOptions: {
|
|
118
|
+
noInlineConfig: false,
|
|
119
|
+
reportUnusedDisableDirectives: true,
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
rules: rules,
|
|
123
|
+
} ];
|
|
124
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Get the ESLint config for complexity checks
|
|
3
|
+
* @returns {import('eslint').Linter.FlatConfig[]}
|
|
4
|
+
*/
|
|
5
|
+
export function createEslintComplexityConfig() {
|
|
6
|
+
return [ { rules: {
|
|
7
|
+
'max-depth': [ 'error', 4 ],
|
|
8
|
+
'complexity': 'off',
|
|
9
|
+
'max-lines': [
|
|
10
|
+
'warn',
|
|
11
|
+
{
|
|
12
|
+
max: 512,
|
|
13
|
+
skipBlankLines: true,
|
|
14
|
+
skipComments: true,
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
'max-lines-per-function': [
|
|
18
|
+
'warn',
|
|
19
|
+
{
|
|
20
|
+
max: 120,
|
|
21
|
+
skipBlankLines: true,
|
|
22
|
+
skipComments: true,
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
'max-nested-callbacks': [ 'warn', 3 ],
|
|
26
|
+
'max-params': [ 'error', 5 ],
|
|
27
|
+
} } ];
|
|
28
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { findUp } from '../utilities/filesystem.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {import('node:url').URL} root root url
|
|
8
|
+
* @returns {Promise<string[]>}
|
|
9
|
+
*/
|
|
10
|
+
async function getGitignore(root) {
|
|
11
|
+
const rootPath = fileURLToPath(root);
|
|
12
|
+
const gitPath = await findUp(rootPath, '.git');
|
|
13
|
+
const path = resolve(gitPath ?? rootPath, '..', '.gitignore');
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
const raw = await fs.readFile(path, 'utf8');
|
|
17
|
+
|
|
18
|
+
return raw
|
|
19
|
+
.split(/\r?\n|\r/)
|
|
20
|
+
.filter(Boolean);
|
|
21
|
+
} catch (error) {
|
|
22
|
+
if (/** @type {NodeJS.ErrnoException} */ (error).code === 'ENOENT') {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Get basic ESLint ignores config
|
|
32
|
+
* @param {import('node:url').URL} root root url
|
|
33
|
+
* @returns {Promise<import('eslint').Linter.FlatConfig[]>}
|
|
34
|
+
*/
|
|
35
|
+
export async function createEslintIgnoresConfig(root) {
|
|
36
|
+
const ignores = await getGitignore(root);
|
|
37
|
+
|
|
38
|
+
return [ {
|
|
39
|
+
ignores: [
|
|
40
|
+
...ignores,
|
|
41
|
+
|
|
42
|
+
// Some things we always want to ignore
|
|
43
|
+
'.yarn/',
|
|
44
|
+
'.pnp.cjs',
|
|
45
|
+
'.pnp.loader.mjs',
|
|
46
|
+
'.git/',
|
|
47
|
+
'coverage/',
|
|
48
|
+
'node_modules/',
|
|
49
|
+
],
|
|
50
|
+
} ];
|
|
51
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { createEditorOverrides } from '../utilities/editorconfig.js';
|
|
2
|
+
|
|
3
|
+
/** @type {import('eslint').Linter.RulesRecord} */
|
|
4
|
+
const rules = {
|
|
5
|
+
'dot-notation': 'error',
|
|
6
|
+
'curly': 'error',
|
|
7
|
+
'unicode-bom': [ 'error', 'never' ],
|
|
8
|
+
|
|
9
|
+
'no-div-regex': 'error',
|
|
10
|
+
'no-control-regex': 'error',
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get basic ESLint style config
|
|
15
|
+
* @param {import('node:url').URL} root root url
|
|
16
|
+
* @returns {Promise<import('eslint').Linter.FlatConfig[]>}
|
|
17
|
+
*/
|
|
18
|
+
export async function createStyleConfigs(root) {
|
|
19
|
+
return [
|
|
20
|
+
{ rules },
|
|
21
|
+
...await createEditorOverrides(root),
|
|
22
|
+
];
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import noUselessExpression from './no-useless-expression.js';
|
|
2
|
+
import looseTypes from './loose-types.js';
|
|
3
|
+
import sequenceExpression from './sequence-expression.js';
|
|
4
|
+
|
|
5
|
+
export default {
|
|
6
|
+
rules: {
|
|
7
|
+
'loose-types': looseTypes,
|
|
8
|
+
'no-useless-expression': noUselessExpression,
|
|
9
|
+
'sequence-expression': sequenceExpression,
|
|
10
|
+
},
|
|
11
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {import('eslint').Rule.RuleContext} context ESLint rule context
|
|
3
|
+
* @returns {import('eslint').Rule.RuleListener}
|
|
4
|
+
*/
|
|
5
|
+
const preferInstance = (context) => ({
|
|
6
|
+
/**
|
|
7
|
+
* @param {import('estree').CallExpression} node The AST function node
|
|
8
|
+
* @returns {void}
|
|
9
|
+
*/
|
|
10
|
+
'CallExpression[callee.object.name = "Array"][callee.property.name = "isArray"]': (node) => {
|
|
11
|
+
const message = 'Use `instanceof Array` instead of `Array.isArray()`';
|
|
12
|
+
|
|
13
|
+
if (node.arguments.length !== 1) {
|
|
14
|
+
return context.report({ node, message });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const left = context.sourceCode.getText(node.arguments[0]);
|
|
18
|
+
/** @type {import('eslint').Rule.ReportFixer} */
|
|
19
|
+
const fix = (fixer) => fixer.replaceText(node, `${left} instanceof Array`);
|
|
20
|
+
|
|
21
|
+
context.report({ node, message, fix });
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {import('eslint').Rule.RuleContext} context ESLint rule context
|
|
27
|
+
* @returns {import('eslint').Rule.RuleListener}
|
|
28
|
+
*/
|
|
29
|
+
const preferIsArray = (context) => ({
|
|
30
|
+
/**
|
|
31
|
+
* @param {import('estree').BinaryExpression} node The AST function node
|
|
32
|
+
* @returns {void}
|
|
33
|
+
*/
|
|
34
|
+
'BinaryExpression[operator="instanceof"][right.type="Identifier"][right.name="Array"]': (node) => {
|
|
35
|
+
const message = 'Use `Array.isArray()` instead of `instanceof Array`';
|
|
36
|
+
|
|
37
|
+
const argument = context.sourceCode.getText(node.left);
|
|
38
|
+
/** @type {import('eslint').Rule.ReportFixer} */
|
|
39
|
+
const fix = (fixer) => fixer.replaceText(node, `Array.isArray(${argument})`);
|
|
40
|
+
|
|
41
|
+
context.report({ node, message, fix });
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
46
|
+
const rule = {
|
|
47
|
+
meta: {
|
|
48
|
+
type: 'suggestion',
|
|
49
|
+
fixable: 'code',
|
|
50
|
+
schema: [
|
|
51
|
+
{
|
|
52
|
+
type: 'string',
|
|
53
|
+
enum: [
|
|
54
|
+
'instanceof',
|
|
55
|
+
'isarray',
|
|
56
|
+
],
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
create: function (context) {
|
|
62
|
+
const [ option = 'instanceof' ] = context.options;
|
|
63
|
+
|
|
64
|
+
if (option === 'instanceof') {
|
|
65
|
+
return preferInstance(context);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return preferIsArray(context);
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export default rule;
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {string} optionName name of the property option
|
|
3
|
+
* @param {import('eslint').Rule.RuleContext} context ESLint rule context
|
|
4
|
+
* @param {string} message error message on report
|
|
5
|
+
* @returns {(node: import('eslint').Rule.Node) => void}
|
|
6
|
+
*/
|
|
7
|
+
function createReporter(optionName, context, message) {
|
|
8
|
+
const [ options ] = context.options;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {(
|
|
12
|
+
* import('eslint').Rule.Node |
|
|
13
|
+
* import('estree').Identifier |
|
|
14
|
+
* import('estree').MemberExpression |
|
|
15
|
+
* import('estree').ChainExpression |
|
|
16
|
+
* import('estree').CallExpression |
|
|
17
|
+
* import('estree').LogicalExpression |
|
|
18
|
+
* import('estree').UnaryExpression
|
|
19
|
+
* )} node The AST function node
|
|
20
|
+
*
|
|
21
|
+
* @returns {void}
|
|
22
|
+
*/
|
|
23
|
+
function validateNode(node) {
|
|
24
|
+
if (options?.[optionName] !== true) {
|
|
25
|
+
context.report({ node, message });
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return validateNode;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @type {import('eslint').Rule.RuleModule} */
|
|
33
|
+
const rule = {
|
|
34
|
+
meta: {
|
|
35
|
+
type: 'problem',
|
|
36
|
+
docs: {
|
|
37
|
+
description: 'Prevent accidental implicit coercion in Expressions',
|
|
38
|
+
recommended: true,
|
|
39
|
+
},
|
|
40
|
+
schema: [
|
|
41
|
+
{
|
|
42
|
+
type: 'object',
|
|
43
|
+
properties: {
|
|
44
|
+
allowConditionalExpressionCallExpression: { type: 'boolean' },
|
|
45
|
+
allowConditionalExpressionIdentifier: { type: 'boolean' },
|
|
46
|
+
allowConditionalExpressionMemberExpression: { type: 'boolean' },
|
|
47
|
+
allowConditionalExpressionChainExpression: { type: 'boolean' },
|
|
48
|
+
|
|
49
|
+
allowLogicalExpressionCallExpression: { type: 'boolean' },
|
|
50
|
+
allowLogicalExpressionIdentifier: { type: 'boolean' },
|
|
51
|
+
allowLogicalExpressionMemberExpression: { type: 'boolean' },
|
|
52
|
+
allowLogicalExpressionChainExpression: { type: 'boolean' },
|
|
53
|
+
|
|
54
|
+
allowNegatedUnaryExpression: { type: 'boolean' },
|
|
55
|
+
},
|
|
56
|
+
additionalProperties: false,
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
create: function (context) {
|
|
62
|
+
return {
|
|
63
|
+
/*
|
|
64
|
+
* var b = a ? 'hello' : 'there';
|
|
65
|
+
* if (a) return 'hello';
|
|
66
|
+
*/
|
|
67
|
+
'Identifier.test': createReporter(
|
|
68
|
+
'allowConditionalExpressionIdentifier',
|
|
69
|
+
context,
|
|
70
|
+
'Unexpected Identifier as Conditional',
|
|
71
|
+
),
|
|
72
|
+
|
|
73
|
+
/*
|
|
74
|
+
* var b = object.key ? 'hello' : 'there';
|
|
75
|
+
* if (object.key) return 'hello';
|
|
76
|
+
*/
|
|
77
|
+
'MemberExpression.test': createReporter(
|
|
78
|
+
'allowConditionalExpressionMemberExpression',
|
|
79
|
+
context,
|
|
80
|
+
'Unexpected MemberExpression as Conditional',
|
|
81
|
+
),
|
|
82
|
+
|
|
83
|
+
/*
|
|
84
|
+
* var b = object?.key ? 'hello' : 'there';
|
|
85
|
+
* if (object?.key) return 'hello';
|
|
86
|
+
*/
|
|
87
|
+
'ChainExpression.test': createReporter(
|
|
88
|
+
'allowConditionalExpressionChainExpression',
|
|
89
|
+
context,
|
|
90
|
+
'Unexpected ChainExpression as Conditional',
|
|
91
|
+
),
|
|
92
|
+
|
|
93
|
+
/*
|
|
94
|
+
* var b = a() ? 'hello' : 'there';
|
|
95
|
+
* if (a()) return 'hello';
|
|
96
|
+
*
|
|
97
|
+
* var b = object.key() ? 'hello' : 'there';
|
|
98
|
+
* if (object.key()) return 'hello';
|
|
99
|
+
*/
|
|
100
|
+
'CallExpression.test': createReporter(
|
|
101
|
+
'allowConditionalExpressionCallExpression',
|
|
102
|
+
context,
|
|
103
|
+
'Unexpected CallExpression as Conditional',
|
|
104
|
+
),
|
|
105
|
+
|
|
106
|
+
/*
|
|
107
|
+
* one === 1 && two
|
|
108
|
+
* one === 1 || two
|
|
109
|
+
*/
|
|
110
|
+
'LogicalExpression[operator="&&"] > Identifier.right, LogicalExpression[operator="||"] > Identifier.right': createReporter(
|
|
111
|
+
'allowLogicalExpressionIdentifier',
|
|
112
|
+
context,
|
|
113
|
+
'Unexpected Identifier as right hand of LogicalExpression',
|
|
114
|
+
),
|
|
115
|
+
|
|
116
|
+
/*
|
|
117
|
+
* one === 1 && two()
|
|
118
|
+
* one === 1 || two()
|
|
119
|
+
*
|
|
120
|
+
* one === 1 && object.two()
|
|
121
|
+
* one === 1 || object.two()
|
|
122
|
+
*/
|
|
123
|
+
'LogicalExpression[operator="&&"] > CallExpression.right, LogicalExpression[operator="||"] > CallExpression.right': createReporter(
|
|
124
|
+
'allowLogicalExpressionCallExpression',
|
|
125
|
+
context,
|
|
126
|
+
'Unexpected CallExpression as right hand of LogicalExpression',
|
|
127
|
+
),
|
|
128
|
+
|
|
129
|
+
/*
|
|
130
|
+
* one === 1 && object.two
|
|
131
|
+
* one === 1 || object.two
|
|
132
|
+
*/
|
|
133
|
+
'LogicalExpression[operator="&&"] > MemberExpression.right, LogicalExpression[operator="||"] > MemberExpression.right': createReporter(
|
|
134
|
+
'allowLogicalExpressionMemberExpression',
|
|
135
|
+
context,
|
|
136
|
+
'Unexpected MemberExpression as right hand of LogicalExpression',
|
|
137
|
+
),
|
|
138
|
+
|
|
139
|
+
/*
|
|
140
|
+
* one === 1 && object?.two
|
|
141
|
+
* one === 1 || object?.two
|
|
142
|
+
*/
|
|
143
|
+
'LogicalExpression[operator="&&"] > ChainExpression.right, LogicalExpression[operator="||"] > ChainExpression.right': createReporter(
|
|
144
|
+
'allowLogicalExpressionChainExpression',
|
|
145
|
+
context,
|
|
146
|
+
'Unexpected ChainExpression as right hand of LogicalExpression',
|
|
147
|
+
),
|
|
148
|
+
|
|
149
|
+
/*
|
|
150
|
+
* one && two === 2
|
|
151
|
+
* one || two === 2
|
|
152
|
+
*/
|
|
153
|
+
'LogicalExpression[operator="&&"] > Identifier.left, LogicalExpression[operator="||"] > Identifier.left': createReporter(
|
|
154
|
+
'allowLogicalExpressionIdentifier',
|
|
155
|
+
context,
|
|
156
|
+
'Unexpected Identifier as left hand of LogicalExpression',
|
|
157
|
+
),
|
|
158
|
+
|
|
159
|
+
/*
|
|
160
|
+
* one() && two === 2
|
|
161
|
+
* one() || two === 2
|
|
162
|
+
*
|
|
163
|
+
* object.one() && two === 2
|
|
164
|
+
* object.one() || two === 2
|
|
165
|
+
*
|
|
166
|
+
* object?.one() && two === 2
|
|
167
|
+
* object?.one() || two === 2
|
|
168
|
+
*/
|
|
169
|
+
'LogicalExpression[operator="&&"] > CallExpression.left, LogicalExpression[operator="||"] > CallExpression.left': createReporter(
|
|
170
|
+
'allowLogicalExpressionCallExpression',
|
|
171
|
+
context,
|
|
172
|
+
'Unexpected CallExpression as left hand of LogicalExpression',
|
|
173
|
+
),
|
|
174
|
+
|
|
175
|
+
/*
|
|
176
|
+
* object.one && two === 2
|
|
177
|
+
* object.one || two === 2
|
|
178
|
+
*/
|
|
179
|
+
'LogicalExpression[operator="&&"] > MemberExpression.left, LogicalExpression[operator="||"] > MemberExpression.left': createReporter(
|
|
180
|
+
'allowLogicalExpressionMemberExpression',
|
|
181
|
+
context,
|
|
182
|
+
'Unexpected MemberExpression as left hand of LogicalExpression',
|
|
183
|
+
),
|
|
184
|
+
|
|
185
|
+
/*
|
|
186
|
+
* object?.one && two === 2
|
|
187
|
+
* object?.one || two === 2
|
|
188
|
+
*/
|
|
189
|
+
'LogicalExpression[operator="&&"] > ChainExpression.left, LogicalExpression[operator="||"] > ChainExpression.left': createReporter(
|
|
190
|
+
'allowLogicalExpressionChainExpression',
|
|
191
|
+
context,
|
|
192
|
+
'Unexpected ChainExpression as left hand of LogicalExpression',
|
|
193
|
+
),
|
|
194
|
+
|
|
195
|
+
'UnaryExpression[operator="!"]': createReporter(
|
|
196
|
+
'allowNegatedUnaryExpression',
|
|
197
|
+
context,
|
|
198
|
+
'Unexpected "!" UnaryExpression',
|
|
199
|
+
),
|
|
200
|
+
};
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
export default rule;
|