@2digits/oxlint-plugin 0.9.0 → 0.11.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 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 [Effect modules over native logic](#effect-modules-over-native-logic).
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-*`,
@@ -48,6 +49,13 @@ The package exports all rules as `rules`, their names as `RuleName`, and the def
48
49
  - Schema compiler hoisting from t3code: `no-inline-schema-compile` reports `Schema.decode*`, `Schema.encode*`,
49
50
  `Schema.is`, and `Schema.asserts` compilers that are built and invoked inside a function body when their schema
50
51
  could be hoisted to module scope.
52
+ - Typed dual APIs: `require-dual-type-arguments` reports every `Function.dual` call without type arguments in
53
+ TypeScript files, also as
54
+ `dual` imported from `effect/Function` and with an existing function such as `Fn.dual(2, BigDecimal.divideUnsafe)`.
55
+ Without them TypeScript does not check the implementation against an annotation on the constant (inline parameters
56
+ become `any`), and the data-last form returns `any`. Write
57
+ `Fn.dual<(that: B) => (self: A) => R, (self: A, that: B) => R>(2, (self, that) => …)` instead, which types both forms
58
+ and infers the parameters.
51
59
  - Log results: `no-effect-as-on-log` reports `Effect.as`, `Effect.asSome` and `Effect.asVoid` applied to an
52
60
  `Effect.log*` or `Console.*` log effect (`.pipe(Effect.as(x))`, `pipe(log, Effect.as(x))` or data-first). Write
53
61
  `Effect.gen` that `yield*`s the log and returns the value instead.
@@ -251,8 +259,8 @@ modules for structured text. Plain `JSON.parse` and `JSON.stringify` are left to
251
259
  ## Effect modules over native logic
252
260
 
253
261
  These rules only run in files that import Effect and complement the `preferEffectArray`, `preferEffectRecord`, and
254
- `preferEffectString` diagnostics from `@effect/tsgo`. `prefer-effect-predicate` autofixes and `prefer-struct-assign`
255
- offers a suggestion; the others report only.
262
+ `preferEffectString` diagnostics from `@effect/tsgo`. `prefer-effect-predicate` and `prefer-effect-number` autofix and
263
+ `prefer-struct-assign` offers a suggestion; the others report only.
256
264
 
257
265
  - `prefer-effect-predicate` reports `typeof x === '…'`, comparisons with `undefined` or `null`, and `instanceof` on
258
266
  built-ins such as `String`, `Date`, or `Array`, and names the guard to use: `P.isString`, `!P.isNumber`,
@@ -285,6 +293,58 @@ offers a suggestion; the others report only.
285
293
  lose their contextual type. `Struct.assign` also drops a class instance's prototype, so rebuild Schema and Data class
286
294
  instances with their constructor. Two-object merges and copies get a `Struct.assign` suggestion rather than an
287
295
  autofix, because `Struct.assign` types its result from its generic parameters.
296
+ - `prefer-effect-number` replaces hand-written arithmetic that `effect/Number` names, adding the import when the file
297
+ lacks one ([Effect-TS/tsgo#1046](https://github.com/Effect-TS/tsgo/issues/1046)):
298
+ - `Math.min(hi, Math.max(lo, x))` and `Math.max(lo, Math.min(hi, x))`, with the arguments of each call in any order,
299
+ and the same with `Num.min` and `Num.max`, become `Num.clamp(x, { minimum: lo, maximum: hi })`. The value is the
300
+ inner argument that is not a number literal or a name such as `min`, `maxWidth`, `upperBound` or `hi`. With
301
+ `Math.min` outside, either inner argument may be the value; with `Math.max` outside, a wrong choice would change the
302
+ result, so when neither or both inner arguments look like a bound it is reported without a fix. The fix differs in
303
+ three edge cases: for `NaN`, `Num.clamp` returns `minimum` where the `Math` version returns `NaN`; when `minimum >
304
+ maximum`, `Num.clamp` returns `maximum` where `Math.max(lo, Math.min(hi, x))` returns `lo` (literal bounds in that
305
+ order are not reported); and with `-0` and `0` among the operands, the sign of a zero result can differ. The fix
306
+ evaluates the value before the bounds, so when more than one operand is a call or another expression with effects it
307
+ is a suggestion.
308
+ - `Math.round(x * 100) / 100`, also with `10 ** n` or `Math.pow(10, n)` as the factor, becomes `Num.round(x, 2)` or
309
+ `Num.round(x, n)`. A precision with effects is reported without a fix, since the fix evaluates it once, and so is
310
+ `10 ** n * x` when `x` has effects, since the fix reads `n` after `x`.
311
+ - `xs.reduce((a, b) => a + b, 0)`, `Arr.reduce(xs, 0, Num.sum)` and the data-last `Arr.reduce(0, …)` become
312
+ `Num.sumAll(xs)` or `Num.sumAll`. The product with `1` gets a `Num.multiplyAll` suggestion, because it returns 0
313
+ at the first 0, so `[0, NaN]` gives 0 where the reduce gives `NaN`. `reduce` skips the holes of a sparse array
314
+ such as `new Array(3)`, while `Num.sumAll` iterates them as `undefined` and returns `NaN`. Optional calls, a reduce
315
+ without a start value, callbacks that take an index, and local bindings that shadow the `Arr` or `Num` import are
316
+ not reported.
317
+ - `(n) => n + 1`, `(n) => ++n` and `(n) => n += 1` become `Num.increment`, and the same with `- 1`, `--n` and `-= 1`
318
+ become `Num.decrement`. Only arrows with one untyped or `number` parameter are reported, and `1 + n` is left alone
319
+ because it concatenates strings the other way round. `n + 1` and `n += 1` are valid TypeScript for a string too, so
320
+ they are only autofixed when the parameter is annotated `number`, and get a suggestion otherwise. `(n) => n++` and
321
+ `(n) => n--` return `n` unchanged, because a postfix update returns the old value, so they are reported as a likely
322
+ bug without a fix.
323
+ - `(a, b) => a - b` passed to `.sort`, `.toSorted` or `Arr.sort` becomes `Num.Order`, and `(a, b) => b - a` becomes
324
+ `Order.flip(Num.Order)`. They sort numbers the same way, except `NaN`, which `Num.Order` puts first.
325
+ - `x >= lo && x <= hi` in any operand order becomes `Num.between(x, { minimum: lo, maximum: hi })`, and `x < lo || x >
326
+ hi` becomes `!Num.between(…)`. `Num.Order` puts `NaN` below every number, so a `NaN` value makes the `||` form true
327
+ where the original is false, and a `NaN` lower bound passes `Num.between`. The rule therefore only autofixes the
328
+ `&&` form with a number literal lower bound and offers a suggestion otherwise. Without types, `from <= date && date
329
+ <= to` may compare strings or Dates, so both bounds must be number literals or have bound names such as `lower` and
330
+ `upper`, and string literals are skipped. A value or bound with effects is reported without a fix.
331
+ - `b === 0 ? fallback : a / b` and `b !== 0 ? a / b : fallback` become `Opt.getOrElse(Num.divide(a, b), () =>
332
+ fallback)`, which checks `b === 0` the same way. A dividend or divisor with effects, or a fallback with `await` or
333
+ `yield`, is reported without a fix. `b > 0` is a different check and is not reported.
334
+ - `Math.max(...xs)` and `Math.min(...xs)` are reported with `Arr.max(xs, Num.Order)` or `Arr.min` as the advice, but
335
+ not fixed: `Arr.max` needs a non-empty array where `Math.max()` of an empty array is `-Infinity`, and it skips
336
+ `NaN`.
337
+ - `Number.isNaN(Number(s))`, or `isNaN(n)` with `const n = Number(s)` (not a `let`, which may be reassigned), also
338
+ with `parseFloat`, is reported with `Num.parse(s)` as the advice but not fixed, because `Number('')` is 0 and
339
+ `parseFloat('12px')` is 12 where `Num.parse` returns none.
340
+
341
+ In JavaScript files the `Num.increment`, `Num.decrement`, `Num.Order` and `Num.between` fixes are suggestions,
342
+ because an untyped parameter may be a numeric string that `+`, `-` and `>=` coerce and Effect does not, or a bigint
343
+ that `++n` and `--n` step and Effect does not. TypeScript rejects those values in the fixed code, so in TypeScript
344
+ files they stay autofixes.
345
+
346
+ Fixes import Number as `Num`, Option as `Opt` and Order as `Order`, the aliases the import style rules use, and
347
+ reuse a namespace import of those modules when the file has one.
288
348
 
289
349
  ## Direct Effect combinators
290
350
 
@@ -577,12 +637,13 @@ The exported `PaddingLineOption` type describes each configuration entry. No opt
577
637
  ### Other fixes
578
638
 
579
639
  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-predicate`,
640
+ `no-effect-alchemy-barrel-imports`, `prefer-effect-alchemy-namespace-imports`, `prefer-effect-number`,
641
+ `prefer-effect-predicate`,
581
642
  `no-forwarding-lambda`, `alchemy-no-v1-worker-properties`, and `alchemy-no-deprecated-docker-constraints`.
582
643
  Fixes skip ambiguous bindings, conflicting properties, and unsupported import references. Oxlint applies one
583
644
  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-predicate`, `prefer-direct-effect-combinators`, `no-forwarding-lambda`,
585
- `no-manual-tag-comparison` and `prefer-struct-assign`; review those changes.
645
+ suggestions from `prefer-effect-number`, `prefer-effect-predicate`, `prefer-direct-effect-combinators`,
646
+ `no-forwarding-lambda`, `no-manual-tag-comparison` and `prefer-struct-assign`; review those changes.
586
647
 
587
648
  ## Adding a rule
588
649
 
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;
@@ -95,6 +96,7 @@ export declare const rules: {
95
96
  readonly 'prefer-with-span': import("effect-oxlint").CreateRule;
96
97
  readonly 'prefer-yield-now': import("effect-oxlint").CreateRule;
97
98
  readonly 'preserve-caught-error': import("effect-oxlint").CreateRule;
99
+ readonly 'require-dual-type-arguments': import("effect-oxlint").CreateRule;
98
100
  readonly 'require-schema-class-brand': import("effect-oxlint").CreateRule;
99
101
  readonly 'require-try-promise-abort-signal': import("effect-oxlint").CreateRule;
100
102
  readonly 'throw-in-effect-gen': import("effect-oxlint").CreateRule;