Skip to content

Commit

Permalink
fix: respect useDefineForClassFields tsconfig value
Browse files Browse the repository at this point in the history
  • Loading branch information
dsyddall committed Nov 12, 2023
1 parent bc910be commit d183146
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface Options {
jsx?: boolean
experimentalDecorators?: boolean
emitDecoratorMetadata?: boolean
useDefineForClassFields?: boolean
dynamicImport?: boolean
esModuleInterop?: boolean
keepClassNames?: boolean
Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test('default values', (t) => {
sourcemap: false,
experimentalDecorators: false,
emitDecoratorMetadata: false,
useDefineForClassFields: false,
esModuleInterop: false,
dynamicImport: true,
externalHelpers: false,
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 20 additions & 1 deletion packages/register/read-default-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ function toModule(moduleKind: ts.ModuleKind) {
}
}

// https://github.com/microsoft/TypeScript/blob/fc418a2e611c88cf9afa0115ff73490b2397d311/src/compiler/utilities.ts#L8556
function getEmitScriptTarget(compilerOptions: {
module?: ts.CompilerOptions['module']
target?: ts.CompilerOptions['target']
}): ts.ScriptTarget {
return (
compilerOptions.target ??
((compilerOptions.module === ts.ModuleKind.Node16 && ts.ScriptTarget.ES2022) ||
(compilerOptions.module === ts.ModuleKind.NodeNext && ts.ScriptTarget.ESNext) ||
ts.ScriptTarget.ES5)
)
}

// https://github.com/microsoft/TypeScript/blob/fc418a2e611c88cf9afa0115ff73490b2397d311/src/compiler/utilities.ts#L8761
function getUseDefineForClassFields(compilerOptions: ts.CompilerOptions): boolean {
return compilerOptions.useDefineForClassFields ?? getEmitScriptTarget(compilerOptions) >= ts.ScriptTarget.ES2022
}

export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filename: string): Options {
const isJsx = filename.endsWith('.tsx') || filename.endsWith('.jsx') || Boolean(options.jsx)
return {
Expand All @@ -106,12 +124,13 @@ export function tsCompilerOptionsToSwcConfig(options: ts.CompilerOptions, filena
sourcemap: options.sourceMap && options.inlineSourceMap ? 'inline' : Boolean(options.sourceMap),
experimentalDecorators: options.experimentalDecorators ?? false,
emitDecoratorMetadata: options.emitDecoratorMetadata ?? false,
useDefineForClassFields: getUseDefineForClassFields(options),
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,
Expand Down

0 comments on commit d183146

Please sign in to comment.