@zukhruf/eslint 0.4.0 → 0.4.2

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
@@ -22,7 +22,7 @@ You need ESLint 9.30 or later.
22
22
  npm install --save-dev @zukhruf/eslint
23
23
  ```
24
24
 
25
- Its peers are `eslint`, `@eslint/js`, `typescript-eslint`, `typescript`, `eslint-plugin-import-x`, `eslint-plugin-functional` and `jsonc-eslint-parser`. `@zukhruf/eslint/nx` also needs `@nx/eslint-plugin`, and `@zukhruf/eslint/react-native` needs `eslint-plugin-es-x`. npm installs the peers for you. With `legacy-peer-deps=true` in `.npmrc`, npm does not, so add them to your devDependencies.
25
+ Its peers are `eslint`, `@eslint/js`, `typescript-eslint`, `typescript`, `eslint-plugin-import-x`, `eslint-plugin-functional` and `jsonc-eslint-parser`. `@zukhruf/eslint/nx` also needs `@nx/eslint-plugin`, `@zukhruf/eslint/react` needs `@nx/eslint-plugin` and the 4 plugins in [React](#react), and `@zukhruf/eslint/react-native` needs `eslint-plugin-es-x`. npm installs the peers for you. With `legacy-peer-deps=true` in `.npmrc`, npm does not, so add them to your devDependencies.
26
26
 
27
27
  ```js
28
28
  // eslint.config.mjs
@@ -36,7 +36,7 @@ export default defineConfig({
36
36
  });
