Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
sxzz committed Aug 25, 2024
commit d54d85ee9ba64a17cf7d28d93d30e6714e296f0c
58 changes: 31 additions & 27 deletions crates/oxc_module_lexer/tests/esm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ pub struct ImportSpecifier {
pub t: bool,
}

impl From<oxc_module_lexer::ImportSpecifier<'_>> for ImportSpecifier {
fn from(value: oxc_module_lexer::ImportSpecifier) -> Self {
Self {
n: value.n.map(|n| n.to_string()),
s: value.s,
e: value.e,
ss: value.ss,
se: value.se,
d: value.d,
a: value.a,
t: value.t,
}
}
}

#[derive(Debug, Clone)]
pub struct ExportSpecifier {
pub n: String,
Expand All @@ -28,6 +43,20 @@ pub struct ExportSpecifier {
pub t: bool,
}

impl From<oxc_module_lexer::ExportSpecifier<'_>> for ExportSpecifier {
fn from(value: oxc_module_lexer::ExportSpecifier) -> Self {
Self {
n: value.n.to_string(),
ln: value.ln.map(|ln| ln.to_string()),
s: value.s,
e: value.e,
ls: value.ls,
le: value.le,
t: value.t,
}
}
}

#[non_exhaustive]
pub struct ModuleLexer {
pub imports: Vec<ImportSpecifier>,
Expand All @@ -44,33 +73,8 @@ fn parse(source: &str) -> ModuleLexer {
let module_lexer = oxc_module_lexer::ModuleLexer::new().build(&ret.program);
// Copy data over because `ModuleLexer<'a>` can't be returned
ModuleLexer {
imports: module_lexer
.imports
.into_iter()
.map(|i| ImportSpecifier {
n: i.n.map(|n| n.to_string()),
s: i.s,
e: i.e,
ss: i.ss,
se: i.se,
d: i.d,
a: i.a,
t: i.t,
})
.collect(),
exports: module_lexer
.exports
.into_iter()
.map(|e| ExportSpecifier {
n: e.n.to_string(),
ln: e.ln.map(|ln| ln.to_string()),
s: e.s,
e: e.e,
ls: e.ls,
le: e.le,
t: e.t,
})
.collect(),
imports: module_lexer.imports.into_iter().map(|i| i.into()).collect(),
exports: module_lexer.exports.into_iter().map(|e| e.into()).collect(),
has_module_syntax: module_lexer.has_module_syntax,
facade: module_lexer.facade,
}
Expand Down
31 changes: 3 additions & 28 deletions crates/oxc_module_lexer/tests/typescript.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod esm;
use esm::{ExportSpecifier, ImportSpecifier, ModuleLexer};
use esm::ModuleLexer;
use oxc_allocator::Allocator;
use oxc_parser::Parser;
use oxc_span::SourceType;
Expand All @@ -11,33 +11,8 @@ fn parse(source: &str) -> ModuleLexer {
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(|i| ImportSpecifier {
n: i.n.map(|n| n.to_string()),
s: i.s,
e: i.e,
ss: i.ss,
se: i.se,
d: i.d,
a: i.a,
t: i.t,
})
.collect(),
exports: module_lexer
.exports
.into_iter()
.map(|e| ExportSpecifier {
n: e.n.to_string(),
ln: e.ln.map(|ln| ln.to_string()),
s: e.s,
e: e.e,
ls: e.ls,
le: e.le,
t: e.t,
})
.collect(),
imports: module_lexer.imports.into_iter().map(|i| i.into()).collect(),
exports: module_lexer.exports.into_iter().map(|e| e.into()).collect(),
has_module_syntax: module_lexer.has_module_syntax,
facade: module_lexer.facade,
}
Expand Down