From 117d95f0aedcc3fe5d92030e1195ec05057b0741 Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 12 Dec 2023 16:28:37 +0800 Subject: [PATCH] fix(linter): improve the span message for no-accumulating-spread --- .../src/rules/oxc/no_accumulating_spread.rs | 17 +-- .../src/snapshots/no_accumulating_spread.snap | 124 +++++++++--------- 2 files changed, 66 insertions(+), 75 deletions(-) diff --git a/crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs b/crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs index bf90e5e1b..a14a0b465 100644 --- a/crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs +++ b/crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs @@ -21,22 +21,13 @@ use crate::{ enum NoAccumulatingSpreadDiagnostic { #[error("oxc(no-accumulating-spread): Do not spread accumulators in Array.prototype.reduce()")] #[diagnostic(severity(warning), help("It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead.\nUsing spreads within accumulators leads to `O(n^2)` time complexity."))] - LikelyArray( - #[label("The accumulator is spread here.")] Span, - #[label("From a reduce call here.")] Span, - ), + LikelyArray(#[label("From this spread")] Span, #[label("For this reduce")] Span), #[error("oxc(no-accumulating-spread): Do not spread accumulators in Array.prototype.reduce()")] #[diagnostic(severity(warning), help("It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead.\nUsing spreads within accumulators leads to `O(n^2)` time complexity."))] - LikelyObject( - #[label("The accumulator is spread here.")] Span, - #[label("From a reduce call here.")] Span, - ), + LikelyObject(#[label("From this spread")] Span, #[label("For this reduce")] Span), #[error("oxc(no-accumulating-spread): Do not spread accumulators in Array.prototype.reduce()")] #[diagnostic(severity(warning), help("Consider using `Object.assign()` or `Array.prototype.push()` to mutate the accumulator instead.\nUsing spreads within accumulators leads to `O(n^2)` time complexity."))] - Unknown( - #[label("The accumulator is spread here.")] Span, - #[label("From a reduce call here.")] Span, - ), + Unknown(#[label("From this spread")] Span, #[label("For this reduce")] Span), } #[derive(Debug, Default, Clone)] @@ -62,7 +53,7 @@ declare_oxc_lint!( /// function fn (x) { /// // ... /// } - /// + /// /// arr.reduce((acc, x) => acc.push(fn(x)), []) /// Object.keys(obj).reduce((acc, el) => { /// acc[el] = fn(el) diff --git a/crates/oxc_linter/src/snapshots/no_accumulating_spread.snap b/crates/oxc_linter/src/snapshots/no_accumulating_spread.snap index 2cbcb4379..f8acf7fe0 100644 --- a/crates/oxc_linter/src/snapshots/no_accumulating_spread.snap +++ b/crates/oxc_linter/src/snapshots/no_accumulating_spread.snap @@ -6,8 +6,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: obj[key] }), {}) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -16,8 +16,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: obj[key] }), ({} as foo)) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -26,8 +26,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ Object.keys(obj).reduce((acc, key) => ({ ...acc, [key]: obj[key] }), foo) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: Consider using `Object.assign()` or `Array.prototype.push()` to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -36,8 +36,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ arr.reduce((acc, x) => ({ ...acc, [x]: x }), {}) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -46,8 +46,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ arr.reduce((differentName, x) => ({ ...differentName, [x]: x }), {}) · ───┬── ────────┬─────── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -56,8 +56,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ a.b.arr.reduce((acc, x) => ({ ...acc, [x]: x }), {}) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -66,8 +66,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ a.b.arr.reduce((acc, x) => ({ ...acc, [x]: x }), (({} as baz))) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -76,8 +76,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ a.b.arr.reduce((acc, x) => ({ ...acc, [x]: x }), (({}))) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -86,8 +86,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ a.b.c.d.reduce((acc,x) => ([...acc, x]), []) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -96,8 +96,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ a.b.c.d.reduce((acc,x) => ([...acc, x]), ([])) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -106,8 +106,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ a.b.c.d.reduce((acc,x) => ([...acc, x]), ([] as foo)) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -116,8 +116,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ a.b.c.d.reduce((acc,x) => ([...acc, x]), (([]) as foo)) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -126,8 +126,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ get_array().reduce((acc, x) => ({ ...acc, [x]: x }), {}) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -136,8 +136,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ arr.reduce(function (acc, x) { return { ...acc, [x]: x } }, {}) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -146,10 +146,10 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ arr.reduce((acc, x) => { · ───┬── - · ╰── From a reduce call here. + · ╰── For this reduce 2 │ let temp = { ...acc, x } · ───┬── - · ╰── The accumulator is spread here. + · ╰── From this spread 3 │ return temp ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. @@ -159,8 +159,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduce((acc, bar) => [...acc, bar], []) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -169,8 +169,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduceRight((acc, bar) => [...acc, bar], []) · ─────┬───── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -179,8 +179,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduce((acc, bar) => {return [...acc, bar];}, []) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -189,8 +189,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduceRight((acc, bar) => {return [...acc, bar];}, []) · ─────┬───── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -199,8 +199,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduce((acc, bar) => [...acc, ...bar], []) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -209,8 +209,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduceRight((acc, bar) => [...acc, ...bar], []) · ─────┬───── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -219,8 +219,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduce((acc, bar) => {return [...acc, ...bar];}, []) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -229,8 +229,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduceRight((acc, bar) => {return [...acc, ...bar];}, []) · ─────┬───── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Array`. Consider using the `Array.push` or `Array.concat` methods to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -239,8 +239,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduce((acc, bar) => ({...acc, [bar.key]: bar.value}), {}) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -249,8 +249,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduceRight((acc, bar) => ({...acc, [bar.key]: bar.value}), {}) · ─────┬───── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -259,8 +259,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduce((acc, bar) => {return {...acc, [bar.key]: bar.value};}, {}) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -269,8 +269,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduceRight((acc, bar) => {return {...acc, [bar.key]: bar.value};}, {}) · ─────┬───── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -279,8 +279,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduce((acc, bar) => ({...acc, ...bar}), {}) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -289,8 +289,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduceRight((acc, bar) => ({...acc, ...bar}), {}) · ─────┬───── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -299,8 +299,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduce((acc, bar) => {return {...acc, ...bar};}, {}) · ───┬── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity. @@ -309,8 +309,8 @@ expression: no_accumulating_spread ╭─[no_accumulating_spread.tsx:1:1] 1 │ foo.reduceRight((acc, bar) => {return {...acc, ...bar};}, {}) · ─────┬───── ───┬── - · │ ╰── The accumulator is spread here. - · ╰── From a reduce call here. + · │ ╰── From this spread + · ╰── For this reduce ╰──── help: It looks like you're spreading an `Object`. Consider using the `Object.assign` or assignment operators to mutate the accumulator instead. Using spreads within accumulators leads to `O(n^2)` time complexity.