@codelaw/rules-js 0.0.0-stage → 0.2.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 +10 -0
- package/README.md +57 -2
- package/codelaw-pack.toml +6 -0
- package/corpus.toml +76 -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/package.json +14 -4
- package/presets/js/conventions.toml +6 -0
- package/presets/js/project.toml +4 -0
- package/presets/js/recommended.toml +67 -0
- package/presets/js/typescript.toml +7 -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
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: js/use-isnan
|
|
3
|
+
title: "Test for NaN with Number.isNaN"
|
|
4
|
+
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "the comparison's operator decides whether to negate"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint/use-isnan]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
Code tests for `NaN` with `Number.isNaN(x)`, never by comparing with `NaN`.
|
|
13
|
+
|
|
14
|
+
**Why:** `NaN` is not equal to anything, itself included, so `x === NaN` is always false.
|
|
15
|
+
|
|
16
|
+
```codelaw
|
|
17
|
+
match:
|
|
18
|
+
kind: binary_expression
|
|
19
|
+
has:
|
|
20
|
+
field: operator
|
|
21
|
+
regex: ^(==|===|!=|!==|<|>|<=|>=)$
|
|
22
|
+
any:
|
|
23
|
+
- has: { field: left, regex: '^(Number\.)?NaN$' }
|
|
24
|
+
- has: { field: right, regex: '^(Number\.)?NaN$' }
|
|
25
|
+
message: "Nothing equals NaN, not even NaN; use Number.isNaN(x)"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## How to fix
|
|
29
|
+
|
|
30
|
+
1. Replace `x === NaN` or `x == NaN` with `Number.isNaN(x)`.
|
|
31
|
+
2. Replace `x !== NaN` or `x != NaN` with `!Number.isNaN(x)`.
|
|
32
|
+
|
|
33
|
+
## Examples
|
|
34
|
+
|
|
35
|
+
### JavaScript
|
|
36
|
+
|
|
37
|
+
#### Don't
|
|
38
|
+
|
|
39
|
+
```js
|
|
40
|
+
if (price === NaN) {
|
|
41
|
+
reset();
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
#### Do
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
if (Number.isNaN(price)) {
|
|
49
|
+
reset();
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### JSX
|
|
54
|
+
|
|
55
|
+
#### Don't
|
|
56
|
+
|
|
57
|
+
```jsx
|
|
58
|
+
if (price === NaN) {
|
|
59
|
+
reset();
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
#### Do
|
|
64
|
+
|
|
65
|
+
```jsx
|
|
66
|
+
if (Number.isNaN(price)) {
|
|
67
|
+
reset();
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### TypeScript
|
|
72
|
+
|
|
73
|
+
#### Don't
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
if (price === NaN) {
|
|
77
|
+
reset();
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### Do
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
if (Number.isNaN(price)) {
|
|
85
|
+
reset();
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### TSX
|
|
90
|
+
|
|
91
|
+
#### Don't
|
|
92
|
+
|
|
93
|
+
```tsx
|
|
94
|
+
if (price === NaN) {
|
|
95
|
+
reset();
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### Do
|
|
100
|
+
|
|
101
|
+
```tsx
|
|
102
|
+
if (Number.isNaN(price)) {
|
|
103
|
+
reset();
|
|
104
|
+
}
|
|
105
|
+
```
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: js/valid-typeof
|
|
3
|
+
title: "typeof is compared with a type it can return"
|
|
4
|
+
languages: [javascript, jsx, typescript, tsx]
|
|
5
|
+
fix: manual
|
|
6
|
+
fix_reason: "which type was meant needs the author"
|
|
7
|
+
stability: preview
|
|
8
|
+
ported_from: [eslint/valid-typeof]
|
|
9
|
+
since: 0.1.0
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
`typeof x` is compared only with a string it can return: `"undefined"`, `"object"`, `"boolean"`,
|
|
13
|
+
`"number"`, `"string"`, `"function"`, `"symbol"` or `"bigint"`.
|
|
14
|
+
|
|
15
|
+
**Why:** A misspelled type such as `"strnig"` makes the comparison always false.
|
|
16
|
+
|
|
17
|
+
```codelaw
|
|
18
|
+
match:
|
|
19
|
+
any:
|
|
20
|
+
- pattern: typeof $X == $S
|
|
21
|
+
- pattern: typeof $X === $S
|
|
22
|
+
- pattern: typeof $X != $S
|
|
23
|
+
- pattern: typeof $X !== $S
|
|
24
|
+
- pattern: $S == typeof $X
|
|
25
|
+
- pattern: $S === typeof $X
|
|
26
|
+
- pattern: $S != typeof $X
|
|
27
|
+
- pattern: $S !== typeof $X
|
|
28
|
+
constraints:
|
|
29
|
+
S:
|
|
30
|
+
kind: string
|
|
31
|
+
not: { regex: "^[\"'](undefined|object|boolean|number|string|function|symbol|bigint)[\"']$" }
|
|
32
|
+
message: "`typeof` never returns $S"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## How to fix
|
|
36
|
+
|
|
37
|
+
1. Correct the string to the type that was meant.
|
|
38
|
+
|
|
39
|
+
## Examples
|
|
40
|
+
|
|
41
|
+
### JavaScript
|
|
42
|
+
|
|
43
|
+
#### Don't
|
|
44
|
+
|
|
45
|
+
```js
|
|
46
|
+
if (typeof name === "strnig") {
|
|
47
|
+
greet(name);
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
#### Do
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
if (typeof name === "string") {
|
|
55
|
+
greet(name);
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### JSX
|
|
60
|
+
|
|
61
|
+
#### Don't
|
|
62
|
+
|
|
63
|
+
```jsx
|
|
64
|
+
if (typeof name === "strnig") {
|
|
65
|
+
greet(name);
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### Do
|
|
70
|
+
|
|
71
|
+
```jsx
|
|
72
|
+
if (typeof name === "string") {
|
|
73
|
+
greet(name);
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### TypeScript
|
|
78
|
+
|
|
79
|
+
#### Don't
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
if (typeof name === "strnig") {
|
|
83
|
+
greet(name);
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Do
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
if (typeof name === "string") {
|
|
91
|
+
greet(name);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### TSX
|
|
96
|
+
|
|
97
|
+
#### Don't
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
if (typeof name === "strnig") {
|
|
101
|
+
greet(name);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
#### Do
|
|
106
|
+
|
|
107
|
+
```tsx
|
|
108
|
+
if (typeof name === "string") {
|
|
109
|
+
greet(name);
|
|
110
|
+
}
|
|
111
|
+
```
|