|
| 1 | +import { RelativePattern, Uri } from 'vscode'; |
| 2 | +import { Indexer } from 'indexer/Indexer'; |
| 3 | +import { IndexerKey } from 'types/indexer'; |
| 4 | +import { Map } from './types'; |
| 5 | +import FileSystem from 'util/FileSystem'; |
| 6 | +import { |
| 7 | + AssignmentProperty, |
| 8 | + Identifier, |
| 9 | + Literal, |
| 10 | + Node, |
| 11 | + ObjectExpression, |
| 12 | + Property, |
| 13 | + VariableDeclaration, |
| 14 | + VariableDeclarator, |
| 15 | + parse, |
| 16 | +} from 'acorn'; |
| 17 | +import * as walk from 'acorn-walk'; |
| 18 | +import { RequireJsConfig } from './types'; |
| 19 | + |
| 20 | +export default class RequireJsIndexer extends Indexer<RequireJsConfig> { |
| 21 | + public static readonly KEY = 'require-js'; |
| 22 | + |
| 23 | + public getVersion(): number { |
| 24 | + return 1; |
| 25 | + } |
| 26 | + |
| 27 | + public getId(): IndexerKey { |
| 28 | + return RequireJsIndexer.KEY; |
| 29 | + } |
| 30 | + |
| 31 | + public getName(): string { |
| 32 | + return 'requirejs-config.js'; |
| 33 | + } |
| 34 | + |
| 35 | + public getPattern(uri: Uri): RelativePattern { |
| 36 | + return new RelativePattern(uri, '**/view/**/requirejs-config.js'); |
| 37 | + } |
| 38 | + |
| 39 | + public async indexFile(uri: Uri): Promise<RequireJsConfig | undefined> { |
| 40 | + const js = await FileSystem.readFile(uri); |
| 41 | + const ast = parse(js, { ecmaVersion: 2020 }); |
| 42 | + |
| 43 | + // Find config identifier |
| 44 | + const configVariableDeclarator = await this.findConfigVariableDeclarator(ast); |
| 45 | + |
| 46 | + if (!configVariableDeclarator) { |
| 47 | + return undefined; |
| 48 | + } |
| 49 | + |
| 50 | + const config: RequireJsConfig = { |
| 51 | + map: {}, |
| 52 | + paths: {}, |
| 53 | + }; |
| 54 | + |
| 55 | + await this.processMaps(config, configVariableDeclarator); |
| 56 | + await this.processPaths(config, configVariableDeclarator); |
| 57 | + |
| 58 | + return config; |
| 59 | + } |
| 60 | + |
| 61 | + private async processMaps(config: RequireJsConfig, configVariableDeclarator: VariableDeclarator) { |
| 62 | + const mapProperty = await this.findProperty(configVariableDeclarator, 'map'); |
| 63 | + |
| 64 | + if (!mapProperty) { |
| 65 | + return config; |
| 66 | + } |
| 67 | + |
| 68 | + const objectExpression = mapProperty.value as ObjectExpression; |
| 69 | + const properties = objectExpression.properties as Property[]; |
| 70 | + |
| 71 | + for (const property of properties) { |
| 72 | + const group = (property.key as Literal).value as string; |
| 73 | + const mappings = this.getGroupMappings(property); |
| 74 | + |
| 75 | + config.map[group] = mappings; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private async processPaths( |
| 80 | + config: RequireJsConfig, |
| 81 | + configVariableDeclarator: VariableDeclarator |
| 82 | + ) { |
| 83 | + const pathsProperty = await this.findProperty(configVariableDeclarator, 'paths'); |
| 84 | + |
| 85 | + if (!pathsProperty) { |
| 86 | + return config; |
| 87 | + } |
| 88 | + |
| 89 | + const objectExpression = pathsProperty.value as ObjectExpression; |
| 90 | + const properties = objectExpression.properties as Property[]; |
| 91 | + |
| 92 | + for (const property of properties) { |
| 93 | + const key = (property.key as Literal).value as string; |
| 94 | + const value = (property.value as Literal).value as string; |
| 95 | + |
| 96 | + config.paths[key] = value; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private getGroupMappings(property: Property): Record<string, string> { |
| 101 | + const objectExpression = property.value as ObjectExpression; |
| 102 | + const properties = objectExpression.properties as Property[]; |
| 103 | + |
| 104 | + const mappings: Record<string, string> = {}; |
| 105 | + |
| 106 | + for (const property of properties) { |
| 107 | + const key = |
| 108 | + property.key.type === 'Literal' |
| 109 | + ? (property.key as Literal).value |
| 110 | + : (property.key as Identifier).name; |
| 111 | + const value = |
| 112 | + property.value.type === 'Literal' |
| 113 | + ? (property.value as Literal).value |
| 114 | + : (property.value as Identifier).name; |
| 115 | + |
| 116 | + mappings[key as string] = value as string; |
| 117 | + } |
| 118 | + |
| 119 | + return mappings; |
| 120 | + } |
| 121 | + |
| 122 | + private findConfigVariableDeclarator(ast: Node): Promise<VariableDeclarator | undefined> { |
| 123 | + return new Promise((resolve, reject) => { |
| 124 | + walk.simple(ast, { |
| 125 | + VariableDeclaration(node: VariableDeclaration) { |
| 126 | + node.declarations.forEach(declarator => { |
| 127 | + if (declarator.id.type === 'Identifier' && declarator.id.name === 'config') { |
| 128 | + resolve(declarator); |
| 129 | + } |
| 130 | + }); |
| 131 | + }, |
| 132 | + }); |
| 133 | + |
| 134 | + resolve(undefined); |
| 135 | + }); |
| 136 | + } |
| 137 | + |
| 138 | + private findProperty( |
| 139 | + variableDeclarator: VariableDeclarator, |
| 140 | + key: string |
| 141 | + ): Promise<Property | AssignmentProperty | undefined> { |
| 142 | + return new Promise(resolve => { |
| 143 | + const objectExpression = variableDeclarator.init as ObjectExpression; |
| 144 | + |
| 145 | + walk.simple(objectExpression, { |
| 146 | + Property(node: Property | AssignmentProperty) { |
| 147 | + if (node.key.type === 'Identifier' && node.key.name === key) { |
| 148 | + resolve(node); |
| 149 | + } |
| 150 | + }, |
| 151 | + }); |
| 152 | + |
| 153 | + resolve(undefined); |
| 154 | + }); |
| 155 | + } |
| 156 | +} |
0 commit comments