Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
5 changes: 5 additions & 0 deletions .changeset/metal-radios-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_ecma_ast: major
---

fix(es/ast): Fix unicode lone surrogates handling
1 change: 1 addition & 0 deletions bindings/binding_core_node/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ impl swc_core::bundler::Hook for Hook {
span,
raw: None,
value: file_name.into(),
lone_surrogates: false,
}))),
},
KeyValueProp {
Expand Down
2 changes: 2 additions & 0 deletions bindings/binding_core_wasm/__tests__/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ describe("parse", () => {
"decorators": [],
"identifier": {
"ctxt": 2,
"nodeId": 4294967295,
"optional": false,
"span": {
"end": 254,
Expand Down Expand Up @@ -157,6 +158,7 @@ describe("parse", () => {
"decorators": [],
"identifier": {
"ctxt": 2,
"nodeId": 4294967295,
"optional": false,
"span": {
"end": 267,
Expand Down
2 changes: 1 addition & 1 deletion bindings/binding_core_wasm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ export interface JSXAttribute extends Node, HasSpan {
export type JSXAttributeName = Identifier | JSXNamespacedName;

export type JSXAttrValue =
| Literal
| StringLiteral
| JSXExpressionContainer
| JSXElement
| JSXFragment;
Expand Down
2 changes: 1 addition & 1 deletion bindings/binding_minifier_wasm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,7 @@ export interface JSXAttribute extends Node, HasSpan {
export type JSXAttributeName = Identifier | JSXNamespacedName;

export type JSXAttrValue =
| Literal
| StringLiteral
| JSXExpressionContainer
| JSXElement
| JSXFragment;
Expand Down
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-7xxx/7678/output/1.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let str="\uD83D\uDC68\\u200D\uD83D\uDE80";let obj={"\uD83D\uDC68\\u200D\uD83D\uDE80":"wrong"};
let str="\\uD83D\\uDC68\\u200D\\uD83D\\uDE80";let obj={"\\uD83D\\uDC68\\u200D\\uD83D\\uDE80":"wrong"};
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu1 be floor((cp – 65536) / 1024) + 0xD800.
// Although we should just get back a single code point value of 0xD800,
// this is a useful edge-case test.
var x = "\u{D800}";
var x = "\ud800";
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu1 be floor((cp – 65536) / 1024) + 0xD800.
// Although we should just get back a single code point value of 0xD800,
// this is a useful edge-case test.
var x = "\u{D800}";
var x = "\ud800";
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu2 be ((cp – 65536) modulo 1024) + 0xDC00.
// Although we should just get back a single code point value of 0xDC00,
// this is a useful edge-case test.
var x = "\u{DC00}";
var x = "\udc00";
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu2 be ((cp – 65536) modulo 1024) + 0xDC00.
// Although we should just get back a single code point value of 0xDC00,
// this is a useful edge-case test.
var x = "\u{DC00}";
var x = "\udc00";
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu1 be floor((cp – 65536) / 1024) + 0xD800.
// Although we should just get back a single code point value of 0xD800,
// this is a useful edge-case test.
var x = "\u{D800}";
var x = "\ud800";
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// 2. Let cu2 be ((cp – 65536) modulo 1024) + 0xDC00.
// Although we should just get back a single code point value of 0xDC00,
// this is a useful edge-case test.
var x = "\u{DC00}";
var x = "\udc00";
6 changes: 3 additions & 3 deletions crates/swc/tests/vercel/full/utf8-1/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import r from './on-demand-entries-client';
import { addMessageListener as n, connectHMR as c } from './error-overlay/websocket';
var o = JSON.parse(document.getElementById('__NEXT_DATA__').textContent);
window.__NEXT_DATA__ = o;
var s = o.assetPrefix, i = o.page, _ = null, u = __webpack_hash__, d = (s = s || '') + (s.endsWith('/') ? '' : '/') + '_next/static/webpack/';
var s = o.assetPrefix, i = o.page, _ = null, d = __webpack_hash__, u = (s = s || '') + (s.endsWith('/') ? '' : '/') + '_next/static/webpack/';
n(function(a) {
if ('\uD83D\uDC93' !== a.data) try {
var r = JSON.parse(a.data);
Expand All @@ -28,7 +28,7 @@ n(function(a) {
5
]), [
4,
fetch('undefined' != typeof __webpack_runtime_id__ ? "".concat(d).concat(u, ".").concat(__webpack_runtime_id__, ".hot-update.json") : "".concat(d).concat(u, ".hot-update.json"))
fetch('undefined' != typeof __webpack_runtime_id__ ? "".concat(u).concat(d, ".").concat(__webpack_runtime_id__, ".hot-update.json") : "".concat(u).concat(d, ".hot-update.json"))
];
case 2:
return [
Expand All @@ -38,7 +38,7 @@ n(function(a) {
case 3:
return e = t.sent(), a = '/' === i ? 'index' : i, (Array.isArray(e.c) ? e.c : Object.keys(e.c)).some(function(e) {
return -1 !== e.indexOf("pages".concat(a.startsWith('/') ? a : "/".concat(a))) || -1 !== e.indexOf("pages".concat(a.startsWith('/') ? a : "/".concat(a)).replace(/\//g, '\\'));
}) ? document.location.reload(!0) : u = _, [
}) ? document.location.reload(!0) : d = _, [
3,
5
];
Expand Down
1 change: 1 addition & 0 deletions crates/swc_bundler/examples/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl swc_bundler::Hook for Hook {
span,
raw: None,
value: file_name.into(),
lone_surrogates: false,
}))),
},
KeyValueProp {
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_bundler/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub(crate) trait VarDeclaratorExt: Into<VarDeclarator> {
span: DUMMY_SP,
raw: None,
value: name.into(),
lone_surrogates: false,
}
.assign_to(Ident::new_no_ctxt(atom!("INJECTED_FROM"), DUMMY_SP)),
]
Expand Down Expand Up @@ -193,6 +194,7 @@ fn metadata(key: &str, value: &str) -> PropOrSpread {
span: DUMMY_SP,
value: value.into(),
raw: None,
lone_surrogates: false,
})
.into(),
})))
Expand Down
1 change: 1 addition & 0 deletions crates/swc_bundler/tests/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,7 @@ impl swc_bundler::Hook for Hook {
span,
raw: None,
value: file_name.into(),
lone_surrogates: false,
}))),
},
KeyValueProp {
Expand Down
1 change: 1 addition & 0 deletions crates/swc_bundler/tests/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl swc_bundler::Hook for Hook {
span,
raw: None,
value: file_name.into(),
lone_surrogates: false,
}))),
},
KeyValueProp {
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub use swc_eq_ignore_macros::{EqIgnoreSpan, TypeEq};
pub use self::{
eq::{EqIgnoreSpan, TypeEq},
errors::{SourceMapper, SourceMapperDyn},
node_id::NodeId,
pos::{
hygiene, BytePos, CharPos, FileName, Globals, Loc, LocWithOpt, Mark, MultiSpan, SourceFile,
SourceFileAndBytePos, SourceFileAndLine, Span, SpanLinesError, Spanned, SyntaxContext,
Expand All @@ -59,6 +60,7 @@ mod eq;
pub mod errors;
pub mod input;
pub mod iter;
mod node_id;
pub mod pass;
pub mod plugin;
mod pos;
Expand Down
27 changes: 27 additions & 0 deletions crates/swc_common/src/node_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::util::take::Take;

#[derive(Eq, Hash, Debug, PartialEq, Clone, Copy, serde::Serialize, serde::Deserialize)]
#[cfg_attr(
any(feature = "rkyv-impl"),
derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)
)]
#[cfg_attr(feature = "rkyv-impl", derive(bytecheck::CheckBytes))]
#[cfg_attr(feature = "rkyv-impl", repr(C))]
#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
pub struct NodeId(#[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))] u32);

impl NodeId {
pub const DUMMY: NodeId = NodeId(u32::MAX);
}

impl Default for NodeId {
fn default() -> Self {
NodeId::DUMMY
}
}

impl Take for NodeId {
fn dummy() -> Self {
NodeId::DUMMY
}
}
12 changes: 12 additions & 0 deletions crates/swc_ecma_ast/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,15 @@ pub struct TplElement {
/// You may need to perform. `.replace("\r\n", "\n").replace('\r', "\n")` on
/// this value.
pub raw: Atom,

/// The string value contains lone surrogates.
///
/// `cooked` is encoded with `\u{FFFD}` to mark the lone surrogate as an
/// escaped value.
///
/// For example, a "\uD808" is a lone surrogate, and it's encoded as
/// `\u{FFFD}D808`.
pub lone_surrogates: bool,
}

impl Take for TplElement {
Expand All @@ -1208,6 +1217,7 @@ impl Take for TplElement {
tail: Default::default(),
cooked: None,
raw: Default::default(),
lone_surrogates: false,
}
}
}
Expand All @@ -1219,12 +1229,14 @@ impl<'a> arbitrary::Arbitrary<'a> for TplElement {
let span = u.arbitrary()?;
let cooked = Some(u.arbitrary::<String>()?.into());
let raw = u.arbitrary::<String>()?.into();
let lone_surrogates = u.arbitrary::<bool>()?.into();

Ok(Self {
span,
tail: false,
cooked,
raw,
lone_surrogates,
})
}
}
Expand Down
9 changes: 8 additions & 1 deletion crates/swc_ecma_ast/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use phf::phf_set;
use rustc_hash::FxHashSet;
use swc_atoms::{atom, Atom, UnsafeAtom};
use swc_common::{
ast_node, util::take::Take, BytePos, EqIgnoreSpan, Mark, Span, Spanned, SyntaxContext, DUMMY_SP,
ast_node, util::take::Take, BytePos, EqIgnoreSpan, Mark, NodeId, Span, Spanned, SyntaxContext,
DUMMY_SP,
};

use crate::{typescript::TsTypeAnn, Expr};
Expand Down Expand Up @@ -91,6 +92,7 @@ impl From<&'_ BindingIdent> for Ident {
ctxt: bi.ctxt,
sym: bi.sym.clone(),
optional: bi.optional,
node_id: bi.node_id,
}
}
}
Expand Down Expand Up @@ -179,6 +181,9 @@ pub struct Ident {
#[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))]
pub ctxt: SyntaxContext,

#[cfg_attr(feature = "__rkyv", rkyv(omit_bounds))]
pub node_id: NodeId,

#[cfg_attr(feature = "serde-impl", serde(rename = "value"))]
pub sym: Atom,

Expand Down Expand Up @@ -565,6 +570,7 @@ impl<'a> arbitrary::Arbitrary<'a> for Ident {
sym,
optional,
ctxt: Default::default(),
node_id: NodeId::DUMMY,
})
}
}
Expand Down Expand Up @@ -592,6 +598,7 @@ impl Ident {
ctxt,
sym,
optional: false,
node_id: NodeId::DUMMY,
}
}

Expand Down
10 changes: 2 additions & 8 deletions crates/swc_ecma_ast/src/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP};
use crate::{
expr::{Expr, SpreadElement},
ident::Ident,
lit::Lit,
typescript::TsTypeParamInstantiation,
IdentName,
IdentName, Str,
};

/// Used for `obj` property of `JSXMemberExpr`.
Expand Down Expand Up @@ -190,12 +189,7 @@ pub enum JSXAttrName {
#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
pub enum JSXAttrValue {
#[tag("StringLiteral")]
#[tag("BooleanLiteral")]
#[tag("NullLiteral")]
#[tag("NumericLiteral")]
#[tag("RegExpLiteral")]
#[tag("JSXText")]
Lit(Lit),
Str(Str),

#[tag("JSXExpressionContainer")]
JSXExprContainer(JSXExprContainer),
Expand Down
Loading
Loading