Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(noNodejsModules): ignore type-only imports #3674

Merged
merged 1 commit into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Jayllyz

- [noNodejsModules](https://biomejs.dev/linter/rules/no-nodejs-modules/) now ignores type-only imports ([#1674](https://github.com/biomejs/biome/issues/1674)).

The rule no longer reports type-only imports such as:

```ts
import type assert from "assert";
import type * as assert2 from "assert";
```

Contributed by @Conaclos


#### Bug fixes

- Don't request alt text for elements hidden from assistive technologies ([#3316](https://github.com/biomejs/biome/issues/3316)). Contributed by @robintown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use biome_analyze::{
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource,
};
use biome_console::markup;
use biome_js_syntax::{inner_string_text, AnyJsImportLike};
use biome_js_syntax::{inner_string_text, AnyJsImportClause, AnyJsImportLike};
use biome_rowan::AstNode;
use biome_rowan::TextRange;

declare_lint_rule! {
Expand Down Expand Up @@ -48,6 +49,14 @@ impl Rule for NoNodejsModules {
if node.is_in_ts_module_declaration() {
return None;
}
if let AnyJsImportLike::JsModuleSource(module_source) = &node {
if let Some(import_clause) = module_source.parent::<AnyJsImportClause>() {
if import_clause.type_token().is_some() {
// Ignore type-only imports
return None;
}
}
}
let module_name = node.module_name_token()?;
is_node_builtin_module(&inner_string_text(&module_name))
.then_some(module_name.text_trimmed_range())
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import type assert from "assert";
import type * as assert2 from "assert";
declare module "node:fs" {}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ expression: valid.ts
---
# Input
```ts
import type assert from "assert";
import type * as assert2 from "assert";
declare module "node:fs" {}
```
20 changes: 9 additions & 11 deletions crates/biome_js_syntax/src/import_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use crate::{
inner_string_text, AnyJsBinding, AnyJsImportClause, AnyJsModuleSource,
AnyJsNamedImportSpecifier, JsCallExpression, JsDefaultImportSpecifier, JsImport,
JsImportAssertion, JsImportCallExpression, JsModuleSource, JsNamedImportSpecifier,
JsNamespaceImportSpecifier, JsShorthandNamedImportSpecifier, JsSyntaxToken,
TsExternalModuleDeclaration,
JsNamespaceImportSpecifier, JsShorthandNamedImportSpecifier, JsSyntaxKind, JsSyntaxToken,
};
use biome_rowan::{
declare_node_union, AstNode, SyntaxError, SyntaxNodeOptionExt, SyntaxResult, TokenText,
Expand Down Expand Up @@ -312,9 +311,11 @@ impl AnyJsImportLike {
}

/// Check whether the js import specifier like is in a ts module declaration:
///
/// ```ts
/// declare "abc" {}
/// declare module "abc" {}
/// ```
///
/// ## Examples
///
/// ```
Expand All @@ -333,14 +334,11 @@ impl AnyJsImportLike {
/// ```
pub fn is_in_ts_module_declaration(&self) -> bool {
// It first has to be a JsModuleSource
if !matches!(self, AnyJsImportLike::JsModuleSource(_)) {
return false;
}
// Then test whether its parent is a TsExternalModuleDeclaration
if let Some(parent_syntax_kind) = self.syntax().parent().kind() {
return TsExternalModuleDeclaration::can_cast(parent_syntax_kind);
}
false
matches!(self, AnyJsImportLike::JsModuleSource(_))
&& matches!(
self.syntax().parent().kind(),
Some(JsSyntaxKind::TS_EXTERNAL_MODULE_DECLARATION)
)
}
}

Expand Down
Loading