Skip to content

Commit

Permalink
feat(codegen)!: remove CodegenOptions::enable_typescript (#3674)
Browse files Browse the repository at this point in the history
The typescript transform pass is now required to strip typescript syntax
for codegen to print things properly.

Codegen will now print whatever is in the AST.
  • Loading branch information
Boshen committed Jun 14, 2024
1 parent 4f2db46 commit 534242a
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 271 deletions.
3 changes: 1 addition & 2 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ fn main() -> std::io::Result<()> {
println!("Original:");
println!("{source_text}");

let options =
CodegenOptions { enable_source_map: false, enable_typescript: true, ..Default::default() };
let options = CodegenOptions { enable_source_map: false, ..Default::default() };
let printed = Codegen::<false>::new("", &source_text, ret.trivias, options)
.build(&ret.program)
.source_text;
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_codegen/examples/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ fn main() -> std::io::Result<()> {
return Ok(());
}

let codegen_options =
CodegenOptions { enable_source_map: true, enable_typescript: true, ..Default::default() };
let codegen_options = CodegenOptions { enable_source_map: true, ..Default::default() };

let CodegenReturn { source_text, source_map } = Codegen::<false>::new(
path.to_string_lossy().as_ref(),
Expand Down
264 changes: 93 additions & 171 deletions crates/oxc_codegen/src/gen.rs

Large diffs are not rendered by default.

27 changes: 4 additions & 23 deletions crates/oxc_codegen/src/gen_ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSTypeParameterInstantiation<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSIndexSignature<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
if !p.options.enable_typescript {
return;
}
p.print_str(b"[");
for (index, parameter) in self.parameters.iter().enumerate() {
if index != 0 {
Expand Down Expand Up @@ -563,9 +560,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleDeclaration<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSTypeAliasDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
if !p.options.enable_typescript {
return;
}
p.print_str(b"type ");
self.id.gen(p, ctx);
if let Some(type_parameters) = &self.type_parameters {
Expand All @@ -579,10 +573,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSTypeAliasDeclaration<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSInterfaceDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
if !p.options.enable_typescript {
return;
}

p.print_str(b"interface");
p.print_hard_space();
self.id.gen(p, ctx);
Expand Down Expand Up @@ -623,15 +613,11 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSInterfaceHeritage<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSEnumDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
if !p.options.enable_typescript {
return;
}

p.print_indent();
if self.modifiers.contains(ModifierKind::Export) {
p.print_str(b"export ");
}
if p.options.enable_typescript && self.modifiers.contains(ModifierKind::Declare) {
if self.modifiers.contains(ModifierKind::Declare) {
p.print_str(b"declare ");
}
if self.modifiers.contains(ModifierKind::Const) {
Expand Down Expand Up @@ -684,9 +670,6 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSConstructorType<'a> {

impl<'a, const MINIFY: bool> Gen<MINIFY> for TSImportEqualsDeclaration<'a> {
fn gen(&self, p: &mut Codegen<{ MINIFY }>, ctx: Context) {
if !p.options.enable_typescript {
return;
}
p.print_str(b"import ");
self.id.gen(p, ctx);
p.print_str(b" = ");
Expand All @@ -709,11 +692,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for TSModuleReference<'a> {

impl<'a, const MINIFY: bool> GenExpr<MINIFY> for TSTypeAssertion<'a> {
fn gen_expr(&self, p: &mut Codegen<{ MINIFY }>, precedence: Precedence, ctx: Context) {
if p.options.enable_typescript {
p.print_str(b"<");
self.type_annotation.gen(p, ctx);
p.print_str(b">");
}
p.print_str(b"<");
self.type_annotation.gen(p, ctx);
p.print_str(b">");
self.expression.gen_expr(p, precedence, ctx);
}
}
Expand Down
18 changes: 1 addition & 17 deletions crates/oxc_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,10 @@ pub struct CodegenOptions {
/// Pass in the filename to enable source map support.
pub enable_source_map: bool,

/// Enable TypeScript code generation.
pub enable_typescript: bool,

/// Enable preserve annotate comments, like `/* #__PURE__ */` and `/* #__NO_SIDE_EFFECTS__ */`.
pub preserve_annotate_comments: bool,
}

impl CodegenOptions {
#[must_use]
pub fn with_typescript(mut self, yes: bool) -> Self {
if yes {
self.enable_typescript = true;
}

self
}
}

pub struct CodegenReturn {
pub source_text: String,
pub source_map: Option<oxc_sourcemap::SourceMap>,
Expand Down Expand Up @@ -471,9 +457,7 @@ impl<'a, const MINIFY: bool> Codegen<'a, MINIFY> {
}
for stmt in statements {
if let Some(decl) = stmt.as_declaration() {
if decl.is_typescript_syntax()
&& !self.options.enable_typescript
&& !matches!(decl, Declaration::TSEnumDeclaration(_))
if decl.is_typescript_syntax() && !matches!(decl, Declaration::TSEnumDeclaration(_))
{
continue;
}
Expand Down
29 changes: 12 additions & 17 deletions crates/oxc_codegen/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ fn test(source_text: &str, expected: &str, options: Option<CodegenOptions>) {
let allocator = Allocator::default();
let source_type = SourceType::default().with_module(true);
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let options = options.unwrap_or_default();
let result =
Codegen::<false>::new("", source_text, ret.trivias, options).build(program).source_text;
let result = Codegen::<false>::new("", source_text, ret.trivias, options)
.build(&ret.program)
.source_text;
assert_eq!(expected, result, "for source {source_text}, expect {expected}, got {result}");
}

Expand All @@ -21,10 +21,9 @@ fn test_ts(source_text: &str, expected: &str, is_typescript_definition: bool) {
.with_typescript_definition(is_typescript_definition)
.with_module(true);
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
let codegen_options = CodegenOptions { enable_typescript: true, ..CodegenOptions::default() };
let codegen_options = CodegenOptions::default();
let result = Codegen::<false>::new("", source_text, ret.trivias, codegen_options)
.build(program)
.build(&ret.program)
.source_text;
assert_eq!(expected, result, "for source {source_text}, expect {expected}, got {result}");
}
Expand Down Expand Up @@ -186,11 +185,7 @@ fn test_comment_helper(source_text: &str, expected: &str) {
test(
source_text,
expected,
Some(CodegenOptions {
enable_source_map: true,
enable_typescript: false,
preserve_annotate_comments: true,
}),
Some(CodegenOptions { enable_source_map: true, preserve_annotate_comments: true }),
);
}
#[test]
Expand Down Expand Up @@ -230,9 +225,9 @@ fn annotate_comment() {
/* #__NO_SIDE_EFFECTS__ */ async () => {},
/* #__NO_SIDE_EFFECTS__ */ async (y) => (y),
])",
r"x([/* #__NO_SIDE_EFFECTS__ */ y => y, /* #__NO_SIDE_EFFECTS__ */ () => {
}, /* #__NO_SIDE_EFFECTS__ */ y => y, /* #__NO_SIDE_EFFECTS__ */ async y => y, /* #__NO_SIDE_EFFECTS__ */ async() => {
}, /* #__NO_SIDE_EFFECTS__ */ async y => y,]);
r"x([/* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ () => {
}, /* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ async (y) => y, /* #__NO_SIDE_EFFECTS__ */ async () => {
}, /* #__NO_SIDE_EFFECTS__ */ async (y) => y,]);
",
);
test_comment_helper(
Expand All @@ -245,9 +240,9 @@ fn annotate_comment() {
/* #__NO_SIDE_EFFECTS__ */ async () => {},
/* #__NO_SIDE_EFFECTS__ */ async (y) => (y),
])",
r"x([/* #__NO_SIDE_EFFECTS__ */ y => y, /* #__NO_SIDE_EFFECTS__ */ () => {
}, /* #__NO_SIDE_EFFECTS__ */ y => y, /* #__NO_SIDE_EFFECTS__ */ async y => y, /* #__NO_SIDE_EFFECTS__ */ async() => {
}, /* #__NO_SIDE_EFFECTS__ */ async y => y,]);
r"x([/* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ () => {
}, /* #__NO_SIDE_EFFECTS__ */ (y) => y, /* #__NO_SIDE_EFFECTS__ */ async (y) => y, /* #__NO_SIDE_EFFECTS__ */ async () => {
}, /* #__NO_SIDE_EFFECTS__ */ async (y) => y,]);
",
);
//
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_minifier/tests/esbuild/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ fn r#for() {
test("for (x(a in b);;);", "for(x(a in b);;);");
test("for (x[a in b];;);", "for(x[a in b];;);");
test("for (x?.[a in b];;);", "for(x?.[a in b];;);");
test("for ((x => a in b);;);", "for(x=>(a in b);;);");
test("for ((x => a in b);;);", "for((x)=>(a in b);;);");

