@paciolan/code-standards 1.0.0 → 1.1.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
@@ -28,7 +28,7 @@ includes everything below it in the table:
28
28
  | --- | --- | --- |
29
29
  | `eslint/base` | eslint recommended, Prettier-as-error | plain JS utilities |
30
30
  | `eslint/typescript` | typescript-eslint recommended, unused-import auto-removal, `_`-prefix conventions | headless TS libraries |
31
- | `eslint/react` | browser globals, react-hooks, import sorting | React components/libs |
31
+ | `eslint/react` | browser globals, react-hooks, import sorting, JSX in `.js`/`.jsx` files | React components/libs (TypeScript or plain JS/JSX) |
32
32
  | `eslint/react-query` | @tanstack/query correctness rules | operator remote components |
33
33
  | `eslint/node` | node globals, security + n plugins, type-aware promise rules, `no-console` | node services (layered on `typescript`) |
34
34
  | `eslint/nest` | Nest-specific guards | NestJS services |
@@ -64,6 +64,10 @@ Notes:
64
64
  this package.
65
65
  - **Unused code:** unused imports are removed by `--fix`; unused
66
66
  vars/args/catch params warn unless named with a leading `_`.
67
+ - **JS/JSX repos:** the react family lints `.js` and `.jsx` files with JSX
68
+ enabled and applies the same unused-code handling. Only browser globals
69
+ are predefined; add test-runner and node globals (jest, webpack config
70
+ files) in a repo-local entry.
67
71
  - Repos that register additional plugins (e.g. `eslint-plugin-react`) do so
68
72
  in their own config entry appended after the shared array.
69
73
 
@@ -95,6 +99,19 @@ the one module that owns the singletons:
95
99
  import { S3Client } from "@aws-sdk/client-s3";
