Skip to content

Commit

Permalink
feat(codegen): prefer backquotes over double / single quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Feb 2, 2025
1 parent dfe2254 commit 7700322
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
8 changes: 4 additions & 4 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ impl Gen for ImportDeclaration<'_> {
p.print_str("from");
}
p.print_soft_space();
self.source.print(p, ctx);
p.print_quoted_utf16(&self.source.value, false);
if let Some(with_clause) = &self.with_clause {
p.print_soft_space();
with_clause.print(p, ctx);
Expand Down Expand Up @@ -1055,7 +1055,7 @@ impl Gen for ExportNamedDeclaration<'_> {
p.print_soft_space();
p.print_str("from");
p.print_soft_space();
source.print(p, ctx);
p.print_quoted_utf16(&source.value, false);
}
p.print_semicolon_after_statement();
}
Expand Down Expand Up @@ -1111,7 +1111,7 @@ impl Gen for ModuleExportName<'_> {
match self {
Self::IdentifierName(ident) => ident.print(p, ctx),
Self::IdentifierReference(ident) => ident.print(p, ctx),
Self::StringLiteral(literal) => literal.print(p, ctx),
Self::StringLiteral(literal) => p.print_quoted_utf16(&literal.value, false),
};
}
}
Expand Down Expand Up @@ -1139,7 +1139,7 @@ impl Gen for ExportAllDeclaration<'_> {

