@cocojs/typescript-transformer-autowired 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/dist/index.js +54 -0
  3. package/package.json +15 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 coco-core
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/dist/index.js ADDED
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var ts = require("typescript");
4
+ function transformer(program) {
5
+ return function (context) {
6
+ return function (sourceFile) {
7
+ function visit(node) {
8
+ // 检查是否是属性声明节点
9
+ if (ts.isPropertyDeclaration(node)) {
10
+ // 遍历修饰符(包括装饰器)
11
+ const decorators = (node.modifiers || []).map(modifier => {
12
+ // 检查是否是装饰器
13
+ if (ts.isDecorator(modifier)) {
14
+ const decoratorExpression = modifier.expression;
15
+ // 检查是否是装饰器调用(如 @a())
16
+ if (ts.isCallExpression(decoratorExpression) &&
17
+ ts.isIdentifier(decoratorExpression.expression) &&
18
+ decoratorExpression.expression.text === 'autowired') {
19
+ // 获取属性的类型
20
+ const type = node.type ? node.type.getText(sourceFile) : 'unknown';
21
+ // 创建类型字符串字面量节点
22
+ const typeNode = ts.factory.createIdentifier(type);
23
+ // 修改装饰器调用,将类型作为参数传递
24
+ return ts.factory.updateDecorator(
25
+ modifier,
26
+ ts.factory.updateCallExpression(
27
+ decoratorExpression,
28
+ decoratorExpression.expression,
29
+ undefined,
30
+ [typeNode]
31
+ )
32
+ );
33
+ }
34
+ }
35
+ return modifier;
36
+ });
37
+
38
+ // 更新属性声明节点
39
+ return ts.factory.updatePropertyDeclaration(
40
+ node,
41
+ decorators,
42
+ node.name,
43
+ node.questionToken,
44
+ node.type,
45
+ node.initializer
46
+ );
47
+ }
48
+ return ts.visitEachChild(node, visit, context);
49
+ }
50
+ return ts.visitNode(sourceFile, visit);
51
+ };
52
+ };
53
+ }
54
+ exports.default = transformer;
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@cocojs/typescript-transformer-autowired",
3
+ "version": "0.0.1",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "scripts": {
10
+ "test": "echo \"Error: no test specified\" && exit 1"
11
+ },
12
+ "keywords": [],
13
+ "author": "",
14
+ "license": "ISC"
15
+ }