Update
This commit is contained in:
parent
84d72d2917
commit
615ee30620
45
a.js
45
a.js
@ -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)
|
16
src/index.ts
16
src/index.ts
@ -53,7 +53,6 @@ export default ({ types: t }: typeof babel): babel.PluginObj<State> => ({
|
||||
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<State> => ({
|
||||
|
||||
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<State> => ({
|
||||
let replaceWith: t.Statement | undefined
|
||||
|
||||
if (classDecorators.length) {
|
||||
if (replace) {
|
||||
replaceWith = t.variableDeclaration("let", [
|
||||
t.variableDeclarator(classID, { ...path.node, type: "ClassExpression" }),
|
||||
])
|
||||
}
|
||||
suffix.push(
|
||||
t.assignmentExpression(
|
||||
"=",
|
||||
@ -158,7 +150,6 @@ export default ({ types: t }: typeof babel): babel.PluginObj<State> => ({
|
||||
])
|
||||
)
|
||||
)
|
||||
if (replace) {
|
||||
if (isNamedExport) {
|
||||
suffix.push(
|
||||
t.exportNamedDeclaration(undefined, [t.exportSpecifier(classID, classID)])
|
||||
@ -166,8 +157,7 @@ export default ({ types: t }: typeof babel): babel.PluginObj<State> => ({
|
||||
} 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<State> => ({
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
|
@ -1,3 +0,0 @@
|
||||
{
|
||||
"presets": [["@babel/preset-env", { "targets": { "node": 14 } }]]
|
||||
}
|
@ -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;
|
||||
import { __decorate } from "tslib";
|
||||
let B = class B {
|
||||
#remote: Service;
|
||||
};
|
||||
B = __decorate([Service()], B);
|
||||
export { B };
|
Loading…
x
Reference in New Issue
Block a user