Completed
This commit is contained in:
@ -1,34 +1,53 @@
|
||||
import { describe, it } from "mocha";
|
||||
import { expect } from "chai";
|
||||
import { transform } from "@babel/core";
|
||||
import { format } from "prettier";
|
||||
import plugin from "../src/index";
|
||||
import { readFileSync } from "fs"
|
||||
import { describe, it } from "mocha"
|
||||
import { resolve } from "path"
|
||||
import { expect } from "chai"
|
||||
import { transform } from "@babel/core"
|
||||
import { format } from "prettier"
|
||||
import type { Snapshot } from "./snapshots"
|
||||
import plugin from "../src/index"
|
||||
|
||||
const javascript = (code: TemplateStringsArray) =>
|
||||
transform(code[0], {
|
||||
const canonize = (code: string) =>
|
||||
format(code, { parser: "babel" }).trim().split("\n").filter(Boolean).join("\n")
|
||||
|
||||
const equal = (name: Snapshot) => {
|
||||
const folder = resolve(__dirname, "snapshots", name)
|
||||
const actual = readFileSync(resolve(folder, "input.txt"), "utf-8")
|
||||
const expected = readFileSync(resolve(folder, "output.txt"), "utf-8")
|
||||
const transformed = transform(actual, {
|
||||
parserOpts: {
|
||||
plugins: ["decorators-legacy", "typescript"],
|
||||
},
|
||||
babelrc: false,
|
||||
configFile: false,
|
||||
plugins: [plugin],
|
||||
})!.code!;
|
||||
})!.code!
|
||||
expect(canonize(transformed)).to.deep.equal(canonize(expected))
|
||||
}
|
||||
|
||||
const canonize = (code: string) => format(code, { parser: "babel" }).trim();
|
||||
describe("tsc-decorator", () => {
|
||||
it("works with property decorators", () => {
|
||||
equal("properties")
|
||||
})
|
||||
|
||||
const equal = (actual: string, expected: string) => {
|
||||
actual = canonize(actual);
|
||||
expected = canonize(expected);
|
||||
expect(actual).to.deep.equal(expected);
|
||||
};
|
||||
it("works with method decorators", () => {
|
||||
equal("methods")
|
||||
})
|
||||
|
||||
describe("", () => {
|
||||
it("should do nothing", () => {
|
||||
equal(
|
||||
javascript`
|
||||
class A {}
|
||||
`,
|
||||
/* javascript */ `
|
||||
class A {}
|
||||
`
|
||||
);
|
||||
});
|
||||
});
|
||||
it("works with parameter decorators", () => {
|
||||
equal("params")
|
||||
})
|
||||
|
||||
// https://github.com/babel/babel/issues/13591
|
||||
it.skip("works with computed property keys", () => {
|
||||
equal("computedProperties")
|
||||
})
|
||||
|
||||
it("works with class/consrtuctor decorators", () => {
|
||||
equal("constructor")
|
||||
})
|
||||
|
||||
it("does not interfere with export declarations", () => {
|
||||
equal("exports")
|
||||
})
|
||||
})
|
||||
|
1
test/snapshots/.eslintignore
Normal file
1
test/snapshots/.eslintignore
Normal file
@ -0,0 +1 @@
|
||||
*
|
4
test/snapshots/computedProperties/input.txt
Normal file
4
test/snapshots/computedProperties/input.txt
Normal file
@ -0,0 +1,4 @@
|
||||
class A {
|
||||
@decorator
|
||||
[Symbol.iterator]() {}
|
||||
}
|
5
test/snapshots/computedProperties/output.txt
Normal file
5
test/snapshots/computedProperties/output.txt
Normal file
@ -0,0 +1,5 @@
|
||||
let _a;
|
||||
class A {
|
||||
[_a = Symbol.iterator]() { }
|
||||
}
|
||||
__decorate([decorator], A.prototype, _a, null);
|
4
test/snapshots/constructor/input.txt
Normal file
4
test/snapshots/constructor/input.txt
Normal file
@ -0,0 +1,4 @@
|
||||
@classDecorator
|
||||
class A {
|
||||
@prop string: string;
|
||||
}
|
8
test/snapshots/constructor/output.txt
Normal file
8
test/snapshots/constructor/output.txt
Normal file
@ -0,0 +1,8 @@
|
||||
import { __decorate } from "tslib";
|
||||
|
||||
let A = class A {
|
||||
string: string;
|
||||
}
|
||||
|
||||
__decorate([prop], A.prototype, "string", void 0);
|
||||
A = __decorate([classDecorator], A);
|
9
test/snapshots/exports/input.txt
Normal file
9
test/snapshots/exports/input.txt
Normal file
@ -0,0 +1,9 @@
|
||||
export class NormalDecorated {
|
||||
@prop a: string
|
||||
}
|
||||
|
||||
@classDecorator
|
||||
export class ClassDecorated {}
|
||||
|
||||
@classDecorator
|
||||
export default class DefaultExport {}
|
15
test/snapshots/exports/output.txt
Normal file
15
test/snapshots/exports/output.txt
Normal file
@ -0,0 +1,15 @@
|
||||
import { __decorate } from "tslib";
|
||||
|
||||
export class NormalDecorated {
|
||||
a: string;
|
||||
}
|
||||
__decorate([prop], NormalDecorated.prototype, "a", void 0);
|
||||
|
||||
let ClassDecorated = class ClassDecorated {};
|
||||
ClassDecorated = __decorate([classDecorator], ClassDecorated);
|
||||
|
||||
export { ClassDecorated };
|
||||
|
||||
let DefaultExport = class DefaultExport {};
|
||||
DefaultExport = __decorate([classDecorator], DefaultExport);
|
||||
export default DefaultExport;
|
3
test/snapshots/methods/input.txt
Normal file
3
test/snapshots/methods/input.txt
Normal file
@ -0,0 +1,3 @@
|
||||
class A {
|
||||
@dec method(): string {}
|
||||
}
|
7
test/snapshots/methods/output.txt
Normal file
7
test/snapshots/methods/output.txt
Normal file
@ -0,0 +1,7 @@
|
||||
import { __decorate } from "tslib";
|
||||
|
||||
class A {
|
||||
method(): string {}
|
||||
}
|
||||
|
||||
__decorate([dec], A.prototype, "method", null);
|
6
test/snapshots/params/input.txt
Normal file
6
test/snapshots/params/input.txt
Normal file
@ -0,0 +1,6 @@
|
||||
import { __decorate } from "tslib"
|
||||
|
||||
class A {
|
||||
method1(skipped: string, @Arg() b: string) {}
|
||||
method2(@Arg() a: string, @Arg() b: string) {}
|
||||
}
|
10
test/snapshots/params/output.txt
Normal file
10
test/snapshots/params/output.txt
Normal file
@ -0,0 +1,10 @@
|
||||
import { __param, __decorate as _decorate } from "tslib";
|
||||
import { __decorate } from "tslib";
|
||||
|
||||
class A {
|
||||
method1(skipped: string, b: string) {}
|
||||
method2(a: string, b: string) {}
|
||||
}
|
||||
|
||||
_decorate([__param(1, Arg())], A.prototype, "method1", null);
|
||||
_decorate([__param(0, Arg()), __param(1, Arg())], A.prototype, "method2", null);
|
4
test/snapshots/properties/input.txt
Normal file
4
test/snapshots/properties/input.txt
Normal file
@ -0,0 +1,4 @@
|
||||
class A {
|
||||
@dec prop: string
|
||||
@dec @dec2 prop2: string
|
||||
}
|
9
test/snapshots/properties/output.txt
Normal file
9
test/snapshots/properties/output.txt
Normal file
@ -0,0 +1,9 @@
|
||||
import { __decorate } from "tslib"
|
||||
|
||||
class A {
|
||||
prop: string;
|
||||
prop2: string;
|
||||
}
|
||||
|
||||
__decorate([dec], A.prototype, "prop", void 0);
|
||||
__decorate([dec, dec2], A.prototype, "prop2", void 0);
|
Reference in New Issue
Block a user