-
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter): noDynamicNamespaceImportAccess (#3241)
Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
Showing
12 changed files
with
334 additions
and
93 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
Large diffs are not rendered by default.
Oops, something went wrong.
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.
119 changes: 119 additions & 0 deletions
119
crates/biome_js_analyze/src/lint/nursery/no_dynamic_namespace_import_access.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,119 @@ | ||
use biome_analyze::{context::RuleContext, declare_rule, Rule, RuleDiagnostic}; | ||
use biome_console::markup; | ||
use biome_js_semantic::ReferencesExtensions; | ||
use biome_js_syntax::{JsComputedMemberExpression, JsImportNamespaceClause}; | ||
use biome_rowan::{AstNode, TextRange}; | ||
|
||
use crate::services::semantic::Semantic; | ||
|
||
declare_rule! { | ||
/// Disallow accessing namespace imports dynamically. | ||
/// | ||
/// Accessing namespace imports dynamically can prevent efficient tree shaking and increase bundle size. | ||
/// This happens because the bundler cannot determine which parts of the namespace are used at compile time, | ||
/// so it must include the entire namespace in the bundle. | ||
/// | ||
/// Instead, consider using named imports or if that is not possible | ||
/// access the namespaced import properties statically. | ||
/// | ||
/// If you want to completely disallow namespace imports, consider using the [noNamespaceImport](https://biomejs.dev/linter/rules/no-namespace-import/) rule. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// import * as foo from "foo" | ||
/// foo["bar"] | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// import * as foo from "foo" | ||
/// const key = "bar" | ||
/// foo[key] | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// import * as foo from "foo" | ||
/// foo.bar | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// import { bar } from "foo" | ||
/// bar | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// import messages from "i18n" | ||
/// const knownMessagesMap = { | ||
/// hello: messages.hello, | ||
/// goodbye: messages.goodbye | ||
/// } | ||
/// | ||
/// const dynamicKey = "hello" | ||
/// knownMessagesMap[dynamicKey] | ||
/// ``` | ||
/// | ||
pub NoDynamicNamespaceImportAccess { | ||
version: "next", | ||
name: "noDynamicNamespaceImportAccess", | ||
language: "js", | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoDynamicNamespaceImportAccess { | ||
type Query = Semantic<JsImportNamespaceClause>; | ||
type State = TextRange; | ||
type Signals = Vec<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
find_dynamic_namespace_import_accesses(ctx).map_or(vec![], |range| range) | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
let diagnostic = RuleDiagnostic::new( | ||
rule_category!(), | ||
range, | ||
markup! { | ||
"Avoid accessing namespace imports dynamically, it can prevent efficient tree shaking and increase bundle size." | ||
}, | ||
) | ||
.note(markup! { | ||
"Prefer static property access or use named imports instead." | ||
}); | ||
|
||
Some(diagnostic) | ||
} | ||
} | ||
|
||
fn find_dynamic_namespace_import_accesses( | ||
ctx: &RuleContext<NoDynamicNamespaceImportAccess>, | ||
) -> Option<Vec<TextRange>> { | ||
let import_namespace_clause: &JsImportNamespaceClause = ctx.query(); | ||
|
||
// Allow type import e.g. `import type * as foo from "foo"` | ||
if import_namespace_clause.type_token().is_some() { | ||
return None; | ||
} | ||
|
||
let specifier = import_namespace_clause.namespace_specifier().ok()?; | ||
let any_binding = specifier.local_name().ok()?; | ||
let identifier = any_binding.as_js_identifier_binding()?; | ||
let reads = identifier.all_reads(ctx.model()); | ||
|
||
let ranges = reads | ||
.into_iter() | ||
.filter_map(|read| { | ||
let syntax = read.syntax().parent()?.parent()?; | ||
let node = JsComputedMemberExpression::cast(syntax)?; | ||
|
||
Some(node.range()) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
Some(ranges) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
crates/biome_js_analyze/tests/specs/nursery/noDynamicNamespaceImportAccess/invalid.js
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,6 @@ | ||
import * as foo from "foo" | ||
|
||
foo["bar"] | ||
foo[1] | ||
const key = "bar" | ||
foo[key] |
62 changes: 62 additions & 0 deletions
62
crates/biome_js_analyze/tests/specs/nursery/noDynamicNamespaceImportAccess/invalid.js.snap
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,62 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
import * as foo from "foo" | ||
|
||
foo["bar"] | ||
foo[1] | ||
const key = "bar" | ||
foo[key] | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:3:1 lint/nursery/noDynamicNamespaceImportAccess ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid accessing namespace imports dynamically, it can prevent efficient tree shaking and increase bundle size. | ||
1 │ import * as foo from "foo" | ||
2 │ | ||
> 3 │ foo["bar"] | ||
│ ^^^^^^^^^^ | ||
4 │ foo[1] | ||
5 │ const key = "bar" | ||
i Prefer static property access or use named imports instead. | ||
``` | ||
|
||
``` | ||
invalid.js:4:1 lint/nursery/noDynamicNamespaceImportAccess ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid accessing namespace imports dynamically, it can prevent efficient tree shaking and increase bundle size. | ||
3 │ foo["bar"] | ||
> 4 │ foo[1] | ||
│ ^^^^^^ | ||
5 │ const key = "bar" | ||
6 │ foo[key] | ||
i Prefer static property access or use named imports instead. | ||
``` | ||
|
||
``` | ||
invalid.js:6:1 lint/nursery/noDynamicNamespaceImportAccess ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Avoid accessing namespace imports dynamically, it can prevent efficient tree shaking and increase bundle size. | ||
4 │ foo[1] | ||
5 │ const key = "bar" | ||
> 6 │ foo[key] | ||
│ ^^^^^^^^ | ||
i Prefer static property access or use named imports instead. | ||
``` |
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/noDynamicNamespaceImportAccess/valid.js
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,5 @@ | ||
import * as foo from "foo" | ||
|
||
foo.bar | ||
const key = "bar" | ||
foo[key] = "bar" |
12 changes: 12 additions & 0 deletions
12
crates/biome_js_analyze/tests/specs/nursery/noDynamicNamespaceImportAccess/valid.js.snap
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,12 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```jsx | ||
import * as foo from "foo" | ||
|
||
foo.bar | ||
const key = "bar" | ||
foo[key] = "bar" | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.