/
indexer.ts
350 lines (303 loc) · 12.7 KB
/
indexer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import { WorkspaceContext, shared, Indexer, TagInfo, utils } from 'lightning-lsp-common';
import { LWCIndexer } from 'lwc-language-server/lib/indexer';
import { Location } from 'vscode-languageserver';
import * as auraUtils from '../aura-utils';
import * as fs from 'fs-extra';
import { AttributeInfo, componentUtil, parse, Node } from 'lightning-lsp-common';
import LineColumnFinder from 'line-column';
import URI from 'vscode-uri';
import changeCase from 'change-case';
import EventsEmitter from 'events';
import { TagType } from 'lightning-lsp-common/lib/indexer/tagInfo';
const { WorkspaceType } = shared;
export default class AuraIndexer implements Indexer {
public readonly eventEmitter = new EventsEmitter();
private context: WorkspaceContext;
private indexingTasks: Promise<void>;
private lwcIndexer: LWCIndexer | null;
private listeners: Map<string, any> = new Map();
private AURA_TAGS: Map<string, TagInfo> = new Map();
private AURA_EVENTS: Map<string, TagInfo> = new Map();
private AURA_NAMESPACES: Set<string> = new Set();
constructor(context: WorkspaceContext) {
this.context = context;
this.context.addIndexingProvider({ name: 'aura', indexer: this });
}
public async configureAndIndex() {
const indexingTasks: Array<Promise<void>> = [];
this.lwcIndexer = new LWCIndexer(this.context, false);
this.context.addIndexingProvider({ name: 'lwc', indexer: this.lwcIndexer });
this.addListener('delete', (tag: string) => {
const transformedName = this.transformLwcTagName(tag);
const auraName = [transformedName.namespace, transformedName.name].join(':');
this.deleteCustomTag(auraName);
});
this.addListener('set', (tagInfo: TagInfo) => {
const tag = tagInfo.name;
if (tagInfo.type === TagType.CUSTOM) {
const interopTagInfo = this.transformLwcTagToAura(tag, tagInfo);
this.setCustomTag(interopTagInfo);
}
});
indexingTasks.push(this.lwcIndexer.configureAndIndex());
indexingTasks.push(this.loadStandardComponents());
indexingTasks.push(this.loadSystemTags());
indexingTasks.push(this.indexCustomComponents());
this.indexingTasks = Promise.all(indexingTasks).then(() => undefined);
return this.indexingTasks;
}
public async waitForIndexing() {
return this.indexingTasks;
}
public resetIndex() {
this.eventEmitter.emit('clear');
if (this.lwcIndexer) {
this.lwcIndexer.eventEmitter.removeAllListeners();
this.lwcIndexer.resetIndex();
}
this.AURA_TAGS.clear();
this.AURA_EVENTS.clear();
}
public getAuraTags(): Map<string, TagInfo> {
return this.AURA_TAGS;
}
public getAuraNamespaces(): string[] {
return [...this.AURA_NAMESPACES];
}
public getAuraByTag(tag: string): TagInfo {
return this.getAuraTags().get(tag);
}
public clearTagsforDirectory(directory: string, sfdxProject: boolean) {
const name = componentUtil.componentFromDirectory(directory, sfdxProject);
this.deleteCustomTag(name);
}
public async indexFile(file: string, sfdxProject: boolean): Promise<TagInfo | undefined> {
if (!fs.existsSync(file)) {
this.clearTagsforFile(file, sfdxProject);
return;
}
const markup = await fs.readFile(file, 'utf-8');
const result = parse(markup);
const tags = [];
for (const root of result.roots) {
tags.push(...this.searchAura(root));
}
const tagInfo = this.getTagInfo(file, sfdxProject, markup, result.roots[0]);
if (!tagInfo) {
this.clearTagsforFile(file, sfdxProject);
return;
}
if (!tagInfo.name) {
console.warn(`File ${file} has malformed tagname, ignoring`);
return;
}
const attributeInfos = tags
.filter(tag => tag.tag.startsWith('aura:attribute'))
.filter(
node =>
(node.parent && (node.parent.tag === 'aura:application' || node.parent.tag === 'aura:component')) ||
node.parent.tag === 'aura:event' ||
node.parent.tag === 'aura:interface',
)
.map(node => {
const attributes = node.attributes || {};
const documentation = this.trimQuotes(attributes.description);
const jsName = this.trimQuotes(attributes.name);
const type = this.trimQuotes(attributes.type);
const startColumn = new LineColumnFinder(markup).fromIndex(node.start);
const endColumn = new LineColumnFinder(markup).fromIndex(node.end - 1);
const location: Location = {
uri: URI.file(file).toString(),
range: {
start: {
line: startColumn.line,
character: startColumn.col,
},
end: {
line: endColumn.line,
character: endColumn.col,
},
},
};
return new AttributeInfo(jsName, documentation, undefined, undefined, type, location);
});
tagInfo.attributes = attributeInfos;
this.setCustomTag(tagInfo);
return tagInfo;
}
private addListener(event: string, listener: any) {
this.listeners.set(event, listener);
this.lwcIndexer.eventEmitter.on(event, listener);
}
private removeListeners() {
for (const [event, listener] of this.listeners.entries()) {
this.lwcIndexer.eventEmitter.removeListener(event, listener);
}
}
private async indexCustomComponents() {
const startTime = process.hrtime();
const markupfiles = await this.context.findAllAuraMarkup();
for (const file of markupfiles) {
try {
await this.indexFile(file, this.context.type === WorkspaceType.SFDX);
} catch (e) {
console.log(`Error parsing markup from ${file}:`, e);
}
}
console.info(`Indexed ${markupfiles.length} files in ${utils.elapsedMillis(startTime)} ms`);
}
private clearTagsforFile(file: string, sfdxProject: boolean) {
const name = componentUtil.componentFromFile(file, sfdxProject);
this.deleteCustomTag(name);
}
private deleteCustomTag(tag: string) {
this.AURA_TAGS.delete(tag);
this.AURA_EVENTS.delete(tag);
this.eventEmitter.emit('delete', tag);
}
private setAuraNamespaceTag(namespace: string) {
if (!this.AURA_NAMESPACES.has(namespace)) {
this.AURA_NAMESPACES.add(namespace);
this.eventEmitter.emit('set-namespace', namespace);
}
}
private setCustomEventTag(info: TagInfo) {
this.setAuraNamespaceTag(info.namespace);
this.AURA_EVENTS.set(info.name, info);
this.eventEmitter.emit('set', info);
}
private setCustomTag(info: TagInfo) {
this.setAuraNamespaceTag(info.namespace);
this.AURA_TAGS.set(info.name, info);
this.eventEmitter.emit('set', info);
}
private async loadSystemTags(): Promise<void> {
const data = await fs.readFile(auraUtils.getAuraSystemResourcePath(), 'utf-8');
const auraSystem = JSON.parse(data);
for (const tag in auraSystem) {
// TODO need to account for LWC tags here
if (auraSystem.hasOwnProperty(tag) && typeof tag === 'string') {
const tagObj = auraSystem[tag];
const info = new TagInfo(null, TagType.SYSTEM, false, []);
if (tagObj.attributes) {
for (const a of tagObj.attributes) {
// TODO - could we use more in depth doc from component library here?
info.attributes.push(new AttributeInfo(a.name, a.description, a.type, undefined, 'Aura Attribute'));
}
}
info.documentation = tagObj.description;
info.name = tag;
info.namespace = tagObj.namespace;
this.setCustomTag(info);
}
}
}
private async loadStandardComponents(): Promise<void> {
const data = await fs.readFile(auraUtils.getAuraStandardResourcePath(), 'utf-8');
const auraStandard = JSON.parse(data);
for (const tag in auraStandard) {
if (auraStandard.hasOwnProperty(tag) && typeof tag === 'string') {
const tagObj = auraStandard[tag];
const info = new TagInfo(null, TagType.STANDARD, false, []);
if (tagObj.attributes) {
tagObj.attributes.sort((a, b) => {
return a.name.localeCompare(b.name);
});
for (const a of tagObj.attributes) {
// TODO - could we use more in depth doc from component library here?
info.attributes.push(new AttributeInfo(a.name, a.description, a.type, undefined, 'Aura Attribute'));
}
}
info.documentation = tagObj.description;
info.name = tag;
info.namespace = tagObj.namespace;
// Update our in memory maps
// TODO should we move interfaces/apps/etc to a separate map also?
if (tagObj.type === 'event') {
this.setCustomEventTag(info);
} else {
this.setCustomTag(info);
}
}
}
}
private searchAura(node: Node): Node[] {
const results = [];
if (node.tag.indexOf(':') !== -1) {
results.push(node);
}
for (const child of node.children) {
results.push(...this.searchAura(child));
}
return results;
}
private trimQuotes(str: string) {
if (!str) {
return '';
}
return str.replace(/"([^"]+(?="))"/g, '$1');
}
private getTagInfo(file: string, sfdxProject: boolean, contents: string, node: Node): TagInfo {
if (!node) {
return;
}
const attributes = node.attributes || {};
const documentation = this.trimQuotes(attributes.description);
const startColumn = new LineColumnFinder(contents).fromIndex(node.start);
const endColumn = new LineColumnFinder(contents).fromIndex(node.end - 1);
const location: Location = {
uri: URI.file(file).toString(),
range: {
start: {
line: startColumn.line,
character: startColumn.col,
},
end: {
line: endColumn.line,
character: endColumn.col,
},
},
};
const name = componentUtil.componentFromFile(file, sfdxProject);
const info = new TagInfo(file, TagType.CUSTOM, false, [], location, documentation, name, 'c');
return info;
}
private isAuraNamespace(namespace: string): boolean {
return this.AURA_NAMESPACES.has(namespace);
}
private transformLwcTagName(tag: string) {
const namespace = tag.split('-')[0];
const name = tag
.split('-')
.slice(1)
.join('-');
return {
namespace,
name: changeCase.camelCase(name),
};
}
private transformLwcTagToAura(tag: string, tagInfo: any): TagInfo {
const interopTagInfo = JSON.parse(JSON.stringify(tagInfo));
const transformedName = this.transformLwcTagName(tag);
interopTagInfo.name = [transformedName.namespace, transformedName.name].join(':');
const attrs: AttributeInfo[] = [];
for (const attribute of interopTagInfo.attributes) {
const attrname = changeCase.camelCase(attribute.jsName || attribute.name);
attrs.push(new AttributeInfo(attrname, attribute.documentation, attribute.memberType, attribute.decorator, attribute.type, attribute.location, ''));
}
const info = new TagInfo(
interopTagInfo.file,
// TagType[interopTagInfo.type as string],
interopTagInfo.type,
true,
attrs,
interopTagInfo.location,
interopTagInfo.documentation,
interopTagInfo.name,
transformedName.namespace,
interopTagInfo.properties,
interopTagInfo.methods,
);
return info;
}
}