This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rome_js_analyze): noAccumulatingSpread (#4426)
* feat(rome_js_analyze): noAccumulatingSpread * get a super general condition working * all my cases pass as expected * make analyzer semantic * move rule & run codegen * nicer tests * tidy doc string * update changelog * update example
- Loading branch information
Showing
15 changed files
with
809 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
crates/rome_js_analyze/src/semantic_analyzers/nursery/no_accumulating_spread.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
use rome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic}; | ||
use rome_console::markup; | ||
use rome_js_semantic::SemanticModel; | ||
use rome_js_syntax::{ | ||
AnyJsFunction, JsCallArgumentList, JsCallArguments, JsCallExpression, JsFormalParameter, | ||
JsParameterList, JsParameters, JsSpread, | ||
}; | ||
use rome_rowan::AstNode; | ||
|
||
use crate::semantic_services::Semantic; | ||
|
||
declare_rule! { | ||
/// Disallow the use of spread (`...`) syntax on accumulators. | ||
/// | ||
/// Spread syntax allows an iterable to be expanded into its individual elements. | ||
/// | ||
/// Spread syntax should be avoided on accumulators (like those in `.reduce`) | ||
/// because it causes a time complexity of `O(n^2)` instead of `O(n)`. | ||
/// | ||
/// Source: https://prateeksurana.me/blog/why-using-object-spread-with-reduce-bad-idea/ | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// var a = ['a', 'b', 'c']; | ||
/// a.reduce((acc, val) => [...acc, val], []); | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// var a = ['a', 'b', 'c']; | ||
/// a.reduce((acc, val) => {return [...acc, val];}, []); | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// var a = ['a', 'b', 'c']; | ||
/// a.reduce((acc, val) => ({...acc, [val]: val}), {}); | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```js | ||
/// var a = ['a', 'b', 'c']; | ||
/// a.reduce((acc, val) => {acc.push(val); return acc}, []); | ||
/// ``` | ||
/// | ||
pub(crate) NoAccumulatingSpread { | ||
version: "next", | ||
name: "noAccumulatingSpread", | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoAccumulatingSpread { | ||
type Query = Semantic<JsSpread>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let node = ctx.query(); | ||
let model = ctx.model(); | ||
|
||
is_known_accumulator(node, model)?.then_some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.range(), | ||
markup! { | ||
"Avoid the use of spread (`...`) syntax on accumulators." | ||
}, | ||
) | ||
.note(markup! { | ||
"Spread syntax should be avoided on accumulators (like those in `.reduce`) because it causes a time complexity of `O(n^2)`." | ||
}), | ||
) | ||
} | ||
} | ||
|
||
fn is_known_accumulator(node: &JsSpread, model: &SemanticModel) -> Option<bool> { | ||
let reference = node | ||
.argument() | ||
.ok()? | ||
.as_js_identifier_expression()? | ||
.name() | ||
.ok()?; | ||
|
||
let parameter = model | ||
.binding(&reference) | ||
.and_then(|declaration| declaration.syntax().parent()) | ||
.and_then(JsFormalParameter::cast)?; | ||
let function = parameter | ||
.parent::<JsParameterList>() | ||
.and_then(|list| list.parent::<JsParameters>()) | ||
.and_then(|parameters| parameters.parent::<AnyJsFunction>())?; | ||
let call_expression = function | ||
.parent::<JsCallArgumentList>() | ||
.and_then(|arguments| arguments.parent::<JsCallArguments>()) | ||
.and_then(|arguments| arguments.parent::<JsCallExpression>())?; | ||
|
||
let name = call_expression | ||
.callee() | ||
.ok()? | ||
.as_js_static_member_expression()? | ||
.member() | ||
.ok()? | ||
.as_js_name()? | ||
.value_token() | ||
.ok()?; | ||
let name = name.text_trimmed(); | ||
|
||
if matches!(name, "reduce" | "reduceRight") { | ||
Some(parameter.syntax().index() == 0) | ||
} else { | ||
None | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
crates/rome_js_analyze/tests/specs/nursery/noAccumulatingSpread/invalid.jsonc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[ | ||
// Array - Arrow return | ||
"foo.reduce((acc, bar) => [...acc, bar], [])", | ||
"foo.reduceRight((acc, bar) => [...acc, bar], [])", | ||
|
||
// Array - Body return | ||
"foo.reduce((acc, bar) => {return [...acc, bar];}, [])", | ||
"foo.reduceRight((acc, bar) => {return [...acc, bar];}, [])", | ||
|
||
// Array - Arrow return with item spread | ||
"foo.reduce((acc, bar) => [...acc, ...bar], [])", | ||
"foo.reduceRight((acc, bar) => [...acc, ...bar], [])", | ||
|
||
// Array - Body return with item spread | ||
"foo.reduce((acc, bar) => {return [...acc, ...bar];}, [])", | ||
"foo.reduceRight((acc, bar) => {return [...acc, ...bar];}, [])", | ||
|
||
// Object - Arrow return | ||
"foo.reduce((acc, bar) => ({...acc, [bar.key]: bar.value}), {})", | ||
"foo.reduceRight((acc, bar) => ({...acc, [bar.key]: bar.value}), {})", | ||
|
||
// Object - Body return | ||
"foo.reduce((acc, bar) => {return {...acc, [bar.key]: bar.value};}, {})", | ||
"foo.reduceRight((acc, bar) => {return {...acc, [bar.key]: bar.value};}, {})", | ||
|
||
// Object - Arrow return with item spread | ||
"foo.reduce((acc, bar) => ({...acc, ...bar}), {})", | ||
"foo.reduceRight((acc, bar) => ({...acc, ...bar}), {})", | ||
|
||
// Object - Body return with item spread | ||
"foo.reduce((acc, bar) => {return {...acc, ...bar};}, {})", | ||
"foo.reduceRight((acc, bar) => {return {...acc, ...bar};}, {})" | ||
] |
Oops, something went wrong.