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

[WIP] check ascii_only first fix #9515 #9814

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bindings/binding_core_wasm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type ToSnakeCaseProperties<T> = {
export interface JsFormatOptions {
/**
* Currently noop.
* @default false
* @default true
* @alias ascii_only
*/
asciiOnly?: boolean
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions bindings/binding_minifier_wasm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type ToSnakeCaseProperties<T> = {
export interface JsFormatOptions {
/**
* Currently noop.
* @default false
* @default true
* @alias ascii_only
*/
asciiOnly?: boolean
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_codegen/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions crates/swc_ecma_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading