Skip to content

Commit f33b0bc

Browse files
authored
refactor(atoms): Remove JsWord alias (#10071)
**Description:** This is a refactoring I always wanted to do
1 parent 1771222 commit f33b0bc

File tree

127 files changed

+518
-514
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+518
-514
lines changed

.changeset/silly-chefs-deliver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
swc_atoms: major
3+
---
4+
5+
refactor(atoms): Remove `JsWord` alias

bindings/binding_core_node/src/bundle.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use napi::{
1111
use rustc_hash::FxHashMap;
1212
use serde::Deserialize;
1313
use swc_core::{
14-
atoms::JsWord,
14+
atoms::Atom,
1515
base::{
1616
config::SourceMapsConfig,
1717
resolver::{environment_resolver, paths_resolver},
@@ -61,7 +61,7 @@ impl Task for BundleTask {
6161
NODE_BUILTINS
6262
.iter()
6363
.copied()
64-
.map(JsWord::from)
64+
.map(Atom::from)
6565
.collect::<Vec<_>>()
6666
} else {
6767
Vec::new()

bindings/binding_html_node/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use lightningcss::{
1313
};
1414
use napi::{bindgen_prelude::*, Task};
1515
use serde::{Deserialize, Serialize};
16-
use swc_atoms::js_word;
16+
use swc_atoms::atom;
1717
use swc_cached::regex::CachedRegex;
1818
use swc_common::{sync::Lrc, FileName, FilePathMapping, SourceMap, DUMMY_SP};
1919
use swc_html::{
@@ -522,7 +522,7 @@ fn minify_inner(
522522
Some(context_element) => create_element(context_element)?,
523523
_ => swc_html_ast::Element {
524524
span: DUMMY_SP,
525-
tag_name: js_word!("template"),
525+
tag_name: atom!("template"),
526526
namespace: Namespace::HTML,
527527
attributes: vec![],
528528
children: vec![],

crates/jsdoc/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use swc_atoms::JsWord;
1+
use swc_atoms::Atom;
22
use swc_common::{ast_node, Span};
33

44
/// Spanned text
55
#[ast_node]
66
#[derive(Eq)]
77
pub struct Text {
88
pub span: Span,
9-
pub value: JsWord,
9+
pub value: Atom,
1010
}
1111

1212
#[ast_node]

crates/swc/src/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33
use compat::es2015::regenerator;
44
use either::Either;
55
use rustc_hash::FxHashMap;
6-
use swc_atoms::JsWord;
6+
use swc_atoms::Atom;
77
use swc_common::{
88
comments::Comments, errors::Handler, sync::Lrc, util::take::Take, FileName, Mark, SourceMap,
99
};
@@ -124,7 +124,7 @@ impl<'a, 'b, P: Pass> PassBuilder<'a, 'b, P> {
124124

125125
pub fn const_modules(
126126
self,
127-
globals: FxHashMap<JsWord, FxHashMap<JsWord, String>>,
127+
globals: FxHashMap<Atom, FxHashMap<Atom, String>>,
128128
) -> PassBuilder<'a, 'b, (P, impl Pass)> {
129129
let cm = self.cm.clone();
130130
self.then(const_modules(cm, globals))

crates/swc/src/config/mod.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use indexmap::IndexMap;
1111
use once_cell::sync::Lazy;
1212
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
1313
use serde::{Deserialize, Serialize};
14-
use swc_atoms::JsWord;
14+
use swc_atoms::Atom;
1515
use swc_cached::regex::CachedRegex;
1616
#[allow(unused)]
1717
use swc_common::plugin::metadata::TransformPluginMetadataContext;
@@ -1490,7 +1490,7 @@ pub struct HiddenTransformConfig {
14901490
#[serde(deny_unknown_fields, rename_all = "camelCase")]
14911491
pub struct ConstModulesConfig {
14921492
#[serde(default)]
1493-
pub globals: FxHashMap<JsWord, FxHashMap<JsWord, String>>,
1493+
pub globals: FxHashMap<Atom, FxHashMap<Atom, String>>,
14941494
}
14951495

14961496
#[derive(Debug, Default, Clone, Serialize, Deserialize, Merge)]
@@ -1551,19 +1551,19 @@ pub struct ErrorConfig {
15511551
#[serde(deny_unknown_fields, rename_all = "camelCase")]
15521552
pub struct GlobalPassOption {
15531553
#[serde(default)]
1554-
pub vars: IndexMap<JsWord, JsWord, FxBuildHasher>,
1554+
pub vars: IndexMap<Atom, Atom, FxBuildHasher>,
15551555
#[serde(default)]
15561556
pub envs: GlobalInliningPassEnvs,
15571557

15581558
#[serde(default)]
1559-
pub typeofs: FxHashMap<JsWord, JsWord>,
1559+
pub typeofs: FxHashMap<Atom, Atom>,
15601560
}
15611561

15621562
#[derive(Debug, Clone, Serialize, Deserialize)]
15631563
#[serde(untagged)]
15641564
pub enum GlobalInliningPassEnvs {
15651565
List(FxHashSet<String>),
1566-
Map(FxHashMap<JsWord, JsWord>),
1566+
Map(FxHashMap<Atom, Atom>),
15671567
}
15681568

15691569
impl Default for GlobalInliningPassEnvs {
@@ -1578,7 +1578,7 @@ impl Default for GlobalInliningPassEnvs {
15781578

15791579
impl GlobalPassOption {
15801580
pub fn build(self, cm: &SourceMap, handler: &Handler) -> impl 'static + Pass {
1581-
type ValuesMap = Arc<FxHashMap<JsWord, Expr>>;
1581+
type ValuesMap = Arc<FxHashMap<Atom, Expr>>;
15821582

15831583
fn expr(cm: &SourceMap, handler: &Handler, src: String) -> Box<Expr> {
15841584
let fm = cm.new_source_file(FileName::Anon.into(), src);
@@ -1605,7 +1605,7 @@ impl GlobalPassOption {
16051605
fn mk_map(
16061606
cm: &SourceMap,
16071607
handler: &Handler,
1608-
values: impl Iterator<Item = (JsWord, JsWord)>,
1608+
values: impl Iterator<Item = (Atom, Atom)>,
16091609
is_env: bool,
16101610
) -> ValuesMap {
16111611
let mut m = HashMap::default();
@@ -1652,7 +1652,7 @@ impl GlobalPassOption {
16521652
}
16531653

16541654
GlobalInliningPassEnvs::Map(map) => {
1655-
static CACHE: Lazy<DashMap<Vec<(JsWord, JsWord)>, ValuesMap, FxBuildHasher>> =
1655+
static CACHE: Lazy<DashMap<Vec<(Atom, Atom)>, ValuesMap, FxBuildHasher>> =
16561656
Lazy::new(Default::default);
16571657

16581658
let cache_key = self
@@ -1677,7 +1677,7 @@ impl GlobalPassOption {
16771677
};
16781678

16791679
let global_exprs = {
1680-
static CACHE: Lazy<DashMap<Vec<(JsWord, JsWord)>, GlobalExprMap, FxBuildHasher>> =
1680+
static CACHE: Lazy<DashMap<Vec<(Atom, Atom)>, GlobalExprMap, FxBuildHasher>> =
16811681
Lazy::new(Default::default);
16821682

16831683
let cache_key = self
@@ -1708,7 +1708,7 @@ impl GlobalPassOption {
17081708
};
17091709

17101710
let global_map = {
1711-
static CACHE: Lazy<DashMap<Vec<(JsWord, JsWord)>, ValuesMap, FxBuildHasher>> =
1711+
static CACHE: Lazy<DashMap<Vec<(Atom, Atom)>, ValuesMap, FxBuildHasher>> =
17121712
Lazy::new(Default::default);
17131713

17141714
let cache_key = self

crates/swc/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
//!
2020
//!
2121
//!
22-
//! ### What is [JsWord](swc_atoms::JsWord)?
22+
//! ### What is [Atom](swc_atoms::Atom)?
2323
//!
2424
//! It's basically an interned string. See [swc_atoms].
2525
//!
26-
//! ### Choosing between [JsWord](swc_atoms::JsWord) vs String
26+
//! ### Choosing between [Atom](swc_atoms::Atom) vs String
2727
//!
28-
//! You should prefer [JsWord](swc_atoms::JsWord) over [String] if it's going
28+
//! You should prefer [Atom](swc_atoms::Atom) over [String] if it's going
2929
//! to be stored in an AST node.
3030
//!
3131
//! See [swc_atoms] for detailed description.

crates/swc_atoms/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! See [JsWord] and [Atom]
1+
//! See [Atom] and [UnsafeAtom]
22
33
#![allow(clippy::unreadable_literal)]
44

@@ -21,7 +21,6 @@ use std::{
2121
use once_cell::sync::Lazy;
2222
use serde::Serializer;
2323

24-
pub use self::{atom as js_word, Atom as JsWord};
2524
pub use crate::fast::UnsafeAtom;
2625

2726
mod fast;

crates/swc_bundler/src/bundler/export.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use indexmap::IndexMap;
22
use rustc_hash::FxBuildHasher;
3-
use swc_atoms::JsWord;
3+
use swc_atoms::Atom;
44
use swc_common::{FileName, SyntaxContext};
55
use swc_ecma_ast::*;
66
use swc_ecma_utils::find_pat_ids;
@@ -69,7 +69,7 @@ where
6969
R: Resolve,
7070
{
7171
/// Returns `(local, export)`.
72-
fn ctxt_for(&self, src: &JsWord) -> Option<(SyntaxContext, SyntaxContext)> {
72+
fn ctxt_for(&self, src: &Atom) -> Option<(SyntaxContext, SyntaxContext)> {
7373
// Don't apply mark if it's a core module.
7474
if self
7575
.bundler
@@ -89,7 +89,7 @@ where
8989
))
9090
}
9191

92-
fn mark_as_wrapping_required(&self, src: &JsWord) {
92+
fn mark_as_wrapping_required(&self, src: &Atom) {
9393
// Don't apply mark if it's a core module.
9494
if self
9595
.bundler

crates/swc_bundler/src/bundler/import/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{Context, Error};
22
use rustc_hash::{FxHashMap, FxHashSet};
3-
use swc_atoms::JsWord;
3+
use swc_atoms::Atom;
44
use swc_common::{sync::Lrc, FileName, Mark, Spanned, SyntaxContext, DUMMY_SP};
55
use swc_ecma_ast::*;
66
use swc_ecma_utils::find_pat_ids;
@@ -92,7 +92,7 @@ pub(super) struct RawImports {
9292
/// function bar() {}
9393
/// foo[bar()]
9494
/// ```
95-
pub forced_ns: FxHashSet<JsWord>,
95+
pub forced_ns: FxHashSet<Atom>,
9696
}
9797

9898
/// This type implements two operation (analysis, deglobbing) to reduce binary
@@ -146,7 +146,7 @@ where
146146
R: Resolve,
147147
{
148148
/// Returns (local, export)
149-
fn ctxt_for(&self, src: &JsWord) -> Option<(SyntaxContext, SyntaxContext)> {
149+
fn ctxt_for(&self, src: &Atom) -> Option<(SyntaxContext, SyntaxContext)> {
150150
// Don't apply mark if it's a core module.
151151
if self.bundler.is_external(src) {
152152
return None;
@@ -160,7 +160,7 @@ where
160160
))
161161
}
162162

163-
fn mark_as_wrapping_required(&self, src: &JsWord) {
163+
fn mark_as_wrapping_required(&self, src: &Atom) {
164164
// Don't apply mark if it's a core module.
165165
if self.bundler.is_external(src) {
166166
return;
@@ -175,7 +175,7 @@ where
175175
self.bundler.scope.mark_as_wrapping_required(id);
176176
}
177177

178-
fn mark_as_cjs(&self, src: &JsWord) {
178+
fn mark_as_cjs(&self, src: &Atom) {
179179
let path = self.bundler.resolve(self.path, src);
180180
let path = match path {
181181
Ok(v) => v,

crates/swc_bundler/src/bundler/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::HashMap;
22

33
use anyhow::{Context, Error};
44
use rustc_hash::FxHashMap;
5-
use swc_atoms::JsWord;
5+
use swc_atoms::Atom;
66
use swc_common::{sync::Lrc, FileName, Globals, Mark, SourceMap, SyntaxContext, GLOBALS};
77
use swc_ecma_ast::Module;
88

@@ -41,7 +41,7 @@ pub struct Config {
4141
pub disable_dce: bool,
4242

4343
/// List of modules which should be preserved.
44-
pub external_modules: Vec<JsWord>,
44+
pub external_modules: Vec<Atom>,
4545

4646
/// Type of emitted module
4747
pub module: ModuleType,
@@ -139,7 +139,7 @@ where
139139
})
140140
}
141141

142-
pub(crate) fn is_external(&self, src: &JsWord) -> bool {
142+
pub(crate) fn is_external(&self, src: &Atom) -> bool {
143143
self.config.external_modules.iter().any(|v| v == src)
144144
}
145145

crates/swc_bundler/src/id.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use rustc_hash::FxHashMap;
7-
use swc_atoms::JsWord;
7+
use swc_atoms::Atom;
88
use swc_common::{sync::Lock, FileName, Mark, SyntaxContext, DUMMY_SP};
99
use swc_ecma_ast::{Expr, Ident};
1010
use swc_ecma_utils::ident::IdentLike;
@@ -49,7 +49,7 @@ impl ModuleIdGenerator {
4949
}
5050

5151
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
52-
pub struct Id(JsWord, SyntaxContext);
52+
pub struct Id(Atom, SyntaxContext);
5353

5454
impl fmt::Debug for Id {
5555
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -58,11 +58,11 @@ impl fmt::Debug for Id {
5858
}
5959

6060
impl Id {
61-
pub fn new(sym: JsWord, ctxt: SyntaxContext) -> Self {
61+
pub fn new(sym: Atom, ctxt: SyntaxContext) -> Self {
6262
Id(sym, ctxt)
6363
}
6464

65-
pub fn sym(&self) -> &JsWord {
65+
pub fn sym(&self) -> &Atom {
6666
&self.0
6767
}
6868

@@ -85,11 +85,11 @@ impl IdentLike for Id {
8585
i.into()
8686
}
8787

88-
fn to_id(&self) -> (JsWord, SyntaxContext) {
88+
fn to_id(&self) -> (Atom, SyntaxContext) {
8989
(self.0.clone(), self.1)
9090
}
9191

92-
fn into_id(self) -> (JsWord, SyntaxContext) {
92+
fn into_id(self) -> (Atom, SyntaxContext) {
9393
(self.0, self.1)
9494
}
9595
}
@@ -112,8 +112,8 @@ impl PartialEq<Ident> for Id {
112112
}
113113
}
114114

115-
impl PartialEq<JsWord> for Id {
116-
fn eq(&self, other: &JsWord) -> bool {
115+
impl PartialEq<Atom> for Id {
116+
fn eq(&self, other: &Atom) -> bool {
117117
self.0 == *other
118118
}
119119
}

crates/swc_common/src/errors/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,11 +340,11 @@ impl error::Error for ExplicitBug {
340340
/// handler
341341
/// .struct_span_err(
342342
/// span,
343-
/// &format!("`{}` used as parameter more than once", js_word),
343+
/// &format!("`{}` used as parameter more than once", atom),
344344
/// )
345345
/// .span_note(
346346
/// old_span,
347-
/// &format!("previous definition of `{}` here", js_word),
347+
/// &format!("previous definition of `{}` here", atom),
348348
/// )
349349
/// .emit();
350350
/// });

0 commit comments

Comments
 (0)