// Make sure for-of loops with commas are wrapped in parentheses
test("for (let a in b, c);", "for(let a in b,c);");
Expand Down Expand Up @@ -303,8 +303,8 @@ fn generator() {
#[test]
fn arrow() {
test("() => {}", "()=>{};");
test("x => (x, 0)", "x=>(x,0);");
test("x => {y}", "x=>{y};");
test("x => (x, 0)", "(x)=>(x,0);");
test("x => {y}", "(x)=>{y};");
test("(a = (b, c), ...d) => {}", "(a=(b,c),...d)=>{};");
test("({[1 + 2]: a = 3} = {[1 + 2]: 3}) => {}", "({[3]:a=3}={[3]:3})=>{};");
test(
Expand Down Expand Up @@ -515,7 +515,7 @@ fn minify() {
test("1.2", "1.2;");

test("() => {}", "()=>{};");
test("(a) => {}", "a=>{};");
test("(a) => {}", "(a)=>{};");
test("(...a) => {}", "(...a)=>{};");
test("(a = 0) => {}", "(a=0)=>{};");
test("(a, b) => {}", "(a,b)=>{};");
Expand Down
22 changes: 11 additions & 11 deletions crates/oxc_minifier/tests/oxc/precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn assignment() {
test("({a,b} = (1, 2))", "({a,b}=(1,2));");
test("a *= yield b", "a*=yield b;");
test("a /= () => {}", "a/=()=>{};");
test("a %= async () => {}", "a%=async()=>{};");
test("a %= async () => {}", "a%=async ()=>{};");
test("a -= (1, 2)", "a-=(1,2);");
test("a >>= b >>= c", "a>>=b>>=c;");
}
Expand All @@ -27,12 +27,12 @@ fn r#yield() {
test("function *foo() { yield * a ? b : c }", "function*foo(){yield*a?b:c}");
test("function *foo() { yield * yield * a }", "function*foo(){yield*yield*a}");
test("function *foo() { yield * () => {} }", "function*foo(){yield*()=>{}}");
test("function *foo() { yield * async () => {} }", "function*foo(){yield*async()=>{}}");
test("function *foo() { yield * async () => {} }", "function*foo(){yield*async ()=>{}}");

test("function *foo() { yield a ? b : c }", "function*foo(){yield a?b:c}");
test("function *foo() { yield yield a }", "function*foo(){yield yield a}");
test("function *foo() { yield () => {} }", "function*foo(){yield ()=>{}}");
test("function *foo() { yield async () => {} }", "function*foo(){yield async()=>{}}");
test("function *foo() { yield async () => {} }", "function*foo(){yield async ()=>{}}");

test(
"function *foo() { yield { a } = [ b ] = c ? b : d }",
Expand All @@ -45,14 +45,14 @@ fn r#yield() {

#[test]
fn arrow() {
test("x => a, b", "x=>a,b;");
test("x => (a, b)", "x=>(a,b);");
test("x => (a => b)", "x=>a=>b;");
test("x => y => a, b", "x=>y=>a,b;");
test("x => y => (a = b)", "x=>y=>a=b;");
test("x => y => z => a = b, c", "x=>y=>z=>a=b,c;");
test("x => y => z => a = (b, c)", "x=>y=>z=>a=(b,c);");
test("x => ({} + 0)", "x=>({})+0;");
test("x => a, b", "(x)=>a,b;");
test("x => (a, b)", "(x)=>(a,b);");
test("x => (a => b)", "(x)=>(a)=>b;");
test("x => y => a, b", "(x)=>(y)=>a,b;");
test("x => y => (a = b)", "(x)=>(y)=>a=b;");
test("x => y => z => a = b, c", "(x)=>(y)=>(z)=>a=b,c;");
test("x => y => z => a = (b, c)", "(x)=>(y)=>(z)=>a=(b,c);");
test("x => ({} + 0)", "(x)=>({})+0;");
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer_dts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl<'a> TransformerDts<'a> {
&source_path.file_name().map(|n| n.to_string_lossy()).unwrap_or_default(),
source_text,
trivias,
CodegenOptions::default().with_typescript(true),
CodegenOptions::default(),
);

let ctx = Rc::new(TransformDtsCtx::new(allocator));
Expand Down
7 changes: 2 additions & 5 deletions crates/oxc_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Oxc {
run_options: &OxcRunOptions,
parser_options: &OxcParserOptions,
_linter_options: &OxcLinterOptions,
codegen_options: &OxcCodegenOptions,
_codegen_options: &OxcCodegenOptions,
minifier_options: &OxcMinifierOptions,
) -> Result<(), serde_wasm_bindgen::Error> {
self.diagnostics = RefCell::default();
Expand Down Expand Up @@ -274,10 +274,7 @@ impl Oxc {
Minifier::new(options).build(&allocator, program);
}

let codegen_options = CodegenOptions {
enable_typescript: codegen_options.enable_typescript,
..CodegenOptions::default()
};
let codegen_options = CodegenOptions::default();
self.codegen_text = if minifier_options.whitespace() {
Codegen::<true>::new("", source_text, ret.trivias, codegen_options)
.build(program)
Expand Down
11 changes: 6 additions & 5 deletions tasks/coverage/codegen_sourcemap.snap
Original file line number Diff line number Diff line change
Expand Up @@ -374,13 +374,14 @@ Unexpected token
(53:16-54:0) "};" --> (106:0-107:0) "\n};"
(54:0-54:4) "\nvar" --> (107:0-107:4) "\nvar"
(54:4-54:8) " z =" --> (107:4-107:8) " z ="
(54:8-54:13) " x =>" --> (107:8-107:13) " x =>"
(54:13-54:15) " {" --> (107:13-108:0) " {"
(54:8-54:13) " x =>" --> (107:8-107:15) " (x) =>"
(54:13-54:15) " {" --> (107:15-108:0) " {"
(54:15-55:0) "};" --> (108:0-109:0) "\n};"
(55:0-55:4) "\nvar" --> (109:0-109:4) "\nvar"
(55:4-55:9) " z = " --> (109:4-109:8) " z ="
(55:9-55:15) "(x) =>" --> (109:8-109:13) " x =>"
(55:15-55:17) " {" --> (109:13-110:0) " {"
(55:4-55:8) " z =" --> (109:4-109:8) " z ="
(55:8-55:9) " " --> (109:8-109:9) " "
(55:9-55:15) "(x) =>" --> (109:9-109:15) "(x) =>"
(55:15-55:17) " {" --> (109:15-110:0) " {"
(55:17-56:0) "};" --> (110:0-111:0) "\n};"
(56:0-56:4) "\nvar" --> (111:0-111:4) "\nvar"
(56:4-56:8) " z =" --> (111:4-111:8) " z ="
Expand Down
4 changes: 2 additions & 2 deletions tasks/coverage/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn get_normal_result(
source_text: &str,
source_type: SourceType,
) -> bool {
let options = CodegenOptions::default().with_typescript(source_type.is_typescript());
let options = CodegenOptions::default();
let allocator = Allocator::default();
let parse_result1 = Parser::new(&allocator, source_text, source_type).parse();
let source_text1 = Codegen::<false>::new("", source_text, parse_result1.trivias, options)
Expand Down Expand Up @@ -108,7 +108,7 @@ fn get_minify_result(
source_text: &str,
source_type: SourceType,
) -> bool {
let options = CodegenOptions::default().with_typescript(source_type.is_typescript());
let options = CodegenOptions::default();
let allocator = Allocator::default();
let parse_result1 = Parser::new(&allocator, source_text, source_type).parse();
let source_text1 =
Expand Down
2 changes: 1 addition & 1 deletion tasks/coverage/src/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn get_result(
&filename,
source_text,
parse_result1.trivias.clone(),
CodegenOptions::default().with_typescript(true),
CodegenOptions::default(),
)
.build(&program)
.source_text;
Expand Down
15 changes: 5 additions & 10 deletions tasks/transform_conformance/src/test_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,9 @@ pub trait TestCase {
.build(&mut program);

result.map(|()| {
Codegen::<false>::new(
"",
&source_text,
ret.trivias,
CodegenOptions::default().with_typescript(true),
)
.build(&program)
.source_text
Codegen::<false>::new("", &source_text, ret.trivias, CodegenOptions::default())
.build(&program)
.source_text
})
}
}
Expand Down Expand Up @@ -256,7 +251,7 @@ impl TestCase for ConformanceTestCase {
println!("output_path: {output_path:?}");
}

let codegen_options = CodegenOptions::default().with_typescript(true);
let codegen_options = CodegenOptions::default();
let mut transformed_code = String::new();
let mut actual_errors = String::new();

Expand Down Expand Up @@ -391,7 +386,7 @@ impl ExecTestCase {
"",
&source_text,
transformed_ret.trivias,
CodegenOptions::default().with_typescript(true),
CodegenOptions::default(),
)
.build(&transformed_ret.program)
.source_text;
Expand Down

0 comments on commit 534242a

Please sign in to comment.