96
100
  ```
97
101
 
102
+ #### pac/jsx-uses-vars
103
+
104
+ Reports nothing. It marks variables referenced only from JSX (and the
105
+ `React` binding the classic JSX runtime needs) as used, so the unused-code
106
+ rules don't remove component imports in plain JS/JSX files. Enabled by the
107
+ react family for `.js`/`.jsx` files; TypeScript files don't need it.
108
+
109
+ Only the `React` pragma is supported. An import named by a custom
110
+ `@jsx`/`@jsxFrag` pragma (`/** @jsx h */` with `import { h } from "preact"`)
111
+ is reported as unused, and `--fix` deletes it, which breaks the file at
112
+ runtime. Under the automatic runtime, a leftover `import React` in a file
113
+ with JSX is not reported.
114
+
98
115
  ## Prettier
99
116
 
100
117
  Point the `prettier` key of `package.json` at the shared config and delete
@@ -157,6 +174,29 @@ Every role is `strict: true`. Heads-up: `types` **replaces** rather than
157
174
  merges across extends — a repo needing extra ambient types (e.g.
158
175
  `@testing-library/jest-dom`) sets the full `types` array itself.
159
176
 
177
+ ## Upgrading from 1.x to 2.0.0
178
+
179
+ 2.0.0 adds plain JS/JSX support to the react family (`eslint/react`,
180
+ `eslint/react-query`). TypeScript files lint as before. A repo on 1.x can get
181
+ new findings:
182
+
183
+ - **`.jsx` files are linted.** 1.x skipped them ("File ignored because no
184
+ matching configuration was supplied"). Expect `prettier/prettier`,
185
+ `simple-import-sort`, unused-import errors, and `no-undef` for jest/node
186
+ names until the repo adds those globals.
187
+ - **`.js`, `.mjs` and `.cjs` files use the unused-code rules of TS files.**
188
+ Core `no-unused-vars` (error) is off. An unused import is an
189
+ `unused-imports/no-unused-imports` error, and `--fix` deletes it. An unused
190
+ variable is an `unused-imports/no-unused-vars` warning, unless its name
191
+ starts with `_`.
192
+ - **`no-unused-vars` disable comments go stale** in those files: each one is
193
+ reported as an unused directive (a warning, or an error under
194
+ `reportUnusedDisableDirectives: "error"`). Above an unused import, the
195
+ import is now an error as well. Remove the comment, and remove the import
196
+ or disable `unused-imports/no-unused-imports` instead.
197
+ - **JSX in `.js` files parses.** 1.x failed with "Parsing error: Unexpected
198
+ token <".
199
+
160
200
  ## Versioning and publishing
161
201
 
162
202
  Releases are cut by semantic-release from conventional commits on `master`
@@ -1,3 +1,4 @@
1
+ import jsxUsesVars from "./rules/jsx-uses-vars.mjs";
1
2
  import noDirectAwsSdkV3Instance from "./rules/no-direct-aws-sdk-v3-instance.mjs";
2
3
 
3
4
  /**
@@ -12,6 +13,7 @@ const plugin = {
12
13
  name: "@paciolan/code-standards"
13
14
  },
14
15
  rules: {
16
+ "jsx-uses-vars": jsxUsesVars,
15
17
  "no-direct-aws-sdk-v3-instance": noDirectAwsSdkV3Instance
16
18
  }
17
19
  };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Marks variables referenced only from JSX as used, so the unused-code rules
3
+ * don't report (or auto-remove) component imports in plain JS/JSX files.
4
+ * Covers the `React` binding the classic JSX runtime needs in scope.
5
+ * TypeScript files don't need this: typescript-eslint's parser tracks both.
6
+ */
7
+ export default {
8
+ meta: {
9
+ type: "problem",
10
+ docs: {
11
+ description: "mark variables used in JSX as used"
12
+ },
13
+ schema: []
14
+ },
15
+ create(context) {
16
+ const { sourceCode } = context;
17
+
18
+ return {
19
+ JSXOpeningElement(node) {
20
+ sourceCode.markVariableAsUsed("React", node);
21
+
22
+ let name = node.name;
23
+ // a lowercase tag like <div /> is an intrinsic element, not a variable
24
+ if (name.type === "JSXIdentifier" && /^[a-z]/.test(name.name)) {
25
+ return;
26
+ }
27
+ // <ns.Item.Leaf /> references ns
28
+ while (name.type === "JSXMemberExpression") {
29
+ name = name.object;
30
+ }
31
+ // <svg:rect /> has no variable
32
+ if (name.type === "JSXIdentifier") {
33
+ sourceCode.markVariableAsUsed(name.name, node);
34
+ }
35
+ },
36
+ JSXOpeningFragment(node) {
37
+ sourceCode.markVariableAsUsed("React", node);
38
+ }
39
+ };
40
+ }
41
+ };
package/eslint/react.mjs CHANGED
@@ -1,8 +1,10 @@
1
1
  import reactHooks from "eslint-plugin-react-hooks";
2
2
  import simpleImportSort from "eslint-plugin-simple-import-sort";
3
+ import unusedImports from "eslint-plugin-unused-imports";
3
4
  import globals from "globals";
4
5
 
5
- import typescript from "./typescript.mjs";
6
+ import pacPlugin from "./plugin/index.mjs";
7
+ import typescript, { unusedVarsOptions } from "./typescript.mjs";
6
8
 
7
9
  /**
8
10
  * React config: typescript + browser globals, react-hooks, import sorting.
@@ -33,5 +35,23 @@ export default [
33
35
  // registering it here would conflict with repo-local instances)
34
36
  "react/prop-types": "off"
35
37
  }
38
+ },
39
+ {
40
+ // JS/JSX sources get the same unused-code handling as TS files.
41
+ // `unused-imports/no-unused-vars` is the single reporter here.
42
+ files: ["**/*.{js,jsx,mjs,cjs}"],
43
+ languageOptions: {
44
+ parserOptions: { ecmaFeatures: { jsx: true } }
45
+ },
46
+ plugins: {
47
+ "unused-imports": unusedImports,
48
+ pac: pacPlugin
49
+ },
50
+ rules: {
51
+ "pac/jsx-uses-vars": "error",
52
+ "no-unused-vars": "off",
53
+ "unused-imports/no-unused-imports": "error",
54
+ "unused-imports/no-unused-vars": ["warn", unusedVarsOptions]
55
+ }
36
56
  }
37
57
  ];
@@ -3,6 +3,15 @@ import tseslint from "typescript-eslint";
3
3
 
4
4
  import base from "./base.mjs";
5
5
 
6
+ export const unusedVarsOptions = {
7
+ vars: "all",
8
+ varsIgnorePattern: "^_",
9
+ args: "after-used",
10
+ argsIgnorePattern: "^_",
11
+ caughtErrors: "all",
12
+ caughtErrorsIgnorePattern: "^_"
13
+ };
14
+
6
15
  /**
7
16
  * TypeScript config: base + typescript-eslint recommended, scoped to TS files.
8
17
  *
@@ -21,17 +30,7 @@ export default tseslint.config(...base, {
21
30
  "unused-imports/no-unused-imports": "error",
22
31
  "unused-imports/no-unused-vars": "off",
23
32
  "no-unused-vars": "off",
24
- "@typescript-eslint/no-unused-vars": [
25
- "warn",
26
- {
27
- vars: "all",
28
- varsIgnorePattern: "^_",
29
- args: "after-used",
30
- argsIgnorePattern: "^_",
31
- caughtErrors: "all",
32
- caughtErrorsIgnorePattern: "^_"
33
- }
34
- ],
33
+ "@typescript-eslint/no-unused-vars": ["warn", unusedVarsOptions],
35
34
  "@typescript-eslint/no-unused-expressions": [
36
35
  "error",
37
36
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paciolan/code-standards",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Paciolan shared code standards: ESLint flat configs, Prettier config, and TypeScript config presets",
5
5
  "keywords": [
6
6
  "eslint",