Skip to content

Commit

Permalink
Reresolve to set global_mark for generated vars
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed May 9, 2022
1 parent 63b441f commit f3b832f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 40 deletions.
14 changes: 3 additions & 11 deletions packages/transformers/js/core/src/global_replacer.rs
Expand Up @@ -18,7 +18,6 @@ pub struct GlobalReplacer<'a> {
pub project_root: &'a Path,
pub filename: &'a Path,
pub decls: &'a mut HashSet<(JsWord, SyntaxContext)>,
pub global_mark: swc_common::Mark,
pub scope_hoist: bool,
}

Expand Down Expand Up @@ -150,15 +149,8 @@ impl<'a> Fold for GlobalReplacer<'a> {
}
}

fn create_decl_stmt(
name: swc_atoms::JsWord,
global_mark: swc_common::Mark,
init: ast::Expr,
) -> (ast::Stmt, SyntaxContext) {
let span = DUMMY_SP
// TODO this shouldn't actually be marked global because it's generated
.apply_mark(global_mark)
.apply_mark(Mark::fresh(Mark::root()));
fn create_decl_stmt(name: swc_atoms::JsWord, init: ast::Expr) -> (ast::Stmt, SyntaxContext) {
let span = DUMMY_SP.apply_mark(Mark::fresh(Mark::root()));

(
ast::Stmt::Decl(ast::Decl::Var(ast::VarDecl {
Expand All @@ -185,7 +177,7 @@ impl GlobalReplacer<'_> {
id.span.ctxt = *ctxt;
false
} else {
let (decl, ctxt) = create_decl_stmt(id.sym.clone(), self.global_mark, expr(self));
let (decl, ctxt) = create_decl_stmt(id.sym.clone(), expr(self));

id.span.ctxt = ctxt;

Expand Down
18 changes: 2 additions & 16 deletions packages/transformers/js/core/src/hoist.rs
Expand Up @@ -785,10 +785,6 @@ impl<'a> Fold for Hoist<'a> {
return Ident::new("$parcel$global".into(), node.span);
}

// TODO
// neither ctxt == DUMMY_SP.apply_mark(global_mark)
// nor span.has_mark(self.collect.global_mark)
// are correct.
if node.span.has_mark(self.collect.global_mark)
&& self.collect.decls.contains(&id!(node))
&& !self.collect.should_wrap
Expand Down Expand Up @@ -1078,7 +1074,6 @@ pub struct Collect {
pub decls: HashSet<IdentId>,
pub ignore_mark: Mark,
pub global_mark: Mark,
pub global_ctxt: SyntaxContext,
pub static_cjs_exports: bool,
pub has_cjs_exports: bool,
pub is_esm: bool,
Expand Down Expand Up @@ -1145,7 +1140,6 @@ impl Collect {
decls,
ignore_mark,
global_mark,
global_ctxt: SyntaxContext::empty().apply_mark(global_mark),
static_cjs_exports: true,
has_cjs_exports: false,
is_esm: false,
Expand Down Expand Up @@ -1554,11 +1548,7 @@ impl Visit for Collect {
.or_insert_with(|| node.id.sym.clone());
}

// TODO
// neither ctxt == DUMMY_SP.apply_mark(global_mark)
// nor span.has_mark(self.collect.global_mark)
// are correct.
if self.in_assign && node.id.span.ctxt() == self.global_ctxt {
if self.in_assign && node.id.span.has_mark(self.global_mark) {
self
.non_const_bindings
.entry(id!(node.id))
Expand All @@ -1583,11 +1573,7 @@ impl Visit for Collect {
.or_insert_with(|| node.key.sym.clone());
}

// TODO
// neither ctxt == DUMMY_SP.apply_mark(global_mark)
// nor span.has_mark(self.collect.global_mark)
// are correct.
if self.in_assign && node.key.span.ctxt() == self.global_ctxt {
if self.in_assign && node.key.span.has_mark(self.global_mark) {
self
.non_const_bindings
.entry(id!(node.key))
Expand Down
20 changes: 18 additions & 2 deletions packages/transformers/js/core/src/lib.rs
Expand Up @@ -345,7 +345,6 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
project_root: Path::new(&config.project_root),
filename: Path::new(&config.filename),
decls: &mut decls,
global_mark,
scope_hoist: config.scope_hoist
},
config.insert_node_globals && config.source_type != SourceType::Script
Expand All @@ -370,6 +369,9 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
module.fold_with(&mut passes)
};

// regnerate decls after preset-env ran
let mut decls = collect_decls(&module);

let mut has_node_replacements = false;
let module = module.fold_with(
// Replace __dirname and __filename with placeholders in Node env
Expand All @@ -381,7 +383,6 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
project_root: Path::new(&config.project_root),
filename: Path::new(&config.filename),
decls: &mut decls,
global_mark,
scope_hoist: config.scope_hoist,
has_node_replacements: &mut has_node_replacements,
},
Expand Down Expand Up @@ -412,6 +413,21 @@ pub fn transform(config: Config) -> Result<TransformResult, std::io::Error> {
return Ok(result);
}

// Flush (JsWord, SyntaxContexts) into unique names and reresolve to
// set global_mark for all nodes, even generated ones.
// (This changes the syntax context ids and therefore invalidates decls)
println!("decls {:?}", decls);
let (decls, module) = if config.scope_hoist {
let module = module.fold_with(&mut chain!(
hygiene(),
resolver(unresolved_mark, global_mark, false)
));
(collect_decls(&module), module)
} else {
(decls, module)
};
println!("decls {:?}", decls);

let mut collect = Collect::new(
source_map.clone(),
decls,
Expand Down
14 changes: 3 additions & 11 deletions packages/transformers/js/core/src/node_replacer.rs
Expand Up @@ -18,7 +18,6 @@ pub struct NodeReplacer<'a> {
pub project_root: &'a Path,
pub filename: &'a Path,
pub decls: &'a mut HashSet<(JsWord, SyntaxContext)>,
pub global_mark: swc_common::Mark,
pub scope_hoist: bool,
pub has_node_replacements: &'a mut bool,
}
Expand Down Expand Up @@ -184,15 +183,8 @@ impl<'a> Fold for NodeReplacer<'a> {
}
}

fn create_decl_stmt(
name: swc_atoms::JsWord,
global_mark: swc_common::Mark,
init: ast::Expr,
) -> (ast::Stmt, SyntaxContext) {
let span = DUMMY_SP
// TODO this shouldn't actually be marked global because it's generated
.apply_mark(global_mark)
.apply_mark(Mark::fresh(Mark::root()));
fn create_decl_stmt(name: swc_atoms::JsWord, init: ast::Expr) -> (ast::Stmt, SyntaxContext) {
let span = DUMMY_SP.apply_mark(Mark::fresh(Mark::root()));

(
ast::Stmt::Decl(ast::Decl::Var(ast::VarDecl {
Expand All @@ -219,7 +211,7 @@ impl NodeReplacer<'_> {
id.span.ctxt = *ctxt;
false
} else {
let (decl, ctxt) = create_decl_stmt(id.sym.clone(), self.global_mark, expr(self));
let (decl, ctxt) = create_decl_stmt(id.sym.clone(), expr(self));

id.span.ctxt = ctxt;

Expand Down

0 comments on commit f3b832f

Please sign in to comment.