-
-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module_lexer): distinguish for types-only imports and exports (#…
…5184) Co-authored-by: Boshen <boshenc@gmail.com>
- Loading branch information
Showing
3 changed files
with
165 additions
and
45 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
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
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,69 @@ | ||
mod esm; | ||
use esm::ModuleLexer; | ||
use oxc_allocator::Allocator; | ||
use oxc_parser::Parser; | ||
use oxc_span::SourceType; | ||
|
||
fn parse(source: &str) -> ModuleLexer { | ||
let allocator = Allocator::default(); | ||
let source_type = SourceType::default().with_module(true).with_typescript_definition(true); | ||
let ret = Parser::new(&allocator, source, source_type).parse(); | ||
assert!(ret.errors.is_empty(), "{source} should not produce errors.\n{:?}", ret.errors); | ||
let module_lexer = oxc_module_lexer::ModuleLexer::new().build(&ret.program); | ||
ModuleLexer { | ||
imports: module_lexer.imports.into_iter().map(Into::into).collect(), | ||
exports: module_lexer.exports.into_iter().map(Into::into).collect(), | ||
has_module_syntax: module_lexer.has_module_syntax, | ||
facade: module_lexer.facade, | ||
} | ||
} | ||
|
||
#[test] | ||
fn import_type_named() { | ||
let source = "import type { foo } from 'foo'"; | ||
let impt = &parse(source).imports[0]; | ||
assert!(impt.t); | ||
} | ||
|
||
#[test] | ||
fn import_type_namespace() { | ||
let source = "import type * as foo from 'foo'"; | ||
let impt = &parse(source).imports[0]; | ||
assert!(impt.t); | ||
} | ||
|
||
#[test] | ||
fn import_type_default() { | ||
let source = "import type foo from 'foo'"; | ||
let impt = &parse(source).imports[0]; | ||
assert!(impt.t); | ||
} | ||
|
||
#[test] | ||
fn dynamic_import_value() { | ||
let source = "import('foo')"; | ||
let impt = &parse(source).imports[0]; | ||
assert!(!impt.t); | ||
} | ||
|
||
#[test] | ||
fn dynamic_import_type() { | ||
let source = "const foo: import('foo')"; | ||
let impt = &parse(source).imports[0]; | ||
assert!(impt.t); | ||
assert_eq!(impt.n.as_ref().unwrap(), "foo"); | ||
} | ||
|
||
#[test] | ||
fn export_type_named() { | ||
let source = "export type { foo } from 'foo'"; | ||
let expt = &parse(source).exports[0]; | ||
assert!(expt.t); | ||
} | ||
|
||
#[test] | ||
fn export_type_namespace() { | ||
let source = "export type * as foo from 'foo'"; | ||
let expt = &parse(source).exports[0]; | ||
assert!(expt.t); | ||
} |