fix(es/config): Respect .swcrc (#4735)

This commit is contained in:
Donny/강동윤 2022-05-21 23:18:43 +09:00 committed by GitHub
parent 286e654c95
commit 9966e98ac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 72 additions and 19 deletions

View File

@ -986,17 +986,6 @@ impl Config {
///
/// - typescript: `tsx` will be modified if file extension is `ts`.
pub fn adjust(&mut self, file: &Path) {
if self.jsc.syntax.is_none() || matches!(self.jsc.syntax, Some(Syntax::Es(..))) {
if file.extension() == Some("ts".as_ref()) {
self.jsc.syntax = Some(Syntax::Typescript(TsConfig::default()));
} else if file.extension() == Some("tsx".as_ref()) {
self.jsc.syntax = Some(Syntax::Typescript(TsConfig {
tsx: true,
..Default::default()
}));
}
}
if let Some(Syntax::Typescript(TsConfig { tsx, dts, .. })) = &mut self.jsc.syntax {
let is_dts = file
.file_name()

View File

@ -1,4 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _config = require("../config");

View File

@ -1 +1 @@
export { };
import { module } from "../config";

View File

@ -1,2 +1,4 @@
import something from "../module_with_default_export.js"; /* This!? */
import { something_else } from "http://bonzo.com/mod"; // There?
/* Hello! */ export * from "./stuff.js";
/* Maybe like this? */ let _var; // Or so?

View File

@ -1,9 +1,10 @@
import * as swcHelpers from "@swc/helpers";
export class Foo {
nested() {
let Foo1 = class Foo {
};
Foo1.foo = "foo";
Foo1.bar = Foo1.foo;
swcHelpers.defineProperty(Foo1, "foo", "foo");
swcHelpers.defineProperty(Foo1, "bar", Foo1.foo);
return new Foo1();
}
}

View File

@ -5,7 +5,7 @@ import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
it("should auto-detect tsx", async () => {
it("should allow using tsx", async () => {
const filename = join(
__dirname,
"..",
@ -21,6 +21,7 @@ it("should auto-detect tsx", async () => {
target: "es5",
parser: {
syntax: "typescript",
tsx: true
},
},
});
@ -35,7 +36,7 @@ export default function foo() {
`);
});
it("should auto-detect tsx even with no parser option", async () => {
it("should respect .swcrc without parser option", async () => {
const filename = join(
__dirname,
"..",

View File

@ -0,0 +1,38 @@
import swc from "../../..";
import { dirname, join } from "path";
import { platform } from "os";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
it("should transpile decorators", async () => {
const filename = join(
__dirname,
"..",
"..",
"tests",
"issue-4734",
"1",
"index.ts"
);
console.log(filename);
const { code } = await swc.transformFile(filename, {});
expect(code).toMatchInlineSnapshot(`
"var __decorate = this && this.__decorate || function(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === \\"object\\" && typeof Reflect.decorate === \\"function\\") r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
class TestClass {
abc() {}
}
__decorate([
foo
], TestClass.prototype, \\"abc\\", null);
function foo(target, propertyKey) {
console.log(\`Decorating \${target}.\${String(propertyKey)}\`);
}
"
`);
});

View File

@ -1,6 +1,10 @@
{
"jsc": {
"target": "es2017",
"parser": {
"syntax": "typescript",
"tsx": true
},
"transform": {
"react": {
"runtime": "automatic"

View File

@ -0,0 +1,12 @@
{
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"transform": {
"legacyDecorator": true
},
"target": "es2020"
}
}

View File

@ -0,0 +1,8 @@
class TestClass {
@foo
abc() { }
}
function foo(target: unknown, propertyKey: string | symbol) {
console.log(`Decorating ${target}.${String(propertyKey)}`)
}