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

pers(es/ast): Introduce IdentName #9185

Merged
merged 80 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
14bccb9
Declare `IdentName`
kdy1 Jul 9, 2024
cdf3239
re-export
kdy1 Jul 9, 2024
02b25a2
More work for IdentName
kdy1 Jul 9, 2024
ace465f
span
kdy1 Jul 9, 2024
e9557eb
IdentName
kdy1 Jul 9, 2024
c100cba
fix AST
kdy1 Jul 9, 2024
423c11a
Fix more
kdy1 Jul 9, 2024
df6dc83
Fix more
kdy1 Jul 9, 2024
cea4233
IdentName
kdy1 Jul 9, 2024
3906285
More fix
kdy1 Jul 9, 2024
a018dc8
swc_ecma_ast/lib.rs
kdy1 Jul 9, 2024
4627a17
Fix more
kdy1 Jul 9, 2024
d5fb0fe
Fix more
kdy1 Jul 9, 2024
054f921
Fix more
kdy1 Jul 9, 2024
8400f99
Fix more
kdy1 Jul 9, 2024
9d87834
Fix more
kdy1 Jul 9, 2024
81d7b8f
More from impl
kdy1 Jul 9, 2024
8abbc4d
More from impl
kdy1 Jul 9, 2024
6f8a094
More from impl
kdy1 Jul 9, 2024
ff46240
Fix visitor
kdy1 Jul 10, 2024
208aad2
Fix visitor
kdy1 Jul 10, 2024
e3eee03
Fix quote
kdy1 Jul 10, 2024
935453c
Fix visitor
kdy1 Jul 10, 2024
a1ec71a
Fix more
kdy1 Jul 10, 2024
322400e
More from impl
kdy1 Jul 10, 2024
50cc436
More fix
kdy1 Jul 10, 2024
250ddbe
More fix
kdy1 Jul 10, 2024
1a6fa53
More fix
kdy1 Jul 10, 2024
d5a2808
More fix
kdy1 Jul 10, 2024
da31112
More fix
kdy1 Jul 10, 2024
791cd9d
More fix
kdy1 Jul 10, 2024
8aba0d0
More fix
kdy1 Jul 10, 2024
d833aaa
More trait impl
kdy1 Jul 10, 2024
b57343a
More fix
kdy1 Jul 10, 2024
a13851c
More fix
kdy1 Jul 10, 2024
87a61d2
More fix
kdy1 Jul 10, 2024
eb59dc6
More fix
kdy1 Jul 10, 2024
3ebd3f1
More fix
kdy1 Jul 10, 2024
f181641
More fix
kdy1 Jul 10, 2024
8e708fe
More fix
kdy1 Jul 10, 2024
f619264
More fix
kdy1 Jul 10, 2024
5e6df97
More fix
kdy1 Jul 10, 2024
bf12e6d
More fix
kdy1 Jul 10, 2024
8efd3be
More fix
kdy1 Jul 10, 2024
715e5d5
More fix
kdy1 Jul 10, 2024
2d4ddb9
More form impl
kdy1 Jul 10, 2024
1783104
more from impl
kdy1 Jul 10, 2024
e19a702
More fix
kdy1 Jul 10, 2024
b41bbee
More work
kdy1 Jul 10, 2024
263da58
More fix
kdy1 Jul 10, 2024
4a8289b
More fix
kdy1 Jul 10, 2024
4f9f7d1
More from impl
kdy1 Jul 10, 2024
6e272c1
More from impl
kdy1 Jul 10, 2024
357cdb7
More from impl
kdy1 Jul 10, 2024
98adc76
More fix
kdy1 Jul 10, 2024
cb14a2b
More fix
kdy1 Jul 10, 2024
7c4738f
More fix
kdy1 Jul 10, 2024
469dd2c
More fix
kdy1 Jul 10, 2024
726c064
More from impl
kdy1 Jul 10, 2024
c5d5485
More fix
kdy1 Jul 10, 2024
b890886
More fix
kdy1 Jul 10, 2024
0b4c422
More fix
kdy1 Jul 10, 2024
ff9a23a
More fix
kdy1 Jul 10, 2024
08436d3
More fix
kdy1 Jul 10, 2024
5f9a6eb
More fi
kdy1 Jul 10, 2024
b16a824
Fix visitor
kdy1 Jul 10, 2024
8626b19
Update test refs
kdy1 Jul 10, 2024
474191f
Fix
kdy1 Jul 10, 2024
8429b9a
Fix regression
kdy1 Jul 10, 2024
4332681
Fix ast
kdy1 Jul 10, 2024
1fec05e
LabeledStmt.label
kdy1 Jul 10, 2024
1ba3efc
fix ast
kdy1 Jul 10, 2024
7746aed
Fix more
kdy1 Jul 10, 2024
eddc88a
Label
kdy1 Jul 10, 2024
1be2536
Fix more
kdy1 Jul 10, 2024
7e2b625
Fix more
kdy1 Jul 10, 2024
6cfcb79
Update test refs
kdy1 Jul 10, 2024
3c55f50
Update test refs
kdy1 Jul 10, 2024
c2164da
Update test refs
kdy1 Jul 10, 2024
bc1bbd9
reset ci
kdy1 Jul 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions crates/swc_bundler/examples/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,23 +190,23 @@ impl swc_bundler::Hook for Hook {

Ok(vec![
KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt("url".into(), span)),
key: PropName::Ident(IdentName::new("url".into(), span)),
value: Box::new(Expr::Lit(Lit::Str(Str {
span,
raw: None,
value: file_name.into(),
}))),
},
KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt("main".into(), span)),
key: PropName::Ident(IdentName::new("main".into(), span)),
value: Box::new(if module_record.is_entry {
Expr::Member(MemberExpr {
span,
obj: Box::new(Expr::MetaProp(MetaPropExpr {
span,
kind: MetaPropKind::ImportMeta,
})),
prop: MemberProp::Ident(Ident::new_no_ctxt("main".into(), span)),
prop: MemberProp::Ident(IdentName::new("main".into(), span)),
})
} else {
Expr::Lit(Lit::Bool(Bool { span, value: false }))
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/bundler/chunk/cjs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn wrap_module(
init: Some(Box::new(Expr::Call(CallExpr {
span: DUMMY_SP,
callee: Ident::new("__swcpack_require__".into(), DUMMY_SP, helper_ctxt)
.make_member(Ident::new_no_ctxt("bind".into(), DUMMY_SP))
.make_member(quote_ident!("bind"))
.as_callee(),
args: vec![Expr::undefined(DUMMY_SP).as_arg(), module_fn.as_arg()],
..Default::default()
Expand Down Expand Up @@ -284,7 +284,7 @@ where
},
ImportSpecifier::Default(s) => {
props.push(ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident(Ident::new_no_ctxt("default".into(), DUMMY_SP)),
key: PropName::Ident(IdentName::new("default".into(), DUMMY_SP)),
value: Box::new(s.local.into()),
}));
}
Expand Down
6 changes: 2 additions & 4 deletions crates/swc_bundler/src/bundler/chunk/computed_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,10 @@ impl ExportToReturn {
.push(PropOrSpread::Prop(Box::new(Prop::Shorthand(i))));
}

fn export_key_value(&mut self, mut key: Ident, value: Ident) {
key.ctxt = SyntaxContext::empty();

fn export_key_value(&mut self, key: Ident, value: Ident) {
self.return_props
.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(key),
key: PropName::Ident(key.into()),
value: Box::new(Expr::Ident(value)),
}))));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_bundler/src/bundler/chunk/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ where
init: Some(
mod_var
.clone()
.make_member(orig.clone())
.make_member(orig.clone().into())
.into(),
),
definite: Default::default(),
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_bundler/src/bundler/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ where
ExportSpecifier::Default(s) => {
props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt(
key: PropName::Ident(IdentName::new(
"default".into(),
DUMMY_SP,
)),
Expand All @@ -224,7 +224,7 @@ where
};
props.push(PropOrSpread::Prop(Box::new(
Prop::KeyValue(KeyValueProp {
key: PropName::Ident(exported),
key: PropName::Ident(exported.into()),
value: Box::new(Expr::Ident(orig)),
}),
)));
Expand Down Expand Up @@ -258,7 +258,7 @@ where

props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt(
key: PropName::Ident(IdentName::new(
"default".into(),
export.span,
)),
Expand All @@ -279,7 +279,7 @@ where

props.push(PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt(
key: PropName::Ident(IdentName::new(
"default".into(),
export.span,
)),
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/bundler/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ where
};
let prop = match &e.prop {
MemberProp::Ident(i) => {
let mut i = i.clone();
let mut i = Ident::from(i.clone());
i.ctxt = exported_ctxt;
i
}
Expand Down Expand Up @@ -351,7 +351,7 @@ where
};

let mut prop = match &me.prop {
MemberProp::Ident(v) => v.clone(),
MemberProp::Ident(v) => Ident::from(v.clone()),
_ => return,
};
prop.ctxt = self.imported_idents.get(&obj.to_id()).copied().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_bundler/src/bundler/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl VisitMut for KeywordRenamer {
Prop::Shorthand(i) => {
if let Some(renamed) = self.renamed(i) {
*n = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(i.clone()),
key: PropName::Ident(i.clone().into()),
value: Box::new(Expr::Ident(renamed)),
});
}
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_bundler/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl VisitMut for Inliner {
}
if i.sym != orig.sym {
*n = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(orig),
key: PropName::Ident(orig.into()),
value: Box::new(Expr::Ident(i.clone())),
});
}
Expand Down
6 changes: 4 additions & 2 deletions crates/swc_bundler/src/modules/sort/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ impl Visit for FieldInitFinder {
if let Expr::Member(callee) = &**callee {
if callee.obj.is_ident_ref_to("Object") {
match &callee.prop {
MemberProp::Ident(Ident { sym: prop_sym, .. })
MemberProp::Ident(IdentName { sym: prop_sym, .. })
if *prop_sym == *"assign" =>
{
let old = self.in_object_assign;
Expand Down Expand Up @@ -441,7 +441,9 @@ impl Visit for FieldInitFinder {
if !self.in_rhs || self.in_object_assign {
if let Expr::Ident(obj) = &*e.obj {
match &e.prop {
MemberProp::Ident(Ident { sym: prop_sym, .. }) if *prop_sym == *"prototype" => {
MemberProp::Ident(IdentName { sym: prop_sym, .. })
if *prop_sym == *"prototype" =>
{
self.accessed.insert(obj.into());
}
_ => {}
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_bundler/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<T> IntoParallelIterator for T where T: IntoIterator {}

fn metadata(key: &str, value: &str) -> PropOrSpread {
PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt(key.into(), DUMMY_SP)),
key: PropName::Ident(IdentName::new(key.into(), DUMMY_SP)),
value: Box::new(Expr::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: value.into(),
Expand Down Expand Up @@ -242,7 +242,7 @@ impl ExportMetadata {
for prop in &with.props {
if let PropOrSpread::Prop(p) = prop {
if let Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident { sym, .. }),
key: PropName::Ident(IdentName { sym, .. }),
value,
..
}) = &**p
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_bundler/tests/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,23 +1112,23 @@ impl swc_bundler::Hook for Hook {

Ok(vec![
KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt("url".into(), span)),
key: PropName::Ident(IdentName::new("url".into(), span)),
value: Box::new(Expr::Lit(Lit::Str(Str {
span,
raw: None,
value: file_name.into(),
}))),
},
KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt("main".into(), span)),
key: PropName::Ident(IdentName::new("main".into(), span)),
value: Box::new(if module_record.is_entry {
Expr::Member(MemberExpr {
span,
obj: Box::new(Expr::MetaProp(MetaPropExpr {
span,
kind: MetaPropKind::ImportMeta,
})),
prop: MemberProp::Ident(Ident::new_no_ctxt("main".into(), span)),
prop: MemberProp::Ident(IdentName::new("main".into(), span)),
})
} else {
Expr::Lit(Lit::Bool(Bool { span, value: false }))
Expand Down
8 changes: 4 additions & 4 deletions crates/swc_bundler/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::Error;
use swc_bundler::{BundleKind, Bundler, Config, ModuleRecord};
use swc_common::{errors::HANDLER, FileName, Globals, Span};
use swc_ecma_ast::{
Bool, Expr, Ident, KeyValueProp, Lit, MemberExpr, MemberProp, MetaPropExpr, MetaPropKind,
Bool, Expr, IdentName, KeyValueProp, Lit, MemberExpr, MemberProp, MetaPropExpr, MetaPropKind,
PropName, Str,
};
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
Expand Down Expand Up @@ -150,23 +150,23 @@ impl swc_bundler::Hook for Hook {

Ok(vec![
KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt("url".into(), span)),
key: PropName::Ident(IdentName::new("url".into(), span)),
value: Box::new(Expr::Lit(Lit::Str(Str {
span,
raw: None,
value: file_name.into(),
}))),
},
KeyValueProp {
key: PropName::Ident(Ident::new_no_ctxt("main".into(), span)),
key: PropName::Ident(IdentName::new("main".into(), span)),
value: Box::new(if module_record.is_entry {
Expr::Member(MemberExpr {
span,
obj: Box::new(Expr::MetaProp(MetaPropExpr {
span,
kind: MetaPropKind::ImportMeta,
})),
prop: MemberProp::Ident(Ident::new_no_ctxt("main".into(), span)),
prop: MemberProp::Ident(IdentName::new("main".into(), span)),
})
} else {
Expr::Lit(Lit::Bool(Bool { span, value: false }))
Expand Down
6 changes: 5 additions & 1 deletion crates/swc_compiler_base/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use swc_common::{
};
use swc_config::config_types::BoolOr;
pub use swc_config::IsModule;
use swc_ecma_ast::{EsVersion, Ident, Program};
use swc_ecma_ast::{EsVersion, Ident, IdentName, Program};
use swc_ecma_codegen::{text_writer::WriteJs, Emitter, Node};
use swc_ecma_minifier::js::JsMinifyCommentOption;
use swc_ecma_parser::{parse_file_as_module, parse_file_as_program, parse_file_as_script, Syntax};
Expand Down Expand Up @@ -409,4 +409,8 @@ impl Visit for IdentCollector {
fn visit_ident(&mut self, ident: &Ident) {
self.names.insert(ident.span.lo, ident.sym.clone());
}

fn visit_ident_name(&mut self, ident: &IdentName) {
self.names.insert(ident.span.lo, ident.sym.clone());
}
}
5 changes: 3 additions & 2 deletions crates/swc_ecma_ast/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
Accessibility, TsExprWithTypeArgs, TsIndexSignature, TsTypeAnn, TsTypeParamDecl,
TsTypeParamInstantiation,
},
BigInt, ComputedPropName, EmptyStmt, Id, Ident, Number,
BigInt, ComputedPropName, EmptyStmt, Id, Ident, IdentName, Number,
};

#[ast_node]
Expand Down Expand Up @@ -295,7 +295,8 @@ pub enum Key {
Public(PropName),
}

bridge_from!(Key, PropName, Ident);
bridge_from!(Key, IdentName, Ident);
bridge_from!(Key, PropName, IdentName);
bridge_from!(Key, PropName, Id);
bridge_from!(Key, PropName, Number);
bridge_from!(Key, PropName, ComputedPropName);
Expand Down
27 changes: 11 additions & 16 deletions crates/swc_ecma_ast/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::{
TsAsExpr, TsConstAssertion, TsInstantiation, TsNonNullExpr, TsSatisfiesExpr, TsTypeAnn,
TsTypeAssertion, TsTypeParamDecl, TsTypeParamInstantiation,
},
ArrayPat, BindingIdent, ComputedPropName, Id, ImportPhase, Invalid, KeyValueProp, Number,
ObjectPat, PropName, Str,
ArrayPat, BindingIdent, ComputedPropName, Id, IdentName, ImportPhase, Invalid, KeyValueProp,
Number, ObjectPat, PropName, Str,
};

#[ast_node(no_clone)]
Expand Down Expand Up @@ -315,18 +315,12 @@ impl Expr {
}

/// #Note
///
/// This preserves SyntaxContext of [`Expr::Ident`], and noop for
/// [`Expr::JSXMember`] and [`Expr::JSXNamespacedName`].
pub fn with_span(mut self, span: Span) -> Expr {
self.set_span(span);
self
}

/// # Note
///
/// This preserves SyntaxContext of [`Expr::Ident`], and noop for
/// [`Expr::JSXMember`] and [`Expr::JSXNamespacedName`].

pub fn set_span(&mut self, span: Span) {
match self {
Expand Down Expand Up @@ -362,8 +356,8 @@ impl Expr {
Expr::MetaProp(e) => e.span = span,
Expr::Await(e) => e.span = span,
Expr::Paren(e) => e.span = span,
Expr::JSXMember(..) => {}
Expr::JSXNamespacedName(..) => {}
Expr::JSXMember(e) => e.span = span,
Expr::JSXNamespacedName(e) => e.span = span,
Expr::JSXEmpty(e) => e.span = span,
Expr::JSXElement(e) => e.span = span,
Expr::JSXFragment(e) => e.span = span,
Expand Down Expand Up @@ -434,6 +428,7 @@ impl Default for Expr {
}
}

bridge_expr_from!(Ident, IdentName);
bridge_expr_from!(Ident, Id);
bridge_expr_from!(FnExpr, Function);
bridge_expr_from!(ClassExpr, Class);
Expand Down Expand Up @@ -538,7 +533,7 @@ impl ObjectLit {
Prop::KeyValue(kv) => {
let key = match &kv.key {
PropName::Ident(i) => i.clone(),
PropName::Str(s) => Ident::new_no_ctxt(s.value.clone(), s.span),
PropName::Str(s) => IdentName::new(s.value.clone(), s.span),
_ => return None,
};

Expand Down Expand Up @@ -604,7 +599,7 @@ impl ImportWith {

#[derive(Debug, Clone, PartialEq, Eq, Hash, EqIgnoreSpan)]
pub struct ImportWithItem {
pub key: Ident,
pub key: IdentName,
pub value: Str,
}

Expand Down Expand Up @@ -850,7 +845,7 @@ pub struct MemberExpr {
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum MemberProp {
#[tag("Identifier")]
Ident(Ident),
Ident(IdentName),
#[tag("PrivateName")]
PrivateName(PrivateName),
#[tag("Computed")]
Expand Down Expand Up @@ -880,7 +875,7 @@ pub struct SuperPropExpr {
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum SuperProp {
#[tag("Identifier")]
Ident(Ident),
Ident(IdentName),
#[tag("Computed")]
Computed(ComputedPropName),
}
Expand All @@ -903,13 +898,13 @@ impl Take for MemberProp {

impl Default for MemberProp {
fn default() -> Self {
MemberProp::Ident(Ident::dummy())
MemberProp::Ident(Default::default())
}
}

impl Take for SuperProp {
fn dummy() -> Self {
SuperProp::Ident(Ident::dummy())
SuperProp::Ident(Default::default())
}
}

Expand Down
Loading
Loading