diff --git a/src/index.ts b/src/index.ts index bd1e1bd..6a0314b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -158,13 +158,14 @@ export default ({ types: t }: typeof babel): babel.PluginObj => ({ ]) ) ) - - if (isNamedExport) { - suffix.push( - t.exportNamedDeclaration(undefined, [t.exportSpecifier(classID, classID)]) - ) - } else if (isDefaultExport) { - suffix.push(t.exportDefaultDeclaration(classID)) + if (replace) { + if (isNamedExport) { + suffix.push( + t.exportNamedDeclaration(undefined, [t.exportSpecifier(classID, classID)]) + ) + } else if (isDefaultExport) { + suffix.push(t.exportDefaultDeclaration(classID)) + } } } else if (replace) { let node: t.Statement = path.node diff --git a/test/index.ts b/test/index.ts index cb83a8f..5652a4e 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,4 +1,4 @@ -import { readFileSync, existsSync } from "fs" +import { readFileSync, existsSync, writeFileSync } from "fs" import { describe, it } from "mocha" import { resolve } from "path" import { expect } from "chai" @@ -10,12 +10,14 @@ import plugin from "../src/index" const canonize = (code: string) => format(code, { parser: "babel" }).trim().split("\n").filter(Boolean).join("\n") +const read = (path: string) => [path, readFileSync(path, "utf-8")] as const + 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 [, actual] = read(resolve(folder, "input.txt")) + const [expectedPath, expected] = read(resolve(folder, "output.txt")) const babelrc: babel.TransformOptions = existsSync(resolve(folder, ".babelrc")) - ? JSON.parse(readFileSync(resolve(folder, ".babelrc"), "utf-8")) + ? JSON.parse(read(resolve(folder, ".babelrc"))[1]) : {} const transformed = transform(actual, { @@ -38,7 +40,12 @@ const equal = (name: Snapshot) => { }, ], })!.code! - expect(canonize(transformed) + "\n").to.deep.equal(canonize(expected) + "\n") + + if (process.env.OVERWRITE) { + writeFileSync(expectedPath, transformed) + } else { + expect(canonize(transformed) + "\n").to.deep.equal(canonize(expected) + "\n") + } } describe("tsc-decorator", () => { diff --git a/test/snapshots/constructor/output.txt b/test/snapshots/constructor/output.txt index ea513bf..ae8d58d 100644 --- a/test/snapshots/constructor/output.txt +++ b/test/snapshots/constructor/output.txt @@ -1,8 +1,8 @@ import { __decorate } from "tslib"; - let A = class A { string: string; -} +}; __decorate([prop], A.prototype, "string", void 0); -A = __decorate([classDecorator], A); + +A = __decorate([classDecorator], A); \ No newline at end of file diff --git a/test/snapshots/exports/output.txt b/test/snapshots/exports/output.txt index 2ea7900..284cb52 100644 --- a/test/snapshots/exports/output.txt +++ b/test/snapshots/exports/output.txt @@ -1,15 +1,13 @@ 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; +export default DefaultExport; \ No newline at end of file diff --git a/test/snapshots/methods/output.txt b/test/snapshots/methods/output.txt index 3cfe9e5..3854be4 100644 --- a/test/snapshots/methods/output.txt +++ b/test/snapshots/methods/output.txt @@ -2,6 +2,7 @@ import { __decorate } from "tslib"; class A { method(): string {} + } __decorate([dec], A.prototype, "method", null); \ No newline at end of file diff --git a/test/snapshots/params/output.txt b/test/snapshots/params/output.txt index 69183a0..74a6813 100644 --- a/test/snapshots/params/output.txt +++ b/test/snapshots/params/output.txt @@ -3,13 +3,17 @@ 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); let B = class B { constructor(a: string) {} + }; B = _decorate([__param(0, Arg())], B); \ No newline at end of file diff --git a/test/snapshots/privateProperties/output.txt b/test/snapshots/privateProperties/output.txt index 2ed2a1e..e121d2f 100644 --- a/test/snapshots/privateProperties/output.txt +++ b/test/snapshots/privateProperties/output.txt @@ -1,22 +1,24 @@ "use strict"; -Object.defineProperty(exports, "__esModule", { - value: true, -}); -exports.B = exports.B = void 0; +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.B = void 0; var _tslib = require("tslib"); -var _remote = /*#__PURE__*/ new WeakMap(); -exports.B = exports.B = B = (0, _tslib.__decorate)([Service()], B); +var _remote = /*#__PURE__*/new WeakMap(); + +exports.B = B = (0, _tslib.__decorate)([Service()], B); class B { constructor() { _remote.set(this, { writable: true, - value: void 0, + value: void 0 }); } + } -exports.B = exports.B = B; +exports.B = B; \ No newline at end of file diff --git a/test/snapshots/properties/output.txt b/test/snapshots/properties/output.txt index 4a4e7d3..14b42db 100644 --- a/test/snapshots/properties/output.txt +++ b/test/snapshots/properties/output.txt @@ -1,4 +1,4 @@ -import { __decorate } from "tslib" +import { __decorate } from "tslib"; class A { prop: string; @@ -9,13 +9,18 @@ class A { } __decorate([dec], A.prototype, "prop", void 0); + __decorate([dec, dec2], A.prototype, "prop2", void 0); + __decorate([dec], A, "staticProp", void 0); + __decorate([called()], A.prototype, "service", void 0); let B = class B { remote: Service; }; + __decorate([Service()], B.prototype, "remote", void 0); + B = __decorate([Service()], B); export { B }; \ No newline at end of file