@travetto/transformer 2.2.4 → 3.0.0-rc.2
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/package.json +2 -2
- package/src/state.ts +2 -2
- package/src/util/decorator.ts +6 -3
- package/src/visitor.ts +11 -6
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/transformer",
|
|
3
3
|
"displayName": "Transformation",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0-rc.2",
|
|
5
5
|
"description": "Functionality for AST transformations, with transformer registration, and general utils",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"typescript",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"directory": "module/transformer"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@travetto/base": "^
|
|
28
|
+
"@travetto/base": "^3.0.0-rc.0"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
package/src/state.ts
CHANGED
|
@@ -164,9 +164,9 @@ export class TransformerState implements State {
|
|
|
164
164
|
* Get list of all #decorators for a node
|
|
165
165
|
*/
|
|
166
166
|
getDecoratorList(node: ts.Node): DecoratorMeta[] {
|
|
167
|
-
return (node.
|
|
167
|
+
return ts.canHaveDecorators(node) ? (ts.getDecorators(node) ?? [])
|
|
168
168
|
.map(dec => this.getDecoratorMeta(dec))
|
|
169
|
-
.filter(x => !!x.ident);
|
|
169
|
+
.filter(x => !!x.ident) : [];
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
/**
|
package/src/util/decorator.ts
CHANGED
|
@@ -24,11 +24,14 @@ export class DecoratorUtil {
|
|
|
24
24
|
/**
|
|
25
25
|
* Replace or add a decorator to a list of decorators
|
|
26
26
|
*/
|
|
27
|
-
static spliceDecorators(node: ts.Node, target: ts.Decorator | undefined, replacements: ts.Decorator[], idx = -1): ts.
|
|
27
|
+
static spliceDecorators(node: ts.Node, target: ts.Decorator | undefined, replacements: ts.Decorator[], idx = -1): ts.ModifierLike[] {
|
|
28
|
+
if (!ts.canHaveDecorators(node)) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
28
31
|
if (idx < 0 && target) {
|
|
29
|
-
idx = node.
|
|
32
|
+
idx = node.modifiers?.indexOf(target) ?? -1;
|
|
30
33
|
}
|
|
31
|
-
const out = (node.
|
|
34
|
+
const out = (node.modifiers ?? []).filter(x => x !== target);
|
|
32
35
|
if (idx < 0) {
|
|
33
36
|
out.push(...replacements);
|
|
34
37
|
} else {
|
package/src/visitor.ts
CHANGED
|
@@ -40,6 +40,7 @@ export class VisitorFactory<S extends State = State> {
|
|
|
40
40
|
#transformers = new Map<TransformerType, TransformerSet<S>>();
|
|
41
41
|
#logTarget: string;
|
|
42
42
|
#getState: (context: ts.TransformationContext, src: ts.SourceFile) => S;
|
|
43
|
+
#logger: Console | undefined;
|
|
43
44
|
|
|
44
45
|
constructor(
|
|
45
46
|
getState: (context: ts.TransformationContext, src: ts.SourceFile) => S,
|
|
@@ -78,17 +79,21 @@ export class VisitorFactory<S extends State = State> {
|
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
|
|
82
|
+
get logger(): Console {
|
|
83
|
+
this.#logger ??= new console.Console({
|
|
84
|
+
stdout: createWriteStream(AppCache.toEntryName(this.#logTarget), { flags: 'a' }),
|
|
85
|
+
inspectOptions: { depth: 4 },
|
|
86
|
+
});
|
|
87
|
+
return this.#logger;
|
|
88
|
+
}
|
|
89
|
+
|
|
81
90
|
/**
|
|
82
91
|
* Produce a visitor for a given a file
|
|
83
92
|
*/
|
|
84
93
|
visitor(): ts.TransformerFactory<ts.SourceFile> {
|
|
85
94
|
return (context: ts.TransformationContext) => (file: ts.SourceFile): ts.SourceFile => {
|
|
86
95
|
try {
|
|
87
|
-
const c =
|
|
88
|
-
stdout: createWriteStream(AppCache.toEntryName(this.#logTarget), { flags: 'a' }),
|
|
89
|
-
inspectOptions: { depth: 4 },
|
|
90
|
-
});
|
|
91
|
-
|
|
96
|
+
const c = this.logger;
|
|
92
97
|
ConsoleManager.set({
|
|
93
98
|
onLog: (level, ctx, args) => c[level](level, ctx, ...LogUtil.collapseNodes(args))
|
|
94
99
|
});
|
|
@@ -120,7 +125,7 @@ export class VisitorFactory<S extends State = State> {
|
|
|
120
125
|
if (!(err instanceof Error)) {
|
|
121
126
|
throw err;
|
|
122
127
|
}
|
|
123
|
-
console.error('Failed transforming', { error: err
|
|
128
|
+
console.error('Failed transforming', { error: `${err.message}\n${err.stack}`, file: file.fileName });
|
|
124
129
|
const out = new Error(`Failed transforming: ${file.fileName}: ${err.message}`);
|
|
125
130
|
out.stack = err.stack;
|
|
126
131
|
throw out;
|