@ls-stack/extended-lint 0.89.5 → 0.90.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 +52 -0
- package/dist/extended-lint.js +24 -24
- package/dist/extended-lint.mjs +24 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -137,3 +137,55 @@ foo<string>('bar') // OK
|
|
|
137
137
|
A fork of the `eslint-plugin-react-hooks` `rules-of-hooks` rule, but with the following changes:
|
|
138
138
|
|
|
139
139
|
- Identifies hooks in camelCase namespaced functions (e.g. `test.useFoo()`)
|
|
140
|
+
|
|
141
|
+
## `strict-casting`
|
|
142
|
+
|
|
143
|
+
Enforces safe and necessary `String()`/`Number()` casts. Replaces the deprecated `no-unnecessary-casting` rule.
|
|
144
|
+
|
|
145
|
+
Checks performed:
|
|
146
|
+
|
|
147
|
+
- **Unnecessary casts** (auto-fixable): casting a value that is already of the target type
|
|
148
|
+
- **Nullish casts**: `String(value)` produces the strings `"null"`/`"undefined"`, and `Number(value)` produces `0` for `null` and `NaN` for `undefined`. A suggestion is provided to add a `?? ''`/`?? 0` fallback
|
|
149
|
+
- **Invalid `Number()` casts**: casting a `symbol` (throws at runtime) or an object without a numeric `valueOf` (always results in `NaN`)
|
|
150
|
+
- **`any`/`unknown` casts**: casting untyped values is unsafe, type or narrow the value before casting
|
|
151
|
+
|
|
152
|
+
Bad:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
declare const str: string
|
|
156
|
+
declare const nullable: string | null
|
|
157
|
+
declare const obj: { a: string }
|
|
158
|
+
|
|
159
|
+
String(str) // Error: unnecessary cast
|
|
160
|
+
String(nullable) // Error: may produce "null"
|
|
161
|
+
Number(obj) // Error: results in NaN
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Good:
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
String(num)
|
|
168
|
+
String(nullable ?? '')
|
|
169
|
+
Number(date) // Date has a numeric valueOf
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Options:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
type Options = [
|
|
176
|
+
{
|
|
177
|
+
/** Extra functions checked for unnecessary casts */
|
|
178
|
+
additionalCastFunctions?: { name: string; expectedType: 'string' | 'number' }[]
|
|
179
|
+
/** Allow casting possibly nullish values (default: false) */
|
|
180
|
+
allowNullishCasting?: boolean
|
|
181
|
+
/** Allow symbol/object Number() casts (default: false) */
|
|
182
|
+
allowInvalidCasting?: boolean
|
|
183
|
+
/** Allow casting `any` values (default: false) */
|
|
184
|
+
allowAnyCasting?: boolean
|
|
185
|
+
/** Allow casting `unknown` values, e.g. `String(err)` in catch blocks (default: false) */
|
|
186
|
+
allowUnknownCasting?: boolean
|
|
187
|
+
},
|
|
188
|
+
]
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
For `String(obj)` producing `"[object Object]"`, use `@typescript-eslint/no-base-to-string`, which covers that case well.
|