37
37
  ```
38
38
 
39
- Put framework presets (for example Nx's `flat/react`) **before** the zukhruf configs. The package's own checks survive any order. But `base` also changes some third-party rules, for example it turns `@typescript-eslint/no-unused-vars` off, and a preset that comes later sets them again.
39
+ Put framework presets (for example [`@zukhruf/eslint/react`](#react), which is Nx's `flat/react`) **before** the zukhruf configs. The package's own checks survive any order. But `base` also changes some third-party rules, for example it turns `@typescript-eslint/no-unused-vars` off, and a preset that comes later sets them again.
40
40
 
41
41
  ## Configs
42
42
 
@@ -201,6 +201,30 @@ The rule decides each manifest's options itself, so your options add to the shar
201
201
 
202
202
  `manifest/recommended` also has `manifest/no-truncated-description`. The npm registry keeps only the first 255 characters of a package's `description`. `npm view` and the npm search do not show the rest. The rule reports a `description` that is longer than 255 characters. It does not check a manifest with `"private": true`, because npm does not publish it. Put the key fact in the description, and put the details in the README.
203
203
 
204
+ ## React
205
+
206
+ `@zukhruf/eslint/react` is Nx's `flat/react` config from `@nx/eslint-plugin`, with no changes. Use it in place of `nx.configs['flat/react']` in the `eslint.config.mjs` of a React project. Put it first, and then the root config, as before:
207
+
208
+ ```js
209
+ import react from '@zukhruf/eslint/react';
210
+
211
+ import baseConfig from '../../eslint.config.mjs';
212
+
213
+ export default [...react, ...baseConfig];
214
+ ```
215
+
216
+ `flat/react` loads `eslint-plugin-import`, `eslint-plugin-jsx-a11y`, `eslint-plugin-react` and `eslint-plugin-react-hooks`. Install these 4 plugins in the React workspace. They are optional peers of this package, because `@nx/eslint-plugin` (checked at 24.0.2) does not declare them. With the peers, npm writes the lockfile edges from `@zukhruf/eslint` to the 4 plugins, so a lint input on `@zukhruf/eslint` also hashes the plugins.
217
+
218
+ The peer ranges of `eslint-plugin-import`, `eslint-plugin-jsx-a11y` and `eslint-plugin-react` stop at ESLint 9. On ESLint 10, add an `overrides` entry for each of the 3 to `package.json`, as factory and whatisgoingon do:
219
+
220
+ ```json
221
+ "overrides": {
222
+ "eslint-plugin-import": { "eslint": "$eslint" },
223
+ "eslint-plugin-jsx-a11y": { "eslint": "$eslint" },
224
+ "eslint-plugin-react": { "eslint": "$eslint" }
225
+ }
226
+ ```
227
+
204
228
  ## React Native: Hermes
205
229
 
206
230
  `@zukhruf/eslint/react-native` needs `eslint-plugin-es-x`. es-x 10 needs ESLint 10.6 or later; on ESLint 9, install `eslint-plugin-es-x@9`. `hermes/recommended` checks the code that ships in the app, and leaves tests, config files and scripts alone, because those run in Node:
@@ -2,7 +2,8 @@ import type { Rule } from 'eslint';
2
2
  /**
3
3
  * A `for await` loop whose only statement pushes each value into an array is
4
4
  * `Array.fromAsync`, with the push's expression as its map function. A loop
5
- * that pushes on a condition, adds to a Set, or does more stays a loop.
5
+ * that pushes on a condition, adds to a Set, or does more stays a loop, and so
6
+ * does a loop whose array other code reads before the loop ends.
6
7
  */
7
8
  declare const rule: Rule.RuleModule;
8
9
  export default rule;
@@ -1 +1 @@
1
- {"version":3,"file":"no-collect-loop.d.ts","sourceRoot":"","sources":["../../src/async-values/no-collect-loop.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAoDnC;;;;GAIG;AACH,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UAyChB,CAAC;AAEF,eAAe,IAAI,CAAC"}
1
+ {"version":3,"file":"no-collect-loop.d.ts","sourceRoot":"","sources":["../../src/async-values/no-collect-loop.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AA+GnC;;;;;GAKG;AACH,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UAmDhB,CAAC;AAEF,eAAe,IAAI,CAAC"}
@@ -6,8 +6,8 @@ function onlyStatement(body) {
6
6
  const statements = Array.isArray(body['body']) ? body['body'] : [];
7
7
  return statements.length === 1 ? statements[0] : undefined;
8
8
  }
9
- /** The argument of `<array>.push(<argument>)`, the whole statement. */
10
- function pushedValue(statement) {
9
+ /** The `<array>.push(<argument>)` call that is the whole statement. */
10
+ function pushCall(statement) {
11
11
  if (!isType(statement, 'ExpressionStatement'))
12
12
  return undefined;
13
13
  const call = statement['expression'];
@@ -22,7 +22,55 @@ function pushedValue(statement) {
22
22
  isType(args[0], 'SpreadElement')) {
23
23
  return undefined;
24
24
  }
25
- return args[0];
25
+ return call;
26
+ }
27
+ /** The function whose body holds `node`, nearest first. */
28
+ function enclosingFunction(node) {
29
+ for (let current = node['parent']; isAstNode(current);) {
30
+ if (isType(current, 'FunctionExpression') ||
31
+ isType(current, 'ArrowFunctionExpression') ||
32
+ isType(current, 'FunctionDeclaration')) {
33
+ return current;
34
+ }
35
+ current = current['parent'];
36
+ }
37
+ return undefined;
38
+ }
39
+ function contains(outer, node) {
40
+ for (let current = node; isAstNode(current); current = current['parent']) {
41
+ if (current === outer)
42
+ return true;
43
+ }
44
+ return false;
45
+ }
46
+ /**
47
+ * Other code can read the array while the loop runs when the loop sits in a
48
+ * function called in place whose promise is stored or returned, and the array
49
+ * is declared outside that function. `Array.fromAsync` gives the array only
50
+ * when the iterable ends. A promise awaited in place, or handed to a call,
51
+ * ends before the next statement reads the array, as far as the AST shows.
52
+ */
53
+ function readWhileFilling(context, loop, push) {
54
+ const fn = enclosingFunction(loop);
55
+ const call = fn?.['parent'];
56
+ if (fn === undefined ||
57
+ !isType(call, 'CallExpression') ||
58
+ call['callee'] !== fn ||
59
+ !(isType(call['parent'], 'VariableDeclarator') ||
60
+ isType(call['parent'], 'AssignmentExpression') ||
61
+ isType(call['parent'], 'ReturnStatement'))) {
62
+ return false;
63
+ }
64
+ const callee = push['callee'];
65
+ const array = isType(callee, 'MemberExpression')
66
+ ? callee['object']
67
+ : undefined;
68
+ if (!isType(array, 'Identifier'))
69
+ return false;
70
+ const variable = context.sourceCode
71
+ .getScope(array)
72
+ .references.find(({ identifier }) => identifier === array)?.resolved;
73
+ return !contains(fn, variable?.scope.block);
26
74
  }
27
75
  /** The identifiers in `node`, a binding pattern or an expression. */
28
76
  function identifiers(node) {
@@ -43,7 +91,8 @@ function identifiers(node) {
43
91
  /**
44
92
  * A `for await` loop whose only statement pushes each value into an array is
45
93
  * `Array.fromAsync`, with the push's expression as its map function. A loop
46
- * that pushes on a condition, adds to a Set, or does more stays a loop.
94
+ * that pushes on a condition, adds to a Set, or does more stays a loop, and so
95
+ * does a loop whose array other code reads before the loop ends.
47
96
  */
48
97
  const rule = {
49
98
  meta: {
@@ -70,9 +119,16 @@ const rule = {
70
119
  const pattern = declarations.length === 1 && isAstNode(declarations[0])
71
120
  ? declarations[0]['id']
72
121
  : undefined;
73
- const value = pushedValue(onlyStatement(loop['body']));
74
- if (pattern === undefined || value === undefined)
122
+ const push = pushCall(onlyStatement(loop['body']));
123
+ const args = Array.isArray(push?.['arguments'])
124
+ ? push['arguments']
125
+ : [];
126
+ const value = args[0];
127
+ if (pattern === undefined ||
128
+ push === undefined ||
129
+ readWhileFilling(context, loop, push)) {
75
130
  return;
131
+ }
76
132
  const bound = identifiers(pattern);
77
133
  if (isType(pattern, 'Identifier') &&
78
134
  identifierName(value) === bound[0]) {
@@ -1 +1 @@
1
- {"version":3,"file":"no-collect-loop.js","sourceRoot":"","sources":["../../src/async-values/no-collect-loop.ts"],"names":[],"mappings":"AAEA,OAAO,EAEL,cAAc,EACd,SAAS,EACT,MAAM,GACP,MAAM,qBAAqB,CAAC;AAE7B,gEAAgE;AAChE,SAAS,aAAa,CAAC,IAAa;IAClC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,gBAAgB,CAAC;QAAE,OAAO,IAAI,CAAC;IACjD,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACnE,OAAO,UAAU,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC7D,CAAC;AAED,uEAAuE;AACvE,SAAS,WAAW,CAAC,SAAkB;IACrC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,qBAAqB,CAAC;QAAE,OAAO,SAAS,CAAC;IAChE,MAAM,IAAI,GAAG,SAAS,CAAC,YAAY,CAAC,CAAC;IACrC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,gBAAgB,CAAC;QAAE,OAAO,SAAS,CAAC;IACtD,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC9B,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC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d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.d.ts","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":";AAOA,wBAAwC"}
package/dist/react.js ADDED
@@ -0,0 +1,8 @@
1
+ import nx from '@nx/eslint-plugin';
2
+ // @nx/eslint-plugin (checked 24.0.2) requires eslint-plugin-import,
3
+ // eslint-plugin-jsx-a11y, eslint-plugin-react and eslint-plugin-react-hooks in
4
+ // flat/react, but declares none of them. This package declares them as optional
5
+ // peers, so npm writes the lockfile edges that Nx hashes for a lint input on
6
+ // @zukhruf/eslint. Delete the peers when @nx/eslint-plugin declares them.
7
+ export default nx.configs['flat/react'];
8
+ //# sourceMappingURL=react.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"react.js","sourceRoot":"","sources":["../src/react.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAEnC,oEAAoE;AACpE,+EAA+E;AAC/E,gFAAgF;AAChF,6EAA6E;AAC7E,0EAA0E;AAC1E,eAAe,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC"}
@@ -1,10 +1,12 @@
1
+ import type { Rule } from 'eslint';
1
2
  /**
2
3
  * The `mock` export of node:test belongs to no test: what it patches stays
3
4
  * patched until someone calls `mock.restoreAll()`, and that call also restores
4
5
  * the mocks of every other test that runs at the same time. `t.mock` belongs to
5
6
  * the test, and node:test restores it when the test ends, also when an assert
6
- * fails first.
7
+ * fails first. The import is allowed only when every use of it is a
8
+ * `mock.module(...)` statement of the program (see `isFileModuleMock`).
7
9
  */
8
- declare const _default: import("eslint").Rule.RuleModule;
9
- export default _default;
10
+ declare const rule: Rule.RuleModule;
11
+ export default rule;
10
12
  //# sourceMappingURL=no-global-test-mock.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"no-global-test-mock.d.ts","sourceRoot":"","sources":["../../src/test-files/no-global-test-mock.ts"],"names":[],"mappings":"AAIA;;;;;;GAMG;;AACH,wBAQG"}
1
+ {"version":3,"file":"no-global-test-mock.d.ts","sourceRoot":"","sources":["../../src/test-files/no-global-test-mock.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AA6BnC;;;;;;;GAOG;AACH,QAAA,MAAM,IAAI,EAAE,IAAI,CAAC,UAiChB,CAAC;AAEF,eAAe,IAAI,CAAC"}
@@ -1,18 +1,59 @@
1
- import { selectorRule } from "../authoring/selector-rule.js";
1
+ import { identifierName, isAstNode, isType } from "../authoring/ast.js";
2
2
  const NODE_TEST = '/^(node:)?test$/';
3
+ /**
4
+ * `mock.module(...)` as a statement of the program itself, outside every
5
+ * function, test and describe. node:test runs each test file in its own
6
+ * process, so that mock lives as long as the file and reaches no other file.
7
+ * A subject that binds a module when it loads needs it: a `t.mock.module`
8
+ * made inside one test cannot reach later tests, because the ESM cache keeps
9
+ * the first link.
10
+ */
11
+ function isFileModuleMock(reference) {
12
+ const access = isAstNode(reference) ? reference['parent'] : undefined;
13
+ const call = isAstNode(access) ? access['parent'] : undefined;
14
+ return (isType(access, 'MemberExpression') &&
15
+ access['object'] === reference &&
16
+ access['computed'] === false &&
17
+ identifierName(access['property']) === 'module' &&
18
+ isType(call, 'CallExpression') &&
19
+ call['callee'] === access &&
20
+ isType(call['parent'], 'ExpressionStatement') &&
21
+ isType(call['parent']['parent'], 'Program'));
22
+ }
3
23
  /**
4
24
  * The `mock` export of node:test belongs to no test: what it patches stays
5
25
  * patched until someone calls `mock.restoreAll()`, and that call also restores
6
26
  * the mocks of every other test that runs at the same time. `t.mock` belongs to
7
27
  * the test, and node:test restores it when the test ends, also when an assert
8
- * fails first.
28
+ * fails first. The import is allowed only when every use of it is a
29
+ * `mock.module(...)` statement of the program (see `isFileModuleMock`).
9
30
  */
10
- export default selectorRule({
11
- description: 'Disallow the global mock of node:test; use t.mock',
12
- selectors: [
13
- `ImportDeclaration[source.value=${NODE_TEST}] > ImportSpecifier[imported.name="mock"]`,
14
- `Program:has(ImportDeclaration[source.value=${NODE_TEST}]) MemberExpression[object.name=/^(test|it)$/][property.name="mock"]`,
15
- ],
16
- message: 'Use t.mock (the TestContext of this test): node:test restores it when the test ends, also when an assert fails.',
17
- });
31
+ const rule = {
32
+ meta: {
33
+ type: 'problem',
34
+ docs: {
35
+ description: 'Disallow the global mock of node:test; use t.mock',
36
+ },
37
+ schema: [],
38
+ messages: {
39
+ matched: 'Use t.mock (the TestContext of this test): node:test restores it when the test ends, also when an assert fails.',
40
+ },
41
+ },
42
+ create(context) {
43
+ const report = (node) => context.report({ node, messageId: 'matched' });
44
+ return {
45
+ [`ImportDeclaration[source.value=${NODE_TEST}] > ImportSpecifier[imported.name="mock"]`](specifier) {
46
+ const references = context.sourceCode
47
+ .getDeclaredVariables(specifier)
48
+ .flatMap((variable) => variable.references);
49
+ if (references.length === 0 ||
50
+ !references.every(({ identifier }) => isFileModuleMock(identifier))) {
51
+ report(specifier);
52
+ }
53
+ },
54
+ [`Program:has(ImportDeclaration[source.value=${NODE_TEST}]) MemberExpression[object.name=/^(test|it)$/][property.name="mock"]`]: report,
55
+ };
56
+ },
57
+ };
58
+ export default rule;
18
59
  //# sourceMappingURL=no-global-test-mock.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"no-global-test-mock.js","sourceRoot":"","sources":["../../src/test-files/no-global-test-mock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,+BAA+B,CAAC;AAE7D,MAAM,SAAS,GAAG,kBAAkB,CAAC;AAErC;;;;;;GAMG;AACH,eAAe,YAAY,CAAC;IAC1B,WAAW,EAAE,mDAAmD;IAChE,SAAS,EAAE;QACT,kCAAkC,SAAS,2CAA2C;QACtF,8CAA8C,SAAS,sEAAsE;KAC9H;IACD,OAAO,EACL,iHAAiH;CACpH,CAAC,CAAC"}
1
+ {"version":3,"file":"no-global-test-mock.js","sourceRoot":"","sources":["../../src/test-files/no-global-test-mock.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cA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;YACL,CAAC,kCAAkC,SAAS,2CAA2C,CAAC,CACtF,SAAoB;gBAEpB,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU;qBAClC,oBAAoB,CAAC,SAAS,CAAC;qBAC/B,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;gBAC9C,IACE,UAAU,CAAC,MAAM,KAAK,CAAC;oBACvB,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,EAAE,UAAU,EAAE,EAAE,EAAE,CAAC,gBAAgB,CAAC,UAAU,CAAC,CAAC,EACnE,CAAC;oBACD,MAAM,CAAC,SAAS,CAAC,CAAC;gBACpB,CAAC;YACH,CAAC;YACD,CAAC,8CAA8C,SAAS,sEAAsE,CAAC,EAC7H,MAAM;SACT,CAAC;IACJ,CAAC;CACF,CAAC;AAEF,eAAe,IAAI,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zukhruf/eslint",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "ESLint rules and configs shared across workspaces. Every check is its own named rule, so a repo's own config cannot silently erase it.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -38,6 +38,12 @@
38
38
  "types": "./dist/react-native.d.ts",
39
39
  "import": "./dist/react-native.js",
40
40
  "default": "./dist/react-native.js"
41
+ },
42
+ "./react": {
43
+ "@zukhruf/source": "./src/react.ts",
44
+ "types": "./dist/react.d.ts",
45
+ "import": "./dist/react.js",
46
+ "default": "./dist/react.js"
41
47
  }
42
48
  },
43
49
  "files": [
@@ -56,7 +62,7 @@
56
62
  "ts-api-utils": "^2.5.0"
57
63
  },
58
64
  "devDependencies": {
59
- "@zukhruf/testing": "^0.4.0"
65
+ "@zukhruf/testing": "^0.4.2"
60
66
  },
61
67
  "peerDependencies": {
62
68
  "@eslint/js": "^9.30.0 || ^10.0.0",
@@ -65,7 +71,11 @@
65
71
  "eslint": "^9.30.0 || ^10.0.0",
66
72
  "eslint-plugin-es-x": "^9.7.0 || ^10.0.0",
67
73
  "eslint-plugin-functional": "^10.0.0",
74
+ "eslint-plugin-import": "^2.31.0",
68
75
  "eslint-plugin-import-x": "^4.16.0",
76
+ "eslint-plugin-jsx-a11y": "^6.10.1",
77
+ "eslint-plugin-react": "^7.35.0",
78
+ "eslint-plugin-react-hooks": "^5.0.0 || ^7.0.0",
69
79
  "jsonc-eslint-parser": "^2.4.0 || ^3.0.0",
70
80
  "typescript": ">=5.8.0 <6.1.0",
71
81
  "typescript-eslint": "^8.47.0"
@@ -79,6 +89,18 @@
79
89
  },
80
90
  "eslint-plugin-es-x": {
81
91
  "optional": true
92
+ },
93
+ "eslint-plugin-import": {
94
+ "optional": true
95
+ },
96
+ "eslint-plugin-jsx-a11y": {
97
+ "optional": true
98
+ },
99
+ "eslint-plugin-react": {
100
+ "optional": true
101
+ },
102
+ "eslint-plugin-react-hooks": {
103
+ "optional": true
82
104
  }
83
105
  }
84
106
  }