Skip to content

Commit

Permalink
pers(es/ast): Introduce IdentName (#9185)
Browse files Browse the repository at this point in the history
**Description:**

The identifier in the `prop` field of `MemberExpr` does not need information like `optional` or `ctxt`.
  • Loading branch information
kdy1 committed Jul 12, 2024
1 parent b2491e5 commit 7b3e5b3
Show file tree
Hide file tree
Showing 2,869 changed files with 30,116 additions and 82,815 deletions.
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

0 comments on commit 7b3e5b3

Please sign in to comment.