-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathFixAllCodeActionsProcessor.ts
More file actions
73 lines (65 loc) · 2.82 KB
/
Copy pathFixAllCodeActionsProcessor.ts
File metadata and controls
73 lines (65 loc) · 2.82 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
import type { BsDiagnostic } from '../../interfaces';
import { DiagnosticCodeMap } from '../../DiagnosticMessages';
import type { DiagnosticMessageType } from '../../DiagnosticMessages';
import type { XmlFile } from '../../files/XmlFile';
import type { ProvideSourceFixAllCodeActionsEvent } from '../../interfaces';
import { isXmlFile } from '../../astUtils/reflection';
import { getMissingExtendsInsertPosition, getRemoveReturnValueChange } from './codeActionHelpers';
export class FixAllCodeActionsProcessor {
public constructor(
public event: ProvideSourceFixAllCodeActionsEvent
) { }
public process() {
const byCode = new Map<number | string, BsDiagnostic[]>();
for (const diagnostic of this.event.diagnostics) {
const key = diagnostic.code;
if (!byCode.has(key)) {
byCode.set(key, []);
}
byCode.get(key).push(diagnostic);
}
const missingExtends = byCode.get(DiagnosticCodeMap.xmlComponentMissingExtendsAttribute);
if (missingExtends) {
this.processMissingExtends(missingExtends as DiagnosticMessageType<'xmlComponentMissingExtendsAttribute'>[]);
}
const voidFunctionReturns = byCode.get(DiagnosticCodeMap.voidFunctionMayNotReturnValue);
if (voidFunctionReturns) {
this.processVoidFunctionReturnActions(voidFunctionReturns);
}
}
/**
* For every `voidFunctionMayNotReturnValue` diagnostic in this file,
* remove the return value expression, leaving the bare `return` keyword.
*/
private processVoidFunctionReturnActions(diagnostics: BsDiagnostic[]) {
const changes = diagnostics.map(diagnostic => getRemoveReturnValueChange(diagnostic, this.event.file.srcPath));
this.event.actions.push({
title: 'Remove all void return values',
kind: 'source.fixAll.brighterscript',
isPreferred: true,
changes: changes
});
}
/**
* For every `xmlComponentMissingExtendsAttribute` diagnostic in this file,
* insert `extends="Group"` — the same choice marked `isPreferred` in the
* per-diagnostic quick-fix.
*/
private processMissingExtends(diagnostics: DiagnosticMessageType<'xmlComponentMissingExtendsAttribute'>[]) {
if (!isXmlFile(this.event.file)) {
return;
}
const changes = diagnostics.map(() => ({
type: 'insert' as const,
filePath: this.event.file.srcPath,
position: getMissingExtendsInsertPosition(this.event.file as XmlFile),
newText: ' extends="Group"'
}));
this.event.actions.push({
title: 'Add missing extends attributes',
kind: 'source.fixAll.brighterscript',
isPreferred: true,
changes: changes
});
}
}