Introduce related spans into tsserver protocol (#24548)

* Introduce related spans into tsserver protocol

* Incorporate pretty output, implement esModuleInterop related span
This commit is contained in:
Wesley Wigham
2018-06-15 10:54:36 -07:00
committed by GitHub
parent b9794134e3
commit 640af3f75e
21 changed files with 371 additions and 142 deletions

View File

@@ -466,6 +466,7 @@ namespace ts.server.protocol {
code: number;
/** May store more in future. For now, this will simply be `true` to indicate when a diagnostic is an unused-identifier diagnostic. */
reportsUnnecessary?: {};
relatedInformation?: DiagnosticRelatedInformation[];
}
/**
@@ -2215,6 +2216,11 @@ namespace ts.server.protocol {
reportsUnnecessary?: {};
/**
* Any related spans the diagnostic may have, such as other locations relevant to an error, such as declarartion sites
*/
relatedInformation?: DiagnosticRelatedInformation[];
/**
* The error code of the diagnostic message.
*/
@@ -2233,6 +2239,23 @@ namespace ts.server.protocol {
fileName: string;
}
/**
* Represents additional spans returned with a diagnostic which are relevant to it
* Like DiagnosticWithLinePosition, this is provided in two forms:
* - start and length of the span
* - startLocation and endLocation a pair of Location objects storing the start/end line offset of the span
*/
export interface DiagnosticRelatedInformation {
/**
* Text of related or additional information.
*/
message: string;
/**
* Associated location
*/
span?: FileSpan;
}
export interface DiagnosticEventBody {
/**
* The file for which diagnostic information is reported.

View File

@@ -76,7 +76,24 @@ namespace ts.server {
code: diag.code,
category: diagnosticCategoryName(diag),
reportsUnnecessary: diag.reportsUnnecessary,
source: diag.source
source: diag.source,
relatedInformation: map(diag.relatedInformation, formatRelatedInformation),
};
}
function formatRelatedInformation(info: DiagnosticRelatedInformation): protocol.DiagnosticRelatedInformation {
if (!info.file) {
return {
message: flattenDiagnosticMessageText(info.messageText, "\n")
};
}
return {
span: {
start: convertToLocation(getLineAndCharacterOfPosition(info.file, info.start!)),
end: convertToLocation(getLineAndCharacterOfPosition(info.file, info.start! + info.length!)), // TODO: GH#18217
file: info.file.fileName
},
message: flattenDiagnosticMessageText(info.messageText, "\n")
};
}
@@ -92,8 +109,19 @@ namespace ts.server {
const text = flattenDiagnosticMessageText(diag.messageText, "\n");
const { code, source } = diag;
const category = diagnosticCategoryName(diag);
return includeFileName ? { start, end, text, code, category, source, reportsUnnecessary: diag.reportsUnnecessary, fileName: diag.file && diag.file.fileName } :
{ start, end, text, code, category, reportsUnnecessary: diag.reportsUnnecessary, source };
const common = {
start,
end,
text,
code,
category,
reportsUnnecessary: diag.reportsUnnecessary,
source,
relatedInformation: map(diag.relatedInformation, formatRelatedInformation),
};
return includeFileName
? { ...common, fileName: diag.file && diag.file.fileName }
: common;
}
export interface PendingErrorCheck {
@@ -612,7 +640,8 @@ namespace ts.server {
category: diagnosticCategoryName(d),
code: d.code,
startLocation: (d.file && convertToLocation(getLineAndCharacterOfPosition(d.file, d.start!)))!, // TODO: GH#18217
endLocation: (d.file && convertToLocation(getLineAndCharacterOfPosition(d.file, d.start! + d.length!)))! // TODO: GH#18217
endLocation: (d.file && convertToLocation(getLineAndCharacterOfPosition(d.file, d.start! + d.length!)))!, // TODO: GH#18217
relatedInformation: map(d.relatedInformation, formatRelatedInformation)
}));
}
@@ -640,7 +669,8 @@ namespace ts.server {
source: d.source,
startLocation: scriptInfo && scriptInfo.positionToLineOffset(d.start!), // TODO: GH#18217
endLocation: scriptInfo && scriptInfo.positionToLineOffset(d.start! + d.length!),
reportsUnnecessary: d.reportsUnnecessary
reportsUnnecessary: d.reportsUnnecessary,
relatedInformation: map(d.relatedInformation, formatRelatedInformation),
});
}