Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
refactor(rome_js_analyze): improve consistency of rules about globals (
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Jul 4, 2023
1 parent c4ceec3 commit f65b0d9
Show file tree
Hide file tree
Showing 33 changed files with 1,147 additions and 1,061 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ multiple files:

- [`noRedeclare`](https://docs.rome.tools/lint/rules/noredeclare/): allow redeclare of index signatures are in different type members [#4478](https://github.com/rome/tools/issues/4478)

- Improve [`noConsoleLog`](https://docs.rome.tools/lint/rules/noconsolelog/), [`noGlobalObjectCalls`](https://docs.rome.tools/lint/rules/noglobalobjectcalls/), [`useIsNan`](https://docs.rome.tools/lint/rules/useisnan/), and [`useNumericLiterals`](https://docs.rome.tools/lint/rules/usenumericliterals/) by handling `globalThis` and `window` namespaces.

For instance, the following code is now reported by `noConsoleLog`:

```js
globalThis.console.log("log")
```

- Fix a crash in the [`NoParameterAssign`](https://docs.rome.tools/lint/rules/noparameterassign/) rule that occurred when there was a bogus binding. [#4323](https://github.com/rome/tools/issues/4323)

- Fix [`useExhaustiveDependencies`](https://docs.rome.tools/lint/rules/useexhaustivedependencies/) rule in the following cases [#4330](https://github.com/rome/tools/issues/4330)
Expand Down
48 changes: 14 additions & 34 deletions crates/rome_js_analyze/src/analyzers/complexity/use_flat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rome_analyze::{declare_rule, ActionCategory, Ast, Rule, RuleDiagnostic};
use rome_console::markup;
use rome_diagnostics::Applicability;
use rome_js_factory::make::{ident, js_name};
use rome_js_syntax::{AnyJsExpression, AnyJsName, JsCallExpression};
use rome_js_syntax::{AnyJsExpression, AnyJsMemberExpression, AnyJsName, JsCallExpression};
use rome_rowan::{AstNode, AstSeparatedList, BatchMutationExt};

declare_rule! {
Expand Down Expand Up @@ -46,8 +46,8 @@ impl Rule for UseFlatMap {
type Options = ();

fn run(ctx: &RuleContext<Self>) -> Self::Signals {
let node = ctx.query();
let arguments = node.arguments().ok()?.args();
let flat_call = ctx.query();
let arguments = flat_call.arguments().ok()?.args();
// Probably not a `flat` call.
if arguments.len() > 1 {
return None;
Expand All @@ -63,39 +63,19 @@ impl Rule for UseFlatMap {
return None;
}
}
let static_member_expression = node.callee().ok()?;
let static_member_expression = static_member_expression.as_js_static_member_expression()?;
let flat_member_name = static_member_expression
.member()
.ok()?
.as_js_name()?
.value_token()
.ok()?;
if flat_member_name.text_trimmed() == "flat" {
let call_expression = static_member_expression.object().ok()?;
let call_expression = call_expression.as_js_call_expression()?;
let map_call_arguments = call_expression.arguments().ok()?.args();

// probably not a `.map` all coming from an array
if map_call_arguments.len() > 2 || map_call_arguments.len() < 1 {
return None;
}

let map_static_member_expression = call_expression.callee().ok()?;
let map_static_member_expression =
map_static_member_expression.as_js_static_member_expression()?;
let map_member_name = map_static_member_expression
.member()
.ok()?
.as_js_name()?
.value_token()
.ok()?;

if map_member_name.text_trimmed() == "map" {
return Some(call_expression.clone());
let flat_member_expression =
AnyJsMemberExpression::cast_ref(flat_call.callee().ok()?.syntax())?;
if flat_member_expression.member_name()?.text() == "flat" {
let object = flat_member_expression.object().ok()?;
let map_call = object.as_js_call_expression()?;
let map_call_arguments = map_call.arguments().ok()?.args();
let map_member_expression =
AnyJsMemberExpression::cast_ref(map_call.callee().ok()?.syntax())?;
if map_member_expression.member_name()?.text() == "map" && map_call_arguments.len() == 1
{
return Some(map_call.clone());
}
}

None
}

Expand Down

0 comments on commit f65b0d9

Please sign in to comment.