Skip to content

Commit

Permalink
feat(core): make sure Semantic up-to-date after transforming (#1531)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
  • Loading branch information
hyf0 authored Jul 5, 2024
1 parent bc260e7 commit a660554
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 8 deletions.
7 changes: 6 additions & 1 deletion crates/rolldown/src/ast_scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SymbolId> {
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("<None>")
)
});
self.scopes.symbol_id_for(ref_id)
}
fn scan_export_default_decl(&mut self, decl: &ExportDefaultDeclaration) {
Expand Down
12 changes: 6 additions & 6 deletions crates/rolldown/src/utils/pre_process_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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| {
Expand All @@ -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))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"config": {
"input": [{
"name": "main_ts",
"import": "main.ts"
}]
}
}
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum LexerState {
inParens,
inSingleQuoteString,
inDoubleQuoteString,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a660554

Please sign in to comment.