-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathXmlScope.ts
More file actions
180 lines (167 loc) · 6.77 KB
/
Copy pathXmlScope.ts
File metadata and controls
180 lines (167 loc) · 6.77 KB
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
import type { Location, LocationLink, Position } from 'vscode-languageserver';
import { Scope } from './Scope';
import { DiagnosticMessages } from './DiagnosticMessages';
import type { XmlFile } from './files/XmlFile';
import type { BscFile, CallableContainerMap, FileReference } from './interfaces';
import type { Program } from './Program';
import { SGFieldTypes } from './parser/SGTypes';
import type { SGTag } from './parser/SGTypes';
import { DefinitionProvider } from './bscPlugin/definition/DefinitionProvider';
export class XmlScope extends Scope {
constructor(
public xmlFile: XmlFile,
public program: Program
) {
super(xmlFile.pkgPath, program);
}
public get dependencyGraphKey() {
return this.xmlFile.dependencyGraphKey;
}
/**
* Get the parent scope of this scope. If we could find the scope for our parentComponent, use that.
* Otherwise default to global scope
*/
public getParentScope() {
return this.cache.getOrAdd('parentScope', () => {
let scope: Scope | undefined;
let parentComponentName = this.xmlFile.parentComponentName?.text;
if (parentComponentName) {
scope = this.program.getComponentScope(parentComponentName);
}
if (scope) {
return scope;
} else {
return this.program.globalScope ?? null;
}
});
}
protected _validate(callableContainerMap: CallableContainerMap) {
//validate brs files
super._validate(callableContainerMap);
//detect when the child imports a script that its ancestor also imports
this.diagnosticDetectDuplicateAncestorScriptImports();
//validate component interface
this.diagnosticValidateInterface(callableContainerMap);
}
private diagnosticValidateInterface(callableContainerMap: CallableContainerMap) {
if (!this.xmlFile.parser.ast?.component?.api) {
return;
}
const { api } = this.xmlFile.parser.ast.component;
//validate functions
for (const fun of api.functions) {
const name = fun.name;
if (!name) {
this.diagnosticMissingAttribute(fun, 'name');
} else if (!callableContainerMap.has(name.toLowerCase())) {
this.diagnostics.push({
...DiagnosticMessages.xmlFunctionNotFound(name),
range: fun.getAttribute('name')?.value.range,
file: this.xmlFile
});
}
}
//validate fields
for (const field of api.fields) {
const { id, type, onChange } = field;
if (!id) {
this.diagnosticMissingAttribute(field, 'id');
}
if (!type) {
if (!field.alias) {
this.diagnosticMissingAttribute(field, 'type');
}
} else if (!SGFieldTypes.includes(type.toLowerCase())) {
this.diagnostics.push({
...DiagnosticMessages.xmlInvalidFieldType(type),
range: field.getAttribute('type')?.value.range,
file: this.xmlFile
});
}
if (onChange) {
if (!callableContainerMap.has(onChange.toLowerCase())) {
this.diagnostics.push({
...DiagnosticMessages.xmlFunctionNotFound(onChange),
range: field.getAttribute('onchange')?.value.range,
file: this.xmlFile
});
}
}
}
}
private diagnosticMissingAttribute(tag: SGTag, name: string) {
const { text, range } = tag.tag;
this.diagnostics.push({
...DiagnosticMessages.xmlTagMissingAttribute(text, name),
range: range,
file: this.xmlFile
});
}
/**
* Detect when a child has imported a script that an ancestor also imported
*/
private diagnosticDetectDuplicateAncestorScriptImports() {
if (this.xmlFile.parentComponent) {
//build a lookup of pkg paths -> FileReference so we can more easily look up collisions
let parentScriptImports = this.xmlFile.getAncestorScriptTagImports();
let lookup = {} as Record<string, FileReference>;
for (let parentScriptImport of parentScriptImports) {
//keep the first occurance of a pkgPath. Parent imports are first in the array
if (!lookup[parentScriptImport.pkgPath]) {
lookup[parentScriptImport.pkgPath] = parentScriptImport;
}
}
//add warning for every script tag that this file shares with an ancestor
for (let scriptImport of this.xmlFile.scriptTagImports) {
let ancestorScriptImport = lookup[scriptImport.pkgPath];
if (ancestorScriptImport) {
let ancestorComponent = ancestorScriptImport.sourceFile as XmlFile;
let ancestorComponentName = ancestorComponent.componentName?.text ?? ancestorComponent.pkgPath;
this.diagnostics.push({
file: this.xmlFile,
range: scriptImport.filePathRange,
...DiagnosticMessages.unnecessaryScriptImportInChildFromParent(ancestorComponentName)
});
}
}
}
}
public getAllFiles() {
return this.cache.getOrAdd('getAllFiles-xmlScope', () => {
const allFiles = super.getAllFiles();
allFiles.push(this.xmlFile);
return allFiles;
});
}
/**
* Get the list of files referenced by this scope that are actually loaded in the program.
* This does not account for parent scope.
*/
public getOwnFiles() {
return this.cache.getOrAdd('getOwnFiles', () => {
let result = [
this.xmlFile
] as BscFile[];
let scriptPkgPaths = this.xmlFile.getOwnDependencies();
for (let scriptPkgPath of scriptPkgPaths) {
let file = this.program.getFileByPkgPath(scriptPkgPath);
if (file) {
result.push(file);
}
}
return result;
});
}
/**
* Get the definition (where was this thing first defined) of the symbol under the position
* @deprecated use `DefinitionProvider.process()`
*/
public getDefinition(file: BscFile, position: Position): Array<Location | LocationLink> {
return new DefinitionProvider({
program: this.program,
file: file,
position: position,
definitions: []
}).process();
}
}