@codelaw/rules-js 0.0.0-stage → 0.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.
Files changed (162) hide show
  1. package/LICENSE +23 -0
  2. package/NOTICE +19 -0
  3. package/README.md +90 -2
  4. package/codelaw-pack.toml +6 -0
  5. package/data/globals/browser.json +1209 -0
  6. package/data/globals/commonjs.json +6 -0
  7. package/data/globals/jest.json +15 -0
  8. package/data/globals/mocha.json +22 -0
  9. package/data/globals/node.json +83 -0
  10. package/data/globals/serviceworker.json +328 -0
  11. package/data/globals/shared-node-browser.json +73 -0
  12. package/data/globals/vitest.json +19 -0
  13. package/data/globals/worker.json +349 -0
  14. package/data/node/deprecated-api.json +83 -0
  15. package/data/react/dom-property-aliases.json +131 -0
  16. package/package.json +14 -4
  17. package/presets/js/conventions.toml +10 -0
  18. package/presets/js/recommended.toml +67 -0
  19. package/presets/node/recommended.toml +13 -0
  20. package/presets/react/recommended.toml +22 -0
  21. package/presets/stencil/recommended.toml +18 -0
  22. package/presets/ts/recommended.toml +27 -0
  23. package/rules/graph/no-unused-exports.md +135 -0
  24. package/rules/js/constructor-super.md +77 -0
  25. package/rules/js/exports-at-end.md +134 -0
  26. package/rules/js/for-direction.md +106 -0
  27. package/rules/js/getter-return.md +74 -0
  28. package/rules/js/import-groups.md +117 -0
  29. package/rules/js/no-async-promise-executor.md +95 -0
  30. package/rules/js/no-case-declarations.md +134 -0
  31. package/rules/js/no-class-assign.md +61 -0
  32. package/rules/js/no-compare-neg-zero.md +105 -0
  33. package/rules/js/no-cond-assign.md +135 -0
  34. package/rules/js/no-const-assign.md +61 -0
  35. package/rules/js/no-constant-binary-expression.md +83 -0
  36. package/rules/js/no-constant-condition.md +150 -0
  37. package/rules/js/no-control-regex.md +84 -0
  38. package/rules/js/no-debugger.md +99 -0
  39. package/rules/js/no-default-export.md +88 -0
  40. package/rules/js/no-delete-var.md +93 -0
  41. package/rules/js/no-dupe-args.md +61 -0
  42. package/rules/js/no-dupe-class-members.md +82 -0
  43. package/rules/js/no-dupe-else-if.md +113 -0
  44. package/rules/js/no-dupe-keys.md +66 -0
  45. package/rules/js/no-duplicate-case.md +137 -0
  46. package/rules/js/no-empty-character-class.md +98 -0
  47. package/rules/js/no-empty-pattern.md +86 -0
  48. package/rules/js/no-empty-static-block.md +99 -0
  49. package/rules/js/no-empty.md +116 -0
  50. package/rules/js/no-ex-assign.md +121 -0
  51. package/rules/js/no-extra-boolean-cast.md +107 -0
  52. package/rules/js/no-fallthrough.md +178 -0
  53. package/rules/js/no-func-assign.md +62 -0
  54. package/rules/js/no-global-assign.md +85 -0
  55. package/rules/js/no-import-assign.md +60 -0
  56. package/rules/js/no-invalid-regexp.md +86 -0
  57. package/rules/js/no-irregular-whitespace.md +87 -0
  58. package/rules/js/no-loss-of-precision.md +82 -0
  59. package/rules/js/no-misleading-character-class.md +83 -0
  60. package/rules/js/no-new-native-nonconstructor.md +55 -0
  61. package/rules/js/no-nonoctal-decimal-escape.md +84 -0
  62. package/rules/js/no-obj-calls.md +61 -0
  63. package/rules/js/no-octal.md +84 -0
  64. package/rules/js/no-prototype-builtins.md +99 -0
  65. package/rules/js/no-redeclare.md +61 -0
  66. package/rules/js/no-regex-spaces.md +75 -0
  67. package/rules/js/no-self-assign.md +91 -0
  68. package/rules/js/no-setter-return.md +69 -0
  69. package/rules/js/no-shadow-restricted-names.md +102 -0
  70. package/rules/js/no-sparse-arrays.md +82 -0
  71. package/rules/js/no-this-before-super.md +74 -0
  72. package/rules/js/no-unassigned-vars.md +102 -0
  73. package/rules/js/no-undef.md +76 -0
  74. package/rules/js/no-unexpected-multiline.md +91 -0
  75. package/rules/js/no-unreachable.md +59 -0
  76. package/rules/js/no-unsafe-finally.md +145 -0
  77. package/rules/js/no-unsafe-negation.md +71 -0
  78. package/rules/js/no-unsafe-optional-chaining.md +83 -0
  79. package/rules/js/no-unused-labels.md +102 -0
  80. package/rules/js/no-unused-private-class-members.md +126 -0
  81. package/rules/js/no-unused-vars.md +69 -0
  82. package/rules/js/no-useless-assignment.md +115 -0
  83. package/rules/js/no-useless-backreference.md +83 -0
  84. package/rules/js/no-useless-catch.md +105 -0
  85. package/rules/js/no-useless-escape.md +81 -0
  86. package/rules/js/no-var.md +76 -0
  87. package/rules/js/no-with.md +60 -0
  88. package/rules/js/prefer-const.md +83 -0
  89. package/rules/js/prefer-rest-params.md +103 -0
  90. package/rules/js/prefer-spread.md +95 -0
  91. package/rules/js/preserve-caught-error.md +122 -0
  92. package/rules/js/require-yield.md +107 -0
  93. package/rules/js/use-isnan.md +105 -0
  94. package/rules/js/valid-typeof.md +111 -0
  95. package/rules/node/hashbang.md +71 -0
  96. package/rules/node/no-deprecated-api.md +91 -0
  97. package/rules/node/no-exports-assign.md +92 -0
  98. package/rules/node/no-extraneous-import.md +118 -0
  99. package/rules/node/no-extraneous-require.md +117 -0
  100. package/rules/node/no-missing-import.md +118 -0
  101. package/rules/node/no-missing-require.md +117 -0
  102. package/rules/node/no-process-exit.md +99 -0
  103. package/rules/node/no-unpublished-import.md +134 -0
  104. package/rules/node/no-unpublished-require.md +133 -0
  105. package/rules/react/display-name.md +91 -0
  106. package/rules/react/exhaustive-deps.md +116 -0
  107. package/rules/react/hook-prefix.md +108 -0
  108. package/rules/react/jsx-key.md +99 -0
  109. package/rules/react/jsx-no-comment-textnodes.md +87 -0
  110. package/rules/react/jsx-no-duplicate-props.md +66 -0
  111. package/rules/react/jsx-no-target-blank.md +78 -0
  112. package/rules/react/jsx-no-undef.md +78 -0
  113. package/rules/react/no-children-prop.md +80 -0
  114. package/rules/react/no-danger-with-children.md +83 -0
  115. package/rules/react/no-deprecated.md +84 -0
  116. package/rules/react/no-direct-mutation-state.md +102 -0
  117. package/rules/react/no-find-dom-node.md +72 -0
  118. package/rules/react/no-is-mounted.md +109 -0
  119. package/rules/react/no-render-return-value.md +75 -0
  120. package/rules/react/no-string-refs.md +72 -0
  121. package/rules/react/no-unescaped-entities.md +69 -0
  122. package/rules/react/no-unknown-property.md +69 -0
  123. package/rules/react/props-type-name.md +43 -0
  124. package/rules/react/require-render-return.md +91 -0
  125. package/rules/react/rules-of-hooks.md +128 -0
  126. package/rules/stencil/ban-default-true.md +70 -0
  127. package/rules/stencil/ban-exported-const-enums.md +60 -0
  128. package/rules/stencil/ban-prefix.md +76 -0
  129. package/rules/stencil/decorators-context.md +61 -0
  130. package/rules/stencil/host-data-deprecated.md +73 -0
  131. package/rules/stencil/methods-must-be-public.md +69 -0
  132. package/rules/stencil/no-unused-watch.md +68 -0
  133. package/rules/stencil/own-methods-must-be-private.md +68 -0
  134. package/rules/stencil/own-props-must-be-private.md +60 -0
  135. package/rules/stencil/prefer-vdom-listener.md +67 -0
  136. package/rules/stencil/prop-readonly.md +63 -0
  137. package/rules/stencil/props-must-be-public.md +68 -0
  138. package/rules/stencil/required-jsdoc.md +66 -0
  139. package/rules/stencil/reserved-member-names.md +70 -0
  140. package/rules/stencil/strict-mutable.md +75 -0
  141. package/rules/stencil/tag-prefix.md +75 -0
  142. package/rules/ts/ban-ts-comment.md +64 -0
  143. package/rules/ts/no-array-constructor.md +58 -0
  144. package/rules/ts/no-duplicate-enum-values.md +65 -0
  145. package/rules/ts/no-empty-object-type.md +65 -0
  146. package/rules/ts/no-explicit-any.md +61 -0
  147. package/rules/ts/no-extra-non-null-assertion.md +50 -0
  148. package/rules/ts/no-misused-new.md +70 -0
  149. package/rules/ts/no-namespace.md +65 -0
  150. package/rules/ts/no-non-null-asserted-optional-chain.md +55 -0
  151. package/rules/ts/no-require-imports.md +54 -0
  152. package/rules/ts/no-this-alias.md +68 -0
  153. package/rules/ts/no-unnecessary-type-constraint.md +64 -0
  154. package/rules/ts/no-unsafe-declaration-merging.md +73 -0
  155. package/rules/ts/no-unsafe-function-type.md +57 -0
  156. package/rules/ts/no-unused-expressions.md +61 -0
  157. package/rules/ts/no-unused-vars.md +68 -0
  158. package/rules/ts/no-wrapper-object-types.md +49 -0
  159. package/rules/ts/prefer-as-const.md +47 -0
  160. package/rules/ts/prefer-namespace-keyword.md +63 -0
  161. package/rules/ts/triple-slash-reference.md +60 -0
  162. package/rules/ts/type-only-imports.md +57 -0
