From 38ecc84c216c0bea0736f7fbd29004f88dc8e109 Mon Sep 17 00:00:00 2001 From: ahaoboy <504595380@qq.com> Date: Fri, 27 Dec 2024 10:33:02 +0800 Subject: [PATCH 1/2] check ascii_only first --- crates/swc_ecma_codegen/src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs index dad7714d6aff..9d09d1f576e1 100644 --- a/crates/swc_ecma_codegen/src/lib.rs +++ b/crates/swc_ecma_codegen/src/lib.rs @@ -4280,21 +4280,22 @@ fn get_quoted_utf16(v: &str, ascii_only: bool, target: EsVersion) -> String { if c.is_ascii() { buf.push(c); } else if c > '\u{FFFF}' { + if !ascii_only { + buf.push(c); + } // if we've got this far the char isn't reserved and if the callee has specified // we should output unicode for non-ascii chars then we have // to make sure we output unicode that is safe for the target // Es5 does not support code point escapes and so surrograte formula must be // used - if target <= EsVersion::Es5 { + else if target <= EsVersion::Es5 { // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae let h = ((c as u32 - 0x10000) / 0x400) + 0xd800; let l = (c as u32 - 0x10000) % 0x400 + 0xdc00; let _ = write!(buf, "\\u{:04X}\\u{:04X}", h, l); - } else if ascii_only { - let _ = write!(buf, "\\u{{{:04X}}}", c as u32); } else { - buf.push(c); + let _ = write!(buf, "\\u{{{:04X}}}", c as u32); } } else if ascii_only { let _ = write!(buf, "\\u{:04X}", c as u16); From 5c7624fc0753784ee3179e5e59cb2266ac42fe67 Mon Sep 17 00:00:00 2001 From: ahaoboy <504595380@qq.com> Date: Fri, 27 Dec 2024 11:01:46 +0800 Subject: [PATCH 2/2] set ascii_only default value is true --- bindings/binding_core_wasm/src/types.rs | 4 ++-- bindings/binding_minifier_wasm/src/types.rs | 4 ++-- crates/swc_ecma_codegen/src/config.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bindings/binding_core_wasm/src/types.rs b/bindings/binding_core_wasm/src/types.rs index 4380d06c3716..823fe04120a3 100644 --- a/bindings/binding_core_wasm/src/types.rs +++ b/bindings/binding_core_wasm/src/types.rs @@ -76,7 +76,7 @@ type ToSnakeCaseProperties = { export interface JsFormatOptions { /** * Currently noop. - * @default false + * @default true * @alias ascii_only */ asciiOnly?: boolean @@ -1069,7 +1069,7 @@ export interface BaseModuleConfig { * Emits `cjs-module-lexer` annotation * `cjs-module-lexer` is used in Node.js core for detecting the named exports available when importing a CJS module into ESM. * swc will emit `cjs-module-lexer` detectable annotation with this option enabled. - * + * * Defaults to `true` if import_interop is Node, else `false` */ exportInteropAnnotation?: boolean; diff --git a/bindings/binding_minifier_wasm/src/types.rs b/bindings/binding_minifier_wasm/src/types.rs index 34adb5187164..dd0bd9a5eefb 100644 --- a/bindings/binding_minifier_wasm/src/types.rs +++ b/bindings/binding_minifier_wasm/src/types.rs @@ -76,7 +76,7 @@ type ToSnakeCaseProperties = { export interface JsFormatOptions { /** * Currently noop. - * @default false + * @default true * @alias ascii_only */ asciiOnly?: boolean @@ -1070,7 +1070,7 @@ export interface BaseModuleConfig { * Emits `cjs-module-lexer` annotation * `cjs-module-lexer` is used in Node.js core for detecting the named exports available when importing a CJS module into ESM. * swc will emit `cjs-module-lexer` detectable annotation with this option enabled. - * + * * Defaults to `true` if import_interop is Node, else `false` */ exportInteropAnnotation?: boolean; diff --git a/crates/swc_ecma_codegen/src/config.rs b/crates/swc_ecma_codegen/src/config.rs index 431028297c02..5bb5f8088f26 100644 --- a/crates/swc_ecma_codegen/src/config.rs +++ b/crates/swc_ecma_codegen/src/config.rs @@ -46,7 +46,7 @@ impl Default for Config { Self { target: EsVersion::latest(), minify: false, - ascii_only: false, + ascii_only: true, omit_last_semi: false, emit_assert_for_import_attributes: false, inline_script: false,