@cocojs/compiler 0.1.0-beta.202601082201
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/index.cjs.js +536 -0
- package/dist/index.d.ts +12 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) jiangchenguang.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ts = require('typescript');
|
|
4
|
+
|
|
5
|
+
function _interopNamespaceDefault(e) {
|
|
6
|
+
var n = Object.create(null);
|
|
7
|
+
if (e) {
|
|
8
|
+
Object.keys(e).forEach(function (k) {
|
|
9
|
+
if (k !== 'default') {
|
|
10
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
11
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return e[k]; }
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
n.default = e;
|
|
19
|
+
return Object.freeze(n);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
var ts__namespace = /*#__PURE__*/_interopNamespaceDefault(ts);
|
|
23
|
+
|
|
24
|
+
function _arrayLikeToArray(r, a) {
|
|
25
|
+
(null == a || a > r.length) && (a = r.length);
|
|
26
|
+
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
27
|
+
return n;
|
|
28
|
+
}
|
|
29
|
+
function _arrayWithoutHoles(r) {
|
|
30
|
+
if (Array.isArray(r)) return _arrayLikeToArray(r);
|
|
31
|
+
}
|
|
32
|
+
function _createForOfIteratorHelper(r, e) {
|
|
33
|
+
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
34
|
+
if (!t) {
|
|
35
|
+
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) {
|
|
36
|
+
t && (r = t);
|
|
37
|
+
var n = 0,
|
|
38
|
+
F = function () {};
|
|
39
|
+
return {
|
|
40
|
+
s: F,
|
|
41
|
+
n: function () {
|
|
42
|
+
return n >= r.length ? {
|
|
43
|
+
done: true
|
|
44
|
+
} : {
|
|
45
|
+
done: false,
|
|
46
|
+
value: r[n++]
|
|
47
|
+
};
|
|
48
|
+
},
|
|
49
|
+
e: function (r) {
|
|
50
|
+
throw r;
|
|
51
|
+
},
|
|
52
|
+
f: F
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
56
|
+
}
|
|
57
|
+
var o,
|
|
58
|
+
a = true,
|
|
59
|
+
u = false;
|
|
60
|
+
return {
|
|
61
|
+
s: function () {
|
|
62
|
+
t = t.call(r);
|
|
63
|
+
},
|
|
64
|
+
n: function () {
|
|
65
|
+
var r = t.next();
|
|
66
|
+
return a = r.done, r;
|
|
67
|
+
},
|
|
68
|
+
e: function (r) {
|
|
69
|
+
u = true, o = r;
|
|
70
|
+
},
|
|
71
|
+
f: function () {
|
|
72
|
+
try {
|
|
73
|
+
a || null == t.return || t.return();
|
|
74
|
+
} finally {
|
|
75
|
+
if (u) throw o;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function _defineProperty(e, r, t) {
|
|
81
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
82
|
+
value: t,
|
|
83
|
+
enumerable: true,
|
|
84
|
+
configurable: true,
|
|
85
|
+
writable: true
|
|
86
|
+
}) : e[r] = t, e;
|
|
87
|
+
}
|
|
88
|
+
function _iterableToArray(r) {
|
|
89
|
+
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
|
|
90
|
+
}
|
|
91
|
+
function _nonIterableSpread() {
|
|
92
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
93
|
+
}
|
|
94
|
+
function ownKeys(e, r) {
|
|
95
|
+
var t = Object.keys(e);
|
|
96
|
+
if (Object.getOwnPropertySymbols) {
|
|
97
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
98
|
+
r && (o = o.filter(function (r) {
|
|
99
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
100
|
+
})), t.push.apply(t, o);
|
|
101
|
+
}
|
|
102
|
+
return t;
|
|
103
|
+
}
|
|
104
|
+
function _objectSpread2(e) {
|
|
105
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
106
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
107
|
+
r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
|
|
108
|
+
_defineProperty(e, r, t[r]);
|
|
109
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
110
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return e;
|
|
114
|
+
}
|
|
115
|
+
function _toConsumableArray(r) {
|
|
116
|
+
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
|
|
117
|
+
}
|
|
118
|
+
function _toPrimitive(t, r) {
|
|
119
|
+
if ("object" != typeof t || !t) return t;
|
|
120
|
+
var e = t[Symbol.toPrimitive];
|
|
121
|
+
if (void 0 !== e) {
|
|
122
|
+
var i = e.call(t, r);
|
|
123
|
+
if ("object" != typeof i) return i;
|
|
124
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
125
|
+
}
|
|
126
|
+
return ("string" === r ? String : Number)(t);
|
|
127
|
+
}
|
|
128
|
+
function _toPropertyKey(t) {
|
|
129
|
+
var i = _toPrimitive(t, "string");
|
|
130
|
+
return "symbol" == typeof i ? i : i + "";
|
|
131
|
+
}
|
|
132
|
+
function _unsupportedIterableToArray(r, a) {
|
|
133
|
+
if (r) {
|
|
134
|
+
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
135
|
+
var t = {}.toString.call(r).slice(8, -1);
|
|
136
|
+
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
var wrapperClassName = ['String', 'Number', 'Boolean'];
|
|
141
|
+
var innerClassName = ['Object', 'Symbol', 'Array', 'Map', 'Set', 'Function'];
|
|
142
|
+
// {}
|
|
143
|
+
function isObjectType(type) {
|
|
144
|
+
return !!type && ts__namespace.isTypeLiteralNode(type);
|
|
145
|
+
}
|
|
146
|
+
// []
|
|
147
|
+
function isTupleArray(type) {
|
|
148
|
+
return !!type && ts__namespace.isTupleTypeNode(type);
|
|
149
|
+
}
|
|
150
|
+
// () => {}
|
|
151
|
+
function isFunctionType(type) {
|
|
152
|
+
return !!type && ts__namespace.isFunctionTypeNode(type);
|
|
153
|
+
}
|
|
154
|
+
function isDecoratorExp(decorator, decoratorName) {
|
|
155
|
+
var expr = decorator.expression;
|
|
156
|
+
return ts__namespace.isCallExpression(expr) && ts__namespace.isIdentifier(expr.expression) && expr.expression.text === decoratorName;
|
|
157
|
+
}
|
|
158
|
+
function hasClassKindDecorator(classDeclaration) {
|
|
159
|
+
var _classDeclaration$mod;
|
|
160
|
+
return !!((_classDeclaration$mod = classDeclaration.modifiers) !== null && _classDeclaration$mod !== void 0 && _classDeclaration$mod.find(function (modifier) {
|
|
161
|
+
return ts__namespace.isDecorator(modifier);
|
|
162
|
+
}));
|
|
163
|
+
}
|
|
164
|
+
function extractIdentifierFromType(type) {
|
|
165
|
+
if (ts__namespace.isTypeReferenceNode(type)) {
|
|
166
|
+
if (ts__namespace.isIdentifier(type.typeName)) {
|
|
167
|
+
if (wrapperClassName.includes(type.typeName.text) || innerClassName.includes(type.typeName.text)) {
|
|
168
|
+
return ts__namespace.factory.createIdentifier('undefined');
|
|
169
|
+
}
|
|
170
|
+
return ts__namespace.factory.createIdentifier(type.typeName.text);
|
|
171
|
+
}
|
|
172
|
+
} else if (type.kind === ts__namespace.SyntaxKind.StringKeyword || type.kind === ts__namespace.SyntaxKind.NumberKeyword || type.kind === ts__namespace.SyntaxKind.BooleanKeyword || type.kind === ts__namespace.SyntaxKind.ObjectKeyword || type && ts__namespace.isLiteralTypeNode(type) && type.literal.kind === ts__namespace.SyntaxKind.NullKeyword || type.kind === ts__namespace.SyntaxKind.UndefinedKeyword || type.kind === ts__namespace.SyntaxKind.SymbolKeyword) {
|
|
173
|
+
return ts__namespace.factory.createIdentifier('undefined');
|
|
174
|
+
} else if (isObjectType(type) || isTupleArray(type) || isFunctionType(type)) {
|
|
175
|
+
return ts__namespace.factory.createIdentifier('undefined');
|
|
176
|
+
}
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
function createNamedImport(className, importPath) {
|
|
180
|
+
return ts__namespace.factory.createImportDeclaration(undefined,
|
|
181
|
+
// modifiers
|
|
182
|
+
ts__namespace.factory.createImportClause(false, undefined, ts__namespace.factory.createNamedImports([ts__namespace.factory.createImportSpecifier(false, undefined, ts__namespace.factory.createIdentifier(className))])), ts__namespace.factory.createStringLiteral(importPath), undefined);
|
|
183
|
+
}
|
|
184
|
+
function createImport(className, importPath) {
|
|
185
|
+
return ts__namespace.factory.createImportDeclaration(undefined, ts__namespace.factory.createImportClause(undefined, ts__namespace.factory.createIdentifier(className), undefined), ts__namespace.factory.createStringLiteral(importPath), undefined);
|
|
186
|
+
}
|
|
187
|
+
function updateTypeImports(sourceFile) {
|
|
188
|
+
var identifyList = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
189
|
+
if (!identifyList.length) {
|
|
190
|
+
return sourceFile;
|
|
191
|
+
}
|
|
192
|
+
var importNames = new Set();
|
|
193
|
+
identifyList.forEach(function (iden) {
|
|
194
|
+
return importNames.add(iden.text);
|
|
195
|
+
});
|
|
196
|
+
var newStatements = [];
|
|
197
|
+
var _iterator = _createForOfIteratorHelper(importNames),
|
|
198
|
+
_step;
|
|
199
|
+
try {
|
|
200
|
+
var _loop = function _loop() {
|
|
201
|
+
var name = _step.value;
|
|
202
|
+
var _iterator2 = _createForOfIteratorHelper(sourceFile.statements),
|
|
203
|
+
_step2;
|
|
204
|
+
try {
|
|
205
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
206
|
+
var stmt = _step2.value;
|
|
207
|
+
if (ts__namespace.isImportDeclaration(stmt)) {
|
|
208
|
+
var _stmt$importClause$na;
|
|
209
|
+
if (((_stmt$importClause$na = stmt.importClause.namedBindings) === null || _stmt$importClause$na === void 0 ? void 0 : _stmt$importClause$na.kind) === ts__namespace.SyntaxKind.NamedImports) {
|
|
210
|
+
var match = stmt.importClause.namedBindings.elements.find(function (e) {
|
|
211
|
+
return name === e.name.text;
|
|
212
|
+
});
|
|
213
|
+
if (match) {
|
|
214
|
+
var importPath = stmt.moduleSpecifier.text;
|
|
215
|
+
newStatements.push(createNamedImport(name, importPath));
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (stmt.importClause.name && ts__namespace.isIdentifier(stmt.importClause.name)) {
|
|
220
|
+
if (stmt.importClause.name.text === name) {
|
|
221
|
+
var _importPath = stmt.moduleSpecifier.text;
|
|
222
|
+
newStatements.push(createImport(name, _importPath));
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
} catch (err) {
|
|
229
|
+
_iterator2.e(err);
|
|
230
|
+
} finally {
|
|
231
|
+
_iterator2.f();
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
235
|
+
_loop();
|
|
236
|
+
}
|
|
237
|
+
} catch (err) {
|
|
238
|
+
_iterator.e(err);
|
|
239
|
+
} finally {
|
|
240
|
+
_iterator.f();
|
|
241
|
+
}
|
|
242
|
+
return ts__namespace.factory.updateSourceFile(sourceFile, [].concat(newStatements, _toConsumableArray(sourceFile.statements)));
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function isConstructParamsDecorator(decorator) {
|
|
246
|
+
return isDecoratorExp(decorator, 'constructorParam');
|
|
247
|
+
}
|
|
248
|
+
function extractIdentifiersFromConstructor(ctor) {
|
|
249
|
+
var identifiers = [];
|
|
250
|
+
var _iterator = _createForOfIteratorHelper(ctor.parameters),
|
|
251
|
+
_step;
|
|
252
|
+
try {
|
|
253
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
254
|
+
var param = _step.value;
|
|
255
|
+
if (!param.type) {
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
var id = extractIdentifierFromType(param.type);
|
|
259
|
+
if (!id) {
|
|
260
|
+
return [];
|
|
261
|
+
}
|
|
262
|
+
identifiers.push(id);
|
|
263
|
+
}
|
|
264
|
+
} catch (err) {
|
|
265
|
+
_iterator.e(err);
|
|
266
|
+
} finally {
|
|
267
|
+
_iterator.f();
|
|
268
|
+
}
|
|
269
|
+
return identifiers;
|
|
270
|
+
}
|
|
271
|
+
function updateConstructorParamDecorator(classDeclaration) {
|
|
272
|
+
var modifiers = classDeclaration.modifiers;
|
|
273
|
+
if (!modifiers) {
|
|
274
|
+
return {
|
|
275
|
+
updated: false,
|
|
276
|
+
modifiers: classDeclaration.modifiers
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
var decorators = modifiers.filter(ts__namespace.isDecorator);
|
|
280
|
+
if (!decorators.length) {
|
|
281
|
+
return {
|
|
282
|
+
updated: false,
|
|
283
|
+
modifiers: classDeclaration.modifiers
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
var constructParams = decorators.find(isConstructParamsDecorator);
|
|
287
|
+
if (!constructParams) {
|
|
288
|
+
return {
|
|
289
|
+
updated: false,
|
|
290
|
+
modifiers: classDeclaration.modifiers
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
var call = constructParams.expression;
|
|
294
|
+
// 已有参数,不处理
|
|
295
|
+
if (call.arguments.length > 0) {
|
|
296
|
+
return {
|
|
297
|
+
updated: false,
|
|
298
|
+
modifiers: classDeclaration.modifiers
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
// 找 constructor
|
|
302
|
+
var ctor = classDeclaration.members.find(function (m) {
|
|
303
|
+
return ts__namespace.isConstructorDeclaration(m);
|
|
304
|
+
});
|
|
305
|
+
if (!ctor) {
|
|
306
|
+
return {
|
|
307
|
+
updated: false,
|
|
308
|
+
modifiers: classDeclaration.modifiers
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
var identifiers = extractIdentifiersFromConstructor(ctor);
|
|
312
|
+
if (!identifiers.length) {
|
|
313
|
+
return {
|
|
314
|
+
updated: false,
|
|
315
|
+
modifiers: classDeclaration.modifiers
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
// 构造数组:[Api, User]
|
|
319
|
+
var arrayLiteral = ts__namespace.factory.createArrayLiteralExpression(identifiers, false);
|
|
320
|
+
var newModifiers = modifiers.map(function (m) {
|
|
321
|
+
return ts__namespace.isDecorator(m) && m === constructParams ? ts__namespace.factory.createDecorator(ts__namespace.factory.updateCallExpression(call, call.expression, call.typeArguments, [arrayLiteral])) : m;
|
|
322
|
+
});
|
|
323
|
+
return {
|
|
324
|
+
updated: true,
|
|
325
|
+
modifiers: newModifiers,
|
|
326
|
+
constructorParamTypeList: identifiers
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function isAutowiredDecorator(decorator) {
|
|
331
|
+
return isDecoratorExp(decorator, 'autowired');
|
|
332
|
+
}
|
|
333
|
+
function updateAutowiredDecorator(member, markUpdate) {
|
|
334
|
+
if (!ts__namespace.isPropertyDeclaration(member)) {
|
|
335
|
+
return member;
|
|
336
|
+
}
|
|
337
|
+
var modifiers = member.modifiers;
|
|
338
|
+
if (!modifiers) {
|
|
339
|
+
return member;
|
|
340
|
+
}
|
|
341
|
+
var decorators = modifiers.filter(ts__namespace.isDecorator);
|
|
342
|
+
if (!decorators.length) {
|
|
343
|
+
return member;
|
|
344
|
+
}
|
|
345
|
+
var autowired = decorators.find(isAutowiredDecorator);
|
|
346
|
+
if (!autowired) {
|
|
347
|
+
return member;
|
|
348
|
+
}
|
|
349
|
+
var call = autowired.expression;
|
|
350
|
+
// 已经有参数,不处理
|
|
351
|
+
if (call.arguments.length > 0) {
|
|
352
|
+
return member;
|
|
353
|
+
}
|
|
354
|
+
if (!member.type) {
|
|
355
|
+
return member;
|
|
356
|
+
}
|
|
357
|
+
var identifier = extractIdentifierFromType(member.type);
|
|
358
|
+
if (!identifier) {
|
|
359
|
+
return member;
|
|
360
|
+
}
|
|
361
|
+
markUpdate(identifier);
|
|
362
|
+
var newModifiers = modifiers.map(function (m) {
|
|
363
|
+
return ts__namespace.isDecorator(m) && m === autowired ? ts__namespace.factory.createDecorator(ts__namespace.factory.updateCallExpression(call, call.expression, call.typeArguments, [identifier])) : m;
|
|
364
|
+
});
|
|
365
|
+
return ts__namespace.factory.updatePropertyDeclaration(member, newModifiers, member.name, ts__namespace.factory.createToken(ts__namespace.SyntaxKind.ExclamationToken), member.type, member.initializer);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function isComponentDecorator(decorator) {
|
|
369
|
+
return isDecoratorExp(decorator, 'component');
|
|
370
|
+
}
|
|
371
|
+
function updateComponentDecorator(member, markUpdate) {
|
|
372
|
+
if (!ts__namespace.isMethodDeclaration(member)) {
|
|
373
|
+
return member;
|
|
374
|
+
}
|
|
375
|
+
var modifiers = member.modifiers;
|
|
376
|
+
if (!modifiers) {
|
|
377
|
+
return member;
|
|
378
|
+
}
|
|
379
|
+
var decorators = modifiers.filter(ts__namespace.isDecorator);
|
|
380
|
+
if (!decorators.length) {
|
|
381
|
+
return member;
|
|
382
|
+
}
|
|
383
|
+
var component = decorators.find(isComponentDecorator);
|
|
384
|
+
if (!component) {
|
|
385
|
+
return member;
|
|
386
|
+
}
|
|
387
|
+
var call = component.expression;
|
|
388
|
+
// 已有参数,不处理
|
|
389
|
+
if (call.arguments.length > 0) {
|
|
390
|
+
return member;
|
|
391
|
+
}
|
|
392
|
+
// 没有返回类型,无法补全
|
|
393
|
+
if (!member.type) {
|
|
394
|
+
return member;
|
|
395
|
+
}
|
|
396
|
+
var identifier = extractIdentifierFromType(member.type);
|
|
397
|
+
if (!identifier) {
|
|
398
|
+
return member;
|
|
399
|
+
}
|
|
400
|
+
markUpdate(identifier);
|
|
401
|
+
var newModifiers = modifiers.map(function (m) {
|
|
402
|
+
return ts__namespace.isDecorator(m) && m === component ? ts__namespace.factory.createDecorator(ts__namespace.factory.updateCallExpression(call, call.expression, call.typeArguments, [identifier])) : m;
|
|
403
|
+
});
|
|
404
|
+
return ts__namespace.factory.updateMethodDeclaration(member, newModifiers, member.asteriskToken, member.name, member.questionToken, member.typeParameters, member.parameters, member.type, member.body);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
var innerIdName = '$$id';
|
|
408
|
+
function ifNeedAdd$$idProperty(classDecorator, prefix) {
|
|
409
|
+
var className = classDecorator.name.text;
|
|
410
|
+
var $$idProperties = classDecorator.members.filter(function (member) {
|
|
411
|
+
var _member$modifiers;
|
|
412
|
+
return ts__namespace.isPropertyDeclaration(member) && ((_member$modifiers = member.modifiers) === null || _member$modifiers === void 0 ? void 0 : _member$modifiers.some(function (modify) {
|
|
413
|
+
return modify.kind === ts__namespace.SyntaxKind.StaticKeyword;
|
|
414
|
+
})) && ts__namespace.isIdentifier(member.name) && member.name.text === '$$id';
|
|
415
|
+
});
|
|
416
|
+
if ($$idProperties.length === 0) {
|
|
417
|
+
return ts__namespace.factory.createPropertyDeclaration([ts__namespace.factory.createModifier(ts__namespace.SyntaxKind.StaticKeyword)], innerIdName, undefined, undefined, ts__namespace.factory.createStringLiteral("".concat(prefix).concat(className)));
|
|
418
|
+
} else if ($$idProperties.length === 1) {
|
|
419
|
+
var property = $$idProperties[0];
|
|
420
|
+
if (property.initializer && ts__namespace.isStringLiteral(property.initializer)) {
|
|
421
|
+
if (!property.initializer.text.trim()) {
|
|
422
|
+
throw new Error("\u60F3\u8981\u4E3A\u7C7B".concat(className, "\u81EA\u5B9A\u4E49\"").concat(innerIdName, "\"\uFF0C\u503C\u4E0D\u80FD\u662F\u7A7A\u5B57\u7B26\u4E32"));
|
|
423
|
+
} else {
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
} else {
|
|
427
|
+
throw new Error("\u60F3\u8981\u4E3A\u7C7B".concat(className, "\u81EA\u5B9A\u4E49\"").concat(innerIdName, "\"\uFF0C\u503C\u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u5B57\u9762\u91CF"));
|
|
428
|
+
}
|
|
429
|
+
} else {
|
|
430
|
+
throw new Error("\u7C7B".concat(className, "\u5B58\u5728\u591A\u4E2A\"").concat(innerIdName, "\"\uFF0C\u6700\u591A\u53EA\u80FD\u6709\u4E00\u4E2A\uFF01"));
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function updateMembers(classDeclaration) {
|
|
435
|
+
var updated = false;
|
|
436
|
+
var autowiredList = [];
|
|
437
|
+
var componentList = [];
|
|
438
|
+
var updateAutowired = function updateAutowired(identifier) {
|
|
439
|
+
autowiredList.push(identifier);
|
|
440
|
+
updated = true;
|
|
441
|
+
};
|
|
442
|
+
var updateComponent = function updateComponent(identifier) {
|
|
443
|
+
componentList.push(identifier);
|
|
444
|
+
updated = true;
|
|
445
|
+
};
|
|
446
|
+
var members = classDeclaration.members.map(function (member) {
|
|
447
|
+
if (ts__namespace.isPropertyDeclaration(member)) {
|
|
448
|
+
return updateAutowiredDecorator(member, updateAutowired);
|
|
449
|
+
} else if (ts__namespace.isMethodDeclaration(member)) {
|
|
450
|
+
return updateComponentDecorator(member, updateComponent);
|
|
451
|
+
} else {
|
|
452
|
+
return member;
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
return {
|
|
456
|
+
updated: updated,
|
|
457
|
+
members: updated ? members : classDeclaration.members,
|
|
458
|
+
autowiredList: autowiredList,
|
|
459
|
+
componentList: componentList
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
function transformerFactory() {
|
|
463
|
+
var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
464
|
+
return function (context) {
|
|
465
|
+
var constructorParamList = [];
|
|
466
|
+
var autowiredList = [];
|
|
467
|
+
var componentList = [];
|
|
468
|
+
var _visit = function visit(node) {
|
|
469
|
+
if (ts__namespace.isClassDeclaration(node)) {
|
|
470
|
+
if (hasClassKindDecorator(node)) {
|
|
471
|
+
var $$idProperty = ifNeedAdd$$idProperty(node, prefix);
|
|
472
|
+
var _updateMembers = updateMembers(node),
|
|
473
|
+
members = _updateMembers.members,
|
|
474
|
+
membersUpdated = _updateMembers.updated,
|
|
475
|
+
_autowiredList = _updateMembers.autowiredList,
|
|
476
|
+
_componentList = _updateMembers.componentList;
|
|
477
|
+
var _updateConstructorPar = updateConstructorParamDecorator(node),
|
|
478
|
+
modifiers = _updateConstructorPar.modifiers,
|
|
479
|
+
constructorParamsUpdated = _updateConstructorPar.updated,
|
|
480
|
+
_constructorParamTypeList = _updateConstructorPar.constructorParamTypeList;
|
|
481
|
+
if (!membersUpdated && !constructorParamsUpdated && !$$idProperty) {
|
|
482
|
+
return node;
|
|
483
|
+
} else {
|
|
484
|
+
if (_autowiredList) {
|
|
485
|
+
autowiredList = _autowiredList;
|
|
486
|
+
}
|
|
487
|
+
if (_componentList) {
|
|
488
|
+
componentList = _componentList;
|
|
489
|
+
}
|
|
490
|
+
if (constructorParamsUpdated) {
|
|
491
|
+
constructorParamList = _constructorParamTypeList;
|
|
492
|
+
}
|
|
493
|
+
return ts__namespace.factory.updateClassDeclaration(node, modifiers, node.name, node.typeParameters, node.heritageClauses, $$idProperty ? [$$idProperty].concat(_toConsumableArray(members)) : members);
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
return node;
|
|
497
|
+
} else {
|
|
498
|
+
return ts__namespace.visitEachChild(node, _visit, context);
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
// @ts-ignore
|
|
502
|
+
return function (sourceFile) {
|
|
503
|
+
var updatedSourceFile = ts__namespace.visitNode(sourceFile, _visit);
|
|
504
|
+
if (!constructorParamList.length && !autowiredList.length && !componentList.length) {
|
|
505
|
+
return updatedSourceFile;
|
|
506
|
+
}
|
|
507
|
+
return updateTypeImports(updatedSourceFile, [].concat(_toConsumableArray(constructorParamList), _toConsumableArray(autowiredList), _toConsumableArray(componentList)));
|
|
508
|
+
};
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
var commonCompilerOptions = {
|
|
513
|
+
target: ts__namespace.ScriptTarget.ESNext,
|
|
514
|
+
module: ts__namespace.ModuleKind.ESNext,
|
|
515
|
+
emitDecoratorMetadata: false
|
|
516
|
+
};
|
|
517
|
+
function compileOneFile(code, fileName) {
|
|
518
|
+
var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
519
|
+
var result = ts__namespace.transpileModule(code, {
|
|
520
|
+
fileName: fileName,
|
|
521
|
+
compilerOptions: _objectSpread2(_objectSpread2({}, commonCompilerOptions), {}, {
|
|
522
|
+
sourceMap: true
|
|
523
|
+
}),
|
|
524
|
+
transformers: {
|
|
525
|
+
before: [transformerFactory(prefix)]
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
return {
|
|
529
|
+
code: result.outputText,
|
|
530
|
+
map: result.sourceMapText
|
|
531
|
+
};
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
exports.commonCompilerOptions = commonCompilerOptions;
|
|
535
|
+
exports.compileOneFile = compileOneFile;
|
|
536
|
+
exports.transformerFactory = transformerFactory;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as ts from 'typescript';
|
|
2
|
+
|
|
3
|
+
export declare const commonCompilerOptions: ts.CompilerOptions;
|
|
4
|
+
|
|
5
|
+
export declare function compileOneFile(code: string, fileName: string, prefix?: string): {
|
|
6
|
+
code: string;
|
|
7
|
+
map?: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export declare function transformerFactory(prefix?: string): ts.TransformerFactory<ts.SourceFile>;
|
|
11
|
+
|
|
12
|
+
export { }
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cocojs/compiler",
|
|
3
|
+
"version": "0.1.0-beta.202601082201",
|
|
4
|
+
"description": "A Coco component transpiler built on the TypeScript compiler API.",
|
|
5
|
+
"main": "dist/index.cjs.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "jiangchenguang",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/cocojs-org/coconut-framework/issues"
|
|
15
|
+
},
|
|
16
|
+
"homepage": "https://github.com/cocojs-org/coconut-framework",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/cocojs-org/coconut-framework.git"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/jest": "^29.5.14",
|
|
23
|
+
"@types/node": "^22.13.1",
|
|
24
|
+
"jest": "^29.7.0"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"typescript": "5.3.3"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "tsc",
|
|
31
|
+
"jest": "jest"
|
|
32
|
+
}
|
|
33
|
+
}
|