@cocojs/compiler 0.1.0-beta202601082200
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/README.md +10 -0
- package/dist/index.cjs.js +522 -0
- package/dist/index.d.ts +15 -0
- package/package.json +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,522 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var ts = require('typescript');
|
|
6
|
+
|
|
7
|
+
function _interopNamespaceDefault(e) {
|
|
8
|
+
var n = Object.create(null);
|
|
9
|
+
if (e) {
|
|
10
|
+
Object.keys(e).forEach(function (k) {
|
|
11
|
+
if (k !== 'default') {
|
|
12
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () { return e[k]; }
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return Object.freeze(n);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var ts__namespace = /*#__PURE__*/_interopNamespaceDefault(ts);
|
|
25
|
+
|
|
26
|
+
function _arrayLikeToArray(r, a) {
|
|
27
|
+
(null == a || a > r.length) && (a = r.length);
|
|
28
|
+
for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
|
|
29
|
+
return n;
|
|
30
|
+
}
|
|
31
|
+
function _arrayWithoutHoles(r) {
|
|
32
|
+
if (Array.isArray(r)) return _arrayLikeToArray(r);
|
|
33
|
+
}
|
|
34
|
+
function _createForOfIteratorHelper(r, e) {
|
|
35
|
+
var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
|
|
36
|
+
if (!t) {
|
|
37
|
+
if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e) {
|
|
38
|
+
t && (r = t);
|
|
39
|
+
var n = 0,
|
|
40
|
+
F = function () {};
|
|
41
|
+
return {
|
|
42
|
+
s: F,
|
|
43
|
+
n: function () {
|
|
44
|
+
return n >= r.length ? {
|
|
45
|
+
done: true
|
|
46
|
+
} : {
|
|
47
|
+
done: false,
|
|
48
|
+
value: r[n++]
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
e: function (r) {
|
|
52
|
+
throw r;
|
|
53
|
+
},
|
|
54
|
+
f: F
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
58
|
+
}
|
|
59
|
+
var o,
|
|
60
|
+
a = true,
|
|
61
|
+
u = false;
|
|
62
|
+
return {
|
|
63
|
+
s: function () {
|
|
64
|
+
t = t.call(r);
|
|
65
|
+
},
|
|
66
|
+
n: function () {
|
|
67
|
+
var r = t.next();
|
|
68
|
+
return a = r.done, r;
|
|
69
|
+
},
|
|
70
|
+
e: function (r) {
|
|
71
|
+
u = true, o = r;
|
|
72
|
+
},
|
|
73
|
+
f: function () {
|
|
74
|
+
try {
|
|
75
|
+
a || null == t.return || t.return();
|
|
76
|
+
} finally {
|
|
77
|
+
if (u) throw o;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
function _defineProperty(e, r, t) {
|
|
83
|
+
return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
|
|
84
|
+
value: t,
|
|
85
|
+
enumerable: true,
|
|
86
|
+
configurable: true,
|
|
87
|
+
writable: true
|
|
88
|
+
}) : e[r] = t, e;
|
|
89
|
+
}
|
|
90
|
+
function _iterableToArray(r) {
|
|
91
|
+
if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r);
|
|
92
|
+
}
|
|
93
|
+
function _nonIterableSpread() {
|
|
94
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
95
|
+
}
|
|
96
|
+
function ownKeys(e, r) {
|
|
97
|
+
var t = Object.keys(e);
|
|
98
|
+
if (Object.getOwnPropertySymbols) {
|
|
99
|
+
var o = Object.getOwnPropertySymbols(e);
|
|
100
|
+
r && (o = o.filter(function (r) {
|
|
101
|
+
return Object.getOwnPropertyDescriptor(e, r).enumerable;
|
|
102
|
+
})), t.push.apply(t, o);
|
|
103
|
+
}
|
|
104
|
+
return t;
|
|
105
|
+
}
|
|
106
|
+
function _objectSpread2(e) {
|
|
107
|
+
for (var r = 1; r < arguments.length; r++) {
|
|
108
|
+
var t = null != arguments[r] ? arguments[r] : {};
|
|
109
|
+
r % 2 ? ownKeys(Object(t), true).forEach(function (r) {
|
|
110
|
+
_defineProperty(e, r, t[r]);
|
|
111
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) {
|
|
112
|
+
Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return e;
|
|
116
|
+
}
|
|
117
|
+
function _toConsumableArray(r) {
|
|
118
|
+
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
|
|
119
|
+
}
|
|
120
|
+
function _toPrimitive(t, r) {
|
|
121
|
+
if ("object" != typeof t || !t) return t;
|
|
122
|
+
var e = t[Symbol.toPrimitive];
|
|
123
|
+
if (void 0 !== e) {
|
|
124
|
+
var i = e.call(t, r);
|
|
125
|
+
if ("object" != typeof i) return i;
|
|
126
|
+
throw new TypeError("@@toPrimitive must return a primitive value.");
|
|
127
|
+
}
|
|
128
|
+
return ("string" === r ? String : Number)(t);
|
|
129
|
+
}
|
|
130
|
+
function _toPropertyKey(t) {
|
|
131
|
+
var i = _toPrimitive(t, "string");
|
|
132
|
+
return "symbol" == typeof i ? i : i + "";
|
|
133
|
+
}
|
|
134
|
+
function _unsupportedIterableToArray(r, a) {
|
|
135
|
+
if (r) {
|
|
136
|
+
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
137
|
+
var t = {}.toString.call(r).slice(8, -1);
|
|
138
|
+
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;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
var wrapperClassName = ['String', 'Number', 'Boolean'];
|
|
143
|
+
var innerClassName = ['Object', 'Symbol', 'Array', 'Map', 'Set', 'Function'];
|
|
144
|
+
// {}
|
|
145
|
+
function isObjectType(type) {
|
|
146
|
+
return !!type && ts__namespace.isTypeLiteralNode(type);
|
|
147
|
+
}
|
|
148
|
+
// []
|
|
149
|
+
function isTupleArray(type) {
|
|
150
|
+
return !!type && ts__namespace.isTupleTypeNode(type);
|
|
151
|
+
}
|
|
152
|
+
// () => {}
|
|
153
|
+
function isFunctionType(type) {
|
|
154
|
+
return !!type && ts__namespace.isFunctionTypeNode(type);
|
|
155
|
+
}
|
|
156
|
+
function isDecoratorExp(decorator, decoratorName) {
|
|
157
|
+
var expr = decorator.expression;
|
|
158
|
+
return ts__namespace.isCallExpression(expr) && ts__namespace.isIdentifier(expr.expression) && expr.expression.text === decoratorName;
|
|
159
|
+
}
|
|
160
|
+
function hasClassKindDecorator(classDeclaration) {
|
|
161
|
+
var _classDeclaration$mod;
|
|
162
|
+
return !!((_classDeclaration$mod = classDeclaration.modifiers) !== null && _classDeclaration$mod !== void 0 && _classDeclaration$mod.find(function (modifier) {
|
|
163
|
+
return ts__namespace.isDecorator(modifier);
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
function extractIdentifierFromType(type) {
|
|
167
|
+
if (ts__namespace.isTypeReferenceNode(type)) {
|
|
168
|
+
if (ts__namespace.isIdentifier(type.typeName)) {
|
|
169
|
+
if (wrapperClassName.includes(type.typeName.text) || innerClassName.includes(type.typeName.text)) {
|
|
170
|
+
return ts__namespace.factory.createIdentifier('undefined');
|
|
171
|
+
}
|
|
172
|
+
return ts__namespace.factory.createIdentifier(type.typeName.text);
|
|
173
|
+
}
|
|
174
|
+
} 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) {
|
|
175
|
+
return ts__namespace.factory.createIdentifier('undefined');
|
|
176
|
+
} else if (isObjectType(type) || isTupleArray(type) || isFunctionType(type)) {
|
|
177
|
+
return ts__namespace.factory.createIdentifier('undefined');
|
|
178
|
+
}
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
function createImport(className, importPath) {
|
|
182
|
+
return ts__namespace.factory.createImportDeclaration(undefined,
|
|
183
|
+
// modifiers
|
|
184
|
+
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),
|
|
185
|
+
// 路径
|
|
186
|
+
undefined);
|
|
187
|
+
}
|
|
188
|
+
function updateTypeImports(sourceFile) {
|
|
189
|
+
var identifyList = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
190
|
+
if (!identifyList.length) {
|
|
191
|
+
return sourceFile;
|
|
192
|
+
}
|
|
193
|
+
var importNames = new Set();
|
|
194
|
+
identifyList.forEach(function (iden) {
|
|
195
|
+
return importNames.add(iden.text);
|
|
196
|
+
});
|
|
197
|
+
var newStatements = [];
|
|
198
|
+
var _iterator = _createForOfIteratorHelper(sourceFile.statements),
|
|
199
|
+
_step;
|
|
200
|
+
try {
|
|
201
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
202
|
+
var stmt = _step.value;
|
|
203
|
+
if (importNames.size === 0) {
|
|
204
|
+
newStatements.push(stmt);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
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 findImport = stmt.importClause.namedBindings.elements.find(function (e) {
|
|
211
|
+
return importNames.has(e.name.text);
|
|
212
|
+
});
|
|
213
|
+
if (findImport) {
|
|
214
|
+
var name = findImport.name.text;
|
|
215
|
+
var importPath = stmt.moduleSpecifier.text;
|
|
216
|
+
newStatements.push(createImport('Render', 'coco-render'));
|
|
217
|
+
importNames["delete"](name);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
newStatements.push(stmt);
|
|
222
|
+
}
|
|
223
|
+
} catch (err) {
|
|
224
|
+
_iterator.e(err);
|
|
225
|
+
} finally {
|
|
226
|
+
_iterator.f();
|
|
227
|
+
}
|
|
228
|
+
return ts__namespace.factory.updateSourceFile(sourceFile, newStatements);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function isConstructParamsDecorator(decorator) {
|
|
232
|
+
return isDecoratorExp(decorator, 'constructorParam');
|
|
233
|
+
}
|
|
234
|
+
function extractIdentifiersFromConstructor(ctor) {
|
|
235
|
+
var identifiers = [];
|
|
236
|
+
var _iterator = _createForOfIteratorHelper(ctor.parameters),
|
|
237
|
+
_step;
|
|
238
|
+
try {
|
|
239
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
240
|
+
var param = _step.value;
|
|
241
|
+
if (!param.type) {
|
|
242
|
+
return [];
|
|
243
|
+
}
|
|
244
|
+
var id = extractIdentifierFromType(param.type);
|
|
245
|
+
if (!id) {
|
|
246
|
+
return [];
|
|
247
|
+
}
|
|
248
|
+
identifiers.push(id);
|
|
249
|
+
}
|
|
250
|
+
} catch (err) {
|
|
251
|
+
_iterator.e(err);
|
|
252
|
+
} finally {
|
|
253
|
+
_iterator.f();
|
|
254
|
+
}
|
|
255
|
+
return identifiers;
|
|
256
|
+
}
|
|
257
|
+
function updateConstructorParamDecorator(classDeclaration) {
|
|
258
|
+
var modifiers = classDeclaration.modifiers;
|
|
259
|
+
if (!modifiers) {
|
|
260
|
+
return {
|
|
261
|
+
updated: false,
|
|
262
|
+
modifiers: classDeclaration.modifiers
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
var decorators = modifiers.filter(ts__namespace.isDecorator);
|
|
266
|
+
if (!decorators.length) {
|
|
267
|
+
return {
|
|
268
|
+
updated: false,
|
|
269
|
+
modifiers: classDeclaration.modifiers
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
var constructParams = decorators.find(isConstructParamsDecorator);
|
|
273
|
+
if (!constructParams) {
|
|
274
|
+
return {
|
|
275
|
+
updated: false,
|
|
276
|
+
modifiers: classDeclaration.modifiers
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
var call = constructParams.expression;
|
|
280
|
+
// 已有参数,不处理
|
|
281
|
+
if (call.arguments.length > 0) {
|
|
282
|
+
return {
|
|
283
|
+
updated: false,
|
|
284
|
+
modifiers: classDeclaration.modifiers
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
// 找 constructor
|
|
288
|
+
var ctor = classDeclaration.members.find(function (m) {
|
|
289
|
+
return ts__namespace.isConstructorDeclaration(m);
|
|
290
|
+
});
|
|
291
|
+
if (!ctor) {
|
|
292
|
+
return {
|
|
293
|
+
updated: false,
|
|
294
|
+
modifiers: classDeclaration.modifiers
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
var identifiers = extractIdentifiersFromConstructor(ctor);
|
|
298
|
+
if (!identifiers.length) {
|
|
299
|
+
return {
|
|
300
|
+
updated: false,
|
|
301
|
+
modifiers: classDeclaration.modifiers
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
// 构造数组:[Api, User]
|
|
305
|
+
var arrayLiteral = ts__namespace.factory.createArrayLiteralExpression(identifiers, false);
|
|
306
|
+
var newModifiers = modifiers.map(function (m) {
|
|
307
|
+
return ts__namespace.isDecorator(m) && m === constructParams ? ts__namespace.factory.createDecorator(ts__namespace.factory.updateCallExpression(call, call.expression, call.typeArguments, [arrayLiteral])) : m;
|
|
308
|
+
});
|
|
309
|
+
return {
|
|
310
|
+
updated: true,
|
|
311
|
+
modifiers: newModifiers,
|
|
312
|
+
constructorParamTypeList: identifiers
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function isAutowiredDecorator(decorator) {
|
|
317
|
+
return isDecoratorExp(decorator, 'autowired');
|
|
318
|
+
}
|
|
319
|
+
function updateAutowiredDecorator(member, markUpdate) {
|
|
320
|
+
if (!ts__namespace.isPropertyDeclaration(member)) {
|
|
321
|
+
return member;
|
|
322
|
+
}
|
|
323
|
+
var modifiers = member.modifiers;
|
|
324
|
+
if (!modifiers) {
|
|
325
|
+
return member;
|
|
326
|
+
}
|
|
327
|
+
var decorators = modifiers.filter(ts__namespace.isDecorator);
|
|
328
|
+
if (!decorators.length) {
|
|
329
|
+
return member;
|
|
330
|
+
}
|
|
331
|
+
var autowired = decorators.find(isAutowiredDecorator);
|
|
332
|
+
if (!autowired) {
|
|
333
|
+
return member;
|
|
334
|
+
}
|
|
335
|
+
var call = autowired.expression;
|
|
336
|
+
// 已经有参数,不处理
|
|
337
|
+
if (call.arguments.length > 0) {
|
|
338
|
+
return member;
|
|
339
|
+
}
|
|
340
|
+
if (!member.type) {
|
|
341
|
+
return member;
|
|
342
|
+
}
|
|
343
|
+
var identifier = extractIdentifierFromType(member.type);
|
|
344
|
+
if (!identifier) {
|
|
345
|
+
return member;
|
|
346
|
+
}
|
|
347
|
+
markUpdate();
|
|
348
|
+
var newModifiers = modifiers.map(function (m) {
|
|
349
|
+
return ts__namespace.isDecorator(m) && m === autowired ? ts__namespace.factory.createDecorator(ts__namespace.factory.updateCallExpression(call, call.expression, call.typeArguments, [identifier])) : m;
|
|
350
|
+
});
|
|
351
|
+
return ts__namespace.factory.updatePropertyDeclaration(member, newModifiers, member.name, ts__namespace.factory.createToken(ts__namespace.SyntaxKind.ExclamationToken), member.type, member.initializer);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function isComponentDecorator(decorator) {
|
|
355
|
+
return isDecoratorExp(decorator, 'component');
|
|
356
|
+
}
|
|
357
|
+
function updateComponentDecorator(member, markUpdate) {
|
|
358
|
+
if (!ts__namespace.isMethodDeclaration(member)) {
|
|
359
|
+
return member;
|
|
360
|
+
}
|
|
361
|
+
var modifiers = member.modifiers;
|
|
362
|
+
if (!modifiers) {
|
|
363
|
+
return member;
|
|
364
|
+
}
|
|
365
|
+
var decorators = modifiers.filter(ts__namespace.isDecorator);
|
|
366
|
+
if (!decorators.length) {
|
|
367
|
+
return member;
|
|
368
|
+
}
|
|
369
|
+
var component = decorators.find(isComponentDecorator);
|
|
370
|
+
if (!component) {
|
|
371
|
+
return member;
|
|
372
|
+
}
|
|
373
|
+
var call = component.expression;
|
|
374
|
+
// 已有参数,不处理
|
|
375
|
+
if (call.arguments.length > 0) {
|
|
376
|
+
return member;
|
|
377
|
+
}
|
|
378
|
+
// 没有返回类型,无法补全
|
|
379
|
+
if (!member.type) {
|
|
380
|
+
return member;
|
|
381
|
+
}
|
|
382
|
+
var identifier = extractIdentifierFromType(member.type);
|
|
383
|
+
if (!identifier) {
|
|
384
|
+
return member;
|
|
385
|
+
}
|
|
386
|
+
markUpdate();
|
|
387
|
+
var newModifiers = modifiers.map(function (m) {
|
|
388
|
+
return ts__namespace.isDecorator(m) && m === component ? ts__namespace.factory.createDecorator(ts__namespace.factory.updateCallExpression(call, call.expression, call.typeArguments, [identifier])) : m;
|
|
389
|
+
});
|
|
390
|
+
return ts__namespace.factory.updateMethodDeclaration(member, newModifiers, member.asteriskToken, member.name, member.questionToken, member.typeParameters, member.parameters, member.type, member.body);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
var innerIdName = '$$id';
|
|
394
|
+
function ifNeedAdd$$idProperty(classDecorator, prefix) {
|
|
395
|
+
var className = classDecorator.name.text;
|
|
396
|
+
var $$idProperties = classDecorator.members.filter(function (member) {
|
|
397
|
+
var _member$modifiers;
|
|
398
|
+
return ts__namespace.isPropertyDeclaration(member) && ((_member$modifiers = member.modifiers) === null || _member$modifiers === void 0 ? void 0 : _member$modifiers.some(function (modify) {
|
|
399
|
+
return modify.kind === ts__namespace.SyntaxKind.StaticKeyword;
|
|
400
|
+
})) && ts__namespace.isIdentifier(member.name) && member.name.text === '$$id';
|
|
401
|
+
});
|
|
402
|
+
if ($$idProperties.length === 0) {
|
|
403
|
+
return ts__namespace.factory.createPropertyDeclaration([ts__namespace.factory.createModifier(ts__namespace.SyntaxKind.StaticKeyword)], innerIdName, undefined, undefined, ts__namespace.factory.createStringLiteral("".concat(prefix).concat(className)));
|
|
404
|
+
} else if ($$idProperties.length === 1) {
|
|
405
|
+
var property = $$idProperties[0];
|
|
406
|
+
if (property.initializer && ts__namespace.isStringLiteral(property.initializer)) {
|
|
407
|
+
if (!property.initializer.text.trim()) {
|
|
408
|
+
throw new Error("\u60F3\u8981\u4E3A\u7C7B".concat(className, "\u81EA\u5B9A\u4E49\"").concat(innerIdName, "\"\uFF0C\u503C\u4E0D\u80FD\u662F\u7A7A\u5B57\u7B26\u4E32"));
|
|
409
|
+
} else {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
} else {
|
|
413
|
+
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"));
|
|
414
|
+
}
|
|
415
|
+
} else {
|
|
416
|
+
throw new Error("\u7C7B".concat(className, "\u5B58\u5728\u591A\u4E2A\"").concat(innerIdName, "\"\uFF0C\u6700\u591A\u53EA\u80FD\u6709\u4E00\u4E2A\uFF01"));
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function updateMembers(classDeclaration) {
|
|
421
|
+
var updated = false;
|
|
422
|
+
var markUpdate = function markUpdate() {
|
|
423
|
+
updated = true;
|
|
424
|
+
};
|
|
425
|
+
var members = classDeclaration.members.map(function (member) {
|
|
426
|
+
if (ts__namespace.isPropertyDeclaration(member)) {
|
|
427
|
+
return updateAutowiredDecorator(member, markUpdate);
|
|
428
|
+
} else if (ts__namespace.isMethodDeclaration(member)) {
|
|
429
|
+
return updateComponentDecorator(member, markUpdate);
|
|
430
|
+
} else {
|
|
431
|
+
return member;
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
return {
|
|
435
|
+
updated: updated,
|
|
436
|
+
members: updated ? members : classDeclaration.members
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
function transformerFactory() {
|
|
440
|
+
var prefix = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
|
|
441
|
+
return function (context) {
|
|
442
|
+
var constructorParamTypeList;
|
|
443
|
+
var _visit = function visit(node) {
|
|
444
|
+
if (ts__namespace.isClassDeclaration(node)) {
|
|
445
|
+
if (hasClassKindDecorator(node)) {
|
|
446
|
+
var $$idProperty = ifNeedAdd$$idProperty(node, prefix);
|
|
447
|
+
var _updateMembers = updateMembers(node),
|
|
448
|
+
members = _updateMembers.members,
|
|
449
|
+
membersUpdated = _updateMembers.updated;
|
|
450
|
+
var _updateConstructorPar = updateConstructorParamDecorator(node),
|
|
451
|
+
modifiers = _updateConstructorPar.modifiers,
|
|
452
|
+
constructorParamsUpdated = _updateConstructorPar.updated,
|
|
453
|
+
_constructorParamTypeList = _updateConstructorPar.constructorParamTypeList;
|
|
454
|
+
if (!membersUpdated && !constructorParamsUpdated && !$$idProperty) {
|
|
455
|
+
return node;
|
|
456
|
+
} else {
|
|
457
|
+
if (constructorParamsUpdated) {
|
|
458
|
+
constructorParamTypeList = _constructorParamTypeList;
|
|
459
|
+
}
|
|
460
|
+
return ts__namespace.factory.updateClassDeclaration(node, modifiers, node.name, node.typeParameters, node.heritageClauses, $$idProperty ? [$$idProperty].concat(_toConsumableArray(members)) : members);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return node;
|
|
464
|
+
} else {
|
|
465
|
+
return ts__namespace.visitEachChild(node, _visit, context);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
// @ts-ignore
|
|
469
|
+
return function (sourceFile) {
|
|
470
|
+
var _constructorParamType;
|
|
471
|
+
var updatedSourceFile = ts__namespace.visitNode(sourceFile, _visit);
|
|
472
|
+
if (!((_constructorParamType = constructorParamTypeList) !== null && _constructorParamType !== void 0 && _constructorParamType.length)) {
|
|
473
|
+
return updatedSourceFile;
|
|
474
|
+
}
|
|
475
|
+
// TODO: @autowired和@component应该也要导入的。
|
|
476
|
+
return updateTypeImports(updatedSourceFile, constructorParamTypeList);
|
|
477
|
+
};
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
var commonCompilerOptions = {
|
|
482
|
+
target: ts__namespace.ScriptTarget.ESNext,
|
|
483
|
+
module: ts__namespace.ModuleKind.ESNext,
|
|
484
|
+
emitDecoratorMetadata: false
|
|
485
|
+
};
|
|
486
|
+
function compiler(files, outDir) {
|
|
487
|
+
var tsOptions = _objectSpread2(_objectSpread2({}, commonCompilerOptions), {}, {
|
|
488
|
+
outDir: outDir
|
|
489
|
+
});
|
|
490
|
+
var host = ts__namespace.createCompilerHost(tsOptions);
|
|
491
|
+
var program = ts__namespace.createProgram({
|
|
492
|
+
rootNames: files,
|
|
493
|
+
options: tsOptions,
|
|
494
|
+
host: host
|
|
495
|
+
});
|
|
496
|
+
var emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
497
|
+
before: [transformerFactory()]
|
|
498
|
+
});
|
|
499
|
+
var diagnostics = ts__namespace.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
|
|
500
|
+
return diagnostics;
|
|
501
|
+
}
|
|
502
|
+
function compileOneFile(code, fileName) {
|
|
503
|
+
var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
|
|
504
|
+
var result = ts__namespace.transpileModule(code, {
|
|
505
|
+
fileName: fileName,
|
|
506
|
+
compilerOptions: _objectSpread2(_objectSpread2({}, commonCompilerOptions), {}, {
|
|
507
|
+
sourceMap: true
|
|
508
|
+
}),
|
|
509
|
+
transformers: {
|
|
510
|
+
before: [transformerFactory(prefix)]
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
return {
|
|
514
|
+
code: result.outputText,
|
|
515
|
+
map: result.sourceMapText
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
exports.commonCompilerOptions = commonCompilerOptions;
|
|
520
|
+
exports.compileOneFile = compileOneFile;
|
|
521
|
+
exports.default = compiler;
|
|
522
|
+
exports.transformerFactory = transformerFactory;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
declare function compiler(files: string[], outDir: string): ts.Diagnostic[];
|
|
11
|
+
export default compiler;
|
|
12
|
+
|
|
13
|
+
export declare function transformerFactory(prefix?: string): ts.TransformerFactory<ts.SourceFile>;
|
|
14
|
+
|
|
15
|
+
export { }
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cocojs/compiler",
|
|
3
|
+
"version": "0.1.0-beta202601082200",
|
|
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
|
+
"scripts": {
|
|
11
|
+
"build": "tsc",
|
|
12
|
+
"jest": "jest"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [],
|
|
15
|
+
"author": "jiangchenguang",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/cocojs-org/coconut-framework/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://github.com/cocojs-org/coconut-framework",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/cocojs-org/coconut-framework.git"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/jest": "^29.5.14",
|
|
27
|
+
"@types/node": "^22.13.1",
|
|
28
|
+
"jest": "^29.7.0",
|
|
29
|
+
"typescript": "5.3.3"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"typescript": "5.3.3"
|
|
33
|
+
}
|
|
34
|
+
}
|