Skip to content

Commit 95f75bd

Browse files
committed
feat(js_syntax): Add AnyJsImportLike
1 parent 5cef6ca commit 95f75bd

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

crates/biome_js_syntax/src/import_ext.rs

+71
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,77 @@ impl JsModuleSource {
386386
}
387387
}
388388

389+
declare_node_union! {
390+
/// This node union is meant to match all variations of static & dynamic imports:
391+
/// ```js
392+
/// require("lodash") // NodeJS require
393+
/// // ^^^^^^^^^^^^^^^^^
394+
/// import("lodash") // Dynamic ESM import
395+
/// // ^^^^^^^^^^^^^^^^
396+
/// import "lodash"; // Static ESM Import (in all its variations)
397+
/// // ^^^^^^^^^^^^^^^
398+
/// import lodash from "lodash";
399+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
400+
/// import * as utils from "lodash";
401+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
402+
/// import { chunk, differenceBy } from "lodash";
403+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
404+
/// import lodash, { chunk as _chunk } from "lodash";
405+
/// // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
406+
/// ```
407+
pub AnyJsImportLike = JsImport | JsCallExpression | JsImportCallExpression
408+
}
409+
410+
impl AnyJsImportLike {
411+
/// Returns the inner text of the specifier:
412+
///
413+
/// ## Examples
414+
///
415+
/// ```
416+
/// use biome_js_factory::make;
417+
/// use biome_js_syntax::AnyJsImportLike;
418+
///
419+
/// let source_name = make::js_module_source(make::js_string_literal("foo"));
420+
/// let any_import_specifier = AnyJsImportLike::JsModuleSource(source_name);
421+
/// assert_eq!(any_import_specifier.inner_string_text().unwrap().text(), "foo")
422+
/// ```
423+
pub fn module_source_text(&self) -> Option<TokenText> {
424+
match self {
425+
AnyJsImportLike::JsImport(import) => import.source_text().ok(),
426+
AnyJsImportLike::JsCallExpression(expression) => {
427+
expression.imported_module_source_text()
428+
}
429+
AnyJsImportLike::JsImportCallExpression(import_call) => {
430+
import_call.module_source_text()
431+
}
432+
}
433+
}
434+
435+
/// Returns the whole token text of the specifier, with quotes included:
436+
///
437+
/// ## Examples
438+
///
439+
/// ```
440+
/// use biome_js_factory::make;
441+
/// use biome_js_syntax::AnyJsImportLike;
442+
///
443+
/// let source_name = make::js_module_source(make::js_string_literal("foo"));
444+
/// let any_import_specifier = AnyJsImportLike::JsModuleSource(source_name);
445+
/// assert_eq!(any_import_specifier.module_name_token().unwrap().text(), "\"foo\"")
446+
/// ```
447+
pub fn module_name_token(&self) -> Option<JsSyntaxToken> {
448+
match self {
449+
AnyJsImportLike::JsImport(source) => source.source_token().ok(),
450+
AnyJsImportLike::JsCallExpression(expression) => {
451+
expression.imported_module_source_token()
452+
}
453+
AnyJsImportLike::JsImportCallExpression(import_call) => {
454+
import_call.module_source_token()
455+
}
456+
}
457+
}
458+
}
459+
389460
declare_node_union! {
390461
/// This node union is meant to match the following syntax:
391462
/// ```js

0 commit comments

Comments
 (0)