Skip to content

Commit

Permalink
refactor(noNodejsModules): ignore type-only imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Aug 18, 2024
1 parent ffb66d9 commit 50d8733
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
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

0 comments on commit 50d8733

Please sign in to comment.