@travetto/transformer 5.0.2 → 5.0.3
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 +3 -3
- package/src/importer.ts +34 -17
- package/src/state.ts +8 -0
- package/src/visitor.ts +11 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/transformer",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.3",
|
|
4
4
|
"description": "Functionality for AST transformations, with transformer registration, and general utils",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
"directory": "module/transformer"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@travetto/manifest": "^5.0.
|
|
27
|
+
"@travetto/manifest": "^5.0.2",
|
|
28
28
|
"tslib": "^2.7.0",
|
|
29
|
-
"typescript": "^5.
|
|
29
|
+
"typescript": "^5.6.2"
|
|
30
30
|
},
|
|
31
31
|
"travetto": {
|
|
32
32
|
"displayName": "Transformation",
|
package/src/importer.ts
CHANGED
|
@@ -29,20 +29,9 @@ export class ImportManager {
|
|
|
29
29
|
this.#importName = this.#resolver.getFileImportName(source.fileName);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
#getImportFile(spec?: ts.Expression): string | undefined {
|
|
33
|
-
if (spec && ts.isStringLiteral(spec)) {
|
|
34
|
-
return spec.text.replace(/^['"]|["']$/g, '');
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
32
|
#rewriteModuleSpecifier(spec: ts.Expression | undefined): ts.Expression | undefined {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
fileOrImport &&
|
|
42
|
-
(fileOrImport.startsWith('.') || this.#resolver.isKnownFile(fileOrImport)) &&
|
|
43
|
-
!/[.]([mc]?js|ts|json)$/.test(fileOrImport)
|
|
44
|
-
) {
|
|
45
|
-
return LiteralUtil.fromLiteral(this.factory, `${fileOrImport}.js`);
|
|
33
|
+
if (spec && ts.isStringLiteral(spec) && this.isUntypedImport(spec)) {
|
|
34
|
+
return LiteralUtil.fromLiteral(this.factory, `${spec.text.replace(/['"]/g, '')}.js`);
|
|
46
35
|
}
|
|
47
36
|
return spec;
|
|
48
37
|
}
|
|
@@ -52,8 +41,7 @@ export class ImportManager {
|
|
|
52
41
|
return clause;
|
|
53
42
|
}
|
|
54
43
|
|
|
55
|
-
|
|
56
|
-
if (!(fileOrImport && (fileOrImport.startsWith('.') || this.#resolver.isKnownFile(fileOrImport)))) {
|
|
44
|
+
if (spec && ts.isStringLiteral(spec) && !this.isKnownImport(spec)) {
|
|
57
45
|
return clause;
|
|
58
46
|
}
|
|
59
47
|
|
|
@@ -83,6 +71,35 @@ export class ImportManager {
|
|
|
83
71
|
}
|
|
84
72
|
}
|
|
85
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Is a known import an untyped file access
|
|
76
|
+
* @param fileOrImport
|
|
77
|
+
*/
|
|
78
|
+
isKnownImport(fileOrImport: ts.StringLiteral | string | undefined): boolean {
|
|
79
|
+
if (fileOrImport && typeof fileOrImport !== 'string') {
|
|
80
|
+
if (ts.isStringLiteral(fileOrImport)) {
|
|
81
|
+
fileOrImport = fileOrImport.text.replace(/['"]g/, '');
|
|
82
|
+
} else {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return fileOrImport ?
|
|
88
|
+
(fileOrImport.startsWith('.') || this.#resolver.isKnownFile(fileOrImport)) :
|
|
89
|
+
false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Is a file or import an untyped file access
|
|
95
|
+
* @param fileOrImport
|
|
96
|
+
*/
|
|
97
|
+
isUntypedImport(fileOrImport: ts.StringLiteral | string | undefined): boolean {
|
|
98
|
+
return this.isKnownImport(fileOrImport) &&
|
|
99
|
+
!/[.](([mc]?[jt]s)|json)['"]?$/
|
|
100
|
+
.test(typeof fileOrImport === 'string' ? fileOrImport : fileOrImport!.text);
|
|
101
|
+
}
|
|
102
|
+
|
|
86
103
|
/**
|
|
87
104
|
* Produces a unique ID for a given file, importing if needed
|
|
88
105
|
*/
|
|
@@ -190,7 +207,7 @@ export class ImportManager {
|
|
|
190
207
|
stmt.isTypeOnly,
|
|
191
208
|
stmt.exportClause,
|
|
192
209
|
this.#rewriteModuleSpecifier(stmt.moduleSpecifier),
|
|
193
|
-
stmt.
|
|
210
|
+
stmt.attributes
|
|
194
211
|
));
|
|
195
212
|
}
|
|
196
213
|
} else if (ts.isImportDeclaration(stmt)) {
|
|
@@ -200,7 +217,7 @@ export class ImportManager {
|
|
|
200
217
|
stmt.modifiers,
|
|
201
218
|
this.#rewriteImportClause(stmt.moduleSpecifier, stmt.importClause)!,
|
|
202
219
|
this.#rewriteModuleSpecifier(stmt.moduleSpecifier)!,
|
|
203
|
-
stmt.
|
|
220
|
+
stmt.attributes
|
|
204
221
|
));
|
|
205
222
|
}
|
|
206
223
|
} else {
|
package/src/state.ts
CHANGED
|
@@ -47,6 +47,14 @@ export class TransformerState implements State {
|
|
|
47
47
|
this.importName = this.#resolver.getFileImportName(this.file, true);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
/**
|
|
51
|
+
* Is a file or an import file path known by the framework, but missing an extensions
|
|
52
|
+
* @param fileOrImport
|
|
53
|
+
*/
|
|
54
|
+
isUntypedImport(fileOrImport: ts.StringLiteral | string | undefined): boolean {
|
|
55
|
+
return this.#imports.isUntypedImport(fileOrImport);
|
|
56
|
+
}
|
|
57
|
+
|
|
50
58
|
/**
|
|
51
59
|
* Get or import the node or external type
|
|
52
60
|
*/
|
package/src/visitor.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
|
|
3
|
-
import { ManifestModuleUtil } from '@travetto/manifest';
|
|
3
|
+
import { ManifestModuleFolderType, ManifestModuleUtil } from '@travetto/manifest';
|
|
4
4
|
|
|
5
5
|
import { DecoratorMeta, TransformerType, NodeTransformer, TransformerSet, State, TransformPhase } from './types/visitor';
|
|
6
6
|
import { CoreUtil } from './util/core';
|
|
7
7
|
|
|
8
|
+
const COMPILER_SRC = new Set<ManifestModuleFolderType>(['support', 'src', '$index']);
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
11
|
* AST Visitor Factory, combines all active transformers into a single pass transformer for the ts compiler
|
|
10
12
|
*/
|
|
@@ -86,6 +88,14 @@ export class VisitorFactory<S extends State = State> {
|
|
|
86
88
|
|
|
87
89
|
try {
|
|
88
90
|
const state = this.#getState(context, file);
|
|
91
|
+
// Skip transforming all the compiler related content
|
|
92
|
+
if (
|
|
93
|
+
/@travetto[/](compiler|manifest|transformer)/.test(state.importName) &&
|
|
94
|
+
COMPILER_SRC.has(ManifestModuleUtil.getFolderKey(state.importName.replace(/@travetto[/][^/]+[/]/, '')))
|
|
95
|
+
) {
|
|
96
|
+
return state.finalize(file);
|
|
97
|
+
}
|
|
98
|
+
|
|
89
99
|
let ret = this.visit(state, context, file);
|
|
90
100
|
|
|
91
101
|
// Process added content
|