Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: should respect legacy decorators
Browse files Browse the repository at this point in the history
nonzzz committed Oct 7, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent ef2d387 commit 506b14b
Showing 7 changed files with 1,424 additions and 1,234 deletions.
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ import {
} from '@swc/core';
import createDeepMerge from '@fastify/deepmerge';

import { getOptions, getEnableExperimentalDecorators } from './options';
import { getOptions, checkIsLegacyTypeScript } from './options';

import type { Plugin as VitePlugin } from 'vite';

@@ -67,7 +67,7 @@ function swc(options: PluginOptions = {}): RollupPlugin {
return null;
};

const enableExperimentalDecorators = getEnableExperimentalDecorators();
const isLegacyTypeScript = checkIsLegacyTypeScript();

return {
name: 'swc',
@@ -127,7 +127,7 @@ function swc(options: PluginOptions = {}): RollupPlugin {
parser: {
syntax: isTypeScript ? 'typescript' : 'ecmascript',
[isTypeScript ? 'tsx' : 'jsx']: isTypeScript ? isTsx : isJsx,
decorators: enableExperimentalDecorators || tsconfigOptions.experimentalDecorators
decorators: !isLegacyTypeScript || tsconfigOptions.experimentalDecorators
},
transform: {
decoratorMetadata: tsconfigOptions.emitDecoratorMetadata,
@@ -140,7 +140,7 @@ function swc(options: PluginOptions = {}): RollupPlugin {
pragmaFrag: tsconfigOptions.jsxFragmentFactory,
development: tsconfigOptions.jsx === 'react-jsxdev' ? true : undefined
},
decoratorVersion: enableExperimentalDecorators ? '2022-03' : '2021-12'
decoratorVersion: isLegacyTypeScript ? '2021-12' : (tsconfigOptions.experimentalDecorators ? '2021-12' : '2022-03')
},
target: tsconfigOptions.target?.toLowerCase() as JscTarget | undefined,
baseUrl: tsconfigOptions.baseUrl,
4 changes: 2 additions & 2 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -59,14 +59,14 @@ export const getOptions = (
return compilerOptions;
};

export const getEnableExperimentalDecorators = () => {
export const checkIsLegacyTypeScript = () => {
try {
// @ts-expect-error -- It's required to using 'import.mtea.url' but i don't want to change the tsconfig.
const tsPath = resolve('typescript/package.json', import.meta.url);
const { version } = JSON.parse(fs.readFileSync(fileURLToPath(tsPath), 'utf-8'));
const [major] = version.split('.');
// Only check experimental decorators for TypeScript 5+
return +major >= 5;
return +major < 5;
} catch {
return false;
}
2,570 changes: 1,352 additions & 1,218 deletions test/__snapshots__/index.ts.snap

Large diffs are not rendered by default.

32 changes: 23 additions & 9 deletions test/fixtures/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
const printMemberName = (target: any, memberName: string) => {
console.log(memberName);
};

class Person {
// @ts-expect-error
@printMemberName
name: string = "Jon";
}
function trace(value, {kind, name}) {
if (kind === 'method') {
return function (...args) {
console.log('trace')
return value.apply(this, args);
};
}
}

class People {
xzy: string
constructor(){
this.xzy = 'xzy';
}
@trace
test() {
return this.xzy
}
}

const p = new People();

p.test();
25 changes: 25 additions & 0 deletions test/fixtures/legacy-decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function trace(_target: any, _name: string, descriptor: PropertyDescriptor) {
const original = descriptor.value;
descriptor.value = function () {
console.log('trace');
return original.call(this);
};

return descriptor;
}

class People {
xzy: string;
constructor() {
this.xzy = 'xzy';
}

@trace
test() {
return this.xzy;
}
}

const p = new People();

p.test();
9 changes: 9 additions & 0 deletions test/fixtures/legacy-decorators/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ES2017",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "ES2022",
"baseUrl": "./"
}
}
10 changes: 9 additions & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
@@ -376,14 +376,22 @@ const tests = (rollupImpl: typeof rollup2 | typeof rollup3 | typeof rollup4, iso
))[0].code.should.matchSnapshot();
});

it('detect decorator for typescript5', async () => {
it('detect decorator for typescript5', async () => {
const dir = await fixture('decorators');
(await build(
rollupImpl,
{ tsconfig: false },
{ input: './index.ts', dir }
))[0].code.should.matchSnapshot();
});
it('detect legacy decorator for typescript5', async () => {
const dir = await fixture('legacy-decorators');
(await build(
rollupImpl,
{},
{ input: './index.ts', dir }
))[0].code.should.matchSnapshot();
});
};

describe('rollup-plugin-swc3', () => {

0 comments on commit 506b14b

Please sign in to comment.