Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove decl_collector pass and use SWC's unresolved_mark instead #9520

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 23 additions & 23 deletions packages/transformers/js/core/src/collect.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::id;
use crate::utils::{
match_export_name, match_export_name_ident, match_import, match_member_expr, match_property_name,
match_require, Bailout, BailoutReason, SourceLocation,
is_unresolved, match_export_name, match_export_name_ident, match_import, match_member_expr,
match_property_name, match_require, Bailout, BailoutReason, SourceLocation,
};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct Export {

pub struct Collect {
pub source_map: Lrc<swc_core::common::SourceMap>,
pub decls: HashSet<Id>,
pub unresolved_mark: Mark,
pub ignore_mark: Mark,
pub global_mark: Mark,
pub static_cjs_exports: bool,
Expand Down Expand Up @@ -117,15 +117,15 @@ pub struct CollectResult {
impl Collect {
pub fn new(
source_map: Lrc<swc_core::common::SourceMap>,
decls: HashSet<Id>,
unresolved_mark: Mark,
ignore_mark: Mark,
global_mark: Mark,
trace_bailouts: bool,
is_module: bool,
) -> Self {
Collect {
source_map,
decls,
unresolved_mark,
ignore_mark,
global_mark,
is_module,
Expand Down Expand Up @@ -629,17 +629,17 @@ impl Visit for Collect {
// if exports, ensure only static member expression
// if require, could be static access (handle in fold)

if match_member_expr(node, vec!["module", "exports"], &self.decls) {
if match_member_expr(node, vec!["module", "exports"], self.unresolved_mark) {
self.static_cjs_exports = false;
self.has_cjs_exports = true;
return;
}

if match_member_expr(node, vec!["module", "hot"], &self.decls) {
if match_member_expr(node, vec!["module", "hot"], self.unresolved_mark) {
return;
}

if match_member_expr(node, vec!["module", "require"], &self.decls) {
if match_member_expr(node, vec!["module", "require"], self.unresolved_mark) {
return;
}

Expand All @@ -665,17 +665,17 @@ impl Visit for Collect {

match &*node.obj {
Expr::Member(member) => {
if match_member_expr(member, vec!["module", "exports"], &self.decls) {
if match_member_expr(member, vec!["module", "exports"], self.unresolved_mark) {
handle_export!();
return;
} else {
member.visit_with(self);
}
}
Expr::Ident(ident) => {
if &*ident.sym == "exports" && !self.decls.contains(&id!(ident)) {
if &*ident.sym == "exports" && is_unresolved(&ident, self.unresolved_mark) {
handle_export!();
} else if ident.sym == js_word!("module") && !self.decls.contains(&id!(ident)) {
} else if ident.sym == js_word!("module") && is_unresolved(&ident, self.unresolved_mark) {
self.has_cjs_exports = true;
self.static_cjs_exports = false;
self.should_wrap = true;
Expand Down Expand Up @@ -713,7 +713,7 @@ impl Visit for Collect {
if node.op == UnaryOp::TypeOf {
match &*node.arg {
Expr::Ident(ident)
if ident.sym == js_word!("module") && !self.decls.contains(&id!(ident)) =>
if ident.sym == js_word!("module") && is_unresolved(&ident, self.unresolved_mark) =>
{
// Do nothing to avoid the ident visitor from marking the module as non-static.
}
Expand Down Expand Up @@ -751,7 +751,7 @@ impl Visit for Collect {
// Bail if `module` or `exports` are accessed non-statically.
let is_module = ident.sym == js_word!("module");
let is_exports = &*ident.sym == "exports";
if (is_module || is_exports) && !self.decls.contains(&id!(ident)) {
if (is_module || is_exports) && is_unresolved(&ident, self.unresolved_mark) {
self.has_cjs_exports = true;
self.static_cjs_exports = false;
if is_module {
Expand Down Expand Up @@ -823,7 +823,7 @@ impl Visit for Collect {
node.right.visit_with(self);

if let PatOrExpr::Pat(pat) = &node.left {
if has_binding_identifier(pat, &"exports".into(), &self.decls) {
if has_binding_identifier(pat, &"exports".into(), self.unresolved_mark) {
// Must wrap for cases like
// ```
// function logExports() {
Expand All @@ -838,7 +838,7 @@ impl Visit for Collect {
self.has_cjs_exports = true;
self.should_wrap = true;
self.add_bailout(node.span, BailoutReason::ExportsReassignment);
} else if has_binding_identifier(pat, &"module".into(), &self.decls) {
} else if has_binding_identifier(pat, &"module".into(), self.unresolved_mark) {
// Same for `module`. If it is reassigned we can't correctly statically analyze.
self.static_cjs_exports = false;
self.has_cjs_exports = true;
Expand Down Expand Up @@ -910,7 +910,7 @@ impl Visit for Collect {
if let Callee::Expr(expr) = &node.callee {
match &**expr {
Expr::Ident(ident) => {
if ident.sym == js_word!("eval") && !self.decls.contains(&id!(ident)) {
if ident.sym == js_word!("eval") && is_unresolved(&ident, self.unresolved_mark) {
self.should_wrap = true;
self.add_bailout(node.span, BailoutReason::Eval);
}
Expand Down Expand Up @@ -950,7 +950,7 @@ impl Visit for Collect {

impl Collect {
pub fn match_require(&self, node: &Expr) -> Option<JsWord> {
match_require(node, &self.decls, self.ignore_mark)
match_require(node, self.unresolved_mark, self.ignore_mark)
}

fn add_pat_imports(&mut self, node: &Pat, src: &JsWord, kind: ImportKind) {
Expand Down Expand Up @@ -1124,28 +1124,28 @@ impl Collect {
}
}

fn has_binding_identifier(node: &Pat, sym: &JsWord, decls: &HashSet<Id>) -> bool {
fn has_binding_identifier(node: &Pat, sym: &JsWord, unresolved_mark: Mark) -> bool {
match node {
Pat::Ident(ident) => {
if ident.id.sym == *sym && !decls.contains(&id!(ident.id)) {
if ident.id.sym == *sym && is_unresolved(&ident, unresolved_mark) {
return true;
}
}
Pat::Object(object) => {
for prop in &object.props {
match prop {
ObjectPatProp::KeyValue(kv) => {
if has_binding_identifier(&kv.value, sym, decls) {
if has_binding_identifier(&kv.value, sym, unresolved_mark) {
return true;
}
}
ObjectPatProp::Assign(assign) => {
if assign.key.sym == *sym && !decls.contains(&id!(assign.key)) {
if assign.key.sym == *sym && is_unresolved(&assign.key, unresolved_mark) {
return true;
}
}
ObjectPatProp::Rest(rest) => {
if has_binding_identifier(&rest.arg, sym, decls) {
if has_binding_identifier(&rest.arg, sym, unresolved_mark) {
return true;
}
}
Expand All @@ -1154,7 +1154,7 @@ fn has_binding_identifier(node: &Pat, sym: &JsWord, decls: &HashSet<Id>) -> bool
}
Pat::Array(array) => {
for el in array.elems.iter().flatten() {
if has_binding_identifier(el, sym, decls) {
if has_binding_identifier(el, sym, unresolved_mark) {
return true;
}
}
Expand Down
109 changes: 0 additions & 109 deletions packages/transformers/js/core/src/decl_collector.rs

This file was deleted.