diff --git a/crates/oxc_codegen/tests/integration/main.rs b/crates/oxc_codegen/tests/integration/main.rs index 86cc7907378fe..14d4cbfd5a48f 100644 --- a/crates/oxc_codegen/tests/integration/main.rs +++ b/crates/oxc_codegen/tests/integration/main.rs @@ -4,3 +4,36 @@ pub mod pure_comments; pub mod tester; pub mod ts; pub mod unit; + +use oxc_allocator::Allocator; +use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions}; +use oxc_parser::Parser; +use oxc_span::SourceType; + +pub fn codegen(source_text: &str) -> String { + let allocator = Allocator::default(); + let source_type = SourceType::ts(); + let ret = Parser::new(&allocator, source_text, source_type).parse(); + CodeGenerator::new() + .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) + .enable_comment( + source_text, + ret.trivias, + CommentOptions { preserve_annotate_comments: true }, + ) + .build(&ret.program) + .source_text +} + +pub fn snapshot(name: &str, cases: &[&str]) { + use std::fmt::Write; + + let snapshot = cases.iter().enumerate().fold(String::new(), |mut w, (i, case)| { + write!(w, "########## {i}\n{case}\n----------\n{}\n", codegen(case)).unwrap(); + w + }); + + insta::with_settings!({ prepend_module_to_snapshot => false, omit_expression => true }, { + insta::assert_snapshot!(name, snapshot); + }); +} diff --git a/crates/oxc_codegen/tests/integration/pure_comments.rs b/crates/oxc_codegen/tests/integration/pure_comments.rs index 3c0af87954981..42ecc79b52056 100644 --- a/crates/oxc_codegen/tests/integration/pure_comments.rs +++ b/crates/oxc_codegen/tests/integration/pure_comments.rs @@ -1,62 +1,39 @@ -use crate::tester::test; +use crate::snapshot; #[test] fn annotate_comment() { - test( + let cases = vec![ r" - x([ - /* #__NO_SIDE_EFFECTS__ */ function() {}, - /* #__NO_SIDE_EFFECTS__ */ function y() {}, - /* #__NO_SIDE_EFFECTS__ */ function*() {}, - /* #__NO_SIDE_EFFECTS__ */ function* y() {}, - /* #__NO_SIDE_EFFECTS__ */ async function() {}, - /* #__NO_SIDE_EFFECTS__ */ async function y() {}, - /* #__NO_SIDE_EFFECTS__ */ async function*() {}, - /* #__NO_SIDE_EFFECTS__ */ async function* y() {}, - ])", - r"x([/* #__NO_SIDE_EFFECTS__ */ function() {}, /* #__NO_SIDE_EFFECTS__ */ function y() {}, /* #__NO_SIDE_EFFECTS__ */ function* () {}, /* #__NO_SIDE_EFFECTS__ */ function* y() {}, /* #__NO_SIDE_EFFECTS__ */ async function() {}, /* #__NO_SIDE_EFFECTS__ */ async function y() {}, /* #__NO_SIDE_EFFECTS__ */ async function* () {}, /* #__NO_SIDE_EFFECTS__ */ async function* y() {}]); -", - ); - - test( +x([ + /* #__NO_SIDE_EFFECTS__ */ function() {}, + /* #__NO_SIDE_EFFECTS__ */ function y() {}, + /* #__NO_SIDE_EFFECTS__ */ function*() {}, + /* #__NO_SIDE_EFFECTS__ */ function* y() {}, + /* #__NO_SIDE_EFFECTS__ */ async function() {}, + /* #__NO_SIDE_EFFECTS__ */ async function y() {}, + /* #__NO_SIDE_EFFECTS__ */ async function*() {}, + /* #__NO_SIDE_EFFECTS__ */ async function* 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( +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), - ])", - 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( +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" - // #__NO_SIDE_EFFECTS__ - function a() {} - // #__NO_SIDE_EFFECTS__ - function* b() {} - // #__NO_SIDE_EFFECTS__ - async function c() {} - // #__NO_SIDE_EFFECTS__ - async function* d() {} - ", - r"// #__NO_SIDE_EFFECTS__ +// #__NO_SIDE_EFFECTS__ function a() {} // #__NO_SIDE_EFFECTS__ function* b() {} @@ -64,21 +41,9 @@ function* b() {} async function c() {} // #__NO_SIDE_EFFECTS__ async function* d() {} -", - ); - - test( + ", r" - // #__NO_SIDE_EFFECTS__ - function a() {} - // #__NO_SIDE_EFFECTS__ - function* b() {} - // #__NO_SIDE_EFFECTS__ - async function c() {} - // #__NO_SIDE_EFFECTS__ - async function* d() {} - ", - r"// #__NO_SIDE_EFFECTS__ +// #__NO_SIDE_EFFECTS__ function a() {} // #__NO_SIDE_EFFECTS__ function* b() {} @@ -86,72 +51,45 @@ function* b() {} async function c() {} // #__NO_SIDE_EFFECTS__ async function* d() {} -", - ); - - test( + ", r" - /* @__NO_SIDE_EFFECTS__ */ export function a() {} - /* @__NO_SIDE_EFFECTS__ */ export function* b() {} - /* @__NO_SIDE_EFFECTS__ */ export async function c() {} - /* @__NO_SIDE_EFFECTS__ */ export async function* d() {}", +/* @__NO_SIDE_EFFECTS__ */ export function a() {} +/* @__NO_SIDE_EFFECTS__ */ export function* b() {} +/* @__NO_SIDE_EFFECTS__ */ export async function c() {} +/* @__NO_SIDE_EFFECTS__ */ export async function* d() {}", r"/* @__NO_SIDE_EFFECTS__ */ export function a() {} /* @__NO_SIDE_EFFECTS__ */ export function* b() {} /* @__NO_SIDE_EFFECTS__ */ export async function c() {} /* @__NO_SIDE_EFFECTS__ */ export async function* d() {} -", - ); - // Only "c0" and "c2" should have "no side effects" (Rollup only respects "const" and only for the first one) - test( + ", + // Only "c0" and "c2" should have "no side effects" (Rollup only respects "const" and only for the first one) r" - /* #__NO_SIDE_EFFECTS__ */ export var v0 = function() {}, v1 = function() {} - /* #__NO_SIDE_EFFECTS__ */ export let l0 = function() {}, l1 = function() {} - /* #__NO_SIDE_EFFECTS__ */ export const c0 = function() {}, c1 = function() {} - /* #__NO_SIDE_EFFECTS__ */ export var v2 = () => {}, v3 = () => {} - /* #__NO_SIDE_EFFECTS__ */ export let l2 = () => {}, l3 = () => {} - /* #__NO_SIDE_EFFECTS__ */ export const c2 = () => {}, c3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ export var v0 = function() {}, v1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ export let l0 = function() {}, l1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ export const c0 = function() {}, c1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ export var v2 = () => {}, v3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ export let l2 = () => {}, l3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ export const c2 = () => {}, c3 = () => {} ", - r"export var v0 = function() {}, v1 = function() {}; -export let l0 = function() {}, l1 = function() {}; -export const c0 = /* #__NO_SIDE_EFFECTS__ */ function() {}, c1 = function() {}; -export var v2 = () => {}, v3 = () => {}; -export let l2 = () => {}, l3 = () => {}; -export const c2 = /* #__NO_SIDE_EFFECTS__ */ () => {}, c3 = () => {}; -", - ); - // Only "c0" and "c2" should have "no side effects" (Rollup only respects "const" and only for the first one) - test( + // Only "c0" and "c2" should have "no side effects" (Rollup only respects "const" and only for the first one) r" - /* #__NO_SIDE_EFFECTS__ */ var v0 = function() {}, v1 = function() {} - /* #__NO_SIDE_EFFECTS__ */ let l0 = function() {}, l1 = function() {} - /* #__NO_SIDE_EFFECTS__ */ const c0 = function() {}, c1 = function() {} - /* #__NO_SIDE_EFFECTS__ */ var v2 = () => {}, v3 = () => {} - /* #__NO_SIDE_EFFECTS__ */ let l2 = () => {}, l3 = () => {} - /* #__NO_SIDE_EFFECTS__ */ const c2 = () => {}, c3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ var v0 = function() {}, v1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ let l0 = function() {}, l1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ const c0 = function() {}, c1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ var v2 = () => {}, v3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ let l2 = () => {}, l3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ const c2 = () => {}, c3 = () => {} ", - r"var v0 = function() {}, v1 = function() {}; -let l0 = function() {}, l1 = function() {}; -const c0 = /* #__NO_SIDE_EFFECTS__ */ function() {}, c1 = function() {}; -var v2 = () => {}, v3 = () => {}; -let l2 = () => {}, l3 = () => {}; -const c2 = /* #__NO_SIDE_EFFECTS__ */ () => {}, c3 = () => {}; -", - ); - - test( r" isFunction(options) - ? // #8326: extend call and options.name access are considered side-effects - // by Rollup, so we have to wrap it in a pure-annotated IIFE. - /*#__PURE__*/ (() => - extend({ name: options.name }, extraOptions, { setup: options }))() - : options - ", +? // #8326: extend call and options.name access are considered side-effects + // by Rollup, so we have to wrap it in a pure-annotated IIFE. + /*#__PURE__*/ (() => + extend({ name: options.name }, extraOptions, { setup: options }))() +: options + ", r"isFunction(options) ? /*#__PURE__*/ (() => extend({ name: options.name }, extraOptions, { setup: options }))() : options; -", - ); - - test( + ", r" const obj = { props: /*#__PURE__*/ extend({}, TransitionPropsValidators, { @@ -160,52 +98,36 @@ const obj = { }), }; const p = /*#__PURE__*/ Promise.resolve(); -", - "const obj = { props: /*#__PURE__*/ extend({}, TransitionPropsValidators, {\n\ttag: String,\n\tmoveClass: String\n}) };\nconst p = /*#__PURE__*/ Promise.resolve();\n", - ); - - test( + ", r" const staticCacheMap = /*#__PURE__*/ new WeakMap() -", - "const staticCacheMap = /*#__PURE__*/ new WeakMap();\n", - ); - - test( + ", r#" const builtInSymbols = new Set( /*#__PURE__*/ Object.getOwnPropertyNames(Symbol) .filter(key => key !== "arguments" && key !== "caller") -)"#, - "const builtInSymbols = new Set(/*#__PURE__*/ Object.getOwnPropertyNames(Symbol).filter((key) => key !== \"arguments\" && key !== \"caller\"));\n", - ); - - test("(/* @__PURE__ */ new Foo()).bar();\n", "(/* @__PURE__ */ new Foo()).bar();\n"); - - test("(/* @__PURE__ */ Foo()).bar();\n", "(/* @__PURE__ */ Foo()).bar();\n"); - - // https://github.com/oxc-project/oxc/issues/4843 - test( +) + "#, + "(/* @__PURE__ */ new Foo()).bar();\n", + "(/* @__PURE__ */ Foo()).bar();\n", + // https://github.com/oxc-project/oxc/issues/4843 r" /* #__NO_SIDE_EFFECTS__ */ const defineSSRCustomElement = /* @__NO_SIDE_EFFECTS__ */ ( - options, - extraOptions, + options, + extraOptions, ) => { - return /* @__PURE__ */ defineCustomElement(options, extraOptions, hydrate); + return /* @__PURE__ */ defineCustomElement(options, extraOptions, hydrate); }; -", - "const defineSSRCustomElement = /* #__NO_SIDE_EFFECTS__ */ (options, extraOptions) => {\n\treturn /* @__PURE__ */ defineCustomElement(options, extraOptions, hydrate);\n};\n", - ); - - // Range leading comments - test( + ", + // Range leading comments r" const defineSSRCustomElement = () => { - return /* @__PURE__ */ /* @__NO_SIDE_EFFECTS__ */ /* #__NO_SIDE_EFFECTS__ */ defineCustomElement(options, extraOptions, hydrate); + return /* @__PURE__ */ /* @__NO_SIDE_EFFECTS__ */ /* #__NO_SIDE_EFFECTS__ */ defineCustomElement(options, extraOptions, hydrate); }; -", - "const defineSSRCustomElement = () => {\n\treturn /* @__PURE__ */ /* @__NO_SIDE_EFFECTS__ */ defineCustomElement(options, extraOptions, hydrate);\n};\n", - ); + ", + ]; + + snapshot("pure_comments", &cases); } diff --git a/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap b/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap new file mode 100644 index 0000000000000..0047e85acf0e1 --- /dev/null +++ b/crates/oxc_codegen/tests/integration/snapshots/pure_comments.snap @@ -0,0 +1,234 @@ +--- +source: crates/oxc_codegen/tests/integration/main.rs +--- +########## 0 + +x([ + /* #__NO_SIDE_EFFECTS__ */ function() {}, + /* #__NO_SIDE_EFFECTS__ */ function y() {}, + /* #__NO_SIDE_EFFECTS__ */ function*() {}, + /* #__NO_SIDE_EFFECTS__ */ function* y() {}, + /* #__NO_SIDE_EFFECTS__ */ async function() {}, + /* #__NO_SIDE_EFFECTS__ */ async function y() {}, + /* #__NO_SIDE_EFFECTS__ */ async function*() {}, + /* #__NO_SIDE_EFFECTS__ */ async function* y() {}, +]) +---------- +x([/* #__NO_SIDE_EFFECTS__ */ function() {}, /* #__NO_SIDE_EFFECTS__ */ function y() {}, /* #__NO_SIDE_EFFECTS__ */ function* () {}, /* #__NO_SIDE_EFFECTS__ */ function* y() {}, /* #__NO_SIDE_EFFECTS__ */ async function() {}, /* #__NO_SIDE_EFFECTS__ */ async function y() {}, /* #__NO_SIDE_EFFECTS__ */ async function* () {}, /* #__NO_SIDE_EFFECTS__ */ async function* y() {}]); + +########## 1 + +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), +]) +---------- +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]); + +########## 2 + +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), +]) +---------- +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]); + +########## 3 + +// #__NO_SIDE_EFFECTS__ +function a() {} +// #__NO_SIDE_EFFECTS__ +function* b() {} +// #__NO_SIDE_EFFECTS__ +async function c() {} +// #__NO_SIDE_EFFECTS__ +async function* d() {} + +---------- +// #__NO_SIDE_EFFECTS__ +function a() {} +// #__NO_SIDE_EFFECTS__ +function* b() {} +// #__NO_SIDE_EFFECTS__ +async function c() {} +// #__NO_SIDE_EFFECTS__ +async function* d() {} + +########## 4 + +// #__NO_SIDE_EFFECTS__ +function a() {} +// #__NO_SIDE_EFFECTS__ +function* b() {} +// #__NO_SIDE_EFFECTS__ +async function c() {} +// #__NO_SIDE_EFFECTS__ +async function* d() {} + +---------- +// #__NO_SIDE_EFFECTS__ +function a() {} +// #__NO_SIDE_EFFECTS__ +function* b() {} +// #__NO_SIDE_EFFECTS__ +async function c() {} +// #__NO_SIDE_EFFECTS__ +async function* d() {} + +########## 5 + +/* @__NO_SIDE_EFFECTS__ */ export function a() {} +/* @__NO_SIDE_EFFECTS__ */ export function* b() {} +/* @__NO_SIDE_EFFECTS__ */ export async function c() {} +/* @__NO_SIDE_EFFECTS__ */ export async function* d() {} +---------- +/* @__NO_SIDE_EFFECTS__ */ export function a() {} +/* @__NO_SIDE_EFFECTS__ */ export function* b() {} +/* @__NO_SIDE_EFFECTS__ */ export async function c() {} +/* @__NO_SIDE_EFFECTS__ */ export async function* d() {} + +########## 6 +/* @__NO_SIDE_EFFECTS__ */ export function a() {} +/* @__NO_SIDE_EFFECTS__ */ export function* b() {} +/* @__NO_SIDE_EFFECTS__ */ export async function c() {} +/* @__NO_SIDE_EFFECTS__ */ export async function* d() {} + +---------- +/* @__NO_SIDE_EFFECTS__ */ export function a() {} +/* @__NO_SIDE_EFFECTS__ */ export function* b() {} +/* @__NO_SIDE_EFFECTS__ */ export async function c() {} +/* @__NO_SIDE_EFFECTS__ */ export async function* d() {} + +########## 7 + +/* #__NO_SIDE_EFFECTS__ */ export var v0 = function() {}, v1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ export let l0 = function() {}, l1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ export const c0 = function() {}, c1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ export var v2 = () => {}, v3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ export let l2 = () => {}, l3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ export const c2 = () => {}, c3 = () => {} + +---------- +export var v0 = function() {}, v1 = function() {}; +export let l0 = function() {}, l1 = function() {}; +export const c0 = /* #__NO_SIDE_EFFECTS__ */ function() {}, c1 = function() {}; +export var v2 = () => {}, v3 = () => {}; +export let l2 = () => {}, l3 = () => {}; +export const c2 = /* #__NO_SIDE_EFFECTS__ */ () => {}, c3 = () => {}; + +########## 8 + +/* #__NO_SIDE_EFFECTS__ */ var v0 = function() {}, v1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ let l0 = function() {}, l1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ const c0 = function() {}, c1 = function() {} +/* #__NO_SIDE_EFFECTS__ */ var v2 = () => {}, v3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ let l2 = () => {}, l3 = () => {} +/* #__NO_SIDE_EFFECTS__ */ const c2 = () => {}, c3 = () => {} + +---------- +var v0 = function() {}, v1 = function() {}; +let l0 = function() {}, l1 = function() {}; +const c0 = /* #__NO_SIDE_EFFECTS__ */ function() {}, c1 = function() {}; +var v2 = () => {}, v3 = () => {}; +let l2 = () => {}, l3 = () => {}; +const c2 = /* #__NO_SIDE_EFFECTS__ */ () => {}, c3 = () => {}; + +########## 9 + +isFunction(options) +? // #8326: extend call and options.name access are considered side-effects + // by Rollup, so we have to wrap it in a pure-annotated IIFE. + /*#__PURE__*/ (() => + extend({ name: options.name }, extraOptions, { setup: options }))() +: options + +---------- +isFunction(options) ? /*#__PURE__*/ (() => extend({ name: options.name }, extraOptions, { setup: options }))() : options; + +########## 10 +isFunction(options) ? /*#__PURE__*/ (() => extend({ name: options.name }, extraOptions, { setup: options }))() : options; + +---------- +isFunction(options) ? /*#__PURE__*/ (() => extend({ name: options.name }, extraOptions, { setup: options }))() : options; + +########## 11 + +const obj = { + props: /*#__PURE__*/ extend({}, TransitionPropsValidators, { + tag: String, + moveClass: String, + }), +}; +const p = /*#__PURE__*/ Promise.resolve(); + +---------- +const obj = { props: /*#__PURE__*/ extend({}, TransitionPropsValidators, { + tag: String, + moveClass: String +}) }; +const p = /*#__PURE__*/ Promise.resolve(); + +########## 12 + +const staticCacheMap = /*#__PURE__*/ new WeakMap() + +---------- +const staticCacheMap = /*#__PURE__*/ new WeakMap(); + +########## 13 + +const builtInSymbols = new Set( + /*#__PURE__*/ + Object.getOwnPropertyNames(Symbol) + .filter(key => key !== "arguments" && key !== "caller") +) + +---------- +const builtInSymbols = new Set(/*#__PURE__*/ Object.getOwnPropertyNames(Symbol).filter((key) => key !== 'arguments' && key !== 'caller')); + +########## 14 +(/* @__PURE__ */ new Foo()).bar(); + +---------- +(/* @__PURE__ */ new Foo()).bar(); + +########## 15 +(/* @__PURE__ */ Foo()).bar(); + +---------- +(/* @__PURE__ */ Foo()).bar(); + +########## 16 + +/* #__NO_SIDE_EFFECTS__ */ +const defineSSRCustomElement = /* @__NO_SIDE_EFFECTS__ */ ( + options, + extraOptions, +) => { + return /* @__PURE__ */ defineCustomElement(options, extraOptions, hydrate); +}; + +---------- +const defineSSRCustomElement = /* #__NO_SIDE_EFFECTS__ */ (options, extraOptions) => { + return /* @__PURE__ */ defineCustomElement(options, extraOptions, hydrate); +}; + +########## 17 + +const defineSSRCustomElement = () => { + return /* @__PURE__ */ /* @__NO_SIDE_EFFECTS__ */ /* #__NO_SIDE_EFFECTS__ */ defineCustomElement(options, extraOptions, hydrate); +}; + +---------- +const defineSSRCustomElement = () => { + return /* @__PURE__ */ /* @__NO_SIDE_EFFECTS__ */ defineCustomElement(options, extraOptions, hydrate); +}; diff --git a/crates/oxc_codegen/tests/integration/snapshots/ts.snap b/crates/oxc_codegen/tests/integration/snapshots/ts.snap index dd3646a463123..7333861d1a2d5 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/ts.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/ts.snap @@ -1,114 +1,174 @@ --- -source: crates/oxc_codegen/tests/integration/ts.rs +source: crates/oxc_codegen/tests/integration/main.rs --- +########## 0 let x: string = `\x01`; +---------- let x: string = `\x01`; +########## 1 function foo(x: T, y: string, ...restOfParams: Omit): T { return x; } +---------- function foo(x: T, y: string, ...restOfParams: Omit): T { return x; } +########## 2 let x: string[] = ['abc', 'def', 'ghi']; +---------- let x: string[] = ['abc', 'def', 'ghi']; +########## 3 let x: Array = ['abc', 'def', 'ghi',]; +---------- let x: Array = ['abc', 'def', 'ghi']; +########## 4 let x: [string, number] = ['abc', 123]; +---------- let x: [string, number] = ['abc', 123]; +########## 5 let x: string | number = 'abc'; +---------- let x: string | number = 'abc'; +########## 6 let x: string & number = 'abc'; +---------- let x: string & number = 'abc'; +########## 7 let x: typeof String = 'string'; +---------- let x: typeof String = 'string'; +########## 8 let x: keyof string = 'length'; +---------- let x: keyof string = 'length'; +########## 9 let x: keyof typeof String = 'length'; +---------- let x: keyof typeof String = 'length'; +########## 10 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'); } } +########## 12 import type { Foo } from 'foo'; +---------- import type { Foo } from 'foo'; +########## 13 import { Foo, type Bar } from 'foo'; +---------- import { Foo, type Bar } from 'foo'; +########## 14 export { Foo, type Bar } from 'foo'; +---------- export { Foo, type Bar } from 'foo'; +########## 15 type A = { [K in keyof T as K extends string ? B : K ]: T[K] } +---------- type A = { [K in keyof T as K extends string ? B : K] : T[K]}; +########## 16 class A {readonly type = 'frame'} +---------- class A { readonly type = 'frame'; } +########## 17 let foo: { (t: T): void } +---------- let foo: {(t: T): void}; +########## 18 let foo: { new (t: T): void } +---------- let foo: {new (t: T): void}; +########## 19 function (){} +---------- function() {} +########## 20 class A {m?(): void} +---------- class A { m?(): void; } +########## 21 class A {constructor(public readonly a: number) {}} +---------- class A { constructor(public readonly a: number) {} } +########## 22 abstract class A {private abstract static m() {}} +---------- abstract class A { private abstract static m() {} } +########## 23 abstract class A {private abstract static readonly prop: string} +---------- abstract class A { private abstract static readonly prop: string; } +########## 24 a = x!; +---------- a = x!; +########## 25 b = (x as y); +---------- b = ((x) as y); +########## 26 c = foo; +---------- c = foo; +########## 27 d = x satisfies y; +---------- d = ((x) satisfies y); +########## 28 export @x declare abstract class C {} +---------- export @x declare abstract class C {} +########## 29 div`` +---------- div``; diff --git a/crates/oxc_codegen/tests/integration/ts.rs b/crates/oxc_codegen/tests/integration/ts.rs index 7de69c2551727..148575424bdaf 100644 --- a/crates/oxc_codegen/tests/integration/ts.rs +++ b/crates/oxc_codegen/tests/integration/ts.rs @@ -1,19 +1,4 @@ -use std::fmt::Write; - -use oxc_allocator::Allocator; -use oxc_codegen::{CodeGenerator, CodegenOptions}; -use oxc_parser::Parser; -use oxc_span::SourceType; - -fn codegen(source_text: &str) -> String { - let allocator = Allocator::default(); - let source_type = SourceType::ts(); - let ret = Parser::new(&allocator, source_text, source_type).parse(); - CodeGenerator::new() - .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) - .build(&ret.program) - .source_text -} +use crate::snapshot; #[test] fn ts() { @@ -54,12 +39,5 @@ fn ts() { "div``", ]; - let snapshot = cases.into_iter().fold(String::new(), |mut w, case| { - write!(w, "{case}\n{}\n", codegen(case)).unwrap(); - w - }); - - insta::with_settings!({ prepend_module_to_snapshot => false, omit_expression => true }, { - insta::assert_snapshot!("ts", snapshot); - }); + snapshot("ts", &cases); }