@virmator/lint 14.30.3 → 14.30.4

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.
@@ -18,6 +18,7 @@ import preferArrowObjectBlockBodyRule from '../src/rules/prefer-arrow-object-blo
18
18
  import preferEnsureErrorRule from '../src/rules/prefer-ensure-error.lint.js';
19
19
  import preferIfElseChainRule from '../src/rules/prefer-if-else-chain.lint.js';
20
20
  import preferMapEnumToObjectRule from '../src/rules/prefer-map-enum-to-object.lint.js';
21
+ import preferMultilineArrowBlockBodyRule from '../src/rules/prefer-multiline-arrow-block-body.lint.js';
21
22
  import preferParamsObjectRule from '../src/rules/prefer-params-object.lint.js';
22
23
  import preferParseUrlRule from '../src/rules/prefer-parse-url.lint.js';
23
24
  import preferProtectedOverPrivateRule from '../src/rules/prefer-protected-over-private.lint.js';
@@ -87,6 +88,7 @@ export function defineEslintConfig(repoDir: string) {
87
88
  'prefer-ensure-error': preferEnsureErrorRule,
88
89
  'prefer-if-else-chain': preferIfElseChainRule,
89
90
  'prefer-map-enum-to-object': preferMapEnumToObjectRule,
91
+ 'prefer-multiline-arrow-block-body': preferMultilineArrowBlockBodyRule,
90
92
  'prefer-params-object': preferParamsObjectRule,
91
93
  'prefer-parse-url': preferParseUrlRule,
92
94
  'prefer-protected-over-private': preferProtectedOverPrivateRule,
@@ -209,6 +211,7 @@ export function defineEslintConfig(repoDir: string) {
209
211
  '@virmator/prefer-ensure-error': 'error',
210
212
  '@virmator/prefer-if-else-chain': 'error',
211
213
  '@virmator/prefer-map-enum-to-object': 'error',
214
+ '@virmator/prefer-multiline-arrow-block-body': 'error',
212
215
  '@virmator/prefer-params-object': 'error',
213
216
  '@virmator/prefer-parse-url': 'error',
214
217
  '@virmator/prefer-protected-over-private': 'error',
@@ -110,7 +110,9 @@ function childrenOf(node, visitorKeys) {
110
110
  return keys.flatMap((key) => {
111
111
  const value = node[key];
112
112
  if (Array.isArray(value)) {
113
- return value.filter((item) => item != undefined && typeof item.type === 'string');
113
+ return value.filter((item) => {
114
+ return item != undefined && typeof item.type === 'string';
115
+ });
114
116
  }
115
117
  else if (value != undefined && typeof value.type === 'string') {
116
118
  return [value];
@@ -176,7 +178,10 @@ function isTestCall(node) {
176
178
  }
177
179
  }
178
180
  function getTestCallback(node) {
179
- return node.arguments.find((argument) => argument.type === 'FunctionExpression' || argument.type === 'ArrowFunctionExpression');
181
+ return node.arguments.find((argument) => {
182
+ return (argument.type === 'FunctionExpression' ||
183
+ argument.type === 'ArrowFunctionExpression');
184
+ });
180
185
  }
181
186
  /**
182
187
  * A `@ts-expect-error` (or `@ts-ignore`) directive inside a test is a deliberate compile-time
@@ -25,10 +25,12 @@ function isThisNameMember(node) {
25
25
  }
26
26
  /** Determine whether a class already declares a (non-computed) `name` member. */
27
27
  function hasNameMember(node) {
28
- return node.body.body.some((member) => (member.type === 'PropertyDefinition' || member.type === 'MethodDefinition') &&
29
- !member.computed &&
30
- member.key.type === 'Identifier' &&
31
- member.key.name === 'name');
28
+ return node.body.body.some((member) => {
29
+ return ((member.type === 'PropertyDefinition' || member.type === 'MethodDefinition') &&
30
+ !member.computed &&
31
+ member.key.type === 'Identifier' &&
32
+ member.key.name === 'name');
33
+ });
32
34
  }
33
35
  const rule = {
34
36
  meta: {
@@ -92,7 +94,9 @@ const rule = {
92
94
  * constructor body (e.g. a nested `class Inner { method() { this.name = 'x'; }
93
95
  * }`).
94
96
  */
95
- const enclosingFunctionIndex = ancestors.findLastIndex((ancestor) => functionNodeTypes.includes(ancestor.type));
97
+ const enclosingFunctionIndex = ancestors.findLastIndex((ancestor) => {
98
+ return functionNodeTypes.includes(ancestor.type);
99
+ });
96
100
  if (enclosingFunctionIndex < 0) {
97
101
  return;
98
102
  }
@@ -0,0 +1,3 @@
1
+ import { type Rule } from 'eslint';
2
+ declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,69 @@
1
+ const rule = {
2
+ meta: {
3
+ type: 'suggestion',
4
+ fixable: 'code',
5
+ messages: {
6
+ preferBlockBody: 'An arrow function that spans multiple lines must use a block body with an explicit `return`.',
7
+ },
8
+ schema: [],
9
+ },
10
+ create(context) {
11
+ const sourceCode = context.sourceCode;
12
+ return {
13
+ ArrowFunctionExpression(node) {
14
+ /** `() => ({...})` is handled by the `prefer-arrow-object-block-body` rule. */
15
+ if (node.body.type === 'BlockStatement' || node.body.type === 'ObjectExpression') {
16
+ return;
17
+ }
18
+ const arrowToken = sourceCode.getTokenBefore(node.body, {
19
+ filter(token) {
20
+ return token.type === 'Punctuator' && token.value === '=>';
21
+ },
22
+ });
23
+ /**
24
+ * Measure from `=>` rather than from the start of the arrow function so that a
25
+ * wrapped parameter list with a body that still fits on the `=>` line is not
26
+ * flagged.
27
+ */
28
+ if (!arrowToken?.loc ||
29
+ !node.body.loc ||
30
+ arrowToken.loc.start.line === node.body.loc.end.line) {
31
+ return;
32
+ }
33
+ context.report({
34
+ node: node.body,
35
+ messageId: 'preferBlockBody',
36
+ fix(fixer) {
37
+ /**
38
+ * A concise body can be wrapped in parentheses (`() => (\n a + b \n)`),
39
+ * which are tokens rather than AST nodes. The replacement must swallow them
40
+ * or the fix leaves the new block body inside stray parentheses.
41
+ */
42
+ const openParens = sourceCode.getTokensBetween(arrowToken, node.body);
43
+ const closeParens = sourceCode.getTokensAfter(node.body, {
44
+ count: openParens.length,
45
+ });
46
+ const startToken = openParens[0] ?? sourceCode.getFirstToken(node.body);
47
+ const endToken = closeParens.at(-1) ?? sourceCode.getLastToken(node.body);
48
+ if (!startToken ||
49
+ !endToken ||
50
+ closeParens.length !== openParens.length ||
51
+ openParens.some((token) => {
52
+ return token.value !== '(';
53
+ }) ||
54
+ closeParens.some((token) => {
55
+ return token.value !== ')';
56
+ })) {
57
+ return null;
58
+ }
59
+ return fixer.replaceTextRange([
60
+ startToken.range[0],
61
+ endToken.range[1],
62
+ ], `{ return ${sourceCode.getText(node.body)}; }`);
63
+ },
64
+ });
65
+ },
66
+ };
67
+ },
68
+ };
69
+ export default rule;
@@ -108,7 +108,11 @@ function buildParamsObjectFix(params, sourceCode) {
108
108
  }
109
109
  const definedParams = fixableParams.filter((fixable) => fixable != undefined);
110
110
  const patternText = definedParams
111
- .map((param) => param.defaultText == undefined ? param.name : `${param.name} = ${param.defaultText}`)
111
+ .map((param) => {
112
+ return param.defaultText == undefined
113
+ ? param.name
114
+ : `${param.name} = ${param.defaultText}`;
115
+ })
112
116
  .join(', ');
113
117
  const typeText = definedParams
114
118
  .map((param) => `${param.name}${param.optional ? '?' : ''}: ${param.typeText}`)
@@ -120,10 +124,12 @@ function buildParamsObjectFix(params, sourceCode) {
120
124
  }
121
125
  const start = sourceCode.getRange(firstParam)[0];
122
126
  const end = sourceCode.getRange(lastParam)[1];
123
- return (fixer) => fixer.replaceTextRange([
124
- start,
125
- end,
126
- ], `{${patternText}}: Readonly<{${typeText}}>`);
127
+ return (fixer) => {
128
+ return fixer.replaceTextRange([
129
+ start,
130
+ end,
131
+ ], `{${patternText}}: Readonly<{${typeText}}>`);
132
+ };
127
133
  }
128
134
  const rule = {
129
135
  meta: {
@@ -11,9 +11,11 @@ function addParseUrlImport(fixer, sourceCode) {
11
11
  const importDeclarations = sourceCode.ast.body.filter((statement) => statement.type === 'ImportDeclaration');
12
12
  const existingUrlVirImport = importDeclarations.find((imp) => imp.source.value === 'url-vir');
13
13
  if (existingUrlVirImport) {
14
- const hasParseUrl = existingUrlVirImport.specifiers.some((specifier) => specifier.type === 'ImportSpecifier' &&
15
- specifier.imported.type === 'Identifier' &&
16
- specifier.imported.name === 'parseUrl');
14
+ const hasParseUrl = existingUrlVirImport.specifiers.some((specifier) => {
15
+ return (specifier.type === 'ImportSpecifier' &&
16
+ specifier.imported.type === 'Identifier' &&
17
+ specifier.imported.name === 'parseUrl');
18
+ });
17
19
  if (!hasParseUrl) {
18
20
  const lastSpecifier = existingUrlVirImport.specifiers.at(-1);
19
21
  if (lastSpecifier) {
@@ -14,11 +14,15 @@ function findRemoveDuplicatesImport(program) {
14
14
  importDecl.importKind === 'type') {
15
15
  return accum;
16
16
  }
17
- const namedSpecifiers = importDecl.specifiers.filter((spec) => spec.type === 'ImportSpecifier' &&
18
- spec.importKind !== 'type');
19
- const removeDuplicatesSpecifier = namedSpecifiers.find((spec) => spec.type === 'ImportSpecifier' &&
20
- spec.imported.type === 'Identifier' &&
21
- spec.imported.name === 'removeDuplicates');
17
+ const namedSpecifiers = importDecl.specifiers.filter((spec) => {
18
+ return (spec.type === 'ImportSpecifier' &&
19
+ spec.importKind !== 'type');
20
+ });
21
+ const removeDuplicatesSpecifier = namedSpecifiers.find((spec) => {
22
+ return (spec.type === 'ImportSpecifier' &&
23
+ spec.imported.type === 'Identifier' &&
24
+ spec.imported.name === 'removeDuplicates');
25
+ });
22
26
  const localName = removeDuplicatesSpecifier?.type === 'ImportSpecifier'
23
27
  ? removeDuplicatesSpecifier.local.name
24
28
  : undefined;
@@ -28,7 +28,9 @@ const rule = {
28
28
  node,
29
29
  messageId: 'preferToSorted',
30
30
  fix(fixer) {
31
- const argumentSourceTexts = node.arguments.map((argument) => context.sourceCode.getText(argument));
31
+ const argumentSourceTexts = node.arguments.map((argument) => {
32
+ return context.sourceCode.getText(argument);
33
+ });
32
34
  const sourceText = context.sourceCode.getText(spreadArgument);
33
35
  /**
34
36
  * Member access binds tighter than most expression forms, so a receiver
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/lint",
3
- "version": "14.30.3",
3
+ "version": "14.30.4",
4
4
  "description": "Default ESLint linting plugin for virmator.",
5
5
  "keywords": [
6
6
  "virmator",
@@ -35,8 +35,8 @@
35
35
  "@augment-vir/assert": "^32.2.1",
36
36
  "@augment-vir/common": "^32.2.1",
37
37
  "@augment-vir/node": "^32.2.1",
38
- "@virmator/core": "^14.30.3",
39
- "globals": "^17.7.0",
38
+ "@virmator/core": "^14.30.4",
39
+ "globals": "^17.9.0",
40
40
  "jiti": "^2.7.0",
41
41
  "mri": "^1.2.0"
42
42
  },
@@ -46,18 +46,18 @@
46
46
  "@stylistic/eslint-plugin": "^5.10.0",
47
47
  "@stylistic/eslint-plugin-ts": "^4.4.1",
48
48
  "@types/estree": "^1.0.9",
49
- "@types/node": "^26.1.1",
50
- "@typescript-eslint/eslint-plugin": "^8.65.0",
49
+ "@types/node": "^26.1.2",
50
+ "@typescript-eslint/eslint-plugin": "^8.66.0",
51
51
  "c8": "^12.0.0",
52
- "eslint": "^10.7.0",
52
+ "eslint": "^10.8.0",
53
53
  "eslint-config-prettier": "^10.1.8",
54
- "eslint-plugin-jsdoc": "^63.2.0",
55
- "eslint-plugin-playwright": "^2.10.5",
54
+ "eslint-plugin-jsdoc": "^63.3.3",
55
+ "eslint-plugin-playwright": "^2.11.0",
56
56
  "eslint-plugin-prettier": "^5.5.6",
57
57
  "eslint-plugin-sonarjs": "^4.2.0",
58
- "eslint-plugin-unicorn": "^72.0.0",
58
+ "eslint-plugin-unicorn": "^73.0.0",
59
59
  "istanbul-smart-text-reporter": "^1.1.5",
60
- "typescript-eslint": "^8.65.0"
60
+ "typescript-eslint": "^8.66.0"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">=22"
@@ -160,10 +160,9 @@ function childrenOf(
160
160
  const value = (node as unknown as Record<string, unknown>)[key];
161
161
 
162
162
  if (Array.isArray(value)) {
163
- return value.filter(
164
- (item): item is Node =>
165
- item != undefined && typeof (item as Node).type === 'string',
166
- );
163
+ return value.filter((item): item is Node => {
164
+ return item != undefined && typeof (item as Node).type === 'string';
165
+ });
167
166
  } else if (value != undefined && typeof (value as Node).type === 'string') {
168
167
  return [value as Node];
169
168
  } else {
@@ -256,8 +255,12 @@ function getTestCallback(
256
255
  node: CallExpression,
257
256
  ): ArrowFunctionExpression | FunctionExpression | undefined {
258
257
  return node.arguments.find(
259
- (argument): argument is ArrowFunctionExpression | FunctionExpression =>
260
- argument.type === 'FunctionExpression' || argument.type === 'ArrowFunctionExpression',
258
+ (argument): argument is ArrowFunctionExpression | FunctionExpression => {
259
+ return (
260
+ argument.type === 'FunctionExpression' ||
261
+ argument.type === 'ArrowFunctionExpression'
262
+ );
263
+ },
261
264
  );
262
265
  }
263
266
 
@@ -36,13 +36,14 @@ function isThisNameMember(node: AssignmentExpression['left']) {
36
36
 
37
37
  /** Determine whether a class already declares a (non-computed) `name` member. */
38
38
  function hasNameMember(node: Readonly<Class>) {
39
- return node.body.body.some(
40
- (member) =>
39
+ return node.body.body.some((member) => {
40
+ return (
41
41
  (member.type === 'PropertyDefinition' || member.type === 'MethodDefinition') &&
42
42
  !member.computed &&
43
43
  member.key.type === 'Identifier' &&
44
- member.key.name === 'name',
45
- );
44
+ member.key.name === 'name'
45
+ );
46
+ });
46
47
  }
47
48
 
48
49
  const rule: Rule.RuleModule = {
@@ -119,9 +120,9 @@ const rule: Rule.RuleModule = {
119
120
  * constructor body (e.g. a nested `class Inner { method() { this.name = 'x'; }
120
121
  * }`).
121
122
  */
122
- const enclosingFunctionIndex = ancestors.findLastIndex((ancestor) =>
123
- functionNodeTypes.includes(ancestor.type),
124
- );
123
+ const enclosingFunctionIndex = ancestors.findLastIndex((ancestor) => {
124
+ return functionNodeTypes.includes(ancestor.type);
125
+ });
125
126
  if (enclosingFunctionIndex < 0) {
126
127
  return;
127
128
  }
@@ -0,0 +1,87 @@
1
+ import {type Rule} from 'eslint';
2
+ import type {ArrowFunctionExpression} from 'estree';
3
+
4
+ const rule: Rule.RuleModule = {
5
+ meta: {
6
+ type: 'suggestion',
7
+ fixable: 'code',
8
+ messages: {
9
+ preferBlockBody:
10
+ 'An arrow function that spans multiple lines must use a block body with an explicit `return`.',
11
+ },
12
+ schema: [],
13
+ },
14
+ create(context) {
15
+ const sourceCode = context.sourceCode;
16
+
17
+ return {
18
+ ArrowFunctionExpression(node: ArrowFunctionExpression & Rule.NodeParentExtension) {
19
+ /** `() => ({...})` is handled by the `prefer-arrow-object-block-body` rule. */
20
+ if (node.body.type === 'BlockStatement' || node.body.type === 'ObjectExpression') {
21
+ return;
22
+ }
23
+
24
+ const arrowToken = sourceCode.getTokenBefore(node.body, {
25
+ filter(token) {
26
+ return token.type === 'Punctuator' && token.value === '=>';
27
+ },
28
+ });
29
+
30
+ /**
31
+ * Measure from `=>` rather than from the start of the arrow function so that a
32
+ * wrapped parameter list with a body that still fits on the `=>` line is not
33
+ * flagged.
34
+ */
35
+ if (
36
+ !arrowToken?.loc ||
37
+ !node.body.loc ||
38
+ arrowToken.loc.start.line === node.body.loc.end.line
39
+ ) {
40
+ return;
41
+ }
42
+
43
+ context.report({
44
+ node: node.body,
45
+ messageId: 'preferBlockBody',
46
+ fix(fixer) {
47
+ /**
48
+ * A concise body can be wrapped in parentheses (`() => (\n a + b \n)`),
49
+ * which are tokens rather than AST nodes. The replacement must swallow them
50
+ * or the fix leaves the new block body inside stray parentheses.
51
+ */
52
+ const openParens = sourceCode.getTokensBetween(arrowToken, node.body);
53
+ const closeParens = sourceCode.getTokensAfter(node.body, {
54
+ count: openParens.length,
55
+ });
56
+ const startToken = openParens[0] ?? sourceCode.getFirstToken(node.body);
57
+ const endToken = closeParens.at(-1) ?? sourceCode.getLastToken(node.body);
58
+
59
+ if (
60
+ !startToken ||
61
+ !endToken ||
62
+ closeParens.length !== openParens.length ||
63
+ openParens.some((token) => {
64
+ return token.value !== '(';
65
+ }) ||
66
+ closeParens.some((token) => {
67
+ return token.value !== ')';
68
+ })
69
+ ) {
70
+ return null;
71
+ }
72
+
73
+ return fixer.replaceTextRange(
74
+ [
75
+ startToken.range[0],
76
+ endToken.range[1],
77
+ ],
78
+ `{ return ${sourceCode.getText(node.body)}; }`,
79
+ );
80
+ },
81
+ });
82
+ },
83
+ };
84
+ },
85
+ };
86
+
87
+ export default rule;
@@ -165,9 +165,11 @@ function buildParamsObjectFix(
165
165
  const definedParams = fixableParams.filter((fixable) => fixable != undefined);
166
166
 
167
167
  const patternText = definedParams
168
- .map((param) =>
169
- param.defaultText == undefined ? param.name : `${param.name} = ${param.defaultText}`,
170
- )
168
+ .map((param) => {
169
+ return param.defaultText == undefined
170
+ ? param.name
171
+ : `${param.name} = ${param.defaultText}`;
172
+ })
171
173
  .join(', ');
172
174
  const typeText = definedParams
173
175
  .map((param) => `${param.name}${param.optional ? '?' : ''}: ${param.typeText}`)
@@ -182,14 +184,15 @@ function buildParamsObjectFix(
182
184
  const start = sourceCode.getRange(firstParam)[0];
183
185
  const end = sourceCode.getRange(lastParam)[1];
184
186
 
185
- return (fixer) =>
186
- fixer.replaceTextRange(
187
+ return (fixer) => {
188
+ return fixer.replaceTextRange(
187
189
  [
188
190
  start,
189
191
  end,
190
192
  ],
191
193
  `{${patternText}}: Readonly<{${typeText}}>`,
192
194
  );
195
+ };
193
196
  }
194
197
 
195
198
  const rule: Rule.RuleModule = {
@@ -25,12 +25,13 @@ function addParseUrlImport(
25
25
  const existingUrlVirImport = importDeclarations.find((imp) => imp.source.value === 'url-vir');
26
26
 
27
27
  if (existingUrlVirImport) {
28
- const hasParseUrl = existingUrlVirImport.specifiers.some(
29
- (specifier) =>
28
+ const hasParseUrl = existingUrlVirImport.specifiers.some((specifier) => {
29
+ return (
30
30
  specifier.type === 'ImportSpecifier' &&
31
31
  specifier.imported.type === 'Identifier' &&
32
- specifier.imported.name === 'parseUrl',
33
- );
32
+ specifier.imported.name === 'parseUrl'
33
+ );
34
+ });
34
35
 
35
36
  if (!hasParseUrl) {
36
37
  const lastSpecifier = existingUrlVirImport.specifiers.at(-1);
@@ -42,18 +42,20 @@ function findRemoveDuplicatesImport(program: Readonly<Rule.Node>): RemoveDuplica
42
42
  return accum;
43
43
  }
44
44
 
45
- const namedSpecifiers = importDecl.specifiers.filter(
46
- (spec) =>
45
+ const namedSpecifiers = importDecl.specifiers.filter((spec) => {
46
+ return (
47
47
  spec.type === 'ImportSpecifier' &&
48
- (spec as unknown as {importKind?: string}).importKind !== 'type',
49
- );
48
+ (spec as unknown as {importKind?: string}).importKind !== 'type'
49
+ );
50
+ });
50
51
 
51
- const removeDuplicatesSpecifier = namedSpecifiers.find(
52
- (spec) =>
52
+ const removeDuplicatesSpecifier = namedSpecifiers.find((spec) => {
53
+ return (
53
54
  spec.type === 'ImportSpecifier' &&
54
55
  spec.imported.type === 'Identifier' &&
55
- spec.imported.name === 'removeDuplicates',
56
- );
56
+ spec.imported.name === 'removeDuplicates'
57
+ );
58
+ });
57
59
 
58
60
  const localName =
59
61
  removeDuplicatesSpecifier?.type === 'ImportSpecifier'
@@ -39,9 +39,9 @@ const rule: Rule.RuleModule = {
39
39
  node,
40
40
  messageId: 'preferToSorted',
41
41
  fix(fixer) {
42
- const argumentSourceTexts = node.arguments.map((argument) =>
43
- context.sourceCode.getText(argument as unknown as Rule.Node),
44
- );
42
+ const argumentSourceTexts = node.arguments.map((argument) => {
43
+ return context.sourceCode.getText(argument as unknown as Rule.Node);
44
+ });
45
45
  const sourceText = context.sourceCode.getText(
46
46
  spreadArgument as unknown as Rule.Node,
47
47
  );