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