@2digits/oxlint-plugin 0.9.0 → 0.10.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 +60 -6
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +6 -6
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -28,7 +28,8 @@ The package exports all rules as `rules`, their names as `RuleName`, and the def
|
|
|
28
28
|
- Copied and adapted Effect practices: `avoid-data-tagged-error`, `ban-error-string`,
|
|
29
29
|
`effect-promise-vs-trypromise`, `no-logging-in-catch`, and `throw-in-effect-gen`.
|
|
30
30
|
- Effect modules over native or hand-written logic: `prefer-effect-predicate`, `prefer-effect-array-ops`,
|
|
31
|
-
`prefer-equal`, and `prefer-struct-assign`. See
|
|
31
|
+
`prefer-effect-number`, `prefer-equal`, and `prefer-struct-assign`. See
|
|
32
|
+
[Effect modules over native logic](#effect-modules-over-native-logic).
|
|
32
33
|
- Direct Effect combinators: `prefer-direct-effect-combinators` replaces indirect Option, Array, Result and Filter
|
|
33
34
|
chains with the API that does the same in one step. See [Direct Effect combinators](#direct-effect-combinators).
|
|
34
35
|
- Syntax-safe Effect diagnostics proposed in open `Effect-TS/tsgo` issues: the remaining `cors-*`, `dual-*`,
|
|
@@ -251,8 +252,8 @@ modules for structured text. Plain `JSON.parse` and `JSON.stringify` are left to
|
|
|
251
252
|
## Effect modules over native logic
|
|
252
253
|
|
|
253
254
|
These rules only run in files that import Effect and complement the `preferEffectArray`, `preferEffectRecord`, and
|
|
254
|
-
`preferEffectString` diagnostics from `@effect/tsgo`. `prefer-effect-predicate`
|
|
255
|
-
offers a suggestion; the others report only.
|
|
255
|
+
`preferEffectString` diagnostics from `@effect/tsgo`. `prefer-effect-predicate` and `prefer-effect-number` autofix and
|
|
256
|
+
`prefer-struct-assign` offers a suggestion; the others report only.
|
|
256
257
|
|
|
257
258
|
- `prefer-effect-predicate` reports `typeof x === '…'`, comparisons with `undefined` or `null`, and `instanceof` on
|
|
258
259
|
built-ins such as `String`, `Date`, or `Array`, and names the guard to use: `P.isString`, `!P.isNumber`,
|
|
@@ -285,6 +286,58 @@ offers a suggestion; the others report only.
|
|
|
285
286
|
lose their contextual type. `Struct.assign` also drops a class instance's prototype, so rebuild Schema and Data class
|
|
286
287
|
instances with their constructor. Two-object merges and copies get a `Struct.assign` suggestion rather than an
|
|
287
288
|
autofix, because `Struct.assign` types its result from its generic parameters.
|
|
289
|
+
- `prefer-effect-number` replaces hand-written arithmetic that `effect/Number` names, adding the import when the file
|
|
290
|
+
lacks one ([Effect-TS/tsgo#1046](https://github.com/Effect-TS/tsgo/issues/1046)):
|
|
291
|
+
- `Math.min(hi, Math.max(lo, x))` and `Math.max(lo, Math.min(hi, x))`, with the arguments of each call in any order,
|
|
292
|
+
and the same with `Num.min` and `Num.max`, become `Num.clamp(x, { minimum: lo, maximum: hi })`. The value is the
|
|
293
|
+
inner argument that is not a number literal or a name such as `min`, `maxWidth`, `upperBound` or `hi`. With
|
|
294
|
+
`Math.min` outside, either inner argument may be the value; with `Math.max` outside, a wrong choice would change the
|
|
295
|
+
result, so when neither or both inner arguments look like a bound it is reported without a fix. The fix differs in
|
|
296
|
+
three edge cases: for `NaN`, `Num.clamp` returns `minimum` where the `Math` version returns `NaN`; when `minimum >
|
|
297
|
+
maximum`, `Num.clamp` returns `maximum` where `Math.max(lo, Math.min(hi, x))` returns `lo` (literal bounds in that
|
|
298
|
+
order are not reported); and with `-0` and `0` among the operands, the sign of a zero result can differ. The fix
|
|
299
|
+
evaluates the value before the bounds, so when more than one operand is a call or another expression with effects it
|
|
300
|
+
is a suggestion.
|
|
301
|
+
- `Math.round(x * 100) / 100`, also with `10 ** n` or `Math.pow(10, n)` as the factor, becomes `Num.round(x, 2)` or
|
|
302
|
+
`Num.round(x, n)`. A precision with effects is reported without a fix, since the fix evaluates it once, and so is
|
|
303
|
+
`10 ** n * x` when `x` has effects, since the fix reads `n` after `x`.
|
|
304
|
+
- `xs.reduce((a, b) => a + b, 0)`, `Arr.reduce(xs, 0, Num.sum)` and the data-last `Arr.reduce(0, …)` become
|
|
305
|
+
`Num.sumAll(xs)` or `Num.sumAll`. The product with `1` gets a `Num.multiplyAll` suggestion, because it returns 0
|
|
306
|
+
at the first 0, so `[0, NaN]` gives 0 where the reduce gives `NaN`. `reduce` skips the holes of a sparse array
|
|
307
|
+
such as `new Array(3)`, while `Num.sumAll` iterates them as `undefined` and returns `NaN`. Optional calls, a reduce
|
|
308
|
+
without a start value, callbacks that take an index, and local bindings that shadow the `Arr` or `Num` import are
|
|
309
|
+
not reported.
|
|
310
|
+
- `(n) => n + 1`, `(n) => ++n` and `(n) => n += 1` become `Num.increment`, and the same with `- 1`, `--n` and `-= 1`
|
|
311
|
+
become `Num.decrement`. Only arrows with one untyped or `number` parameter are reported, and `1 + n` is left alone
|
|
312
|
+
because it concatenates strings the other way round. `n + 1` and `n += 1` are valid TypeScript for a string too, so
|
|
313
|
+
they are only autofixed when the parameter is annotated `number`, and get a suggestion otherwise. `(n) => n++` and
|
|
314
|
+
`(n) => n--` return `n` unchanged, because a postfix update returns the old value, so they are reported as a likely
|
|
315
|
+
bug without a fix.
|
|
316
|
+
- `(a, b) => a - b` passed to `.sort`, `.toSorted` or `Arr.sort` becomes `Num.Order`, and `(a, b) => b - a` becomes
|
|
317
|
+
`Order.flip(Num.Order)`. They sort numbers the same way, except `NaN`, which `Num.Order` puts first.
|
|
318
|
+
- `x >= lo && x <= hi` in any operand order becomes `Num.between(x, { minimum: lo, maximum: hi })`, and `x < lo || x >
|
|
319
|
+
hi` becomes `!Num.between(…)`. `Num.Order` puts `NaN` below every number, so a `NaN` value makes the `||` form true
|
|
320
|
+
where the original is false, and a `NaN` lower bound passes `Num.between`. The rule therefore only autofixes the
|
|
321
|
+
`&&` form with a number literal lower bound and offers a suggestion otherwise. Without types, `from <= date && date
|
|
322
|
+
<= to` may compare strings or Dates, so both bounds must be number literals or have bound names such as `lower` and
|
|
323
|
+
`upper`, and string literals are skipped. A value or bound with effects is reported without a fix.
|
|
324
|
+
- `b === 0 ? fallback : a / b` and `b !== 0 ? a / b : fallback` become `Opt.getOrElse(Num.divide(a, b), () =>
|
|
325
|
+
fallback)`, which checks `b === 0` the same way. A dividend or divisor with effects, or a fallback with `await` or
|
|
326
|
+
`yield`, is reported without a fix. `b > 0` is a different check and is not reported.
|
|
327
|
+
- `Math.max(...xs)` and `Math.min(...xs)` are reported with `Arr.max(xs, Num.Order)` or `Arr.min` as the advice, but
|
|
328
|
+
not fixed: `Arr.max` needs a non-empty array where `Math.max()` of an empty array is `-Infinity`, and it skips
|
|
329
|
+
`NaN`.
|
|
330
|
+
- `Number.isNaN(Number(s))`, or `isNaN(n)` with `const n = Number(s)` (not a `let`, which may be reassigned), also
|
|
331
|
+
with `parseFloat`, is reported with `Num.parse(s)` as the advice but not fixed, because `Number('')` is 0 and
|
|
332
|
+
`parseFloat('12px')` is 12 where `Num.parse` returns none.
|
|
333
|
+
|
|
334
|
+
In JavaScript files the `Num.increment`, `Num.decrement`, `Num.Order` and `Num.between` fixes are suggestions,
|
|
335
|
+
because an untyped parameter may be a numeric string that `+`, `-` and `>=` coerce and Effect does not, or a bigint
|
|
336
|
+
that `++n` and `--n` step and Effect does not. TypeScript rejects those values in the fixed code, so in TypeScript
|
|
337
|
+
files they stay autofixes.
|
|
338
|
+
|
|
339
|
+
Fixes import Number as `Num`, Option as `Opt` and Order as `Order`, the aliases the import style rules use, and
|
|
340
|
+
reuse a namespace import of those modules when the file has one.
|
|
288
341
|
|
|
289
342
|
## Direct Effect combinators
|
|
290
343
|
|
|
@@ -577,12 +630,13 @@ The exported `PaddingLineOption` type describes each configuration entry. No opt
|
|
|
577
630
|
### Other fixes
|
|
578
631
|
|
|
579
632
|
Run `vpr lint:oxlint:fix` from the repository root to apply fixes from `consistent-schema-pipe`, `prefer-effect-duration`, `prefer-direct-effect-combinators`, `no-empty-effect-callback`,
|
|
580
|
-
`no-effect-alchemy-barrel-imports`, `prefer-effect-alchemy-namespace-imports`, `prefer-effect-
|
|
633
|
+
`no-effect-alchemy-barrel-imports`, `prefer-effect-alchemy-namespace-imports`, `prefer-effect-number`,
|
|
634
|
+
`prefer-effect-predicate`,
|
|
581
635
|
`no-forwarding-lambda`, `alchemy-no-v1-worker-properties`, and `alchemy-no-deprecated-docker-constraints`.
|
|
582
636
|
Fixes skip ambiguous bindings, conflicting properties, and unsupported import references. Oxlint applies one
|
|
583
637
|
import-adding fix per file per run, so run it again until nothing changes. `oxlint --fix-suggestions` also applies the
|
|
584
|
-
suggestions from `prefer-effect-
|
|
585
|
-
`no-manual-tag-comparison` and `prefer-struct-assign`; review those changes.
|
|
638
|
+
suggestions from `prefer-effect-number`, `prefer-effect-predicate`, `prefer-direct-effect-combinators`,
|
|
639
|
+
`no-forwarding-lambda`, `no-manual-tag-comparison` and `prefer-struct-assign`; review those changes.
|
|
586
640
|
|
|
587
641
|
## Adding a rule
|
|
588
642
|
|
package/dist/index.d.mts
CHANGED
|
@@ -82,6 +82,7 @@ export declare const rules: {
|
|
|
82
82
|
readonly 'prefer-effect-match-status': import("effect-oxlint").CreateRule;
|
|
83
83
|
readonly 'prefer-effect-ndjson': import("effect-oxlint").CreateRule;
|
|
84
84
|
readonly 'prefer-effect-net-address': import("effect-oxlint").CreateRule;
|
|
85
|
+
readonly 'prefer-effect-number': import("effect-oxlint").CreateRule;
|
|
85
86
|
readonly 'prefer-effect-path': import("effect-oxlint").CreateRule;
|
|
86
87
|
readonly 'prefer-effect-predicate': import("effect-oxlint").CreateRule;
|
|
87
88
|
readonly 'prefer-effect-sse': import("effect-oxlint").CreateRule;
|