p.print_str("from");
p.print_soft_space();
self.source.print(p, ctx);
p.print_quoted_utf16(&self.source.value, false);
if let Some(with_clause) = &self.with_clause {
p.print_hard_space();
with_clause.print(p, ctx);
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,13 +612,13 @@ impl<'a> Codegen<'a> {
}
}
let mut quote = b'"';
if double_cost > single_cost {
quote = b'\'';
if single_cost > backtick_cost && allow_backtick {
quote = b'`';
}
} else if double_cost > backtick_cost && allow_backtick {
if allow_backtick && double_cost >= backtick_cost {
quote = b'`';
if backtick_cost > single_cost {
quote = b'\'';
}
} else if double_cost > single_cost {
quote = b'\'';
}
quote
} else {
Expand Down
24 changes: 12 additions & 12 deletions crates/oxc_codegen/tests/integration/snapshots/minify.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,51 @@ function foo<T extends string>(x: T, y: string, ...restOfParams: Omit<T, 'x'>):
return x;
}
----------
function foo<T extends string>(x:T,y:string,...restOfParams:Omit<T,"x">): T{return x}
function foo<T extends string>(x:T,y:string,...restOfParams:Omit<T,`x`>): T{return x}
########## 2
let x: string[] = ['abc', 'def', 'ghi'];
----------
let x:string[]=["abc","def","ghi"];
let x:string[]=[`abc`,`def`,`ghi`];
########## 3
let x: Array<string> = ['abc', 'def', 'ghi',];
----------
let x:Array<string>=["abc","def","ghi"];
let x:Array<string>=[`abc`,`def`,`ghi`];
########## 4
let x: [string, number] = ['abc', 123];
----------
let x:[string,number]=["abc",123];
let x:[string,number]=[`abc`,123];
########## 5
let x: string | number = 'abc';
----------
let x:string|number="abc";
let x:string|number=`abc`;
########## 6
let x: string & number = 'abc';
----------
let x:string&number="abc";
let x:string&number=`abc`;
########## 7
let x: typeof String = 'string';
----------
let x:typeof String="string";
let x:typeof String=`string`;
########## 8
let x: keyof string = 'length';
----------
let x:keyof string="length";
let x:keyof string=`length`;
########## 9
let x: keyof typeof String = 'length';
----------
let x:keyof typeof String="length";
let x:keyof typeof String=`length`;
########## 10
let x: string['length'] = 123;
----------
let x:string["length"]=123;
let x:string[`length`]=123;
########## 11
function isString(value: unknown): asserts value is string {
if (typeof value !== 'string') {
throw new Error('Not a string');
}
}
----------
function isString(value:unknown): asserts value is string{if(typeof value!=="string"){throw new Error("Not a string")}}
function isString(value:unknown): asserts value is string{if(typeof value!==`string`){throw new Error(`Not a string`)}}
########## 12
import type { Foo } from 'foo';
----------
Expand All @@ -74,7 +74,7 @@ type A<T>={[K in keyof T as K extends string ? B<K> : K]:T[K]};
########## 16
class A {readonly type = 'frame'}
----------
class A{readonly type="frame"}
class A{readonly type=`frame`}
########## 17
let foo: { <T>(t: T): void }
----------
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@ fn expr() {
test("1000000000000000128.0.toFixed(0)", "0xde0b6b3a7640080.toFixed(0);\n");
test_minify("1000000000000000128.0.toFixed(0)", "0xde0b6b3a7640080.toFixed(0);");

test_minify("throw 'foo'", "throw\"foo\";");
test_minify("return 'foo'", "return\"foo\";");
test_minify("throw 'foo'", "throw`foo`;");
test_minify("return 'foo'", "return`foo`;");
test_minify("return class {}", "return class{};");
test_minify("return async function foo() {}", "return async function foo(){};");
test_minify_same("return super();");
test_minify_same("return new.target;");
test_minify_same("throw await 1;");
test_minify_same("await import(\"\");");
test_minify_same("await import(``);");

test("delete 2e308", "delete (0, Infinity);\n");
test_minify("delete 2e308", "delete(1/0);");

test_minify_same(r#"({"http://a\r\" \n<'b:b@c\r\nd/e?f":{}});"#);
test_minify_same("new(import(\"\"),function(){});");
test_minify_same("new(import(``),function(){});");
}

#[test]
Expand Down
22 changes: 11 additions & 11 deletions tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
| Oxc | ESBuild | Oxc | ESBuild |
Original | minified | minified | gzip | gzip | Fixture
-------------------------------------------------------------------------------------
72.14 kB | 23.56 kB | 23.70 kB | 8.51 kB | 8.54 kB | react.development.js
72.14 kB | 23.56 kB | 23.70 kB | 8.50 kB | 8.54 kB | react.development.js

173.90 kB | 59.55 kB | 59.82 kB | 19.19 kB | 19.33 kB | moment.js
173.90 kB | 59.55 kB | 59.82 kB | 19.18 kB | 19.33 kB | moment.js

287.63 kB | 89.48 kB | 90.07 kB | 30.95 kB | 31.95 kB | jquery.js

342.15 kB | 117.68 kB | 118.14 kB | 43.56 kB | 44.37 kB | vue.js
342.15 kB | 117.68 kB | 118.14 kB | 43.47 kB | 44.37 kB | vue.js

544.10 kB | 71.43 kB | 72.48 kB | 25.87 kB | 26.20 kB | lodash.js
544.10 kB | 71.43 kB | 72.48 kB | 25.86 kB | 26.20 kB | lodash.js

555.77 kB | 271.25 kB | 270.13 kB | 88.35 kB | 90.80 kB | d3.js
555.77 kB | 271.25 kB | 270.13 kB | 88.34 kB | 90.80 kB | d3.js

1.01 MB | 440.97 kB | 458.89 kB | 122.50 kB | 126.71 kB | bundle.min.js
1.01 MB | 440.97 kB | 458.89 kB | 122.53 kB | 126.71 kB | bundle.min.js

1.25 MB | 650.36 kB | 646.76 kB | 161.01 kB | 163.73 kB | three.js
1.25 MB | 650.36 kB | 646.76 kB | 160.96 kB | 163.73 kB | three.js

2.14 MB | 718.69 kB | 724.14 kB | 162.15 kB | 181.07 kB | victory.js
2.14 MB | 718.69 kB | 724.14 kB | 162.18 kB | 181.07 kB | victory.js

3.20 MB | 1.01 MB | 1.01 MB | 324.44 kB | 331.56 kB | echarts.js
3.20 MB | 1.01 MB | 1.01 MB | 324.40 kB | 331.56 kB | echarts.js

6.69 MB | 2.30 MB | 2.31 MB | 468.54 kB | 488.28 kB | antd.js
6.69 MB | 2.30 MB | 2.31 MB | 468.58 kB | 488.28 kB | antd.js

10.95 MB | 3.36 MB | 3.49 MB | 862.14 kB | 915.50 kB | typescript.js
10.95 MB | 3.36 MB | 3.49 MB | 862.08 kB | 915.50 kB | typescript.js

0 comments on commit 7700322

Please sign in to comment.