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)
|
|
38
src/index.ts
38
src/index.ts
@ -53,7 +53,6 @@ export default ({ types: t }: typeof babel): babel.PluginObj<State> => ({
|
|||||||
const suffix: (t.Expression | t.Statement)[] = []
|
const suffix: (t.Expression | t.Statement)[] = []
|
||||||
const classDecorators: t.Expression[] = []
|
const classDecorators: t.Expression[] = []
|
||||||
const prefixes: t.Statement[] = []
|
const prefixes: t.Statement[] = []
|
||||||
let replace = true
|
|
||||||
|
|
||||||
const classID = path.node.id
|
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) {
|
for (const child of path.node.body.body) {
|
||||||
switch (child.type) {
|
switch (child.type) {
|
||||||
case "ClassPrivateMethod":
|
|
||||||
case "ClassPrivateProperty":
|
|
||||||
replace = false
|
|
||||||
break
|
|
||||||
|
|
||||||
case "ClassProperty":
|
case "ClassProperty":
|
||||||
if (!child.decorators) break
|
if (!child.decorators) break
|
||||||
suffix.push(
|
suffix.push(
|
||||||
@ -143,11 +137,9 @@ export default ({ types: t }: typeof babel): babel.PluginObj<State> => ({
|
|||||||
let replaceWith: t.Statement | undefined
|
let replaceWith: t.Statement | undefined
|
||||||
|
|
||||||
if (classDecorators.length) {
|
if (classDecorators.length) {
|
||||||
if (replace) {
|
replaceWith = t.variableDeclaration("let", [
|
||||||
replaceWith = t.variableDeclaration("let", [
|
t.variableDeclarator(classID, { ...path.node, type: "ClassExpression" }),
|
||||||
t.variableDeclarator(classID, { ...path.node, type: "ClassExpression" }),
|
])
|
||||||
])
|
|
||||||
}
|
|
||||||
suffix.push(
|
suffix.push(
|
||||||
t.assignmentExpression(
|
t.assignmentExpression(
|
||||||
"=",
|
"=",
|
||||||
@ -158,16 +150,14 @@ export default ({ types: t }: typeof babel): babel.PluginObj<State> => ({
|
|||||||
])
|
])
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if (replace) {
|
if (isNamedExport) {
|
||||||
if (isNamedExport) {
|
suffix.push(
|
||||||
suffix.push(
|
t.exportNamedDeclaration(undefined, [t.exportSpecifier(classID, classID)])
|
||||||
t.exportNamedDeclaration(undefined, [t.exportSpecifier(classID, classID)])
|
)
|
||||||
)
|
} else if (isDefaultExport) {
|
||||||
} else if (isDefaultExport) {
|
suffix.push(t.exportDefaultDeclaration(classID))
|
||||||
suffix.push(t.exportDefaultDeclaration(classID))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (replace) {
|
} else {
|
||||||
let node: t.Statement = path.node
|
let node: t.Statement = path.node
|
||||||
if (isNamedExport) {
|
if (isNamedExport) {
|
||||||
node = t.exportNamedDeclaration(node, [], null)
|
node = t.exportNamedDeclaration(node, [], null)
|
||||||
@ -183,12 +173,8 @@ export default ({ types: t }: typeof babel): babel.PluginObj<State> => ({
|
|||||||
}
|
}
|
||||||
if (suffix.length) {
|
if (suffix.length) {
|
||||||
const nodes = suffix.map(s => (t.isStatement(s) ? s : t.expressionStatement(s)))
|
const nodes = suffix.map(s => (t.isStatement(s) ? s : t.expressionStatement(s)))
|
||||||
if (replace) {
|
const [newPath] = pathInContext.replaceWith(replaceWith!)
|
||||||
const [newPath] = pathInContext.replaceWith(replaceWith!)
|
newPath.insertAfter(nodes)
|
||||||
newPath.insertAfter(nodes)
|
|
||||||
} else {
|
|
||||||
pathInContext.insertAfter(nodes)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"presets": [["@babel/preset-env", { "targets": { "node": 14 } }]]
|
|
||||||
}
|
|
@ -1,24 +1,6 @@
|
|||||||
"use strict";
|
import { __decorate } from "tslib";
|
||||||
|
let B = class B {
|
||||||
Object.defineProperty(exports, "__esModule", {
|
#remote: Service;
|
||||||
value: true
|
};
|
||||||
});
|
B = __decorate([Service()], B);
|
||||||
exports.B = void 0;
|
export { B };
|
||||||
|
|
||||||
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;
|
|
Loading…
x
Reference in New Issue
Block a user