Skip to content

Commit

Permalink
lintfix
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed Oct 22, 2021
1 parent 7c5c1a0 commit 96ef4ce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
25 changes: 13 additions & 12 deletions src/test/transpilers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,34 @@
// Should consolidate them here.

import { context } from './testlib';
import {
contextTsNodeUnderTest,
testsDirRequire,
} from './helpers';
import { contextTsNodeUnderTest, testsDirRequire } from './helpers';
import * as expect from 'expect';

const test = context(contextTsNodeUnderTest);

test.suite('swc', (test) => {
test('verify that TS->SWC target mappings suppport all possible values from both TS and SWC', async (t) => {
const swcTranspiler = testsDirRequire('ts-node/transpilers/swc-experimental') as typeof import('../transpilers/swc');
const swcTranspiler = testsDirRequire(
'ts-node/transpilers/swc-experimental'
) as typeof import('../transpilers/swc');

// Detect when mapping is missing any ts.ScriptTargets
const ts = testsDirRequire('typescript') as typeof import('typescript');
for(const key of Object.keys(ts.ScriptTarget)) {
if(/^\d+$/.test(key)) continue;
if(key === 'JSON') continue;
expect(swcTranspiler.targetMapping.has(ts.ScriptTarget[key as any] as any)).toBe(true);
for (const key of Object.keys(ts.ScriptTarget)) {
if (/^\d+$/.test(key)) continue;
if (key === 'JSON') continue;
expect(
swcTranspiler.targetMapping.has(ts.ScriptTarget[key as any] as any)
).toBe(true);
}

// Detect when mapping is missing any swc targets
// Assuming that tests/package.json declares @swc/core: latest
const swc = testsDirRequire('@swc/core');
let msg: string | undefined = undefined;
try {
swc.transformSync('', {jsc: {target: 'invalid'}});
} catch(e) {
swc.transformSync('', { jsc: { target: 'invalid' } });
} catch (e) {
msg = (e as Error).message;
}
expect(msg).toBeDefined();
Expand All @@ -39,7 +40,7 @@ test.suite('swc', (test) => {
expect(match).toBeDefined();
const targets = match![1].split(', ').map((v: string) => v.slice(1, -1));

for(const target of targets) {
for (const target of targets) {
expect([...swcTranspiler.targetMapping.values()]).toContain(target);
}
});
Expand Down
20 changes: 16 additions & 4 deletions src/transpilers/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
// Perhaps project has an older version of swc.
// TODO cache the results of this; slightly faster
let swcTargetIndex = swcTargets.indexOf(swcTarget);
for(; swcTargetIndex >= 0; swcTargetIndex--) {
for (; swcTargetIndex >= 0; swcTargetIndex--) {
try {
swcInstance.transformSync('', {jsc: {target: swcTargets[swcTargetIndex]}});
swcInstance.transformSync('', {
jsc: { target: swcTargets[swcTargetIndex] },
});
break;
} catch(e) {}
} catch (e) {}
}
swcTarget = swcTargets[swcTargetIndex];
const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3;
Expand Down Expand Up @@ -148,7 +150,17 @@ type SwcTarget = typeof swcTargets[number];
* @internal
* We use this list to downgrade to a prior target when we probe swc to detect if it supports a particular target
*/
const swcTargets = ['es3', 'es5', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es2021'] as const;
const swcTargets = [
'es3',
'es5',
'es2015',
'es2016',
'es2017',
'es2018',
'es2019',
'es2020',
'es2021',
] as const;

const ModuleKind = {
None: 0,
Expand Down

0 comments on commit 96ef4ce

Please sign in to comment.