From 615ee30620e290013a4050efa97e58edb0a94b28 Mon Sep 17 00:00:00 2001 From: proteriax <8125011+proteriax@users.noreply.github.com> Date: Thu, 22 Jul 2021 15:17:02 -0400 Subject: [PATCH] Update --- a.js | 45 --------------------- src/index.ts | 38 ++++++----------- test/snapshots/privateProperties/.babelrc | 3 -- test/snapshots/privateProperties/output.txt | 30 +++----------- 4 files changed, 18 insertions(+), 98 deletions(-) delete mode 100755 a.js delete mode 100644 test/snapshots/privateProperties/.babelrc diff --git a/a.js b/a.js deleted file mode 100755 index ae98514..0000000 --- a/a.js +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env node -const fs = require("fs") -const babel = require("@babel/core") - -const code = ` -export class A { - #a: string; -} -` - -const res = babel.transform(code, { - filename: "a.ts", - presets: [ - ["@babel/preset-env", { targets: { node: 14 } }], - ["@babel/preset-typescript"], - ], - plugins: [ - ({ types: t }) => ({ - visitor: { - ClassDeclaration(path, state) { - state.a ??= new WeakSet() - if (state.a.has(path.node)) { - return - } - state.a.add(path.node) - - path.parentPath.replaceWithMultiple([ - t.exportNamedDeclaration( - t.variableDeclaration("let", [ - t.variableDeclarator(path.node.id, { - ...path.node, - type: "ClassExpression", - }), - ]), - [], - null - ), - ]) - }, - }, - }), - ], -}) - -console.log(res.code) diff --git a/src/index.ts b/src/index.ts index 6a0314b..544af7a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -53,7 +53,6 @@ export default ({ types: t }: typeof babel): babel.PluginObj => ({ const suffix: (t.Expression | t.Statement)[] = [] const classDecorators: t.Expression[] = [] const prefixes: t.Statement[] = [] - let replace = true const classID = path.node.id @@ -67,11 +66,6 @@ export default ({ types: t }: typeof babel): babel.PluginObj => ({ for (const child of path.node.body.body) { switch (child.type) { - case "ClassPrivateMethod": - case "ClassPrivateProperty": - replace = false - break - case "ClassProperty": if (!child.decorators) break suffix.push( @@ -143,11 +137,9 @@ export default ({ types: t }: typeof babel): babel.PluginObj => ({ let replaceWith: t.Statement | undefined if (classDecorators.length) { - if (replace) { - replaceWith = t.variableDeclaration("let", [ - t.variableDeclarator(classID, { ...path.node, type: "ClassExpression" }), - ]) - } + replaceWith = t.variableDeclaration("let", [ + t.variableDeclarator(classID, { ...path.node, type: "ClassExpression" }), + ]) suffix.push( t.assignmentExpression( "=", @@ -158,16 +150,14 @@ export default ({ types: t }: typeof babel): babel.PluginObj => ({ ]) ) ) - if (replace) { - if (isNamedExport) { - suffix.push( - t.exportNamedDeclaration(undefined, [t.exportSpecifier(classID, classID)]) - ) - } else if (isDefaultExport) { - suffix.push(t.exportDefaultDeclaration(classID)) - } + if (isNamedExport) { + suffix.push( + t.exportNamedDeclaration(undefined, [t.exportSpecifier(classID, classID)]) + ) + } else if (isDefaultExport) { + suffix.push(t.exportDefaultDeclaration(classID)) } - } else if (replace) { + } else { let node: t.Statement = path.node if (isNamedExport) { node = t.exportNamedDeclaration(node, [], null) @@ -183,12 +173,8 @@ export default ({ types: t }: typeof babel): babel.PluginObj => ({ } if (suffix.length) { const nodes = suffix.map(s => (t.isStatement(s) ? s : t.expressionStatement(s))) - if (replace) { - const [newPath] = pathInContext.replaceWith(replaceWith!) - newPath.insertAfter(nodes) - } else { - pathInContext.insertAfter(nodes) - } + const [newPath] = pathInContext.replaceWith(replaceWith!) + newPath.insertAfter(nodes) } }, }, diff --git a/test/snapshots/privateProperties/.babelrc b/test/snapshots/privateProperties/.babelrc deleted file mode 100644 index 5c676bb..0000000 --- a/test/snapshots/privateProperties/.babelrc +++ /dev/null @@ -1,3 +0,0 @@ -{ - "presets": [["@babel/preset-env", { "targets": { "node": 14 } }]] -} diff --git a/test/snapshots/privateProperties/output.txt b/test/snapshots/privateProperties/output.txt index e121d2f..85461f2 100644 --- a/test/snapshots/privateProperties/output.txt +++ b/test/snapshots/privateProperties/output.txt @@ -1,24 +1,6 @@ -"use strict"; - -Object.defineProperty(exports, "__esModule", { - value: true -}); -exports.B = void 0; - -var _tslib = require("tslib"); - -var _remote = /*#__PURE__*/new WeakMap(); - -exports.B = B = (0, _tslib.__decorate)([Service()], B); - -class B { - constructor() { - _remote.set(this, { - writable: true, - value: void 0 - }); - } - -} - -exports.B = B; \ No newline at end of file +import { __decorate } from "tslib"; +let B = class B { + #remote: Service; +}; +B = __decorate([Service()], B); +export { B }; \ No newline at end of file