UNPKG

8.48 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7var _core = require("@babel/core");
8const buildClassDecorator = _core.template.statement(`
9 DECORATOR(CLASS_REF = INNER) || CLASS_REF;
10`);
11const buildClassPrototype = (0, _core.template)(`
12 CLASS_REF.prototype;
13`);
14const buildGetDescriptor = (0, _core.template)(`
15 Object.getOwnPropertyDescriptor(TARGET, PROPERTY);
16`);
17const buildGetObjectInitializer = (0, _core.template)(`
18 (TEMP = Object.getOwnPropertyDescriptor(TARGET, PROPERTY), (TEMP = TEMP ? TEMP.value : undefined), {
19 enumerable: true,
20 configurable: true,
21 writable: true,
22 initializer: function(){
23 return TEMP;
24 }
25 })
26`);
27const WARNING_CALLS = new WeakSet();
28function applyEnsureOrdering(path) {
29 const decorators = (path.isClass() ? [path, ...path.get("body.body")] : path.get("properties")).reduce((acc, prop) => acc.concat(prop.node.decorators || []), []);
30 const identDecorators = decorators.filter(decorator => !_core.types.isIdentifier(decorator.expression));
31 if (identDecorators.length === 0) return;
32 return _core.types.sequenceExpression(identDecorators.map(decorator => {
33 const expression = decorator.expression;
34 const id = decorator.expression = path.scope.generateDeclaredUidIdentifier("dec");
35 return _core.types.assignmentExpression("=", id, expression);
36 }).concat([path.node]));
37}
38function applyClassDecorators(classPath) {
39 if (!hasClassDecorators(classPath.node)) return;
40 const decorators = classPath.node.decorators || [];
41 classPath.node.decorators = null;
42 const name = classPath.scope.generateDeclaredUidIdentifier("class");
43 return decorators.map(dec => dec.expression).reverse().reduce(function (acc, decorator) {
44 return buildClassDecorator({
45 CLASS_REF: _core.types.cloneNode(name),
46 DECORATOR: _core.types.cloneNode(decorator),
47 INNER: acc
48 }).expression;
49 }, classPath.node);
50}
51function hasClassDecorators(classNode) {
52 return !!(classNode.decorators && classNode.decorators.length);
53}
54function applyMethodDecorators(path, state) {
55 if (!hasMethodDecorators(path.node.body.body)) return;
56 return applyTargetDecorators(path, state, path.node.body.body);
57}
58function hasMethodDecorators(body) {
59 return body.some(node => {
60 var _node$decorators;
61 return (_node$decorators = node.decorators) == null ? void 0 : _node$decorators.length;
62 });
63}
64function applyObjectDecorators(path, state) {
65 if (!hasMethodDecorators(path.node.properties)) return;
66 return applyTargetDecorators(path, state, path.node.properties.filter(prop => prop.type !== "SpreadElement"));
67}
68function applyTargetDecorators(path, state, decoratedProps) {
69 const name = path.scope.generateDeclaredUidIdentifier(path.isClass() ? "class" : "obj");
70 const exprs = decoratedProps.reduce(function (acc, node) {
71 let decorators = [];
72 if (node.decorators != null) {
73 decorators = node.decorators;
74 node.decorators = null;
75 }
76 if (decorators.length === 0) return acc;
77 if (node.computed) {
78 throw path.buildCodeFrameError("Computed method/property decorators are not yet supported.");
79 }
80 const property = _core.types.isLiteral(node.key) ? node.key : _core.types.stringLiteral(node.key.name);
81 const target = path.isClass() && !node.static ? buildClassPrototype({
82 CLASS_REF: name
83 }).expression : name;
84 if (_core.types.isClassProperty(node, {
85 static: false
86 })) {
87 const descriptor = path.scope.generateDeclaredUidIdentifier("descriptor");
88 const initializer = node.value ? _core.types.functionExpression(null, [], _core.types.blockStatement([_core.types.returnStatement(node.value)])) : _core.types.nullLiteral();
89 node.value = _core.types.callExpression(state.addHelper("initializerWarningHelper"), [descriptor, _core.types.thisExpression()]);
90 WARNING_CALLS.add(node.value);
91 acc.push(_core.types.assignmentExpression("=", _core.types.cloneNode(descriptor), _core.types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core.types.cloneNode(target), _core.types.cloneNode(property), _core.types.arrayExpression(decorators.map(dec => _core.types.cloneNode(dec.expression))), _core.types.objectExpression([_core.types.objectProperty(_core.types.identifier("configurable"), _core.types.booleanLiteral(true)), _core.types.objectProperty(_core.types.identifier("enumerable"), _core.types.booleanLiteral(true)), _core.types.objectProperty(_core.types.identifier("writable"), _core.types.booleanLiteral(true)), _core.types.objectProperty(_core.types.identifier("initializer"), initializer)])])));
92 } else {
93 acc.push(_core.types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core.types.cloneNode(target), _core.types.cloneNode(property), _core.types.arrayExpression(decorators.map(dec => _core.types.cloneNode(dec.expression))), _core.types.isObjectProperty(node) || _core.types.isClassProperty(node, {
94 static: true
95 }) ? buildGetObjectInitializer({
96 TEMP: path.scope.generateDeclaredUidIdentifier("init"),
97 TARGET: _core.types.cloneNode(target),
98 PROPERTY: _core.types.cloneNode(property)
99 }).expression : buildGetDescriptor({
100 TARGET: _core.types.cloneNode(target),
101 PROPERTY: _core.types.cloneNode(property)
102 }).expression, _core.types.cloneNode(target)]));
103 }
104 return acc;
105 }, []);
106 return _core.types.sequenceExpression([_core.types.assignmentExpression("=", _core.types.cloneNode(name), path.node), _core.types.sequenceExpression(exprs), _core.types.cloneNode(name)]);
107}
108function decoratedClassToExpression({
109 node,
110 scope
111}) {
112 if (!hasClassDecorators(node) && !hasMethodDecorators(node.body.body)) {
113 return;
114 }
115 const ref = node.id ? _core.types.cloneNode(node.id) : scope.generateUidIdentifier("class");
116 return _core.types.variableDeclaration("let", [_core.types.variableDeclarator(ref, _core.types.toExpression(node))]);
117}
118const visitor = {
119 ExportDefaultDeclaration(path) {
120 const decl = path.get("declaration");
121 if (!decl.isClassDeclaration()) return;
122 const replacement = decoratedClassToExpression(decl);
123 if (replacement) {
124 const [varDeclPath] = path.replaceWithMultiple([replacement, _core.types.exportNamedDeclaration(null, [_core.types.exportSpecifier(_core.types.cloneNode(replacement.declarations[0].id), _core.types.identifier("default"))])]);
125 if (!decl.node.id) {
126 path.scope.registerDeclaration(varDeclPath);
127 }
128 }
129 },
130 ClassDeclaration(path) {
131 const replacement = decoratedClassToExpression(path);
132 if (replacement) {
133 const [newPath] = path.replaceWith(replacement);
134 const decl = newPath.get("declarations.0");
135 const id = decl.node.id;
136 const binding = path.scope.getOwnBinding(id.name);
137 binding.identifier = id;
138 binding.path = decl;
139 }
140 },
141 ClassExpression(path, state) {
142 const decoratedClass = applyEnsureOrdering(path) || applyClassDecorators(path) || applyMethodDecorators(path, state);
143 if (decoratedClass) path.replaceWith(decoratedClass);
144 },
145 ObjectExpression(path, state) {
146 const decoratedObject = applyEnsureOrdering(path) || applyObjectDecorators(path, state);
147 if (decoratedObject) path.replaceWith(decoratedObject);
148 },
149 AssignmentExpression(path, state) {
150 if (!WARNING_CALLS.has(path.node.right)) return;
151 path.replaceWith(_core.types.callExpression(state.addHelper("initializerDefineProperty"), [_core.types.cloneNode(path.get("left.object").node), _core.types.stringLiteral(path.get("left.property").node.name || path.get("left.property").node.value), _core.types.cloneNode(path.get("right.arguments")[0].node), _core.types.cloneNode(path.get("right.arguments")[1].node)]));
152 },
153 CallExpression(path, state) {
154 if (path.node.arguments.length !== 3) return;
155 if (!WARNING_CALLS.has(path.node.arguments[2])) return;
156 if (path.node.callee.name !== state.addHelper("defineProperty").name) {
157 return;
158 }
159 path.replaceWith(_core.types.callExpression(state.addHelper("initializerDefineProperty"), [_core.types.cloneNode(path.get("arguments")[0].node), _core.types.cloneNode(path.get("arguments")[1].node), _core.types.cloneNode(path.get("arguments.2.arguments")[0].node), _core.types.cloneNode(path.get("arguments.2.arguments")[1].node)]));
160 }
161};
162var _default = visitor;
163exports.default = _default;
164
165//# sourceMappingURL=transformer-legacy.js.map