@@ -3,7 +3,6 @@ import { Indexer } from 'indexer/Indexer';
3
3
import { IndexerKey } from 'types/indexer' ;
4
4
import FileSystem from 'util/FileSystem' ;
5
5
import {
6
- AssignmentProperty ,
7
6
Identifier ,
8
7
Literal ,
9
8
Node ,
@@ -40,7 +39,7 @@ export default class RequireJsIndexer extends Indexer<RequireJsConfig> {
40
39
const ast = parse ( js , { ecmaVersion : 2020 } ) ;
41
40
42
41
// Find config identifier
43
- const configVariableDeclarator = await this . findConfigVariableDeclarator ( ast ) ;
42
+ const configVariableDeclarator = this . findConfigVariableDeclarator ( ast ) ;
44
43
45
44
if ( ! configVariableDeclarator ) {
46
45
return undefined ;
@@ -51,14 +50,14 @@ export default class RequireJsIndexer extends Indexer<RequireJsConfig> {
51
50
paths : { } ,
52
51
} ;
53
52
54
- await this . processMaps ( config , configVariableDeclarator ) ;
55
- await this . processPaths ( config , configVariableDeclarator ) ;
53
+ this . processMaps ( config , configVariableDeclarator ) ;
54
+ this . processPaths ( config , configVariableDeclarator ) ;
56
55
57
56
return config ;
58
57
}
59
58
60
- private async processMaps ( config : RequireJsConfig , configVariableDeclarator : VariableDeclarator ) {
61
- const mapProperty = await this . findProperty ( configVariableDeclarator , 'map' ) ;
59
+ private processMaps ( config : RequireJsConfig , configVariableDeclarator : VariableDeclarator ) {
60
+ const mapProperty = this . findProperty ( configVariableDeclarator , 'map' ) ;
62
61
63
62
if ( ! mapProperty ) {
64
63
return config ;
@@ -75,11 +74,8 @@ export default class RequireJsIndexer extends Indexer<RequireJsConfig> {
75
74
}
76
75
}
77
76
78
- private async processPaths (
79
- config : RequireJsConfig ,
80
- configVariableDeclarator : VariableDeclarator
81
- ) {
82
- const pathsProperty = await this . findProperty ( configVariableDeclarator , 'paths' ) ;
77
+ private processPaths ( config : RequireJsConfig , configVariableDeclarator : VariableDeclarator ) {
78
+ const pathsProperty = this . findProperty ( configVariableDeclarator , 'paths' ) ;
83
79
84
80
if ( ! pathsProperty ) {
85
81
return config ;
@@ -118,38 +114,41 @@ export default class RequireJsIndexer extends Indexer<RequireJsConfig> {
118
114
return mappings ;
119
115
}
120
116
121
- private findConfigVariableDeclarator ( ast : Node ) : Promise < VariableDeclarator | undefined > {
122
- return new Promise ( ( resolve , reject ) => {
123
- walk . simple ( ast , {
124
- VariableDeclaration ( node : VariableDeclaration ) {
125
- node . declarations . forEach ( declarator => {
126
- if ( declarator . id . type === 'Identifier' && declarator . id . name === 'config' ) {
127
- resolve ( declarator ) ;
128
- }
129
- } ) ;
130
- } ,
131
- } ) ;
132
-
133
- resolve ( undefined ) ;
117
+ private findConfigVariableDeclarator ( ast : Node ) : VariableDeclarator | undefined {
118
+ const found = walk . findNodeAfter ( ast , 0 , ( type : string , node : Node ) => {
119
+ if ( type !== 'VariableDeclaration' ) {
120
+ return false ;
121
+ }
122
+
123
+ const variableDeclaration = node as VariableDeclaration ;
124
+
125
+ for ( const declarator of variableDeclaration . declarations ) {
126
+ if ( declarator . id . type === 'Identifier' && declarator . id . name === 'config' ) {
127
+ return true ;
128
+ }
129
+ }
130
+
131
+ return false ;
134
132
} ) ;
133
+
134
+ return found ?. node as VariableDeclarator ;
135
135
}
136
136
137
- private findProperty (
138
- variableDeclarator : VariableDeclarator ,
139
- key : string
140
- ) : Promise < Property | AssignmentProperty | undefined > {
141
- return new Promise ( resolve => {
142
- const objectExpression = variableDeclarator . init as ObjectExpression ;
143
-
144
- walk . simple ( objectExpression , {
145
- Property ( node : Property | AssignmentProperty ) {
146
- if ( node . key . type === 'Identifier' && node . key . name === key ) {
147
- resolve ( node ) ;
148
- }
149
- } ,
150
- } ) ;
151
-
152
- resolve ( undefined ) ;
137
+ private findProperty ( variableDeclarator : VariableDeclarator , key : string ) : Property | undefined {
138
+ const found = walk . findNodeAfter ( variableDeclarator , 0 , ( type : string , node : Node ) => {
139
+ if ( type !== 'Property' ) {
140
+ return false ;
141
+ }
142
+
143
+ const property = node as Property ;
144
+
145
+ if ( property . key . type === 'Identifier' && property . key . name === key ) {
146
+ return true ;
147
+ }
148
+
149
+ return false ;
153
150
} ) ;
151
+
152
+ return found ?. node as Property ;
154
153
}
155
154
}
0 commit comments