Skip to content

Commit

Permalink
chore: follow #162
Browse files Browse the repository at this point in the history
  • Loading branch information
fz6m committed Jan 30, 2024
1 parent 1d8fc70 commit 4a431b1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
50 changes: 29 additions & 21 deletions crates/core/src/visitor.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use std::cmp;

use swc_common::{sync::Lrc, SourceFile, SourceMap, Span};
use swc_common::{
Spanned,
{sync::Lrc, SourceFile, SourceMap, Span},
};
use swc_ecmascript::ast;
use swc_ecmascript::visit::{VisitMut, VisitMutWith};

use crate::constants::*;
use crate::decl::{ImportSpecifier, ExportSpecifier};
use crate::decl::{ExportSpecifier, ImportSpecifier};

pub struct ImportExportVisitor {
pub imports: Vec<ImportSpecifier>,
Expand Down Expand Up @@ -711,28 +714,33 @@ impl VisitMut for ImportExportVisitor {
let d = self.find_code_idx_by_string(import_end, *BRACKET_LEFT);

let mut name = None;
// offset 1 for `(`
let mut start = d + 1;
let mut end = se - 1;

let mut a = *NOT;
if let ast::Expr::Lit(lit) = arg.expr.as_ref() {
if let ast::Lit::Str(src) = lit {
name = Some(src.value.to_string());

// not need trim quotes
(start, end) = self.get_real_span(src.span);

// calc assert
let second_arg = call.args.get(1);
if let Some(arg) = second_arg {
// support object only
if let ast::Expr::Object(obj) = arg.expr.as_ref() {
let obj_span = self.get_real_span(obj.span);
a = obj_span.0;
}

let (start, end) = self.get_real_span(arg.span());

// get static value
match arg.expr.as_ref() {
// import('abc')
ast::Expr::Lit(lit) => {
if let ast::Lit::Str(src) = lit {
name = Some(src.value.to_string());
}
}
// import(`abc`)
ast::Expr::Tpl(_tpl) => {
// TODO: actually, we know what is in there. but `es-module-lexer` does not know.
}
_ => {}
}

// calc assert
let second_arg = call.args.get(1);
if let Some(arg) = second_arg {
// support object only
if let ast::Expr::Object(obj) = arg.expr.as_ref() {
let obj_span = self.get_real_span(obj.span);
a = obj_span.0;
}
}

self.add_import(ImportSpecifier {
Expand Down
8 changes: 6 additions & 2 deletions test/equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@ export const isEqual = async (filename: string, code: string) => {
// hasModuleSyntax
expect(output[0].hasModuleSyntax).toEqual(result[3])
// import
if (process.env.DEBUG_RESULT) {
console.log('rs-module-lexer imports: ', output[0].imports)
console.log('es-module-lexer imports: ', result[0])
}
expect(output[0].imports).toEqual(result[0])
// export
try {
if (process.env.DEBUG_RESULT) {
console.log('rs-module-lexer: ', output[0].exports)
console.log('es-module-lexer: ', result[1])
console.log('rs-module-lexer exports: ', output[0].exports)
console.log('es-module-lexer exports: ', result[1])
}
expect(output[0].exports).toEqual(result[1])
} catch {
Expand Down
25 changes: 25 additions & 0 deletions test/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ describe('Lexer', () => {
await isEqual(FILENAME, source)
})

test(`Dynamic import expression range 2`, async () => {
const source = 'import(/* comment */ `asdf` /* comment */)'
await isEqual(FILENAME, source)
})

test(`Dynamic import expression range 3`, async () => {
const source = 'import(`asdf` // comment\n)'
await isEqual(FILENAME, source)
})

test(`Dynamic import expression range 4`, async () => {
const source = 'import("foo" + /* comment */ "bar")'
await isEqual(FILENAME, source)
})

test(`Dynamic import expression range 5`, async () => {
const source = 'import((() => { return "foo" })() /* comment */)'
await isEqual(FILENAME, source)
})

test(`Dynamic import expression range 6`, async () => {
const source = 'import(/* comment */ `asdf` /* comment */ /* comment 2 */)'
await isEqual(FILENAME, source)
})

// 🟡 `es-module-lexer` seems to be parsed incorrectly.
test.skip(`Simple export destructuring`, async () => {
const source = `
Expand Down

0 comments on commit 4a431b1

Please sign in to comment.