From a6605546a3aaf5fd1ce25132df8eda0c2fa0da4b Mon Sep 17 00:00:00 2001 From: Yunfei He Date: Fri, 5 Jul 2024 17:01:05 +0800 Subject: [PATCH] feat(core): make sure `Semantic` up-to-date after transforming (#1531) ### Description --- crates/rolldown/src/ast_scanner/mod.rs | 7 ++++++- crates/rolldown/src/utils/pre_process_ast.rs | 12 +++++------ .../module_types/ts/{ => basic}/_config.json | 0 .../ts/{ => basic}/artifacts.snap | 0 .../module_types/ts/{ => basic}/main.ts | 0 .../_config.json | 8 +++++++ .../artifacts.snap | 21 +++++++++++++++++++ .../main.ts | 5 +++++ .../fixtures__filename_with_hash.snap | 6 +++++- 9 files changed, 51 insertions(+), 8 deletions(-) rename crates/rolldown/tests/fixtures/module_types/ts/{ => basic}/_config.json (100%) rename crates/rolldown/tests/fixtures/module_types/ts/{ => basic}/artifacts.snap (100%) rename crates/rolldown/tests/fixtures/module_types/ts/{ => basic}/main.ts (100%) create mode 100644 crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/_config.json create mode 100644 crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/artifacts.snap create mode 100644 crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/main.ts diff --git a/crates/rolldown/src/ast_scanner/mod.rs b/crates/rolldown/src/ast_scanner/mod.rs index d01334679e90..afbfe7fb0baa 100644 --- a/crates/rolldown/src/ast_scanner/mod.rs +++ b/crates/rolldown/src/ast_scanner/mod.rs @@ -373,7 +373,12 @@ impl<'me> AstScanner<'me> { // If the reference is a global variable, `None` will be returned. fn resolve_symbol_from_reference(&self, id_ref: &IdentifierReference) -> Option { - let ref_id = id_ref.reference_id.get().expect("must have reference id"); + let ref_id = id_ref.reference_id.get().unwrap_or_else(|| { + panic!( + "{id_ref:#?} must have reference id in code```\n{}\n```\n", + self.current_stmt_info.debug_label.as_deref().unwrap_or("") + ) + }); self.scopes.symbol_id_for(ref_id) } fn scan_export_default_decl(&mut self, decl: &ExportDefaultDeclaration) { diff --git a/crates/rolldown/src/utils/pre_process_ast.rs b/crates/rolldown/src/utils/pre_process_ast.rs index 5657daf6e407..5cb3f15808a1 100644 --- a/crates/rolldown/src/utils/pre_process_ast.rs +++ b/crates/rolldown/src/utils/pre_process_ast.rs @@ -19,8 +19,6 @@ pub fn pre_process_ast( path: &Path, source_type: SourceType, ) -> anyhow::Result<(OxcAst, SymbolTable, ScopeTree)> { - let (mut symbols, mut scopes) = ast.make_symbol_table_and_scope_tree(); - if !matches!(parse_type, OxcParseType::Js) { let trivias = ast.trivias.clone(); let ret = ast.program.with_mut(move |fields| { @@ -44,15 +42,14 @@ pub fn pre_process_ast( trivias, transformer_options, ) - .build_with_symbols_and_scopes(symbols, scopes, fields.program) + .build(fields.program) }); if !ret.errors.is_empty() { return Err(anyhow::anyhow!("Transform failed, got {:#?}", ret.errors)); } - - symbols = ret.symbols; - scopes = ret.scopes; + // symbols = ret.symbols; + // scopes = ret.scopes; } ast.program.with_mut(|fields| { @@ -63,5 +60,8 @@ pub fn pre_process_ast( tweak_ast_for_scanning(&mut ast); + // We have to re-create the symbol table and scope tree after the transformation so far to make sure they are up-to-date. + let (symbols, scopes) = ast.make_symbol_table_and_scope_tree(); + Ok((ast, symbols, scopes)) } diff --git a/crates/rolldown/tests/fixtures/module_types/ts/_config.json b/crates/rolldown/tests/fixtures/module_types/ts/basic/_config.json similarity index 100% rename from crates/rolldown/tests/fixtures/module_types/ts/_config.json rename to crates/rolldown/tests/fixtures/module_types/ts/basic/_config.json diff --git a/crates/rolldown/tests/fixtures/module_types/ts/artifacts.snap b/crates/rolldown/tests/fixtures/module_types/ts/basic/artifacts.snap similarity index 100% rename from crates/rolldown/tests/fixtures/module_types/ts/artifacts.snap rename to crates/rolldown/tests/fixtures/module_types/ts/basic/artifacts.snap diff --git a/crates/rolldown/tests/fixtures/module_types/ts/main.ts b/crates/rolldown/tests/fixtures/module_types/ts/basic/main.ts similarity index 100% rename from crates/rolldown/tests/fixtures/module_types/ts/main.ts rename to crates/rolldown/tests/fixtures/module_types/ts/basic/main.ts diff --git a/crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/_config.json b/crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/_config.json new file mode 100644 index 000000000000..c7e693a6cfcb --- /dev/null +++ b/crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/_config.json @@ -0,0 +1,8 @@ +{ + "config": { + "input": [{ + "name": "main_ts", + "import": "main.ts" + }] + } +} diff --git a/crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/artifacts.snap b/crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/artifacts.snap new file mode 100644 index 000000000000..24ad79161a44 --- /dev/null +++ b/crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/artifacts.snap @@ -0,0 +1,21 @@ +--- +source: crates/rolldown/tests/common/case.rs +expression: content +input_file: crates/rolldown/tests/fixtures/module_types/ts/reference_id_not_exist_after_transform +--- +# Assets + +## main_ts.mjs + +```js + +//#region main.ts +var LexerState = function(LexerState$1) { + LexerState$1[LexerState$1['inParens'] = 0] = 'inParens'; + LexerState$1[LexerState$1['inSingleQuoteString'] = 1] = 'inSingleQuoteString'; + LexerState$1[LexerState$1['inDoubleQuoteString'] = 2] = 'inDoubleQuoteString'; + return LexerState$1; +}(LexerState || {}); + +//#endregion +``` diff --git a/crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/main.ts b/crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/main.ts new file mode 100644 index 000000000000..e163634f2f6f --- /dev/null +++ b/crates/rolldown/tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform/main.ts @@ -0,0 +1,5 @@ +enum LexerState { + inParens, + inSingleQuoteString, + inDoubleQuoteString, +} \ No newline at end of file diff --git a/crates/rolldown/tests/snapshots/fixtures__filename_with_hash.snap b/crates/rolldown/tests/snapshots/fixtures__filename_with_hash.snap index e25bc8d25710..759c84f416a7 100644 --- a/crates/rolldown/tests/snapshots/fixtures__filename_with_hash.snap +++ b/crates/rolldown/tests/snapshots/fixtures__filename_with_hash.snap @@ -1577,10 +1577,14 @@ expression: "snapshot_outputs.join(\"\\n\")" - main_jsx-!~{000}~.mjs => main_jsx-TL8s4o_9.mjs -# tests/fixtures/module_types/ts +# tests/fixtures/module_types/ts/basic - main_ts-!~{000}~.mjs => main_ts-NyiJuHHI.mjs +# tests/fixtures/module_types/ts/enum_reference_id_not_exist_after_transform + +- main_ts-!~{000}~.mjs => main_ts-m4kmDcdH.mjs + # tests/fixtures/module_types/tsx - main_tsx-!~{000}~.mjs => main_tsx-7hnq3HXl.mjs