@2digits/oxlint-plugin 0.8.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 CHANGED
@@ -28,7 +28,10 @@ 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).
33
+ - Direct Effect combinators: `prefer-direct-effect-combinators` replaces indirect Option, Array, Result and Filter
34
+ chains with the API that does the same in one step. See [Direct Effect combinators](#direct-effect-combinators).
32
35
  - Syntax-safe Effect diagnostics proposed in open `Effect-TS/tsgo` issues: the remaining `cors-*`, `dual-*`,
33
36
  `no-*`, `prefer-*`, `preserve-*`, and `require-*` Effect rules.
34
37
  - Effect v4 API guidance for Array, DateTime, Duration, binary encodings, FileSystem, Filter, Headers, Match, Path, and Url,
@@ -249,8 +252,8 @@ modules for structured text. Plain `JSON.parse` and `JSON.stringify` are left to
249
252
  ## Effect modules over native logic
250
253
 
251
254
  These rules only run in files that import Effect and complement the `preferEffectArray`, `preferEffectRecord`, and
252
- `preferEffectString` diagnostics from `@effect/tsgo`. `prefer-effect-predicate` autofixes and `prefer-struct-assign`
253
- 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.
254
257
 
255
258
  - `prefer-effect-predicate` reports `typeof x === '…'`, comparisons with `undefined` or `null`, and `instanceof` on
256
259
  built-ins such as `String`, `Date`, or `Array`, and names the guard to use: `P.isString`, `!P.isNumber`,
@@ -283,6 +286,114 @@ offers a suggestion; the others report only.
283
286
  lose their contextual type. `Struct.assign` also drops a class instance's prototype, so rebuild Schema and Data class
284
287
  instances with their constructor. Two-object merges and copies get a `Struct.assign` suggestion rather than an
285
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.
341
+
342
+ ## Direct Effect combinators
343
+
344
+ `prefer-direct-effect-combinators` runs in files that import Effect. It reports two calls in a row that one Option, Array,
345
+ Result or Filter API expresses, and callbacks that go through an API the outer call could apply itself. Each pattern
346
+ matches the nested data-first form (`Opt.isSome(Opt.filter(o, p))`), `.pipe(...)` (`o.pipe(Opt.filter(p), Opt.isSome)`),
347
+ `pipe(...)` and `flow(...)`, and a data-first call that starts a pipe (`Opt.some(x).pipe(Opt.filter(p))`). Calls are
348
+ resolved through the file's imports, so `O.filter` after `import { Option as O } from 'effect'` or a named `filter`
349
+ import match too, and the fix reuses the namespace the chain already names, adding an import only when it has none.
350
+
351
+ - Option: `Opt.filter(Opt.some(x), p)` → `Opt.liftPredicate(x, p)`; `Opt.filter` then `Opt.isSome`/`isNone` →
352
+ `Opt.exists` (negated for `isNone`); `Opt.map` then `Opt.flatten` → `Opt.flatMap`; `Opt.map(f)` then
353
+ `Opt.getOrElse(g)` → `Opt.match({ onSome: f, onNone: g })`, or `Opt.exists(f)` when `g` is `() => false` and `f`
354
+ visibly returns a boolean; `Opt.match(o, { onNone: () => false, onSome: p })` with such a `p` → `Opt.exists(o, p)`; `Opt.flatMap` with a callback that returns
355
+ `Opt.fromNullishOr(…)` or `Opt.some(…)` → `Opt.flatMapNullishOr` or `Opt.map`; `Opt.orElse(() => Opt.some(d))` →
356
+ `Opt.orElseSome(() => d)`; `Opt.getOrElse(() => null)` or `Fn.constNull` → `Opt.getOrNull`, and the same for
357
+ `undefined`.
358
+ - Array: `Arr.findFirst` or `Arr.findFirstIndex` then `Opt.isSome`/`isNone` → `Arr.some`; `Arr.filter` then `Arr.head`
359
+ → `Arr.findFirst`; `Arr.filter` then `Arr.isReadonlyArrayNonEmpty`/`isReadonlyArrayEmpty` (or the mutable variants) →
360
+ `Arr.some`; `Arr.filter` then `Arr.length` → `Arr.countBy`; `Arr.map` then `Arr.flatten` → `Arr.flatMap`;
361
+ `Arr.filter(Opt.isSome)` then `Arr.map((o) => o.value)` or `Opt.getOrThrow` → `Arr.getSomes`, and likewise
362
+ `Arr.getSuccesses` and `Arr.getFailures`; `Arr.map(f)` then `Arr.filter(P.isNotNullish)` and `Arr.flatMap` with a
363
+ callback that returns `Arr.fromNullishOr(…)` → `Arr.flatMapNullishOr`; `Arr.head`/`Arr.last` then
364
+ `Opt.isSome`/`isNone` → `Arr.isReadonlyArrayNonEmpty`/`isReadonlyArrayEmpty`; `Arr.reverse` then `Arr.head` →
365
+ `Arr.last`.
366
+ - Result: `Result.map(f)` then `Result.getOrElse(g)` → `Result.match`; `Result.map` and `Result.mapError` in either
367
+ order → `Result.mapBoth`; `Result.flatMap` with a callback that returns `Result.succeed(…)` → `Result.map`;
368
+ `Result.getOrElse(() => null)` → `Result.getOrNull` (and `undefined`); `Result.getSuccess`/`getFailure` then
369
+ `Opt.isSome`/`isNone` → `Result.isSuccess`/`isFailure`; `Result.getSuccess` then `Opt.getOrNull`, `getOrUndefined` or
370
+ `getOrElse` → the `Result` getter.
371
+ - Filter: `(x) => Result.isSuccess(f(x))` → `Filter.toPredicate(f)`; `(x) => Result.getSuccess(f(x))` →
372
+ `Filter.toOption(f)`; `Opt.flatMap(o, Filter.toOption(f))` → `Opt.filterMap(o, f)`.
373
+
374
+ Rewrites that keep the behavior are autofixed. The rest are suggestions or reports:
375
+
376
+ - `Arr.filter` followed by `Arr.head` or an emptiness check is a suggestion, because the direct API stops at the first
377
+ match: the callback runs fewer times, and an exception a later element would throw no longer happens.
378
+ - `Arr.findFirst` → `Arr.some` is autofixed only when the callback visibly returns a boolean (a comparison, `!`, a
379
+ boolean literal, or an Effect guard such as `P.isString`). Otherwise it is a suggestion, since `Arr.findFirst` also
380
+ takes a callback that returns an Option, and a function literal that returns an Option is not reported.
381
+ - `Arr.some` and `Arr.last` take an array where `Arr.findFirst`, `Arr.findFirstIndex` and `Arr.reverse` take any
382
+ iterable, so those rewrites are autofixed only when the input is visibly an array (an array literal or the result of
383
+ an `Arr` call). On other input, such as a `Set` or a variable, they are suggestions.
384
+ - `Arr.map(f)` → `Arr.flatMapNullishOr(f)` and the Filter lambdas are reported without a fix when the new API would pass
385
+ `f` different arguments (an index) or read `f` too early (a `let`, or a binding declared after the function), and
386
+ `Filter.toPredicate(obj.check)` would lose `this`. An annotated Filter lambda gets a suggestion, because its type
387
+ annotations go away.
388
+ - `Opt.getOrElse(Result.getSuccess(r), fallback)` is reported without a fix unless the fallback is a thunk, because
389
+ `Result.getOrElse` passes the failure to it.
390
+ - A negation (`isNone`, emptiness checks) in the middle of a pipe, or in `flow`, and code with comments inside the
391
+ rewrite are reported without a fix.
392
+ - A nested data-first chain whose outer call has arguments that may have side effects, such as
393
+ `Opt.getOrElse(Opt.map(o, f), makeFallback())`, is reported without a fix, because fusing the calls would run them in
394
+ a different order.
395
+ - Callbacks with rest parameters or that read `arguments`, and Filter lambdas whose inner call has explicit type
396
+ arguments, are reported without a fix.
286
397
 
287
398
  ## Effect HTTP modules
288
399
 
@@ -518,13 +629,14 @@ The exported `PaddingLineOption` type describes each configuration entry. No opt
518
629
 
519
630
  ### Other fixes
520
631
 
521
- Run `vpr lint:oxlint:fix` from the repository root to apply fixes from `consistent-schema-pipe`, `prefer-effect-duration`, `no-empty-effect-callback`,
522
- `no-effect-alchemy-barrel-imports`, `prefer-effect-alchemy-namespace-imports`, `prefer-effect-predicate`,
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`,
633
+ `no-effect-alchemy-barrel-imports`, `prefer-effect-alchemy-namespace-imports`, `prefer-effect-number`,
634
+ `prefer-effect-predicate`,
523
635
  `no-forwarding-lambda`, `alchemy-no-v1-worker-properties`, and `alchemy-no-deprecated-docker-constraints`.
524
636
  Fixes skip ambiguous bindings, conflicting properties, and unsupported import references. Oxlint applies one
525
637
  import-adding fix per file per run, so run it again until nothing changes. `oxlint --fix-suggestions` also applies the
526
- suggestions from `prefer-effect-predicate`, `no-forwarding-lambda`, `no-manual-tag-comparison` and
527
- `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.
528
640
 
529
641
  ## Adding a rule
530
642
 
package/dist/index.d.mts CHANGED
@@ -59,6 +59,7 @@ export declare const rules: {
59
59
  readonly 'no-zero-retry-times': import("effect-oxlint").CreateRule;
60
60
  readonly 'padding-line-between-statements': import("effect-oxlint").CreateRule;
61
61
  readonly 'prefer-data-match': import("effect-oxlint").CreateRule;
62
+ readonly 'prefer-direct-effect-combinators': import("effect-oxlint").CreateRule;
62
63
  readonly 'prefer-effect-alchemy-namespace-imports': import("effect-oxlint").CreateRule;
63
64
  readonly 'prefer-effect-archive': import("effect-oxlint").CreateRule;
64
65
  readonly 'prefer-effect-array-ops': import("effect-oxlint").CreateRule;
@@ -81,6 +82,7 @@ export declare const rules: {
81
82
  readonly 'prefer-effect-match-status': import("effect-oxlint").CreateRule;
82
83
  readonly 'prefer-effect-ndjson': import("effect-oxlint").CreateRule;
83
84
  readonly 'prefer-effect-net-address': import("effect-oxlint").CreateRule;
85
+ readonly 'prefer-effect-number': import("effect-oxlint").CreateRule;
84
86
  readonly 'prefer-effect-path': import("effect-oxlint").CreateRule;
85
87
  readonly 'prefer-effect-predicate': import("effect-oxlint").CreateRule;
86
88
  readonly 'prefer-effect-sse': import("effect-oxlint").CreateRule;