@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.
- package/LICENSE +23 -0
- package/NOTICE +19 -0
- package/README.md +90 -2
- package/codelaw-pack.toml +6 -0
- package/data/globals/browser.json +1209 -0
- package/data/globals/commonjs.json +6 -0
- package/data/globals/jest.json +15 -0
- package/data/globals/mocha.json +22 -0
- package/data/globals/node.json +83 -0
- package/data/globals/serviceworker.json +328 -0
- package/data/globals/shared-node-browser.json +73 -0
- package/data/globals/vitest.json +19 -0
- package/data/globals/worker.json +349 -0
- package/data/node/deprecated-api.json +83 -0
- package/data/react/dom-property-aliases.json +131 -0
- package/package.json +14 -4
- package/presets/js/conventions.toml +10 -0
- package/presets/js/recommended.toml +67 -0
- package/presets/node/recommended.toml +13 -0
- package/presets/react/recommended.toml +22 -0
- package/presets/stencil/recommended.toml +18 -0
- package/presets/ts/recommended.toml +27 -0
- package/rules/graph/no-unused-exports.md +135 -0
- package/rules/js/constructor-super.md +77 -0
- package/rules/js/exports-at-end.md +134 -0
- package/rules/js/for-direction.md +106 -0
- package/rules/js/getter-return.md +74 -0
- package/rules/js/import-groups.md +117 -0
- package/rules/js/no-async-promise-executor.md +95 -0
- package/rules/js/no-case-declarations.md +134 -0
- package/rules/js/no-class-assign.md +61 -0
- package/rules/js/no-compare-neg-zero.md +105 -0
- package/rules/js/no-cond-assign.md +135 -0
- package/rules/js/no-const-assign.md +61 -0
- package/rules/js/no-constant-binary-expression.md +83 -0
- package/rules/js/no-constant-condition.md +150 -0
- package/rules/js/no-control-regex.md +84 -0
- package/rules/js/no-debugger.md +99 -0
- package/rules/js/no-default-export.md +88 -0
- package/rules/js/no-delete-var.md +93 -0
- package/rules/js/no-dupe-args.md +61 -0
- package/rules/js/no-dupe-class-members.md +82 -0
- package/rules/js/no-dupe-else-if.md +113 -0
- package/rules/js/no-dupe-keys.md +66 -0
- package/rules/js/no-duplicate-case.md +137 -0
- package/rules/js/no-empty-character-class.md +98 -0
- package/rules/js/no-empty-pattern.md +86 -0
- package/rules/js/no-empty-static-block.md +99 -0
- package/rules/js/no-empty.md +116 -0
- package/rules/js/no-ex-assign.md +121 -0
- package/rules/js/no-extra-boolean-cast.md +107 -0
- package/rules/js/no-fallthrough.md +178 -0
- package/rules/js/no-func-assign.md +62 -0
- package/rules/js/no-global-assign.md +85 -0
- package/rules/js/no-import-assign.md +60 -0
- package/rules/js/no-invalid-regexp.md +86 -0
- package/rules/js/no-irregular-whitespace.md +87 -0
- package/rules/js/no-loss-of-precision.md +82 -0
- package/rules/js/no-misleading-character-class.md +83 -0
- package/rules/js/no-new-native-nonconstructor.md +55 -0
- package/rules/js/no-nonoctal-decimal-escape.md +84 -0
- package/rules/js/no-obj-calls.md +61 -0
- package/rules/js/no-octal.md +84 -0
- package/rules/js/no-prototype-builtins.md +99 -0
- package/rules/js/no-redeclare.md +61 -0
- package/rules/js/no-regex-spaces.md +75 -0
- package/rules/js/no-self-assign.md +91 -0
- package/rules/js/no-setter-return.md +69 -0
- package/rules/js/no-shadow-restricted-names.md +102 -0
- package/rules/js/no-sparse-arrays.md +82 -0
- package/rules/js/no-this-before-super.md +74 -0
- package/rules/js/no-unassigned-vars.md +102 -0
- package/rules/js/no-undef.md +76 -0
- package/rules/js/no-unexpected-multiline.md +91 -0
- package/rules/js/no-unreachable.md +59 -0
- package/rules/js/no-unsafe-finally.md +145 -0
- package/rules/js/no-unsafe-negation.md +71 -0
- package/rules/js/no-unsafe-optional-chaining.md +83 -0
- package/rules/js/no-unused-labels.md +102 -0
- package/rules/js/no-unused-private-class-members.md +126 -0
- package/rules/js/no-unused-vars.md +69 -0
- package/rules/js/no-useless-assignment.md +115 -0
- package/rules/js/no-useless-backreference.md +83 -0
- package/rules/js/no-useless-catch.md +105 -0
- package/rules/js/no-useless-escape.md +81 -0
- package/rules/js/no-var.md +76 -0
- package/rules/js/no-with.md +60 -0
- package/rules/js/prefer-const.md +83 -0
- package/rules/js/prefer-rest-params.md +103 -0
- package/rules/js/prefer-spread.md +95 -0
- package/rules/js/preserve-caught-error.md +122 -0
- package/rules/js/require-yield.md +107 -0
- package/rules/js/use-isnan.md +105 -0
- package/rules/js/valid-typeof.md +111 -0
- package/rules/node/hashbang.md +71 -0
- package/rules/node/no-deprecated-api.md +91 -0
- package/rules/node/no-exports-assign.md +92 -0
- package/rules/node/no-extraneous-import.md +118 -0
- package/rules/node/no-extraneous-require.md +117 -0
- package/rules/node/no-missing-import.md +118 -0
- package/rules/node/no-missing-require.md +117 -0
- package/rules/node/no-process-exit.md +99 -0
- package/rules/node/no-unpublished-import.md +134 -0
- package/rules/node/no-unpublished-require.md +133 -0
- package/rules/react/display-name.md +91 -0
- package/rules/react/exhaustive-deps.md +116 -0
- package/rules/react/hook-prefix.md +108 -0
- package/rules/react/jsx-key.md +99 -0
- package/rules/react/jsx-no-comment-textnodes.md +87 -0
- package/rules/react/jsx-no-duplicate-props.md +66 -0
- package/rules/react/jsx-no-target-blank.md +78 -0
- package/rules/react/jsx-no-undef.md +78 -0
- package/rules/react/no-children-prop.md +80 -0
- package/rules/react/no-danger-with-children.md +83 -0
- package/rules/react/no-deprecated.md +84 -0
- package/rules/react/no-direct-mutation-state.md +102 -0
- package/rules/react/no-find-dom-node.md +72 -0
- package/rules/react/no-is-mounted.md +109 -0
- package/rules/react/no-render-return-value.md +75 -0
- package/rules/react/no-string-refs.md +72 -0
- package/rules/react/no-unescaped-entities.md +69 -0
- package/rules/react/no-unknown-property.md +69 -0
- package/rules/react/props-type-name.md +43 -0
- package/rules/react/require-render-return.md +91 -0
- package/rules/react/rules-of-hooks.md +128 -0
- package/rules/stencil/ban-default-true.md +70 -0
- package/rules/stencil/ban-exported-const-enums.md +60 -0
- package/rules/stencil/ban-prefix.md +76 -0
- package/rules/stencil/decorators-context.md +61 -0
- package/rules/stencil/host-data-deprecated.md +73 -0
- package/rules/stencil/methods-must-be-public.md +69 -0
- package/rules/stencil/no-unused-watch.md +68 -0
- package/rules/stencil/own-methods-must-be-private.md +68 -0
- package/rules/stencil/own-props-must-be-private.md +60 -0
- package/rules/stencil/prefer-vdom-listener.md +67 -0
- package/rules/stencil/prop-readonly.md +63 -0
- package/rules/stencil/props-must-be-public.md +68 -0
- package/rules/stencil/required-jsdoc.md +66 -0
- package/rules/stencil/reserved-member-names.md +70 -0
- package/rules/stencil/strict-mutable.md +75 -0
- package/rules/stencil/tag-prefix.md +75 -0
- package/rules/ts/ban-ts-comment.md +64 -0
- package/rules/ts/no-array-constructor.md +58 -0
- package/rules/ts/no-duplicate-enum-values.md +65 -0
- package/rules/ts/no-empty-object-type.md +65 -0
- package/rules/ts/no-explicit-any.md +61 -0
- package/rules/ts/no-extra-non-null-assertion.md +50 -0
- package/rules/ts/no-misused-new.md +70 -0
- package/rules/ts/no-namespace.md +65 -0
- package/rules/ts/no-non-null-asserted-optional-chain.md +55 -0
- package/rules/ts/no-require-imports.md +54 -0
- package/rules/ts/no-this-alias.md +68 -0
- package/rules/ts/no-unnecessary-type-constraint.md +64 -0
- package/rules/ts/no-unsafe-declaration-merging.md +73 -0
- package/rules/ts/no-unsafe-function-type.md +57 -0
- package/rules/ts/no-unused-expressions.md +61 -0
- package/rules/ts/no-unused-vars.md +68 -0
- package/rules/ts/no-wrapper-object-types.md +49 -0
- package/rules/ts/prefer-as-const.md +47 -0
- package/rules/ts/prefer-namespace-keyword.md +63 -0
- package/rules/ts/triple-slash-reference.md +60 -0
- package/rules/ts/type-only-imports.md +57 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: js/no-ex-assign
|
|
3
|
+
title: "A caught error is not reassigned"
|
|
4
|
+
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "where the original error should go is a decision"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint/no-ex-assign]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
Code doesn't assign a new value to the parameter of a `catch` clause.
|
|
13
|
+
|
|
14
|
+
**Why:** Reassigning the caught error discards it, so nothing can report or rethrow it.
|
|
15
|
+
|
|
16
|
+
```codelaw
|
|
17
|
+
match:
|
|
18
|
+
kind: identifier
|
|
19
|
+
where:
|
|
20
|
+
$$MATCH: [write, { binding: catch-param }]
|
|
21
|
+
message: "Assigning to the caught error `$$MATCH` loses it"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## How to fix
|
|
25
|
+
|
|
26
|
+
1. Use a new variable for the new value.
|
|
27
|
+
2. Keep the original error, e.g. as `{ cause: err }`.
|
|
28
|
+
|
|
29
|
+
## Examples
|
|
30
|
+
|
|
31
|
+
### JavaScript
|
|
32
|
+
|
|
33
|
+
#### Don't
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
try {
|
|
37
|
+
load();
|
|
38
|
+
} catch (err) {
|
|
39
|
+
err = new Error("failed");
|
|
40
|
+
throw err;
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
#### Do
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
try {
|
|
48
|
+
load();
|
|
49
|
+
} catch (err) {
|
|
50
|
+
throw new Error("failed", { cause: err });
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### JSX
|
|
55
|
+
|
|
56
|
+
#### Don't
|
|
57
|
+
|
|
58
|
+
```jsx
|
|
59
|
+
try {
|
|
60
|
+
load();
|
|
61
|
+
} catch (err) {
|
|
62
|
+
err = new Error("failed");
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### Do
|
|
68
|
+
|
|
69
|
+
```jsx
|
|
70
|
+
try {
|
|
71
|
+
load();
|
|
72
|
+
} catch (err) {
|
|
73
|
+
throw new Error("failed", { cause: err });
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### TypeScript
|
|
78
|
+
|
|
79
|
+
#### Don't
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
try {
|
|
83
|
+
load();
|
|
84
|
+
} catch (err) {
|
|
85
|
+
err = new Error("failed");
|
|
86
|
+
throw err;
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Do
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
try {
|
|
94
|
+
load();
|
|
95
|
+
} catch (err) {
|
|
96
|
+
throw new Error("failed", { cause: err });
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### TSX
|
|
101
|
+
|
|
102
|
+
#### Don't
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
try {
|
|
106
|
+
load();
|
|
107
|
+
} catch (err) {
|
|
108
|
+
err = new Error("failed");
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### Do
|
|
114
|
+
|
|
115
|
+
```tsx
|
|
116
|
+
try {
|
|
117
|
+
load();
|
|
118
|
+
} catch (err) {
|
|
119
|
+
throw new Error("failed", { cause: err });
|
|
120
|
+
}
|
|
121
|
+
```
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: js/no-extra-boolean-cast
|
|
3
|
+
title: "No needless !! or Boolean() in a condition"
|
|
4
|
+
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
+
fix: auto
|
|
6
|
+
stability: preview
|
|
7
|
+
ported_from: [eslint/no-extra-boolean-cast]
|
|
8
|
+
since: 0.1.0
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
A condition (`if`, `while`, `do … while`, `? :`) doesn't convert its value with `!!`.
|
|
12
|
+
|
|
13
|
+
**Why:** A condition is already evaluated as a boolean, so `!!` only adds noise.
|
|
14
|
+
|
|
15
|
+
```codelaw
|
|
16
|
+
match:
|
|
17
|
+
pattern: "!!$X"
|
|
18
|
+
any:
|
|
19
|
+
- inside:
|
|
20
|
+
stopBy: neighbor
|
|
21
|
+
kind: parenthesized_expression
|
|
22
|
+
inside:
|
|
23
|
+
stopBy: neighbor
|
|
24
|
+
any: [{ kind: if_statement }, { kind: while_statement }, { kind: do_statement }]
|
|
25
|
+
field: condition
|
|
26
|
+
- inside: { stopBy: neighbor, kind: ternary_expression, field: condition }
|
|
27
|
+
message: "A condition already converts to boolean; `!!` is redundant"
|
|
28
|
+
fix: "$X"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Examples
|
|
32
|
+
|
|
33
|
+
### JavaScript
|
|
34
|
+
|
|
35
|
+
#### Don't
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
if (!!user) {
|
|
39
|
+
greet();
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### Do
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
if (user) {
|
|
47
|
+
greet();
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### JSX
|
|
52
|
+
|
|
53
|
+
#### Don't
|
|
54
|
+
|
|
55
|
+
```jsx
|
|
56
|
+
if (!!user) {
|
|
57
|
+
greet();
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### Do
|
|
62
|
+
|
|
63
|
+
```jsx
|
|
64
|
+
if (user) {
|
|
65
|
+
greet();
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### TypeScript
|
|
70
|
+
|
|
71
|
+
#### Don't
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
if (!!user) {
|
|
75
|
+
greet();
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
#### Do
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
if (user) {
|
|
83
|
+
greet();
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### TSX
|
|
88
|
+
|
|
89
|
+
#### Don't
|
|
90
|
+
|
|
91
|
+
```tsx
|
|
92
|
+
if (!!user) {
|
|
93
|
+
greet();
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### Do
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
if (user) {
|
|
101
|
+
greet();
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Differences from the source
|
|
106
|
+
|
|
107
|
+
`Boolean(x)` in a condition is not checked yet.
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: js/no-fallthrough
|
|
3
|
+
title: "A switch case does not fall through into the next by accident"
|
|
4
|
+
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "whether the fall-through is intended needs the author"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint/no-fallthrough]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
A `case` with statements ends in `break`, `return`, `throw` or `continue`, or says `// falls
|
|
13
|
+
through` before the next case.
|
|
14
|
+
|
|
15
|
+
**Why:** Falling through is easy to do by accident and hard to see in review.
|
|
16
|
+
|
|
17
|
+
```codelaw
|
|
18
|
+
native: js/no-fallthrough
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## How to fix
|
|
22
|
+
|
|
23
|
+
1. If the case should stop, end it with `break` (or `return`).
|
|
24
|
+
2. If it should fall through, say so with a `// falls through` comment before the next case.
|
|
25
|
+
|
|
26
|
+
## Examples
|
|
27
|
+
|
|
28
|
+
### JavaScript
|
|
29
|
+
|
|
30
|
+
#### Don't
|
|
31
|
+
|
|
32
|
+
```js
|
|
33
|
+
switch (kind) {
|
|
34
|
+
case "a":
|
|
35
|
+
first();
|
|
36
|
+
case "b":
|
|
37
|
+
second();
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
#### Do
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
switch (kind) {
|
|
46
|
+
case "a":
|
|
47
|
+
first();
|
|
48
|
+
break;
|
|
49
|
+
case "b":
|
|
50
|
+
second();
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
switch (kind) {
|
|
57
|
+
case "a":
|
|
58
|
+
first();
|
|
59
|
+
// falls through
|
|
60
|
+
case "b":
|
|
61
|
+
second();
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### JSX
|
|
67
|
+
|
|
68
|
+
#### Don't
|
|
69
|
+
|
|
70
|
+
```jsx
|
|
71
|
+
switch (kind) {
|
|
72
|
+
case "a":
|
|
73
|
+
first();
|
|
74
|
+
case "b":
|
|
75
|
+
second();
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### Do
|
|
81
|
+
|
|
82
|
+
```jsx
|
|
83
|
+
switch (kind) {
|
|
84
|
+
case "a":
|
|
85
|
+
first();
|
|
86
|
+
break;
|
|
87
|
+
case "b":
|
|
88
|
+
second();
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
```jsx
|
|
94
|
+
switch (kind) {
|
|
95
|
+
case "a":
|
|
96
|
+
first();
|
|
97
|
+
// falls through
|
|
98
|
+
case "b":
|
|
99
|
+
second();
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### TypeScript
|
|
105
|
+
|
|
106
|
+
#### Don't
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
switch (kind) {
|
|
110
|
+
case "a":
|
|
111
|
+
first();
|
|
112
|
+
case "b":
|
|
113
|
+
second();
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### Do
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
switch (kind) {
|
|
122
|
+
case "a":
|
|
123
|
+
first();
|
|
124
|
+
break;
|
|
125
|
+
case "b":
|
|
126
|
+
second();
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
switch (kind) {
|
|
133
|
+
case "a":
|
|
134
|
+
first();
|
|
135
|
+
// falls through
|
|
136
|
+
case "b":
|
|
137
|
+
second();
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### TSX
|
|
143
|
+
|
|
144
|
+
#### Don't
|
|
145
|
+
|
|
146
|
+
```tsx
|
|
147
|
+
switch (kind) {
|
|
148
|
+
case "a":
|
|
149
|
+
first();
|
|
150
|
+
case "b":
|
|
151
|
+
second();
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
#### Do
|
|
157
|
+
|
|
158
|
+
```tsx
|
|
159
|
+
switch (kind) {
|
|
160
|
+
case "a":
|
|
161
|
+
first();
|
|
162
|
+
break;
|
|
163
|
+
case "b":
|
|
164
|
+
second();
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
```tsx
|
|
170
|
+
switch (kind) {
|
|
171
|
+
case "a":
|
|
172
|
+
first();
|
|
173
|
+
// falls through
|
|
174
|
+
case "b":
|
|
175
|
+
second();
|
|
176
|
+
break;
|
|
177
|
+
}
|
|
178
|
+
```
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: js/no-func-assign
|
|
3
|
+
title: "A function declaration is not reassigned"
|
|
4
|
+
languages: [javascript, jsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "what the assignment was for is the author's call"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint/no-func-assign]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
Code doesn't assign a new value to the name a function declaration binds.
|
|
13
|
+
|
|
14
|
+
**Why:** Overwriting a declared function is almost always a mistake, and every later call means
|
|
15
|
+
something else.
|
|
16
|
+
|
|
17
|
+
```codelaw
|
|
18
|
+
match:
|
|
19
|
+
kind: identifier
|
|
20
|
+
where:
|
|
21
|
+
$$MATCH: [write, { binding: function }]
|
|
22
|
+
message: "`$$MATCH` is a function declaration; don't assign to it"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## How to fix
|
|
26
|
+
|
|
27
|
+
1. Assign to a new variable instead.
|
|
28
|
+
2. If the function must be replaceable, declare it with `let` as a function expression.
|
|
29
|
+
|
|
30
|
+
## Examples
|
|
31
|
+
|
|
32
|
+
### JavaScript
|
|
33
|
+
|
|
34
|
+
#### Don't
|
|
35
|
+
|
|
36
|
+
```js
|
|
37
|
+
function save() {}
|
|
38
|
+
save = null;
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
#### Do
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
function save() {}
|
|
45
|
+
const saveHandler = null;
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### JSX
|
|
49
|
+
|
|
50
|
+
#### Don't
|
|
51
|
+
|
|
52
|
+
```jsx
|
|
53
|
+
function save() {}
|
|
54
|
+
save = null;
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### Do
|
|
58
|
+
|
|
59
|
+
```jsx
|
|
60
|
+
function save() {}
|
|
61
|
+
const saveHandler = null;
|
|
62
|
+
```
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: js/no-global-assign
|
|
3
|
+
title: "No assignment to built-in globals"
|
|
4
|
+
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "the intended variable needs a name of its own"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint/no-global-assign]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
Code doesn't assign to JavaScript's built-in globals such as `Object`, `undefined` or `NaN`.
|
|
13
|
+
|
|
14
|
+
**Why:** Built-in globals are read-only or shared by every module, so assigning to one is a bug or a
|
|
15
|
+
hazard.
|
|
16
|
+
|
|
17
|
+
```codelaw
|
|
18
|
+
match:
|
|
19
|
+
kind: identifier
|
|
20
|
+
where:
|
|
21
|
+
$$MATCH: [write, builtin]
|
|
22
|
+
message: "`$$MATCH` is a built-in global; don't assign to it"
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## How to fix
|
|
26
|
+
|
|
27
|
+
1. Declare a local variable with its own name.
|
|
28
|
+
|
|
29
|
+
## Examples
|
|
30
|
+
|
|
31
|
+
### JavaScript
|
|
32
|
+
|
|
33
|
+
#### Don't
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
Object = null;
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### Do
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
const object = null;
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### JSX
|
|
46
|
+
|
|
47
|
+
#### Don't
|
|
48
|
+
|
|
49
|
+
```jsx
|
|
50
|
+
Object = null;
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
#### Do
|
|
54
|
+
|
|
55
|
+
```jsx
|
|
56
|
+
const object = null;
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### TypeScript
|
|
60
|
+
|
|
61
|
+
#### Don't
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
Object = null;
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### Do
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
const object = null;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### TSX
|
|
74
|
+
|
|
75
|
+
#### Don't
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
Object = null;
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Do
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
const object = null;
|
|
85
|
+
```
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: js/no-import-assign
|
|
3
|
+
title: "An imported binding is not reassigned"
|
|
4
|
+
languages: [javascript, jsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "the new value needs a variable of its own"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint/no-import-assign]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
Code doesn't assign a new value to a name bound by `import`.
|
|
13
|
+
|
|
14
|
+
**Why:** Imported bindings are read-only: assigning to one throws a TypeError.
|
|
15
|
+
|
|
16
|
+
```codelaw
|
|
17
|
+
match:
|
|
18
|
+
kind: identifier
|
|
19
|
+
where:
|
|
20
|
+
$$MATCH: [write, { binding: import }]
|
|
21
|
+
message: "`$$MATCH` is an import; imports are read-only"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## How to fix
|
|
25
|
+
|
|
26
|
+
1. Copy the import into a local variable and change that.
|
|
27
|
+
|
|
28
|
+
## Examples
|
|
29
|
+
|
|
30
|
+
### JavaScript
|
|
31
|
+
|
|
32
|
+
#### Don't
|
|
33
|
+
|
|
34
|
+
```js
|
|
35
|
+
import config from "./config.js";
|
|
36
|
+
config = {};
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### Do
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import config from "./config.js";
|
|
43
|
+
const local = { ...config };
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### JSX
|
|
47
|
+
|
|
48
|
+
#### Don't
|
|
49
|
+
|
|
50
|
+
```jsx
|
|
51
|
+
import config from "./config.js";
|
|
52
|
+
config = {};
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### Do
|
|
56
|
+
|
|
57
|
+
```jsx
|
|
58
|
+
import config from "./config.js";
|
|
59
|
+
const local = { ...config };
|
|
60
|
+
```
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: js/no-invalid-regexp
|
|
3
|
+
title: "RegExp() is given a valid pattern and flags"
|
|
4
|
+
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "the intended pattern needs the author"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint/no-invalid-regexp]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
A `RegExp(...)` or `new RegExp(...)` whose pattern and flags are string literals is valid: brackets
|
|
13
|
+
and groups close, quantifiers follow something, and the flags are known and not repeated.
|
|
14
|
+
|
|
15
|
+
**Why:** An invalid pattern throws a SyntaxError when that line runs, not when the file loads.
|
|
16
|
+
|
|
17
|
+
```codelaw
|
|
18
|
+
native: js/no-invalid-regexp
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## How to fix
|
|
22
|
+
|
|
23
|
+
1. Correct the pattern or the flags the message names.
|
|
24
|
+
|
|
25
|
+
## Examples
|
|
26
|
+
|
|
27
|
+
### JavaScript
|
|
28
|
+
|
|
29
|
+
#### Don't
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
const re = new RegExp("[a-z");
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
#### Do
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const re = new RegExp("[a-z]");
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### JSX
|
|
42
|
+
|
|
43
|
+
#### Don't
|
|
44
|
+
|
|
45
|
+
```jsx
|
|
46
|
+
const re = new RegExp("[a-z");
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### Do
|
|
50
|
+
|
|
51
|
+
```jsx
|
|
52
|
+
const re = new RegExp("[a-z]");
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### TypeScript
|
|
56
|
+
|
|
57
|
+
#### Don't
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const re = new RegExp("[a-z");
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### Do
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const re = new RegExp("[a-z]");
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### TSX
|
|
70
|
+
|
|
71
|
+
#### Don't
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
const re = new RegExp("[a-z");
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
#### Do
|
|
78
|
+
|
|
79
|
+
```tsx
|
|
80
|
+
const re = new RegExp("[a-z]");
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Differences from the source
|
|
84
|
+
|
|
85
|
+
Only structural problems are found: unbalanced brackets and groups, misplaced quantifiers, out-of-
|
|
86
|
+
order ranges, invalid group syntax and flags.
|