@virmator/lint 14.7.2 → 14.8.1
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/README.md +1 -1
- package/configs/c8.config.json +2 -1
- package/configs/{eslint.config.base.mjs → eslint.config.base.ts} +10 -3
- package/configs/{eslint.config.share.mjs → eslint.config.share.ts} +1 -1
- package/configs/tsconfig.eslint.json +4 -1
- package/dist/lint.js +2 -2
- package/dist/rules/prefer-if-else-chain.lint.d.ts +3 -0
- package/dist/rules/prefer-if-else-chain.lint.js +78 -0
- package/package.json +13 -11
- package/src/lint.ts +271 -0
- package/src/rules/prefer-if-else-chain.lint.ts +113 -0
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ A default plugin for [virmator](https://www.npmjs.com/package/virmator).
|
|
|
12
12
|
- `virmator lint`
|
|
13
13
|
- Configs
|
|
14
14
|
- configs/tsconfig.eslint.json
|
|
15
|
-
- eslint.config.
|
|
15
|
+
- eslint.config.ts
|
|
16
16
|
- Deps
|
|
17
17
|
- [eslint](https://npmjs.com/package/eslint)
|
|
18
18
|
- [eslint-plugin-require-extensions](https://npmjs.com/package/eslint-plugin-require-extensions)
|
package/configs/c8.config.json
CHANGED
|
@@ -9,13 +9,14 @@ import globals from 'globals';
|
|
|
9
9
|
import {existsSync} from 'node:fs';
|
|
10
10
|
import {join} from 'node:path';
|
|
11
11
|
import tsEslint from 'typescript-eslint';
|
|
12
|
+
import preferIfElseChainRule from '../src/rules/prefer-if-else-chain.lint.js';
|
|
12
13
|
|
|
13
14
|
export const globalVars = {
|
|
14
15
|
...globals.node,
|
|
15
16
|
...globals.browser,
|
|
16
17
|
};
|
|
17
18
|
|
|
18
|
-
export function determineTsconfigPath(repoDir) {
|
|
19
|
+
export function determineTsconfigPath(repoDir: string) {
|
|
19
20
|
const eslintTsconfig = join(repoDir, 'configs', 'tsconfig.eslint.json');
|
|
20
21
|
const rootTsconfig = join(repoDir, 'tsconfig.json');
|
|
21
22
|
|
|
@@ -26,7 +27,7 @@ export function determineTsconfigPath(repoDir) {
|
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
export function defineEslintConfig(repoDir) {
|
|
30
|
+
export function defineEslintConfig(repoDir: string) {
|
|
30
31
|
const compat = new FlatCompat({
|
|
31
32
|
baseDirectory: repoDir,
|
|
32
33
|
});
|
|
@@ -64,7 +65,7 @@ export function defineEslintConfig(repoDir) {
|
|
|
64
65
|
},
|
|
65
66
|
jsEslint.configs.recommended,
|
|
66
67
|
...tsEslint.configs.strictTypeChecked,
|
|
67
|
-
sonarJsEslint.configs
|
|
68
|
+
sonarJsEslint.configs?.recommended,
|
|
68
69
|
prettierEslintRecommended,
|
|
69
70
|
{
|
|
70
71
|
languageOptions: {
|
|
@@ -79,6 +80,11 @@ export function defineEslintConfig(repoDir) {
|
|
|
79
80
|
'@stylistic': stylistic,
|
|
80
81
|
'@jsdoc': jsdoc,
|
|
81
82
|
unicorn: eslintPluginUnicorn,
|
|
83
|
+
'@virmator': {
|
|
84
|
+
rules: {
|
|
85
|
+
'prefer-if-else-chain': preferIfElseChainRule,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
82
88
|
},
|
|
83
89
|
rules: {
|
|
84
90
|
'@typescript-eslint/no-confusing-void-expression': 'off',
|
|
@@ -177,6 +183,7 @@ export function defineEslintConfig(repoDir) {
|
|
|
177
183
|
'@jsdoc/no-undefined-types': 'error',
|
|
178
184
|
'@typescript-eslint/await-thenable': 'error',
|
|
179
185
|
'@typescript-eslint/no-unused-vars': 'error',
|
|
186
|
+
'@virmator/prefer-if-else-chain': 'error',
|
|
180
187
|
'no-lonely-if': 'error',
|
|
181
188
|
curly: 'error',
|
|
182
189
|
'object-shorthand': 'error',
|
package/dist/lint.js
CHANGED
|
@@ -51,8 +51,8 @@ export const virmatorLintPlugin = defineVirmatorPlugin(import.meta.dirname, {
|
|
|
51
51
|
required: true,
|
|
52
52
|
},
|
|
53
53
|
eslint: {
|
|
54
|
-
copyFromPath: join('configs', 'eslint.config.share.
|
|
55
|
-
copyToPath: join('eslint.config.
|
|
54
|
+
copyFromPath: join('configs', 'eslint.config.share.ts'),
|
|
55
|
+
copyToPath: join('eslint.config.ts'),
|
|
56
56
|
env: {
|
|
57
57
|
[RuntimeEnv.Node]: true,
|
|
58
58
|
[RuntimeEnv.Web]: true,
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
function isTerminating(node) {
|
|
2
|
+
const body = node.consequent.type === 'BlockStatement' ? node.consequent.body : [node.consequent];
|
|
3
|
+
const last = body.at(-1);
|
|
4
|
+
return last != undefined && (last.type === 'ReturnStatement' || last.type === 'ThrowStatement');
|
|
5
|
+
}
|
|
6
|
+
function isConsecutiveTerminatingIfs(curr, next) {
|
|
7
|
+
return (curr.type === 'IfStatement' &&
|
|
8
|
+
!curr.alternate &&
|
|
9
|
+
next.type === 'IfStatement' &&
|
|
10
|
+
isTerminating(curr) &&
|
|
11
|
+
isTerminating(next));
|
|
12
|
+
}
|
|
13
|
+
function reconstructComment(comment) {
|
|
14
|
+
if (comment.type === 'Block') {
|
|
15
|
+
return `/*${comment.value}*/`;
|
|
16
|
+
}
|
|
17
|
+
return `//${comment.value}`;
|
|
18
|
+
}
|
|
19
|
+
function checkBlock(statements, context, sourceCode) {
|
|
20
|
+
statements.forEach((curr, index) => {
|
|
21
|
+
const next = statements[index + 1];
|
|
22
|
+
if (!next || !isConsecutiveTerminatingIfs(curr, next) || next.type !== 'IfStatement') {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const currRange = sourceCode.getRange(curr);
|
|
26
|
+
const nextRange = sourceCode.getRange(next);
|
|
27
|
+
const commentsBetween = sourceCode.getCommentsBefore(next);
|
|
28
|
+
context.report({
|
|
29
|
+
node: next,
|
|
30
|
+
messageId: 'useIfElse',
|
|
31
|
+
fix(fixer) {
|
|
32
|
+
const fixes = [
|
|
33
|
+
fixer.replaceTextRange([
|
|
34
|
+
currRange[1],
|
|
35
|
+
nextRange[0],
|
|
36
|
+
], ' else '),
|
|
37
|
+
];
|
|
38
|
+
if (commentsBetween.length > 0) {
|
|
39
|
+
const targetStatement = next.consequent.type === 'BlockStatement' && next.consequent.body[0]
|
|
40
|
+
? next.consequent.body[0]
|
|
41
|
+
: next.consequent;
|
|
42
|
+
const targetRange = sourceCode.getRange(targetStatement);
|
|
43
|
+
const sourceText = sourceCode.getText();
|
|
44
|
+
const lineStart = sourceText.lastIndexOf('\n', targetRange[0] - 1) + 1;
|
|
45
|
+
const indent = sourceText.slice(lineStart, targetRange[0]);
|
|
46
|
+
const commentText = commentsBetween
|
|
47
|
+
.map((comment) => {
|
|
48
|
+
return reconstructComment(comment);
|
|
49
|
+
})
|
|
50
|
+
.join('\n' + indent);
|
|
51
|
+
fixes.push(fixer.insertTextBefore(targetStatement, commentText + '\n' + indent));
|
|
52
|
+
}
|
|
53
|
+
return fixes;
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
const rule = {
|
|
59
|
+
meta: {
|
|
60
|
+
type: 'suggestion',
|
|
61
|
+
fixable: 'code',
|
|
62
|
+
messages: {
|
|
63
|
+
useIfElse: 'Consecutive if statements with return/throw should be chained as if-else.',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
create(context) {
|
|
67
|
+
const sourceCode = context.sourceCode;
|
|
68
|
+
return {
|
|
69
|
+
BlockStatement(node) {
|
|
70
|
+
checkBlock(node.body, context, sourceCode);
|
|
71
|
+
},
|
|
72
|
+
Program(node) {
|
|
73
|
+
checkBlock(node.body, context, sourceCode);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
export default rule;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@virmator/lint",
|
|
3
|
-
"version": "14.
|
|
3
|
+
"version": "14.8.1",
|
|
4
4
|
"description": "Default ESLint linting plugin for virmator.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"virmator",
|
|
@@ -32,32 +32,34 @@
|
|
|
32
32
|
"test:update": "npm test update"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@augment-vir/assert": "^31.
|
|
36
|
-
"@augment-vir/common": "^31.
|
|
37
|
-
"@augment-vir/node": "^31.
|
|
38
|
-
"@virmator/core": "^14.
|
|
35
|
+
"@augment-vir/assert": "^31.67.1",
|
|
36
|
+
"@augment-vir/common": "^31.67.1",
|
|
37
|
+
"@augment-vir/node": "^31.67.1",
|
|
38
|
+
"@virmator/core": "^14.8.1",
|
|
39
|
+
"jiti": "^2.6.1",
|
|
39
40
|
"mri": "^1.2.0"
|
|
40
41
|
},
|
|
41
42
|
"devDependencies": {
|
|
42
|
-
"@augment-vir/test": "^31.
|
|
43
|
+
"@augment-vir/test": "^31.67.1",
|
|
43
44
|
"@eslint/eslintrc": "^3.3.3",
|
|
44
45
|
"@eslint/js": "9.39.2",
|
|
45
46
|
"@stylistic/eslint-plugin": "^5.9.0",
|
|
46
47
|
"@stylistic/eslint-plugin-ts": "^4.4.1",
|
|
48
|
+
"@types/estree": "^1.0.8",
|
|
47
49
|
"@types/node": "^25.3.0",
|
|
48
|
-
"@typescript-eslint/eslint-plugin": "^8.56.
|
|
49
|
-
"c8": "^
|
|
50
|
+
"@typescript-eslint/eslint-plugin": "^8.56.1",
|
|
51
|
+
"c8": "^11.0.0",
|
|
50
52
|
"eslint": "^9.39.2",
|
|
51
53
|
"eslint-config-prettier": "^10.1.8",
|
|
52
|
-
"eslint-plugin-jsdoc": "^62.7.
|
|
54
|
+
"eslint-plugin-jsdoc": "^62.7.1",
|
|
53
55
|
"eslint-plugin-monorepo-cop": "^1.0.2",
|
|
54
|
-
"eslint-plugin-playwright": "^2.
|
|
56
|
+
"eslint-plugin-playwright": "^2.9.0",
|
|
55
57
|
"eslint-plugin-prettier": "^5.5.5",
|
|
56
58
|
"eslint-plugin-require-extensions": "^0.1.3",
|
|
57
59
|
"eslint-plugin-sonarjs": "^4.0.0",
|
|
58
60
|
"eslint-plugin-unicorn": "^63.0.0",
|
|
59
61
|
"istanbul-smart-text-reporter": "^1.1.5",
|
|
60
|
-
"typescript-eslint": "^8.56.
|
|
62
|
+
"typescript-eslint": "^8.56.1"
|
|
61
63
|
},
|
|
62
64
|
"engines": {
|
|
63
65
|
"node": ">=22"
|
package/src/lint.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import {check} from '@augment-vir/assert';
|
|
2
|
+
import {RuntimeEnv} from '@augment-vir/common';
|
|
3
|
+
import {interpolationSafeWindowsPath} from '@augment-vir/node';
|
|
4
|
+
import {defineVirmatorPlugin, NpmDepType, PackageType} from '@virmator/core';
|
|
5
|
+
import mri from 'mri';
|
|
6
|
+
import {join} from 'node:path';
|
|
7
|
+
|
|
8
|
+
/** A virmator plugin for running ESLint. */
|
|
9
|
+
export const virmatorLintPlugin = defineVirmatorPlugin(
|
|
10
|
+
import.meta.dirname,
|
|
11
|
+
{
|
|
12
|
+
name: 'Lint',
|
|
13
|
+
cliCommands: {
|
|
14
|
+
lint: {
|
|
15
|
+
doc: {
|
|
16
|
+
sections: [
|
|
17
|
+
`
|
|
18
|
+
Runs ESLint.
|
|
19
|
+
`,
|
|
20
|
+
],
|
|
21
|
+
examples: [
|
|
22
|
+
{
|
|
23
|
+
content: 'virmator lint',
|
|
24
|
+
},
|
|
25
|
+
],
|
|
26
|
+
},
|
|
27
|
+
subCommands: {
|
|
28
|
+
fix: {
|
|
29
|
+
doc: {
|
|
30
|
+
examples: [
|
|
31
|
+
{
|
|
32
|
+
content: 'virmator lint fix',
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
sections: [
|
|
36
|
+
`
|
|
37
|
+
Auto fix all fixable ESLint issues.
|
|
38
|
+
`,
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
configFiles: {
|
|
44
|
+
eslintTsconfig: {
|
|
45
|
+
copyFromPath: join('configs', 'tsconfig.eslint.json'),
|
|
46
|
+
copyToPath: join('configs', 'tsconfig.eslint.json'),
|
|
47
|
+
env: {
|
|
48
|
+
[RuntimeEnv.Node]: true,
|
|
49
|
+
[RuntimeEnv.Web]: true,
|
|
50
|
+
},
|
|
51
|
+
packageType: {
|
|
52
|
+
[PackageType.MonoRoot]: true,
|
|
53
|
+
},
|
|
54
|
+
required: true,
|
|
55
|
+
},
|
|
56
|
+
eslint: {
|
|
57
|
+
copyFromPath: join('configs', 'eslint.config.share.ts'),
|
|
58
|
+
copyToPath: join('eslint.config.ts'),
|
|
59
|
+
env: {
|
|
60
|
+
[RuntimeEnv.Node]: true,
|
|
61
|
+
[RuntimeEnv.Web]: true,
|
|
62
|
+
},
|
|
63
|
+
packageType: {
|
|
64
|
+
[PackageType.MonoRoot]: true,
|
|
65
|
+
[PackageType.TopPackage]: true,
|
|
66
|
+
},
|
|
67
|
+
required: true,
|
|
68
|
+
configFlags: [
|
|
69
|
+
'-c',
|
|
70
|
+
'--config',
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
npmDeps: {
|
|
75
|
+
eslint: {
|
|
76
|
+
type: NpmDepType.Dev,
|
|
77
|
+
env: {
|
|
78
|
+
[RuntimeEnv.Node]: true,
|
|
79
|
+
[RuntimeEnv.Web]: true,
|
|
80
|
+
},
|
|
81
|
+
packageType: {
|
|
82
|
+
[PackageType.TopPackage]: true,
|
|
83
|
+
[PackageType.MonoRoot]: true,
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
'eslint-plugin-require-extensions': {
|
|
87
|
+
type: NpmDepType.Dev,
|
|
88
|
+
env: {
|
|
89
|
+
[RuntimeEnv.Node]: true,
|
|
90
|
+
[RuntimeEnv.Web]: true,
|
|
91
|
+
},
|
|
92
|
+
packageType: {
|
|
93
|
+
[PackageType.TopPackage]: true,
|
|
94
|
+
[PackageType.MonoRoot]: true,
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
'eslint-plugin-unicorn': {
|
|
98
|
+
type: NpmDepType.Dev,
|
|
99
|
+
env: {
|
|
100
|
+
[RuntimeEnv.Node]: true,
|
|
101
|
+
[RuntimeEnv.Web]: true,
|
|
102
|
+
},
|
|
103
|
+
packageType: {
|
|
104
|
+
[PackageType.TopPackage]: true,
|
|
105
|
+
[PackageType.MonoRoot]: true,
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
'@eslint/js': {
|
|
109
|
+
type: NpmDepType.Dev,
|
|
110
|
+
env: {
|
|
111
|
+
[RuntimeEnv.Node]: true,
|
|
112
|
+
[RuntimeEnv.Web]: true,
|
|
113
|
+
},
|
|
114
|
+
packageType: {
|
|
115
|
+
[PackageType.TopPackage]: true,
|
|
116
|
+
[PackageType.MonoRoot]: true,
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
'@eslint/eslintrc': {
|
|
120
|
+
type: NpmDepType.Dev,
|
|
121
|
+
env: {
|
|
122
|
+
[RuntimeEnv.Node]: true,
|
|
123
|
+
[RuntimeEnv.Web]: true,
|
|
124
|
+
},
|
|
125
|
+
packageType: {
|
|
126
|
+
[PackageType.TopPackage]: true,
|
|
127
|
+
[PackageType.MonoRoot]: true,
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
'@stylistic/eslint-plugin': {
|
|
131
|
+
type: NpmDepType.Dev,
|
|
132
|
+
env: {
|
|
133
|
+
[RuntimeEnv.Node]: true,
|
|
134
|
+
[RuntimeEnv.Web]: true,
|
|
135
|
+
},
|
|
136
|
+
packageType: {
|
|
137
|
+
[PackageType.TopPackage]: true,
|
|
138
|
+
[PackageType.MonoRoot]: true,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
'eslint-plugin-monorepo-cop': {
|
|
142
|
+
type: NpmDepType.Dev,
|
|
143
|
+
env: {
|
|
144
|
+
[RuntimeEnv.Node]: true,
|
|
145
|
+
[RuntimeEnv.Web]: true,
|
|
146
|
+
},
|
|
147
|
+
packageType: {
|
|
148
|
+
[PackageType.TopPackage]: true,
|
|
149
|
+
[PackageType.MonoRoot]: true,
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
'@stylistic/eslint-plugin-ts': {
|
|
153
|
+
type: NpmDepType.Dev,
|
|
154
|
+
env: {
|
|
155
|
+
[RuntimeEnv.Node]: true,
|
|
156
|
+
[RuntimeEnv.Web]: true,
|
|
157
|
+
},
|
|
158
|
+
packageType: {
|
|
159
|
+
[PackageType.TopPackage]: true,
|
|
160
|
+
[PackageType.MonoRoot]: true,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
'@typescript-eslint/eslint-plugin': {
|
|
164
|
+
type: NpmDepType.Dev,
|
|
165
|
+
env: {
|
|
166
|
+
[RuntimeEnv.Node]: true,
|
|
167
|
+
[RuntimeEnv.Web]: true,
|
|
168
|
+
},
|
|
169
|
+
packageType: {
|
|
170
|
+
[PackageType.TopPackage]: true,
|
|
171
|
+
[PackageType.MonoRoot]: true,
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
'eslint-config-prettier': {
|
|
175
|
+
type: NpmDepType.Dev,
|
|
176
|
+
env: {
|
|
177
|
+
[RuntimeEnv.Node]: true,
|
|
178
|
+
[RuntimeEnv.Web]: true,
|
|
179
|
+
},
|
|
180
|
+
packageType: {
|
|
181
|
+
[PackageType.TopPackage]: true,
|
|
182
|
+
[PackageType.MonoRoot]: true,
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
'eslint-plugin-jsdoc': {
|
|
186
|
+
type: NpmDepType.Dev,
|
|
187
|
+
env: {
|
|
188
|
+
[RuntimeEnv.Node]: true,
|
|
189
|
+
[RuntimeEnv.Web]: true,
|
|
190
|
+
},
|
|
191
|
+
packageType: {
|
|
192
|
+
[PackageType.TopPackage]: true,
|
|
193
|
+
[PackageType.MonoRoot]: true,
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
'eslint-plugin-playwright': {
|
|
197
|
+
type: NpmDepType.Dev,
|
|
198
|
+
env: {
|
|
199
|
+
[RuntimeEnv.Node]: true,
|
|
200
|
+
[RuntimeEnv.Web]: true,
|
|
201
|
+
},
|
|
202
|
+
packageType: {
|
|
203
|
+
[PackageType.TopPackage]: true,
|
|
204
|
+
[PackageType.MonoRoot]: true,
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
'eslint-plugin-prettier': {
|
|
208
|
+
type: NpmDepType.Dev,
|
|
209
|
+
env: {
|
|
210
|
+
[RuntimeEnv.Node]: true,
|
|
211
|
+
[RuntimeEnv.Web]: true,
|
|
212
|
+
},
|
|
213
|
+
packageType: {
|
|
214
|
+
[PackageType.TopPackage]: true,
|
|
215
|
+
[PackageType.MonoRoot]: true,
|
|
216
|
+
},
|
|
217
|
+
},
|
|
218
|
+
'eslint-plugin-sonarjs': {
|
|
219
|
+
type: NpmDepType.Dev,
|
|
220
|
+
env: {
|
|
221
|
+
[RuntimeEnv.Node]: true,
|
|
222
|
+
[RuntimeEnv.Web]: true,
|
|
223
|
+
},
|
|
224
|
+
packageType: {
|
|
225
|
+
[PackageType.TopPackage]: true,
|
|
226
|
+
[PackageType.MonoRoot]: true,
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
'typescript-eslint': {
|
|
230
|
+
type: NpmDepType.Dev,
|
|
231
|
+
env: {
|
|
232
|
+
[RuntimeEnv.Node]: true,
|
|
233
|
+
[RuntimeEnv.Web]: true,
|
|
234
|
+
},
|
|
235
|
+
packageType: {
|
|
236
|
+
[PackageType.TopPackage]: true,
|
|
237
|
+
[PackageType.MonoRoot]: true,
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
},
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
},
|
|
244
|
+
async ({
|
|
245
|
+
package: {monoRepoRootPath},
|
|
246
|
+
runShellCommand,
|
|
247
|
+
cliInputs: {usedCommands, filteredArgs},
|
|
248
|
+
}) => {
|
|
249
|
+
const args = mri(filteredArgs);
|
|
250
|
+
|
|
251
|
+
const dirPath = args._.length ? '' : '.';
|
|
252
|
+
|
|
253
|
+
const cacheLocation = join(monoRepoRootPath, 'node_modules', '.cache', '.eslintcache');
|
|
254
|
+
|
|
255
|
+
const eslintCommand = [
|
|
256
|
+
'npx',
|
|
257
|
+
'eslint',
|
|
258
|
+
'--cache',
|
|
259
|
+
`--cache-location='${interpolationSafeWindowsPath(cacheLocation)}'`,
|
|
260
|
+
usedCommands.lint?.subCommands.fix && !args.fix ? '--fix' : '',
|
|
261
|
+
dirPath,
|
|
262
|
+
...filteredArgs,
|
|
263
|
+
]
|
|
264
|
+
.filter(check.isTruthy)
|
|
265
|
+
.join(' ');
|
|
266
|
+
|
|
267
|
+
await runShellCommand(eslintCommand, {
|
|
268
|
+
cwd: monoRepoRootPath,
|
|
269
|
+
});
|
|
270
|
+
},
|
|
271
|
+
);
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {type Rule, type SourceCode} from 'eslint';
|
|
2
|
+
import type {Directive, IfStatement, ModuleDeclaration, Statement} from 'estree';
|
|
3
|
+
|
|
4
|
+
type AnyStatement = Directive | ModuleDeclaration | Statement;
|
|
5
|
+
|
|
6
|
+
function isTerminating(node: IfStatement): boolean {
|
|
7
|
+
const body =
|
|
8
|
+
node.consequent.type === 'BlockStatement' ? node.consequent.body : [node.consequent];
|
|
9
|
+
const last = body.at(-1);
|
|
10
|
+
return last != undefined && (last.type === 'ReturnStatement' || last.type === 'ThrowStatement');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isConsecutiveTerminatingIfs(
|
|
14
|
+
curr: AnyStatement,
|
|
15
|
+
next: AnyStatement,
|
|
16
|
+
): curr is IfStatement & {alternate: null} {
|
|
17
|
+
return (
|
|
18
|
+
curr.type === 'IfStatement' &&
|
|
19
|
+
!curr.alternate &&
|
|
20
|
+
next.type === 'IfStatement' &&
|
|
21
|
+
isTerminating(curr) &&
|
|
22
|
+
isTerminating(next)
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function reconstructComment(comment: Readonly<{type: string; value: string}>): string {
|
|
27
|
+
if (comment.type === 'Block') {
|
|
28
|
+
return `/*${comment.value}*/`;
|
|
29
|
+
}
|
|
30
|
+
return `//${comment.value}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function checkBlock(
|
|
34
|
+
statements: ReadonlyArray<AnyStatement>,
|
|
35
|
+
context: Readonly<Rule.RuleContext>,
|
|
36
|
+
sourceCode: Readonly<SourceCode>,
|
|
37
|
+
) {
|
|
38
|
+
statements.forEach((curr, index) => {
|
|
39
|
+
const next = statements[index + 1];
|
|
40
|
+
|
|
41
|
+
if (!next || !isConsecutiveTerminatingIfs(curr, next) || next.type !== 'IfStatement') {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const currRange = sourceCode.getRange(curr);
|
|
46
|
+
const nextRange = sourceCode.getRange(next);
|
|
47
|
+
const commentsBetween = sourceCode.getCommentsBefore(next);
|
|
48
|
+
|
|
49
|
+
context.report({
|
|
50
|
+
node: next,
|
|
51
|
+
messageId: 'useIfElse',
|
|
52
|
+
fix(fixer) {
|
|
53
|
+
const fixes: Rule.Fix[] = [
|
|
54
|
+
fixer.replaceTextRange(
|
|
55
|
+
[
|
|
56
|
+
currRange[1],
|
|
57
|
+
nextRange[0],
|
|
58
|
+
],
|
|
59
|
+
' else ',
|
|
60
|
+
),
|
|
61
|
+
];
|
|
62
|
+
|
|
63
|
+
if (commentsBetween.length > 0) {
|
|
64
|
+
const targetStatement =
|
|
65
|
+
next.consequent.type === 'BlockStatement' && next.consequent.body[0]
|
|
66
|
+
? next.consequent.body[0]
|
|
67
|
+
: next.consequent;
|
|
68
|
+
|
|
69
|
+
const targetRange = sourceCode.getRange(targetStatement);
|
|
70
|
+
const sourceText = sourceCode.getText();
|
|
71
|
+
const lineStart = sourceText.lastIndexOf('\n', targetRange[0] - 1) + 1;
|
|
72
|
+
const indent = sourceText.slice(lineStart, targetRange[0]);
|
|
73
|
+
|
|
74
|
+
const commentText = commentsBetween
|
|
75
|
+
.map((comment) => {
|
|
76
|
+
return reconstructComment(comment);
|
|
77
|
+
})
|
|
78
|
+
.join('\n' + indent);
|
|
79
|
+
|
|
80
|
+
fixes.push(
|
|
81
|
+
fixer.insertTextBefore(targetStatement, commentText + '\n' + indent),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return fixes;
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const rule: Rule.RuleModule = {
|
|
92
|
+
meta: {
|
|
93
|
+
type: 'suggestion',
|
|
94
|
+
fixable: 'code',
|
|
95
|
+
messages: {
|
|
96
|
+
useIfElse: 'Consecutive if statements with return/throw should be chained as if-else.',
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
create(context) {
|
|
100
|
+
const sourceCode = context.sourceCode;
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
BlockStatement(node) {
|
|
104
|
+
checkBlock(node.body, context, sourceCode);
|
|
105
|
+
},
|
|
106
|
+
Program(node) {
|
|
107
|
+
checkBlock(node.body, context, sourceCode);
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export default rule;
|