mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 02:13:31 -06:00
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
/* @internal */
|
|
namespace ts {
|
|
export interface CodeFix {
|
|
errorCodes: number[];
|
|
getCodeActions(context: CodeFixContext): CodeAction[] | undefined;
|
|
}
|
|
|
|
export interface CodeFixContext {
|
|
errorCode: number;
|
|
sourceFile: SourceFile;
|
|
span: TextSpan;
|
|
program: Program;
|
|
newLineCharacter: string;
|
|
host: LanguageServiceHost;
|
|
cancellationToken: CancellationToken;
|
|
rulesProvider: formatting.RulesProvider;
|
|
}
|
|
|
|
export namespace codefix {
|
|
const codeFixes: CodeFix[][] = [];
|
|
|
|
export function registerCodeFix(codeFix: CodeFix) {
|
|
forEach(codeFix.errorCodes, error => {
|
|
let fixes = codeFixes[error];
|
|
if (!fixes) {
|
|
fixes = [];
|
|
codeFixes[error] = fixes;
|
|
}
|
|
fixes.push(codeFix);
|
|
});
|
|
}
|
|
|
|
export function getSupportedErrorCodes() {
|
|
return Object.keys(codeFixes);
|
|
}
|
|
|
|
export function getFixes(context: CodeFixContext): CodeAction[] {
|
|
const fixes = codeFixes[context.errorCode];
|
|
const allActions: CodeAction[] = [];
|
|
|
|
forEach(fixes, f => {
|
|
const actions = f.getCodeActions(context);
|
|
if (actions && actions.length > 0) {
|
|
for (const action of actions) {
|
|
if (action === undefined) {
|
|
context.host.log(`Action for error code ${context.errorCode} added an invalid action entry; please log a bug`);
|
|
}
|
|
else {
|
|
allActions.push(action);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
return allActions;
|
|
}
|
|
}
|
|
}
|