@zukhruf/eslint 0.7.0 → 0.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 CHANGED
@@ -40,20 +40,20 @@ Put framework configs (for example [`@zukhruf/eslint/react`](#react)) **before**
40
40
 
41
41
  ## Configs
42
42
 
43
- | Config | Files | Rules |
44
- | ----------------- | ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
45
- | `base` | all source | `@eslint/js` and `typescript-eslint` recommended; `consistent-type-assertions` (never), `no-namespace` (a `declare` namespace allowed), `zukhruf/no-enum`, `no-empty-object-type` (an interface that extends one type allowed, such as `ProcessEnv` in `env.ts`; `no-empty-interface`, which it replaced, is off); `import-x/no-duplicates`, `import-x/no-unassigned-import` (allows `**/*.css`, `**/env.ts` and `**/startup.ts`); `functional/no-let` (module scope); `zukhruf/no-state-managers`, `zukhruf/no-playwright-test`, `zukhruf/no-other-test-runner`, `zukhruf/no-test-file-import`, `zukhruf/no-raw-database-sync`, `zukhruf/no-collect-loop`; typed lint: `no-floating-promises`, `no-misused-promises`, `only-throw-error`, `switch-exhaustiveness-check` (a `default` counts as exhaustive), `no-deprecated`, `zukhruf/no-promise-field`, `zukhruf/no-phase-flag`; `no-require-imports` is off in `.cjs` and `.cts`, because `require()` is the only import form in a CommonJS file |
46
- | `tests` | test files, `src/testing` | `no-test-lifecycle-hooks`, `no-process-env-write`, `no-direct-msw-server`, `no-catch-all-msw-handler`, `no-timer-race`, `no-sleep-poll-loop`, `no-global-test-mock`, `no-raw-temp-directory` |
47
- | `diagnostics` | source, not tests | `no-fabricated-fallback`, `no-hardcoded-id-shape`, `consistent-event-name` |
48
- | `env` | TypeScript, not tests or tool configs | `no-undeclared-process-env`, `no-single-env-read-wrapper`, `no-default-for-path-env` |
49
- | `packaged-app` | TypeScript, not tests | `no-bare-spawn` |
50
- | `react` | `.tsx`, `.jsx` | `no-as-child` |
51
- | `react-router` | source | `require-loader-data-type-argument`; outside tests: `no-raw-route-path`, `no-internal-anchor`; TypeScript: `only-throw-error` allows a `Response` and `DataWithResponseInit` |
52
- | `hono` | TypeScript, not tests | `no-untyped-empty-json`, `no-http-exception-factory`, `no-catch-around-next` |
53
- | `better-auth` | TypeScript, not tests | `no-api-error-instanceof`, `no-on-api-error-throw` |
54
- | `tailwind` | source | `no-h-screen`, `no-arbitrary-z-index`, `no-will-change` |
55
- | `pulumi` | TypeScript | `require-server-replace-guard` |
56
- | `nx-project-json` | `project.json` | `no-missing-asset-input` |
43
+ | Config | Files | Rules |
44
+ | ----------------- | ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
45
+ | `base` | all source | `@eslint/js` and `typescript-eslint` recommended; `consistent-type-assertions` (never), `no-namespace` (a `declare` namespace allowed), `zukhruf/no-enum`, `no-empty-object-type` (an interface that extends one type allowed, such as `ProcessEnv` in `env.ts`; `no-empty-interface`, which it replaced, is off); `import-x/no-duplicates`, `import-x/no-unassigned-import` (allows `**/*.css`, `**/env.ts` and `**/startup.ts`); `functional/no-let` (module scope); `zukhruf/no-state-managers`, `zukhruf/no-playwright-test`, `zukhruf/no-other-test-runner`, `zukhruf/no-test-file-import`, `zukhruf/no-raw-database-sync`, `zukhruf/no-collect-loop`, `zukhruf/no-captured-assignment` (off, see [ADR 0007](./docs/adr/0007-no-captured-assignment-is-off-until-the-repos-are-clean.md)); typed lint: `no-floating-promises`, `no-misused-promises`, `only-throw-error`, `switch-exhaustiveness-check` (a `default` counts as exhaustive), `no-deprecated`, `zukhruf/no-promise-field`, `zukhruf/no-phase-flag`; `no-require-imports` is off in `.cjs` and `.cts`, because `require()` is the only import form in a CommonJS file |
46
+ | `tests` | test files, `src/testing` | `no-test-lifecycle-hooks`, `no-process-env-write`, `no-direct-msw-server`, `no-catch-all-msw-handler`, `no-timer-race`, `no-sleep-poll-loop`, `no-global-test-mock`, `no-raw-temp-directory` |
47
+ | `diagnostics` | source, not tests | `no-fabricated-fallback`, `no-hardcoded-id-shape`, `consistent-event-name` |
48
+ | `env` | TypeScript, not tests or tool configs | `no-undeclared-process-env`, `no-single-env-read-wrapper`, `no-default-for-path-env` |
49
+ | `packaged-app` | TypeScript, not tests | `no-bare-spawn` |
50
+ | `react` | `.tsx`, `.jsx` | `no-as-child` |
51
+ | `react-router` | source | `require-loader-data-type-argument`; outside tests: `no-raw-route-path`, `no-internal-anchor`; TypeScript: `only-throw-error` allows a `Response` and `DataWithResponseInit` |
52
+ | `hono` | TypeScript, not tests | `no-untyped-empty-json`, `no-http-exception-factory`, `no-catch-around-next` |
53
+ | `better-auth` | TypeScript, not tests | `no-api-error-instanceof`, `no-on-api-error-throw` |
54
+ | `tailwind` | source | `no-h-screen`, `no-arbitrary-z-index`, `no-will-change` |
55
+ | `pulumi` | TypeScript | `require-server-replace-guard` |
56
+ | `nx-project-json` | `project.json` | `no-missing-asset-input` |
57
57
 
58
58
  `base` turns on typed lint (`parserOptions.projectService`). Each linted TypeScript file must be in a tsconfig.
59
59
 
@@ -67,6 +67,8 @@ The rule also reports a `let` with the same types when a function that its funct
67
67
 
68
68
  A lazy memo is not a phase, so the rule does not report it. In a lazy memo, each write is `x ??= …` (for a field, the private `this.#x ??= …`), so the value never becomes empty again. Each read is the value of that `??=`, or comes after an `x ??= …;` statement at the top level of the same function body, as in `this.#x ??= build(); return this.#x;` or `return () => (value ??= read());`. A field or `let` that has `??=` writes but no read is a flag, and the rule reports it.
69
69
 
70
+ `zukhruf/no-captured-assignment` reports a nested function that assigns a `let`, a `var` or a parameter of an enclosing function. Such a variable is state that the closures share. Its value depends on the order in which they run (temporal coupling), and a reader of one function cannot see who changes it. The rule reports `=`, a compound assignment, `++`, `--`, a destructuring target and the target of a `for…of` or `for…in` loop. A nested function is an arrow function, a function expression, a function declaration, or a method or an accessor of an object or a class that the outer function defines. An immediately invoked function (IIFE) is reported too: return the value from it. A `new Promise((r) => { resolve = r; })` is reported: use `Promise.withResolvers()`. A counter in a callback, such as `items.forEach((item) => { total += item; })`, is reported: use `reduce()` or a `for…of` loop. A function that assigns its own variables is not reported, also a generator between its `yield`s. A module-scope `let` is not reported, because `functional/no-let` bans it. A lazy memo, `x ??= …`, is not reported, because it sets the value one time and never clears it. `||=` and `&&=` are reported, because they can set the value again. The rule reads the scope, not the types. Thus it does not report a member assignment (`this.#x = …`, `state.n += 1`) or a call that changes a captured object (`map.set()`, `list.push()`). A check of those needs the type of the object, to know if the call changes it. To share state, give it to an object that owns it, or return the value. The rule is off in `base` until the repos fix their findings ([ADR 0007](./docs/adr/0007-no-captured-assignment-is-off-until-the-repos-are-clean.md)).
71
+
70
72
  `zukhruf/no-test-lifecycle-hooks` keeps the arrange and the teardown of a test in that test. A hook moves them out, so the reader does not see the full test, and the state that one test leaves can reach the next test. A per-test `t.after()` of node:test also moves the teardown out of the flow of the test. The rule reads the names of the scope, not only the text. It reports a hook from the Vitest, Jest or Mocha globals, and from an import, also under a different name (`import { after as cleanup }`). It reports a hook on `test` or `it` of node:test, or on its namespace (`test.after()`). It reports a hook on a test context: the first parameter of a test callback or a subtest callback of node:test, or a parameter of the type `TestContext`. A function of the file that has the name `after` is not a hook. Put the teardown in `try`/`finally`, or use `await using` with a disposable.
71
73
 
72
74
  `zukhruf/no-raw-database-sync` reports `new DatabaseSync` of `node:sqlite`. Each such open sets up its connection in its own way: its busy timeout, its journal, and which errors mean that the file is missing or denied. `open(path, options)` of `@zukhruf/sqlite-open` takes the same options and does that setup in one place; a reader is `open(path, { readOnly: true })`. The rule follows the bindings of the file: a named import under any local name, a namespace or default import, and `require` or `import()` of `node:sqlite`. A type-only import constructs nothing, so the rule does not report it. Turn it off only where a package cannot depend on `@zukhruf/sqlite-open`, and write the reason beside the override.
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/base/config.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,KAAK,OAAO,EAAU,MAAM,eAAe,CAAC;AAoCrD;;;;GAIG;AACH,eAAO,MAAM,IAAI,EAAE,OA2JlB,CAAC"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/base/config.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,KAAK,OAAO,EAAU,MAAM,eAAe,CAAC;AAqCrD;;;;GAIG;AACH,eAAO,MAAM,IAAI,EAAE,OAoKlB,CAAC"}
@@ -7,6 +7,7 @@ import tseslint from 'typescript-eslint';
7
7
  import noCollectLoop from "../async-values/no-collect-loop.js";
8
8
  import noPromiseField from "../async-values/no-promise-field.js";
9
9
  import noPhaseFlag from "../class-state/no-phase-flag.js";
10
+ import noCapturedAssignment from "../closure-state/no-captured-assignment.js";
10
11
  import { enable } from "../concept.js";
11
12
  import { BUILD_OUTPUT, COMMONJS, FIXTURES, JAVASCRIPT, SOURCE, TESTS, TEST_SUPPORT, TYPESCRIPT, } from "../files.js";
12
13
  import noOtherTestRunner from "../imports/no-other-test-runner.js";
@@ -29,6 +30,7 @@ const rules = {
29
30
  'no-promise-field': noPromiseField,
30
31
  'no-phase-flag': noPhaseFlag,
31
32
  'no-collect-loop': noCollectLoop,
33
+ 'no-captured-assignment': noCapturedAssignment,
32
34
  ...importBans,
33
35
  };
34
36
  /**
@@ -137,6 +139,14 @@ export const base = {
137
139
  files: TYPESCRIPT,
138
140
  ignores: [...TESTS, ...TEST_SUPPORT, ...FIXTURES],
139
141
  rules: { 'functional/no-let': ['error', noLet()] },
142
+ }, {
143
+ // A closure that assigns a variable of its enclosing function shares
144
+ // that state with every other closure, so the result depends on the
145
+ // order in which they run. Off until zukhruf, elt and
146
+ // context-compiler fix their findings (ADR 0007, #3878).
147
+ name: 'zukhruf/base/closure-state',
148
+ files: SOURCE,
149
+ rules: { 'zukhruf/no-captured-assignment': 'off' },
140
150
  }, {
141
151
  // Typed lint: the project service gives each file its nearest
142
152
  // tsconfig.json. The root comes from where ESLint runs, walked up to the
@@ -1 +1 @@
1
- {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/base/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,UAAU,MAAM,0BAA0B,CAAC;AAClD,OAAO,OAAO,MAAM,wBAAwB,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,QAAQ,MAAM,mBAAmB,CAAC;AAEzC,OAAO,aAAa,MAAM,oCAAoC,CAAC;AAC/D,OAAO,cAAc,MAAM,qCAAqC,CAAC;AACjE,OAAO,WAAW,MAAM,iCAAiC,CAAC;AAC1D,OAAO,EAAgB,MAAM,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EACL,YAAY,EACZ,QAAQ,EACR,QAAQ,EACR,UAAU,EACV,MAAM,EACN,KAAK,EACL,YAAY,EACZ,UAAU,GACX,MAAM,aAAa,CAAC;AACrB,OAAO,iBAAiB,MAAM,oCAAoC,CAAC;AACnE,OAAO,gBAAgB,MAAM,kCAAkC,CAAC;AAChE,OAAO,iBAAiB,MAAM,oCAAoC,CAAC;AACnE,OAAO,eAAe,MAAM,iCAAiC,CAAC;AAC9D,OAAO,gBAAgB,MAAM,mCAAmC,CAAC;AACjE,OAAO,MAAM,MAAM,wBAAwB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,KAAK,EAAE,kBAAkB,EAA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AO;SACjC;KACF,CACF;CACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const _default: import("eslint").Rule.RuleModule;
2
+ export default _default;
3
+ //# sourceMappingURL=no-captured-assignment.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-captured-assignment.d.ts","sourceRoot":"","sources":["../../src/closure-state/no-captured-assignment.ts"],"names":[],"mappings":";AAmEA,wBAAgC"}
@@ -0,0 +1,56 @@
1
+ import { ESLintUtils } from '@typescript-eslint/utils';
2
+ import { eslintRule } from "../authoring/rule-module.js";
3
+ /** A `let`, a `var` or a parameter: a binding that its function can assign again. */
4
+ function assignable(variable) {
5
+ return variable.defs.some((def) => def.type === 'Parameter' ||
6
+ (def.type === 'Variable' && def.parent.kind !== 'const'));
7
+ }
8
+ /**
9
+ * `x ??= …` is a lazy memo: it sets the value one time and never clears it,
10
+ * so after the first write every closure reads the same value.
11
+ */
12
+ function lazyMemo(reference) {
13
+ const { parent } = reference.identifier;
14
+ return (parent.type === 'AssignmentExpression' &&
15
+ parent.operator === '??=' &&
16
+ parent.left === reference.identifier);
17
+ }
18
+ const rule = ESLintUtils.RuleCreator.withoutDocs({
19
+ meta: {
20
+ type: 'problem',
21
+ docs: {
22
+ description: 'Disallow a nested function that assigns a let, a var or a parameter of an enclosing function',
23
+ },
24
+ schema: [],
25
+ messages: {
26
+ capturedAssignment: 'Do not assign `{{name}}` from a nested function: it belongs to an enclosing function, so its value depends on the order in which the closures run. Return the value, give the state to an object that owns it, or use the primitive for the job, such as Promise.withResolvers or Array.prototype.reduce.',
27
+ },
28
+ },
29
+ create(context) {
30
+ return {
31
+ 'Program:exit'() {
32
+ for (const scope of context.sourceCode.scopeManager?.scopes ?? []) {
33
+ for (const variable of scope.variables) {
34
+ const owner = variable.scope.variableScope;
35
+ if (owner.type !== 'function' || !assignable(variable))
36
+ continue;
37
+ for (const reference of variable.references) {
38
+ if (reference.isWrite() &&
39
+ !reference.init &&
40
+ reference.from.variableScope !== owner &&
41
+ !lazyMemo(reference)) {
42
+ context.report({
43
+ node: reference.identifier,
44
+ messageId: 'capturedAssignment',
45
+ data: { name: variable.name },
46
+ });
47
+ }
48
+ }
49
+ }
50
+ }
51
+ },
52
+ };
53
+ },
54
+ });
55
+ export default eslintRule(rule);
56
+ //# sourceMappingURL=no-captured-assignment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-captured-assignment.js","sourceRoot":"","sources":["../../src/closure-state/no-captured-assignment.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAiB,MAAM,0BAA0B,CAAC;AAEtE,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAEzD,qFAAqF;AACrF,SAAS,UAAU,CAAC,QAAiC;IACnD,OAAO,QAAQ,CAAC,IAAI,CAAC,IAAI,CACvB,CAAC,GAAG,EAAE,EAAE,CACN,GAAG,CAAC,IAAI,KAAK,WAAW;QACxB,CAAC,GAAG,CAAC,IAAI,KAAK,UAAU,IAAI,GAAG,CAAC,MAAM,CAAC,IAAI,KAAK,OAAO,CAAC,CAC3D,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,SAAS,QAAQ,CAAC,SAAmC;IACnD,MAAM,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC,UAAU,CAAC;IACxC,OAAO,CACL,MAAM,CAAC,IAAI,KAAK,sBAAsB;QACtC,MAAM,CAAC,QAAQ,KAAK,KAAK;QACzB,MAAM,CAAC,IAAI,KAAK,SAAS,CAAC,UAAU,CACrC,CAAC;AACJ,CAAC;AAED,MAAM,IAAI,GAAG,WAAW,CAAC,WAAW,CAAC,WAAW,CAAC;IAC/C,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EACT,8FAA8F;SACjG;QACD,MAAM,EAAE,EAAE;QACV,QAAQ,EAAE;YACR,kBAAkB,EAChB,2SAA2S;SAC9S;KACF;IACD,MAAM,CAAC,OAAO;QACZ,OAAO;YACL,cAAc;gBACZ,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,UAAU,CAAC,YAAY,EAAE,MAAM,IAAI,EAAE,EAAE,CAAC;oBAClE,KAAK,MAAM,QAAQ,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;wBACvC,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,aAAa,CAAC;wBAC3C,IAAI,KAAK,CAAC,IAAI,KAAK,UAAU,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;4BAAE,SAAS;wBACjE,KAAK,MAAM,SAAS,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;4BAC5C,IACE,SAAS,CAAC,OAAO,EAAE;gCACnB,CAAC,SAAS,CAAC,IAAI;gCACf,SAAS,CAAC,IAAI,CAAC,aAAa,KAAK,KAAK;gCACtC,CAAC,QAAQ,CAAC,SAAS,CAAC,EACpB,CAAC;gCACD,OAAO,CAAC,MAAM,CAAC;oCACb,IAAI,EAAE,SAAS,CAAC,UAAU;oCAC1B,SAAS,EAAE,oBAAoB;oCAC/B,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE;iCAC9B,CAAC,CAAC;4BACL,CAAC;wBACH,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;CACF,CAAC,CAAC;AAEH,eAAe,UAAU,CAAC,IAAI,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zukhruf/eslint",
3
- "version": "0.7.0",
3
+ "version": "0.8.1",
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": {
@@ -62,7 +62,7 @@
62
62
  "ts-api-utils": "^2.5.0"
63
63
  },
64
64
  "devDependencies": {
65
- "@zukhruf/testing": "^0.7.0"
65
+ "@zukhruf/testing": "^0.8.1"
66
66
  },
67
67
  "peerDependencies": {
68
68
  "@eslint-react/eslint-plugin": "^5.28.8",