74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import { readFileSync, existsSync } 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 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 babelrc: babel.TransformOptions = existsSync(resolve(folder, ".babelrc"))
|
|
? JSON.parse(readFileSync(resolve(folder, ".babelrc"), "utf-8"))
|
|
: {}
|
|
|
|
const transformed = transform(actual, {
|
|
parserOpts: {
|
|
...babelrc.parserOpts,
|
|
plugins: ["decorators-legacy", "typescript"],
|
|
},
|
|
babelrc: false,
|
|
configFile: false,
|
|
presets: babelrc.presets,
|
|
plugins: [
|
|
...(babelrc.plugins ?? []),
|
|
plugin,
|
|
{
|
|
visitor: {
|
|
ClassProperty(path) {
|
|
delete path.node.definite
|
|
},
|
|
},
|
|
},
|
|
],
|
|
})!.code!
|
|
expect(canonize(transformed) + "\n").to.deep.equal(canonize(expected) + "\n")
|
|
}
|
|
|
|
describe("tsc-decorator", () => {
|
|
it("works with property decorators", () => {
|
|
equal("properties")
|
|
})
|
|
|
|
it("works with method decorators", () => {
|
|
equal("methods")
|
|
})
|
|
|
|
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/constructor decorators", () => {
|
|
equal("constructor")
|
|
})
|
|
|
|
it("does not interfere with export declarations", () => {
|
|
equal("exports")
|
|
})
|
|
|
|
it("works with private properties", () => {
|
|
equal("privateProperties")
|
|
})
|
|
})
|