From 9330c1a1183723638b3c83cff63ec6f18a09294c Mon Sep 17 00:00:00 2001 From: dsyddall Date: Tue, 14 Nov 2023 12:02:29 +0000 Subject: [PATCH] fix(core): respect useDefineForClassFields tsconfig value (#740) --- packages/core/index.ts | 2 ++ .../ts-compiler-options-to-swc-config.spec.ts | 2 ++ packages/register/read-default-tsconfig.ts | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/core/index.ts b/packages/core/index.ts index a7f50b111..c79573ea5 100644 --- a/packages/core/index.ts +++ b/packages/core/index.ts @@ -17,6 +17,7 @@ export interface Options { jsx?: boolean experimentalDecorators?: boolean emitDecoratorMetadata?: boolean + useDefineForClassFields?: boolean dynamicImport?: boolean esModuleInterop?: boolean keepClassNames?: boolean @@ -49,6 +50,7 @@ function transformOption(path: string, options?: Options, jest = false): SwcOpti transform: { legacyDecorator: Boolean(opts.experimentalDecorators), decoratorMetadata: Boolean(opts.emitDecoratorMetadata), + useDefineForClassFields: Boolean(opts.useDefineForClassFields), react: options?.react, // @ts-expect-error hidden: { diff --git a/packages/register/__test__/ts-compiler-options-to-swc-config.spec.ts b/packages/register/__test__/ts-compiler-options-to-swc-config.spec.ts index fea7b875e..f220c87cb 100644 --- a/packages/register/__test__/ts-compiler-options-to-swc-config.spec.ts +++ b/packages/register/__test__/ts-compiler-options-to-swc-config.spec.ts @@ -15,6 +15,7 @@ test('default values', (t) => { sourcemap: false, experimentalDecorators: false, emitDecoratorMetadata: false, + useDefineForClassFields: false, esModuleInterop: false, dynamicImport: true, externalHelpers: false, @@ -99,6 +100,7 @@ test('should set all values', (t) => { target: 'es5', experimentalDecorators: options.experimentalDecorators, emitDecoratorMetadata: options.emitDecoratorMetadata, + useDefineForClassFields: false, esModuleInterop: options.esModuleInterop, externalHelpers: true, dynamicImport: true, diff --git a/packages/register/read-default-tsconfig.ts b/packages/register/read-default-tsconfig.ts index 9f98cf1b8..6e6b56da9 100644 --- a/packages/register/read-default-tsconfig.ts +++ b/packages/register/read-default-tsconfig.ts @@ -97,21 +97,31 @@ function toModule(moduleKind: ts.ModuleKind) { } } +/** + * The default value for useDefineForClassFields depends on the emit target + * @see https://www.typescriptlang.org/tsconfig#useDefineForClassFields + */ +function getUseDefineForClassFields(compilerOptions: ts.CompilerOptions, target: ts.ScriptTarget): boolean { + return compilerOptions.useDefineForClassFields ?? target >= ts.ScriptTarget.ES2022 +} + export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filename: string): Options { const isJsx = filename.endsWith('.tsx') || filename.endsWith('.jsx') || Boolean(options.jsx) + const target = options.target ?? ts.ScriptTarget.ES2018 return { module: toModule(options.module ?? ts.ModuleKind.ES2015), - target: toTsTarget(options.target ?? ts.ScriptTarget.ES2018), + target: toTsTarget(target), jsx: isJsx, sourcemap: options.sourceMap && options.inlineSourceMap ? 'inline' : Boolean(options.sourceMap), experimentalDecorators: options.experimentalDecorators ?? false, emitDecoratorMetadata: options.emitDecoratorMetadata ?? false, + useDefineForClassFields: getUseDefineForClassFields(options, target), esModuleInterop: options.esModuleInterop ?? false, dynamicImport: true, keepClassNames: true, externalHelpers: Boolean(options.importHelpers), react: - options.jsxFactory || options.jsxFragmentFactory || options.jsx || options.jsxImportSource + options.jsxFactory ?? options.jsxFragmentFactory ?? options.jsx ?? options.jsxImportSource ? { pragma: options.jsxFactory, pragmaFrag: options.jsxFragmentFactory,