@virmator/lint 14.7.2 → 14.8.0

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 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.mjs
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)
@@ -5,7 +5,8 @@
5
5
  "clean": true,
6
6
  "exclude": [
7
7
  "**/*.test.ts",
8
- "**/*.example.ts"
8
+ "**/*.example.ts",
9
+ "src/rules/"
9
10
  ],
10
11
  "include": [
11
12
  "src/**/*.ts"
@@ -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.recommended,
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',
@@ -1,4 +1,4 @@
1
- import {defineEslintConfig} from '@virmator/lint/configs/eslint.config.base.mjs';
1
+ import {defineEslintConfig} from '@virmator/lint/configs/eslint.config.base.js';
2
2
  import {dirname} from 'node:path';
3
3
  import {fileURLToPath} from 'node:url';
4
4
 
@@ -3,5 +3,8 @@
3
3
  "rootDir": ".."
4
4
  },
5
5
  "extends": "./tsconfig.base.json",
6
- "include": ["../packages/**/*.ts"]
6
+ "include": [
7
+ "../eslint.config.ts",
8
+ "../packages/**/*.ts"
9
+ ]
7
10
  }
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.mjs'),
55
- copyToPath: join('eslint.config.mjs'),
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,3 @@
1
+ import { type Rule } from 'eslint';
2
+ declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,56 @@
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 checkBlock(statements, context, sourceCode) {
14
+ statements.forEach((curr, index) => {
15
+ const next = statements[index + 1];
16
+ if (!next || !isConsecutiveTerminatingIfs(curr, next)) {
17
+ return;
18
+ }
19
+ const currRange = sourceCode.getRange(curr);
20
+ const nextRange = sourceCode.getRange(next);
21
+ const hasCommentsBetween = sourceCode.commentsExistBetween(curr, next);
22
+ context.report({
23
+ node: next,
24
+ messageId: 'useIfElse',
25
+ fix: hasCommentsBetween
26
+ ? null
27
+ : (fixer) => {
28
+ return fixer.replaceTextRange([
29
+ currRange[1],
30
+ nextRange[0],
31
+ ], ' else ');
32
+ },
33
+ });
34
+ });
35
+ }
36
+ const rule = {
37
+ meta: {
38
+ type: 'suggestion',
39
+ fixable: 'code',
40
+ messages: {
41
+ useIfElse: 'Consecutive if statements with return/throw should be chained as if-else.',
42
+ },
43
+ },
44
+ create(context) {
45
+ const sourceCode = context.sourceCode;
46
+ return {
47
+ BlockStatement(node) {
48
+ checkBlock(node.body, context, sourceCode);
49
+ },
50
+ Program(node) {
51
+ checkBlock(node.body, context, sourceCode);
52
+ },
53
+ };
54
+ },
55
+ };
56
+ export default rule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/lint",
3
- "version": "14.7.2",
3
+ "version": "14.8.0",
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.64.0",
36
- "@augment-vir/common": "^31.64.0",
37
- "@augment-vir/node": "^31.64.0",
38
- "@virmator/core": "^14.7.2",
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.0",
39
+ "jiti": "^2.6.1",
39
40
  "mri": "^1.2.0"
40
41
  },
41
42
  "devDependencies": {
42
- "@augment-vir/test": "^31.64.0",
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.0",
49
- "c8": "^10.1.3",
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.0",
54
+ "eslint-plugin-jsdoc": "^62.7.1",
53
55
  "eslint-plugin-monorepo-cop": "^1.0.2",
54
- "eslint-plugin-playwright": "^2.7.0",
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.0"
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,82 @@
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 checkBlock(
27
+ statements: ReadonlyArray<AnyStatement>,
28
+ context: Readonly<Rule.RuleContext>,
29
+ sourceCode: Readonly<SourceCode>,
30
+ ) {
31
+ statements.forEach((curr, index) => {
32
+ const next = statements[index + 1];
33
+
34
+ if (!next || !isConsecutiveTerminatingIfs(curr, next)) {
35
+ return;
36
+ }
37
+
38
+ const currRange = sourceCode.getRange(curr);
39
+ const nextRange = sourceCode.getRange(next);
40
+ const hasCommentsBetween = sourceCode.commentsExistBetween(curr, next);
41
+
42
+ context.report({
43
+ node: next,
44
+ messageId: 'useIfElse',
45
+ fix: hasCommentsBetween
46
+ ? null
47
+ : (fixer) => {
48
+ return fixer.replaceTextRange(
49
+ [
50
+ currRange[1],
51
+ nextRange[0],
52
+ ],
53
+ ' else ',
54
+ );
55
+ },
56
+ });
57
+ });
58
+ }
59
+
60
+ const rule: Rule.RuleModule = {
61
+ meta: {
62
+ type: 'suggestion',
63
+ fixable: 'code',
64
+ messages: {
65
+ useIfElse: 'Consecutive if statements with return/throw should be chained as if-else.',
66
+ },
67
+ },
68
+ create(context) {
69
+ const sourceCode = context.sourceCode;
70
+
71
+ return {
72
+ BlockStatement(node) {
73
+ checkBlock(node.body, context, sourceCode);
74
+ },
75
+ Program(node) {
76
+ checkBlock(node.body, context, sourceCode);
77
+ },
78
+ };
79
+ },
80
+ };
81
+
82
+ export default rule;