@@ -0,0 +1,75 @@
1
+ ---
2
+ id: js/no-regex-spaces
3
+ title: "No runs of spaces in a regular expression"
4
+ languages: [javascript, jsx, typescript, tsx]
5
+ fix: auto
6
+ stability: preview
7
+ ported_from: [eslint/no-regex-spaces]
8
+ since: 0.1.0
9
+ ---
10
+
11
+ A regular expression writes several spaces as ` {n}`, not as a run of spaces.
12
+
13
+ **Why:** A run of spaces is impossible to count by eye.
14
+
15
+ ```codelaw
16
+ native: js/no-regex-spaces
17
+ ```
18
+
19
+ ## Examples
20
+
21
+ ### JavaScript
22
+
23
+ #### Don't
24
+
25
+ ```js
26
+ const re = /a b/;
27
+ ```
28
+
29
+ #### Do
30
+
31
+ ```js
32
+ const re = /a {3}b/;
33
+ ```
34
+
35
+ ### JSX
36
+
37
+ #### Don't
38
+
39
+ ```jsx
40
+ const re = /a b/;
41
+ ```
42
+
43
+ #### Do
44
+
45
+ ```jsx
46
+ const re = /a {3}b/;
47
+ ```
48
+
49
+ ### TypeScript
50
+
51
+ #### Don't
52
+
53
+ ```ts
54
+ const re = /a b/;
55
+ ```
56
+
57
+ #### Do
58
+
59
+ ```ts
60
+ const re = /a {3}b/;
61
+ ```
62
+
63
+ ### TSX
64
+
65
+ #### Don't
66
+
67
+ ```tsx
68
+ const re = /a b/;
69
+ ```
70
+
71
+ #### Do
72
+
73
+ ```tsx
74
+ const re = /a {3}b/;
75
+ ```
@@ -0,0 +1,91 @@
1
+ ---
2
+ id: js/no-self-assign
3
+ title: "No assigning a variable to itself"
4
+ languages: [javascript, jsx, typescript, tsx]
5
+ fix: manual
6
+ fix_reason: "assigning a property to itself can run a setter, so removing it may change behaviour"
7
+ stability: preview
8
+ ported_from: [eslint/no-self-assign]
9
+ since: 0.1.0
10
+ ---
11
+
12
+ An assignment's left and right sides are not the same expression.
13
+
14
+ **Why:** `a = a` has no effect; it is usually a typo for another name.
15
+
16
+ ```codelaw
17
+ match:
18
+ pattern: $A = $A
19
+ message: "Assigning `$A` to itself does nothing"
20
+ ```
21
+
22
+ ## How to fix
23
+
24
+ 1. If another value was meant, assign that.
25
+ 2. Otherwise remove the assignment.
26
+
27
+ ## Examples
28
+
29
+ ### JavaScript
30
+
31
+ #### Don't
32
+
33
+ ```js
34
+ let x = 1;
35
+ x = x;
36
+ ```
37
+
38
+ #### Do
39
+
40
+ ```js
41
+ let x = 1;
42
+ x = y;
43
+ ```
44
+
45
+ ### JSX
46
+
47
+ #### Don't
48
+
49
+ ```jsx
50
+ let x = 1;
51
+ x = x;
52
+ ```
53
+
54
+ #### Do
55
+
56
+ ```jsx
57
+ let x = 1;
58
+ x = y;
59
+ ```
60
+
61
+ ### TypeScript
62
+
63
+ #### Don't
64
+
65
+ ```ts
66
+ let x = 1;
67
+ x = x;
68
+ ```
69
+
70
+ #### Do
71
+
72
+ ```ts
73
+ let x = 1;
74
+ x = y;
75
+ ```
76
+
77
+ ### TSX
78
+
79
+ #### Don't
80
+
81
+ ```tsx
82
+ let x = 1;
83
+ x = x;
84
+ ```
85
+
86
+ #### Do
87
+
88
+ ```tsx
89
+ let x = 1;
90
+ x = y;
91
+ ```
@@ -0,0 +1,69 @@
1
+ ---
2
+ id: js/no-setter-return
3
+ title: "A setter does not return a value"
4
+ languages: [javascript, jsx]
5
+ fix: manual
6
+ fix_reason: "whether the returned expression was meant as a side effect needs a reader"
7
+ stability: preview
8
+ ported_from: [eslint/no-setter-return]
9
+ since: 0.1.0
10
+ ---
11
+
12
+ A setter body contains no `return <value>`; a bare `return;` to leave early is fine.
13
+
14
+ **Why:** The value a setter returns is discarded, so returning one hides a mistake.
15
+
16
+ ```codelaw
17
+ native: js/no-setter-return
18
+ ```
19
+
20
+ ## How to fix
21
+
22
+ 1. Keep the expression's side effect as a statement.
23
+ 2. Remove the value from the `return`, or the `return`.
24
+
25
+ ## Examples
26
+
27
+ ### JavaScript
28
+
29
+ #### Don't
30
+
31
+ ```js
32
+ const box = {
33
+ set size(n) {
34
+ return (this.n = n);
35
+ },
36
+ };
37
+ ```
38
+
39
+ #### Do
40
+
41
+ ```js
42
+ const box = {
43
+ set size(n) {
44
+ this.n = n;
45
+ },
46
+ };
47
+ ```
48
+
49
+ ### JSX
50
+
51
+ #### Don't
52
+
53
+ ```jsx
54
+ const box = {
55
+ set size(n) {
56
+ return (this.n = n);
57
+ },
58
+ };
59
+ ```
60
+
61
+ #### Do
62
+
63
+ ```jsx
64
+ const box = {
65
+ set size(n) {
66
+ this.n = n;
67
+ },
68
+ };
69
+ ```
@@ -0,0 +1,102 @@
1
+ ---
2
+ id: js/no-shadow-restricted-names
3
+ title: "No declaring undefined, NaN, Infinity, arguments, eval or globalThis"
4
+ languages: [javascript, jsx, typescript, tsx]
5
+ fix: manual
6
+ fix_reason: "the new name is the author's choice"
7
+ stability: preview
8
+ ported_from: [eslint/no-shadow-restricted-names]
9
+ since: 0.1.0
10
+ ---
11
+
12
+ No variable, parameter or function is named `undefined`, `NaN`, `Infinity`, `arguments`, `eval` or
13
+ `globalThis`.
14
+
15
+ **Why:** Shadowing these names makes code that relies on them behave in confusing ways.
16
+
17
+ ```codelaw
18
+ match:
19
+ kind: identifier
20
+ regex: ^(undefined|NaN|Infinity|arguments|eval|globalThis)$
21
+ where:
22
+ $$MATCH: declaration
23
+ message: "`$$MATCH` is a restricted name; don't declare it"
24
+ ```
25
+
26
+ ## How to fix
27
+
28
+ 1. Rename the declaration and its uses.
29
+
30
+ ## Examples
31
+
32
+ ### JavaScript
33
+
34
+ #### Don't
35
+
36
+ ```js
37
+ function check(NaN) {
38
+ return NaN;
39
+ }
40
+ ```
41
+
42
+ #### Do
43
+
44
+ ```js
45
+ function check(value) {
46
+ return value;
47
+ }
48
+ ```
49
+
50
+ ### JSX
51
+
52
+ #### Don't
53
+
54
+ ```jsx
55
+ function check(NaN) {
56
+ return NaN;
57
+ }
58
+ ```
59
+
60
+ #### Do
61
+
62
+ ```jsx
63
+ function check(value) {
64
+ return value;
65
+ }
66
+ ```
67
+
68
+ ### TypeScript
69
+
70
+ #### Don't
71
+
72
+ ```ts
73
+ function check(NaN) {
74
+ return NaN;
75
+ }
76
+ ```
77
+
78
+ #### Do
79
+
80
+ ```ts
81
+ function check(value) {
82
+ return value;
83
+ }
84
+ ```
85
+
86
+ ### TSX
87
+
88
+ #### Don't
89
+
90
+ ```tsx
91
+ function check(NaN) {
92
+ return NaN;
93
+ }
94
+ ```
95
+
96
+ #### Do
97
+
98
+ ```tsx
99
+ function check(value) {
100
+ return value;
101
+ }
102
+ ```
@@ -0,0 +1,82 @@
1
+ ---
2
+ id: js/no-sparse-arrays
3
+ title: "No holes in array literals"
4
+ languages: [javascript, jsx, typescript, tsx]
5
+ fix: manual
6
+ fix_reason: "whether a value is missing or a comma is extra needs a reader"
7
+ stability: preview
8
+ ported_from: [eslint/no-sparse-arrays]
9
+ since: 0.1.0
10
+ ---
11
+
12
+ An array literal has no empty slots such as `[1, , 2]`.
13
+
14
+ **Why:** A hole is usually a typo, and holes behave differently from `undefined` in many array
15
+ methods.
16
+
17
+ ```codelaw
18
+ native: js/no-sparse-arrays
19
+ ```
20
+
21
+ ## How to fix
22
+
23
+ 1. Put the missing value in the slot, or remove the extra comma.
24
+ 2. If an empty slot is really meant, write `undefined` explicitly.
25
+
26
+ ## Examples
27
+
28
+ ### JavaScript
29
+
30
+ #### Don't
31
+
32
+ ```js
33
+ const colors = ["red", , "blue"];
34
+ ```
35
+
36
+ #### Do
37
+
38
+ ```js
39
+ const colors = ["red", "green", "blue"];
40
+ ```
41
+
42
+ ### JSX
43
+
44
+ #### Don't
45
+
46
+ ```jsx
47
+ const colors = ["red", , "blue"];
48
+ ```
49
+
50
+ #### Do
51
+
52
+ ```jsx
53
+ const colors = ["red", "green", "blue"];
54
+ ```
55
+
56
+ ### TypeScript
57
+
58
+ #### Don't
59
+
60
+ ```ts
61
+ const colors = ["red", , "blue"];
62
+ ```
63
+
64
+ #### Do
65
+
66
+ ```ts
67
+ const colors = ["red", "green", "blue"];
68
+ ```
69
+
70
+ ### TSX
71
+
72
+ #### Don't
73
+
74
+ ```tsx
75
+ const colors = ["red", , "blue"];
76
+ ```
77
+
78
+ #### Do
79
+
80
+ ```tsx
81
+ const colors = ["red", "green", "blue"];
82
+ ```
@@ -0,0 +1,74 @@
1
+ ---
2
+ id: js/no-this-before-super
3
+ title: "this is not used before super() in a derived constructor"
4
+ languages: [javascript, jsx]
5
+ fix: manual
6
+ fix_reason: "moving statements across `super()` can change what the base class sees"
7
+ stability: preview
8
+ ported_from: [eslint/no-this-before-super]
9
+ since: 0.1.0
10
+ ---
11
+
12
+ In the constructor of a class that extends another, `this` and `super.x` are used only after
13
+ `super()` has been called.
14
+
15
+ **Why:** Before `super()` runs, `this` is not initialised and touching it throws a ReferenceError.
16
+
17
+ ```codelaw
18
+ native: js/no-this-before-super
19
+ ```
20
+
21
+ ## How to fix
22
+
23
+ 1. Move the `super(...)` call above the first use of `this`.
24
+ 2. If the base class needs a value computed from `this`, compute it into a local first.
25
+
26
+ ## Examples
27
+
28
+ ### JavaScript
29
+
30
+ #### Don't
31
+
32
+ ```js
33
+ class Cat extends Animal {
34
+ constructor() {
35
+ this.lives = 9;
36
+ super();
37
+ }
38
+ }
39
+ ```
40
+
41
+ #### Do
42
+
43
+ ```js
44
+ class Cat extends Animal {
45
+ constructor() {
46
+ super();
47
+ this.lives = 9;
48
+ }
49
+ }
50
+ ```
51
+
52
+ ### JSX
53
+
54
+ #### Don't
55
+
56
+ ```jsx
57
+ class Cat extends Animal {
58
+ constructor() {
59
+ this.lives = 9;
60
+ super();
61
+ }
62
+ }
63
+ ```
64
+
65
+ #### Do
66
+
67
+ ```jsx
68
+ class Cat extends Animal {
69
+ constructor() {
70
+ super();
71
+ this.lives = 9;
72
+ }
73
+ }
74
+ ```
@@ -0,0 +1,102 @@
1
+ ---
2
+ id: js/no-unassigned-vars
3
+ title: "A variable that is read is also assigned"
4
+ languages: [javascript, jsx, typescript, tsx]
5
+ fix: manual
6
+ fix_reason: "the value the variable should hold is the author's"
7
+ stability: preview
8
+ ported_from: [eslint/no-unassigned-vars]
9
+ since: 0.1.0
10
+ ---
11
+
12
+ A variable declared without a value is assigned somewhere before it's read.
13
+
14
+ **Why:** A variable that is read but never assigned always holds `undefined`, which is a bug or dead
15
+ code.
16
+
17
+ ```codelaw
18
+ match:
19
+ kind: variable_declarator
20
+ has: { field: name, kind: identifier, pattern: $N }
21
+ inside:
22
+ stopBy: neighbor
23
+ any: [{ kind: lexical_declaration }, { kind: variable_declaration }]
24
+ not:
25
+ any:
26
+ - has: { field: value, pattern: $VALUE }
27
+ - inside: { kind: ambient_declaration, stopBy: end }
28
+ where:
29
+ $N: [{ not: reassigned }, { not: unused }]
30
+ message: "`$N` is read but never assigned, so it's always undefined"
31
+ ```
32
+
33
+ ## How to fix
34
+
35
+ 1. Assign the value the variable should hold.
36
+ 2. If it's meant to be `undefined`, use `undefined` directly.
37
+
38
+ ## Examples
39
+
40
+ ### JavaScript
41
+
42
+ #### Don't
43
+
44
+ ```js
45
+ let status;
46
+ report(status);
47
+ ```
48
+
49
+ #### Do
50
+
51
+ ```js
52
+ let status = "ok";
53
+ report(status);
54
+ ```
55
+
56
+ ### JSX
57
+
58
+ #### Don't
59
+
60
+ ```jsx
61
+ let status;
62
+ report(status);
63
+ ```
64
+
65
+ #### Do
66
+
67
+ ```jsx
68
+ let status = "ok";
69
+ report(status);
70
+ ```
71
+
72
+ ### TypeScript
73
+
74
+ #### Don't
75
+
76
+ ```ts
77
+ let status;
78
+ report(status);
79
+ ```
80
+
81
+ #### Do
82
+
83
+ ```ts
84
+ let status = "ok";
85
+ report(status);
86
+ ```
87
+
88
+ ### TSX
89
+
90
+ #### Don't
91
+
92
+ ```tsx
93
+ let status;
94
+ report(status);
95
+ ```
96
+
97
+ #### Do
98
+
99
+ ```tsx
100
+ let status = "ok";
101
+ report(status);
102
+ ```
@@ -0,0 +1,76 @@
1
+ ---
2
+ id: js/no-undef
3
+ title: "No references to undeclared names"
4
+ languages: [javascript, jsx]
5
+ fix: manual
6
+ fix_reason: "the right declaration — an import, a local, or an environment — is the author's call"
7
+ stability: preview
8
+ options:
9
+ env: { type: "string[]", default: ["shared-node-browser"], description: "environments whose globals are declared: browser, node, commonjs, worker, serviceworker, shared-node-browser, jest, mocha, vitest" }
10
+ globals: { type: "string[]", default: [], description: "more names to treat as declared" }
11
+ typeof: { type: "boolean", default: false, description: "also report names used only as `typeof x`" }
12
+ ported_from: [eslint/no-undef]
13
+ since: 0.1.0
14
+ ---
15
+
16
+ Every name the code reads or writes is declared in the file, built into JavaScript, or a global of
17
+ an environment the rule's `env` option names. As in ESLint, a `/* global a, b:writable */` comment
18
+ declares names for that file (`name:off` declares nothing), and a `/* eslint-env node */` comment
19
+ adds environments.
20
+
21
+ **Why:** An undeclared name is usually a typo or a missing import, and throws a ReferenceError when
22
+ that line runs.
23
+
24
+ ```codelaw
25
+ native: js/no-undef
26
+ ```
27
+
28
+ ## How to fix
29
+
30
+ 1. If it's a typo, correct the name.
31
+ 2. If it comes from another module, import it.
32
+ 3. If it's a global of the runtime (browser, Node), add that environment to the rule's `env` option.
33
+
34
+ ## Examples
35
+
36
+ ### JavaScript
37
+
38
+ #### Don't
39
+
40
+ ```js
41
+ const total = prices.reduce(add, 0);
42
+ function add(a, b) {
43
+ return a + b;
44
+ }
45
+ ```
46
+
47
+ #### Do
48
+
49
+ ```js
50
+ const prices = [1, 2];
51
+ const total = prices.reduce(add, 0);
52
+ function add(a, b) {
53
+ return a + b;
54
+ }
55
+ ```
56
+
57
+ ### JSX
58
+
59
+ #### Don't
60
+
61
+ ```jsx
62
+ const total = prices.reduce(add, 0);
63
+ function add(a, b) {
64
+ return a + b;
65
+ }
66
+ ```
67
+
68
+ #### Do
69
+
70
+ ```jsx
71
+ const prices = [1, 2];
72
+ const total = prices.reduce(add, 0);
73
+ function add(a, b) {
74
+ return a + b;
75
+ }
76
+ ```