Skip to content

Commit 6abc2cf

Browse files
committed
feat: using node finder instead of walking the whole tree
1 parent 0be3047 commit 6abc2cf

File tree

1 file changed

+39
-40
lines changed

1 file changed

+39
-40
lines changed

src/indexer/require-js/RequireJsIndexer.ts

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Indexer } from 'indexer/Indexer';
33
import { IndexerKey } from 'types/indexer';
44
import FileSystem from 'util/FileSystem';
55
import {
6-
AssignmentProperty,
76
Identifier,
87
Literal,
98
Node,
@@ -40,7 +39,7 @@ export default class RequireJsIndexer extends Indexer<RequireJsConfig> {
4039
const ast = parse(js, { ecmaVersion: 2020 });
4140

4241
// Find config identifier
43-
const configVariableDeclarator = await this.findConfigVariableDeclarator(ast);
42+
const configVariableDeclarator = this.findConfigVariableDeclarator(ast);
4443

4544
if (!configVariableDeclarator) {
4645
return undefined;
@@ -51,14 +50,14 @@ export default class RequireJsIndexer extends Indexer<RequireJsConfig> {
5150
paths: {},
5251
};
5352

54-
await this.processMaps(config, configVariableDeclarator);
55-
await this.processPaths(config, configVariableDeclarator);
53+
this.processMaps(config, configVariableDeclarator);
54+
this.processPaths(config, configVariableDeclarator);
5655

5756
return config;
5857
}
5958

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');
6261

6362
if (!mapProperty) {
6463
return config;
@@ -75,11 +74,8 @@ export default class RequireJsIndexer extends Indexer<RequireJsConfig> {
7574
}
7675
}
7776

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');
8379

8480
if (!pathsProperty) {
8581
return config;
@@ -118,38 +114,41 @@ export default class RequireJsIndexer extends Indexer<RequireJsConfig> {
118114
return mappings;
119115
}
120116

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;
134132
});
133+
134+
return found?.node as VariableDeclarator;
135135
}
136136

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;
153150
});
151+
152+
return found?.node as Property;
154153
}
155154
}

0 commit comments

Comments
 (0)