@virmator/lint 14.29.1 → 14.30.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.
@@ -12,6 +12,7 @@ import noRawDateRule from '../src/rules/no-raw-date.lint.js';
12
12
  import noReadonlyPrimitiveRule from '../src/rules/no-readonly-primitive.lint.js';
13
13
  import noRelativeImportOutsidePackageRule from '../src/rules/no-relative-import-outside-package.lint.js';
14
14
  import noShouldInTestDescriptionRule from '../src/rules/no-should-in-test-description.lint.js';
15
+ import noSwitchRule from '../src/rules/no-switch.lint.js';
15
16
  import noUnderscoreVariableRule from '../src/rules/no-underscore-variable.lint.js';
16
17
  import preferArrowObjectBlockBodyRule from '../src/rules/prefer-arrow-object-block-body.lint.js';
17
18
  import preferEnsureErrorRule from '../src/rules/prefer-ensure-error.lint.js';
@@ -80,6 +81,7 @@ export function defineEslintConfig(repoDir: string) {
80
81
  'no-readonly-primitive': noReadonlyPrimitiveRule,
81
82
  'no-relative-import-outside-package': noRelativeImportOutsidePackageRule,
82
83
  'no-should-in-test-description': noShouldInTestDescriptionRule,
84
+ 'no-switch': noSwitchRule,
83
85
  'no-underscore-variable': noUnderscoreVariableRule,
84
86
  'prefer-arrow-object-block-body': preferArrowObjectBlockBodyRule,
85
87
  'prefer-ensure-error': preferEnsureErrorRule,
@@ -166,7 +168,6 @@ export function defineEslintConfig(repoDir: string) {
166
168
  'unicorn/no-useless-fallback-in-spread': 'error',
167
169
  'unicorn/no-useless-length-check': 'error',
168
170
  'unicorn/no-useless-spread': 'error',
169
- 'unicorn/no-useless-switch-case': 'error',
170
171
  'unicorn/no-zero-fractions': 'error',
171
172
  'unicorn/numeric-separators-style': 'error',
172
173
  'unicorn/prefer-array-find': 'error',
@@ -202,6 +203,7 @@ export function defineEslintConfig(repoDir: string) {
202
203
  '@virmator/no-readonly-primitive': 'error',
203
204
  '@virmator/no-relative-import-outside-package': 'error',
204
205
  '@virmator/no-should-in-test-description': 'error',
206
+ '@virmator/no-switch': 'error',
205
207
  '@virmator/no-underscore-variable': 'error',
206
208
  '@virmator/prefer-arrow-object-block-body': 'error',
207
209
  '@virmator/prefer-ensure-error': 'error',
@@ -35,6 +35,12 @@ const objectShapeTesterAssertionNames = [
35
35
  'assertValidShape',
36
36
  'assertWrapValidShape',
37
37
  ];
38
+ /**
39
+ * Names of `@augment-vir/test` exports that assert, so a test that calls one is asserting.
40
+ * Recognized by name rather than by treating the whole module as assertion utilities because most
41
+ * of its exports (`describe`, `it`, etc.) define tests instead of asserting within them.
42
+ */
43
+ const augmentVirTestAssertionNames = ['assertSnapshot'];
38
44
  /** Names of the test-defining functions whose callbacks must contain at least one assertion. */
39
45
  const testFunctionNames = [
40
46
  'it',
@@ -209,6 +215,7 @@ const rule = {
209
215
  const names = [
210
216
  ...augmentVirAssertionNames,
211
217
  ...objectShapeTesterAssertionNames,
218
+ ...augmentVirTestAssertionNames,
212
219
  ...(options.additionalAssertionNames || []),
213
220
  ];
214
221
  const assertionIdentifiers = collectAssertionIdentifiers(context.sourceCode.ast.body, {
@@ -0,0 +1,3 @@
1
+ import { type Rule } from 'eslint';
2
+ declare const rule: Rule.RuleModule;
3
+ export default rule;
@@ -0,0 +1,22 @@
1
+ const rule = {
2
+ meta: {
3
+ type: 'suggestion',
4
+ messages: {
5
+ noSwitch: 'Do not use `switch`. Use a `Record` object mapping (for enum or other fixed key sets) or an if/else chain instead.',
6
+ },
7
+ schema: [],
8
+ },
9
+ create(context) {
10
+ return {
11
+ SwitchStatement(node) {
12
+ context.report({
13
+ node,
14
+ /** Report just the `switch` keyword so a long switch body isn't all underlined. */
15
+ loc: context.sourceCode.getFirstToken(node)?.loc ?? node.loc ?? undefined,
16
+ messageId: 'noSwitch',
17
+ });
18
+ },
19
+ };
20
+ },
21
+ };
22
+ export default rule;
@@ -1,17 +1,40 @@
1
+ import { check } from '@augment-vir/assert';
1
2
  /**
2
- * Inline callbacks (function or arrow expressions passed directly as call arguments, like
3
- * `array.reduce((accum, entry, index, wholeArray) => ...)`) have a signature dictated by their
4
- * caller, so they cannot adopt a params object. Such callbacks should be skipped.
3
+ * Determines whether the given node sits inside an argument of a call or `new` expression, looking
4
+ * through object and array literals so that callbacks nested in a config object still count.
5
5
  */
6
- function isInlineCallbackArgument(node) {
7
- if (node.type === 'FunctionDeclaration') {
6
+ function isWithinCallArgument(node) {
7
+ const parent = node.parent;
8
+ if (!parent) {
8
9
  return false;
9
10
  }
10
- const parent = node.parent;
11
- if (!parent || parent.type !== 'CallExpression') {
11
+ else if (parent.type === 'CallExpression' || parent.type === 'NewExpression') {
12
+ return check.hasValue(parent.arguments, node);
13
+ }
14
+ else if (parent.type === 'Property') {
15
+ return parent.value === node && isWithinCallArgument(parent);
16
+ }
17
+ else if (parent.type === 'ObjectExpression' ||
18
+ parent.type === 'ArrayExpression' ||
19
+ parent.type === 'SpreadElement') {
20
+ return isWithinCallArgument(parent);
21
+ }
22
+ else {
23
+ return false;
24
+ }
25
+ }
26
+ /**
27
+ * Callbacks passed to an interface we don't control have a signature dictated by that interface, so
28
+ * they cannot adopt a params object. This covers both function or arrow expressions passed directly
29
+ * as call arguments (like `array.reduce((accum, entry, index, wholeArray) => ...)`) and methods or
30
+ * function properties within an object literal passed as a call argument (like `new
31
+ * IntervalObservable({equalityCheck(a, b) {...}})`). Such callbacks should be skipped.
32
+ */
33
+ function isInlineCallbackArgument(node) {
34
+ if (node.type === 'FunctionDeclaration') {
12
35
  return false;
13
36
  }
14
- return parent.arguments.includes(node);
37
+ return isWithinCallArgument(node);
15
38
  }
16
39
  /**
17
40
  * Class constructors are exempt: they can declare class fields through parameter properties and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@virmator/lint",
3
- "version": "14.29.1",
3
+ "version": "14.30.1",
4
4
  "description": "Default ESLint linting plugin for virmator.",
5
5
  "keywords": [
6
6
  "virmator",
@@ -32,22 +32,22 @@
32
32
  "test:update": "npm test update"
33
33
  },
34
34
  "dependencies": {
35
- "@augment-vir/assert": "^32.1.0",
36
- "@augment-vir/common": "^32.1.0",
37
- "@augment-vir/node": "^32.1.0",
38
- "@virmator/core": "^14.29.1",
35
+ "@augment-vir/assert": "^32.2.1",
36
+ "@augment-vir/common": "^32.2.1",
37
+ "@augment-vir/node": "^32.2.1",
38
+ "@virmator/core": "^14.30.1",
39
39
  "globals": "^17.7.0",
40
40
  "jiti": "^2.7.0",
41
41
  "mri": "^1.2.0"
42
42
  },
43
43
  "devDependencies": {
44
- "@augment-vir/test": "^32.1.0",
44
+ "@augment-vir/test": "^32.2.1",
45
45
  "@eslint/js": "10.0.1",
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
49
  "@types/node": "^26.1.1",
50
- "@typescript-eslint/eslint-plugin": "^8.64.0",
50
+ "@typescript-eslint/eslint-plugin": "^8.65.0",
51
51
  "c8": "^12.0.0",
52
52
  "eslint": "^10.7.0",
53
53
  "eslint-config-prettier": "^10.1.8",
@@ -57,7 +57,7 @@
57
57
  "eslint-plugin-sonarjs": "^4.2.0",
58
58
  "eslint-plugin-unicorn": "^72.0.0",
59
59
  "istanbul-smart-text-reporter": "^1.1.5",
60
- "typescript-eslint": "^8.64.0"
60
+ "typescript-eslint": "^8.65.0"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">=22"
@@ -49,6 +49,13 @@ const objectShapeTesterAssertionNames: ReadonlyArray<string> = [
49
49
  'assertWrapValidShape',
50
50
  ];
51
51
 
52
+ /**
53
+ * Names of `@augment-vir/test` exports that assert, so a test that calls one is asserting.
54
+ * Recognized by name rather than by treating the whole module as assertion utilities because most
55
+ * of its exports (`describe`, `it`, etc.) define tests instead of asserting within them.
56
+ */
57
+ const augmentVirTestAssertionNames: ReadonlyArray<string> = ['assertSnapshot'];
58
+
52
59
  /** Names of the test-defining functions whose callbacks must contain at least one assertion. */
53
60
  const testFunctionNames: ReadonlyArray<string> = [
54
61
  'it',
@@ -295,6 +302,7 @@ const rule: Rule.RuleModule = {
295
302
  const names = [
296
303
  ...augmentVirAssertionNames,
297
304
  ...objectShapeTesterAssertionNames,
305
+ ...augmentVirTestAssertionNames,
298
306
  ...(options.additionalAssertionNames || []),
299
307
  ];
300
308
 
@@ -0,0 +1,27 @@
1
+ import {type Rule} from 'eslint';
2
+ import type {SwitchStatement} from 'estree';
3
+
4
+ const rule: Rule.RuleModule = {
5
+ meta: {
6
+ type: 'suggestion',
7
+ messages: {
8
+ noSwitch:
9
+ 'Do not use `switch`. Use a `Record` object mapping (for enum or other fixed key sets) or an if/else chain instead.',
10
+ },
11
+ schema: [],
12
+ },
13
+ create(context) {
14
+ return {
15
+ SwitchStatement(node: SwitchStatement & Rule.NodeParentExtension) {
16
+ context.report({
17
+ node,
18
+ /** Report just the `switch` keyword so a long switch body isn't all underlined. */
19
+ loc: context.sourceCode.getFirstToken(node)?.loc ?? node.loc ?? undefined,
20
+ messageId: 'noSwitch',
21
+ });
22
+ },
23
+ };
24
+ },
25
+ };
26
+
27
+ export default rule;
@@ -1,3 +1,4 @@
1
+ import {check} from '@augment-vir/assert';
1
2
  import {type Rule, type SourceCode} from 'eslint';
2
3
  import type {
3
4
  ArrowFunctionExpression,
@@ -27,22 +28,42 @@ type FixableParam = {
27
28
  };
28
29
 
29
30
  /**
30
- * Inline callbacks (function or arrow expressions passed directly as call arguments, like
31
- * `array.reduce((accum, entry, index, wholeArray) => ...)`) have a signature dictated by their
32
- * caller, so they cannot adopt a params object. Such callbacks should be skipped.
31
+ * Determines whether the given node sits inside an argument of a call or `new` expression, looking
32
+ * through object and array literals so that callbacks nested in a config object still count.
33
33
  */
34
- function isInlineCallbackArgument(node: AnyFunctionNode): boolean {
35
- if (node.type === 'FunctionDeclaration') {
34
+ function isWithinCallArgument(node: Node): boolean {
35
+ const parent = (node as Rule.Node).parent;
36
+
37
+ if (!parent) {
38
+ return false;
39
+ } else if (parent.type === 'CallExpression' || parent.type === 'NewExpression') {
40
+ return check.hasValue(parent.arguments, node);
41
+ } else if (parent.type === 'Property') {
42
+ return parent.value === node && isWithinCallArgument(parent);
43
+ } else if (
44
+ parent.type === 'ObjectExpression' ||
45
+ parent.type === 'ArrayExpression' ||
46
+ parent.type === 'SpreadElement'
47
+ ) {
48
+ return isWithinCallArgument(parent);
49
+ } else {
36
50
  return false;
37
51
  }
52
+ }
38
53
 
39
- const parent = (node as Rule.Node).parent;
40
-
41
- if (!parent || parent.type !== 'CallExpression') {
54
+ /**
55
+ * Callbacks passed to an interface we don't control have a signature dictated by that interface, so
56
+ * they cannot adopt a params object. This covers both function or arrow expressions passed directly
57
+ * as call arguments (like `array.reduce((accum, entry, index, wholeArray) => ...)`) and methods or
58
+ * function properties within an object literal passed as a call argument (like `new
59
+ * IntervalObservable({equalityCheck(a, b) {...}})`). Such callbacks should be skipped.
60
+ */
61
+ function isInlineCallbackArgument(node: AnyFunctionNode): boolean {
62
+ if (node.type === 'FunctionDeclaration') {
42
63
  return false;
43
64
  }
44
65
 
45
- return parent.arguments.includes(node);
66
+ return isWithinCallArgument(node);
46
67
  }
47
68
 
48
69
  /**