@@ -27,37 +27,49 @@ function pathTo(from: string, to: string): string {
27
27
return result ;
28
28
}
29
29
30
- function moduleNameFromBaseName ( moduleFileName : string , baseFileName : string ) : string {
31
- // Remove the extension
32
- moduleFileName = moduleFileName . replace ( EXT_REGEX , '' ) ;
33
-
34
- // Check for node_modules
35
- const nodeModulesIndex = moduleFileName . lastIndexOf ( NODE_MODULES ) ;
36
- if ( nodeModulesIndex >= 0 ) {
37
- return moduleFileName . substr ( nodeModulesIndex + NODE_MODULES . length ) ;
38
- }
39
- if ( moduleFileName . lastIndexOf ( NODE_MODULES_PREFIX , NODE_MODULES_PREFIX . length ) !== - 1 ) {
40
- return moduleFileName . substr ( NODE_MODULES_PREFIX . length ) ;
41
- }
42
-
43
- // Construct a simplified path from the file to the module
44
- return pathTo ( baseFileName , moduleFileName ) ;
30
+ export interface MetadataCollectorHost {
31
+ reverseModuleResolution : ( moduleFileName : string ) => string ;
45
32
}
46
33
34
+ const nodeModuleResolutionHost : MetadataCollectorHost = {
35
+ // Reverse moduleResolution=node for packages resolved in node_modules
36
+ reverseModuleResolution ( fileName : string ) {
37
+ // Remove the extension
38
+ const moduleFileName = fileName . replace ( EXT_REGEX , '' ) ;
39
+ // Check for node_modules
40
+ const nodeModulesIndex = moduleFileName . lastIndexOf ( NODE_MODULES ) ;
41
+ if ( nodeModulesIndex >= 0 ) {
42
+ return moduleFileName . substr ( nodeModulesIndex + NODE_MODULES . length ) ;
43
+ }
44
+ if ( moduleFileName . lastIndexOf ( NODE_MODULES_PREFIX , NODE_MODULES_PREFIX . length ) !== - 1 ) {
45
+ return moduleFileName . substr ( NODE_MODULES_PREFIX . length ) ;
46
+ }
47
+ return null ;
48
+ }
49
+ } ;
50
+
47
51
/**
48
52
* Collect decorator metadata from a TypeScript module.
49
53
*/
50
54
export class MetadataCollector {
51
- constructor ( ) { }
55
+ constructor ( private host : MetadataCollectorHost = nodeModuleResolutionHost ) { }
52
56
53
57
/**
54
58
* Returns a JSON.stringify friendly form describing the decorators of the exported classes from
55
59
* the source file that is expected to correspond to a module.
56
60
*/
57
61
public getMetadata ( sourceFile : ts . SourceFile , typeChecker : ts . TypeChecker ) : ModuleMetadata {
58
62
const locals = new Symbols ( ) ;
59
- const moduleNameOf = ( fileName : string ) =>
60
- moduleNameFromBaseName ( fileName , sourceFile . fileName ) ;
63
+ const moduleNameOf = ( fileName : string ) => {
64
+ // If the module was resolved with TS moduleResolution, reverse that mapping
65
+ const hostResolved = this . host . reverseModuleResolution ( fileName ) ;
66
+ if ( hostResolved ) {
67
+ return hostResolved ;
68
+ }
69
+ // Construct a simplified path from the file to the module
70
+ return pathTo ( sourceFile . fileName , fileName ) . replace ( EXT_REGEX , '' ) ;
71
+ } ;
72
+
61
73
const evaluator = new Evaluator ( typeChecker , locals , moduleNameOf ) ;
62
74
63
75
function objFromDecorator ( decoratorNode : ts . Decorator ) : MetadataSymbolicExpression {
@@ -85,15 +97,13 @@ export class MetadataCollector {
85
97
}
86
98
87
99
function classMetadataOf ( classDeclaration : ts . ClassDeclaration ) : ClassMetadata {
88
- let result : ClassMetadata =
89
- { __symbolic : "class" }
90
-
91
- function getDecorators ( decorators : ts . Decorator [ ] ) :
92
- MetadataSymbolicExpression [ ] {
93
- if ( decorators && decorators . length )
94
- return decorators . map ( decorator => objFromDecorator ( decorator ) ) ;
95
- return undefined ;
96
- }
100
+ let result : ClassMetadata = { __symbolic : "class" } ;
101
+
102
+ function getDecorators ( decorators : ts . Decorator [ ] ) : MetadataSymbolicExpression [ ] {
103
+ if ( decorators && decorators . length )
104
+ return decorators . map ( decorator => objFromDecorator ( decorator ) ) ;
105
+ return undefined ;
106
+ }
97
107
98
108
// Add class decorators
99
109
if ( classDeclaration . decorators ) {
@@ -175,7 +185,7 @@ export class MetadataCollector {
175
185
const classDeclaration = < ts . ClassDeclaration > declaration ;
176
186
if ( classDeclaration . decorators ) {
177
187
if ( ! metadata ) metadata = { } ;
178
- metadata [ classDeclaration . name . text ] = classMetadataOf ( classDeclaration )
188
+ metadata [ classDeclaration . name . text ] = classMetadataOf ( classDeclaration ) ;
179
189
}
180
190
break ;
181
191
case ts . SyntaxKind . VariableDeclaration :
0 commit comments