mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-29 16:29:19 -05:00
Clean up error messages for using TypeScript syntax in JavaScr… (#35254)
* Fix up quotation marks in error messages in JavaScript files. * Accepted baselines. * Typescript -> TypeScript * Accepted baselines. * Migrate syntactic diagnostics tests to baselining tests. * Accepted baselines. * Update diagnosticMessages.json * Removed markers. * Add ability to baseline both semantic and syntactic diagnostics. * Fix up broken diagnostics when using a server LS. * Accepted baselines. * Lints. * Fake up sourcefile objects in the tsserver session client instead. * Fewer allocations.
This commit is contained in:
committed by
GitHub
parent
3e329469c1
commit
3da85df511
@@ -374,12 +374,14 @@ namespace ts.server {
|
||||
private getDiagnostics(file: string, command: CommandNames): DiagnosticWithLocation[] {
|
||||
const request = this.processRequest<protocol.SyntacticDiagnosticsSyncRequest | protocol.SemanticDiagnosticsSyncRequest | protocol.SuggestionDiagnosticsSyncRequest>(command, { file, includeLinePosition: true });
|
||||
const response = this.processResponse<protocol.SyntacticDiagnosticsSyncResponse | protocol.SemanticDiagnosticsSyncResponse | protocol.SuggestionDiagnosticsSyncResponse>(request);
|
||||
const sourceText = getSnapshotText(this.host.getScriptSnapshot(file)!);
|
||||
const fakeSourceFile = { fileName: file, text: sourceText } as SourceFile; // Warning! This is a huge lie!
|
||||
|
||||
return (<protocol.DiagnosticWithLinePosition[]>response.body).map((entry): DiagnosticWithLocation => {
|
||||
const category = firstDefined(Object.keys(DiagnosticCategory), id =>
|
||||
isString(id) && entry.category === id.toLowerCase() ? (<any>DiagnosticCategory)[id] : undefined);
|
||||
return {
|
||||
file: undefined!, // TODO: GH#18217
|
||||
file: fakeSourceFile,
|
||||
start: entry.start,
|
||||
length: entry.length,
|
||||
messageText: entry.message,
|
||||
@@ -518,14 +520,14 @@ namespace ts.server {
|
||||
return notImplemented();
|
||||
}
|
||||
|
||||
getSignatureHelpItems(fileName: string, position: number): SignatureHelpItems {
|
||||
getSignatureHelpItems(fileName: string, position: number): SignatureHelpItems | undefined {
|
||||
const args: protocol.SignatureHelpRequestArgs = this.createFileLocationRequestArgs(fileName, position);
|
||||
|
||||
const request = this.processRequest<protocol.SignatureHelpRequest>(CommandNames.SignatureHelp, args);
|
||||
const response = this.processResponse<protocol.SignatureHelpResponse>(request);
|
||||
|
||||
if (!response.body) {
|
||||
return undefined!; // TODO: GH#18217
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const { items, applicableSpan: encodedApplicableSpan, selectedItemIndex, argumentIndex, argumentCount } = response.body;
|
||||
|
||||
@@ -221,7 +221,7 @@ namespace FourSlash {
|
||||
}
|
||||
}
|
||||
|
||||
constructor(private basePath: string, private testType: FourSlashTestType, public testData: FourSlashData) {
|
||||
constructor(private originalInputFileName: string, private basePath: string, private testType: FourSlashTestType, public testData: FourSlashData) {
|
||||
// Create a new Services Adapter
|
||||
this.cancellationToken = new TestCancellationToken();
|
||||
let compilationOptions = convertGlobalOptionsToCompilerOptions(this.testData.globalOptions);
|
||||
@@ -1475,7 +1475,7 @@ namespace FourSlash {
|
||||
}
|
||||
|
||||
public baselineCurrentFileBreakpointLocations() {
|
||||
const baselineFile = this.getBaselineFileName().replace("breakpointValidation", "bpSpan");
|
||||
const baselineFile = this.getBaselineFileNameForInternalFourslashFile().replace("breakpointValidation", "bpSpan");
|
||||
Harness.Baseline.runBaseline(baselineFile, this.baselineCurrentFileLocations(pos => this.getBreakpointStatementLocation(pos)!));
|
||||
}
|
||||
|
||||
@@ -1554,8 +1554,49 @@ namespace FourSlash {
|
||||
return result;
|
||||
}
|
||||
|
||||
public baselineSyntacticDiagnostics() {
|
||||
const files = this.getCompilerTestFiles();
|
||||
const result = this.getSyntacticDiagnosticBaselineText(files);
|
||||
Harness.Baseline.runBaseline(this.getBaselineFileNameForContainingTestFile(), result);
|
||||
}
|
||||
|
||||
private getCompilerTestFiles() {
|
||||
return ts.map(this.testData.files, ({ content, fileName }) => ({
|
||||
content, unitName: fileName
|
||||
}));
|
||||
}
|
||||
|
||||
public baselineSyntacticAndSemanticDiagnostics() {
|
||||
const files = this.getCompilerTestFiles();
|
||||
const result = this.getSyntacticDiagnosticBaselineText(files)
|
||||
+ Harness.IO.newLine()
|
||||
+ Harness.IO.newLine()
|
||||
+ this.getSemanticDiagnosticBaselineText(files);
|
||||
Harness.Baseline.runBaseline(this.getBaselineFileNameForContainingTestFile(), result);
|
||||
}
|
||||
|
||||
private getSyntacticDiagnosticBaselineText(files: Harness.Compiler.TestFile[]) {
|
||||
const diagnostics = ts.flatMap(files,
|
||||
file => this.languageService.getSyntacticDiagnostics(file.unitName)
|
||||
);
|
||||
const result = `Syntactic Diagnostics for file '${this.originalInputFileName}':`
|
||||
+ Harness.IO.newLine()
|
||||
+ Harness.Compiler.getErrorBaseline(files, diagnostics, /*pretty*/ false);
|
||||
return result;
|
||||
}
|
||||
|
||||
private getSemanticDiagnosticBaselineText(files: Harness.Compiler.TestFile[]) {
|
||||
const diagnostics = ts.flatMap(files,
|
||||
file => this.languageService.getSemanticDiagnostics(file.unitName)
|
||||
);
|
||||
const result = `Semantic Diagnostics for file '${this.originalInputFileName}':`
|
||||
+ Harness.IO.newLine()
|
||||
+ Harness.Compiler.getErrorBaseline(files, diagnostics, /*pretty*/ false);
|
||||
return result;
|
||||
}
|
||||
|
||||
public baselineQuickInfo() {
|
||||
const baselineFile = this.getBaselineFileName();
|
||||
const baselineFile = this.getBaselineFileNameForInternalFourslashFile();
|
||||
Harness.Baseline.runBaseline(
|
||||
baselineFile,
|
||||
stringify(
|
||||
@@ -1567,7 +1608,7 @@ namespace FourSlash {
|
||||
|
||||
public baselineSmartSelection() {
|
||||
const n = "\n";
|
||||
const baselineFile = this.getBaselineFileName();
|
||||
const baselineFile = this.getBaselineFileNameForInternalFourslashFile();
|
||||
const markers = this.getMarkers();
|
||||
const fileContent = this.activeFile.content;
|
||||
const text = markers.map(marker => {
|
||||
@@ -1652,11 +1693,15 @@ namespace FourSlash {
|
||||
Harness.IO.log(stringify(help.items[help.selectedItemIndex]));
|
||||
}
|
||||
|
||||
private getBaselineFileName() {
|
||||
private getBaselineFileNameForInternalFourslashFile() {
|
||||
return this.testData.globalOptions[MetadataOptionNames.baselineFile] ||
|
||||
ts.getBaseFileName(this.activeFile.fileName).replace(ts.Extension.Ts, ".baseline");
|
||||
}
|
||||
|
||||
private getBaselineFileNameForContainingTestFile() {
|
||||
return ts.getBaseFileName(this.originalInputFileName).replace(ts.Extension.Ts, ".baseline");
|
||||
}
|
||||
|
||||
private getSignatureHelp({ triggerReason }: FourSlashInterface.VerifySignatureHelpOptions): ts.SignatureHelpItems | undefined {
|
||||
return this.languageService.getSignatureHelpItems(this.activeFile.fileName, this.currentCaretPosition, {
|
||||
triggerReason
|
||||
@@ -3263,7 +3308,7 @@ namespace FourSlash {
|
||||
|
||||
// Parse out the files and their metadata
|
||||
const testData = parseTestData(absoluteBasePath, content, absoluteFileName);
|
||||
const state = new TestState(absoluteBasePath, testType, testData);
|
||||
const state = new TestState(absoluteFileName, absoluteBasePath, testType, testData);
|
||||
const output = ts.transpileModule(content, { reportDiagnostics: true, compilerOptions: { target: ts.ScriptTarget.ES2015 } });
|
||||
if (output.diagnostics!.length > 0) {
|
||||
throw new Error(`Syntax error in ${absoluteBasePath}: ${output.diagnostics![0].messageText}`);
|
||||
@@ -4122,6 +4167,14 @@ namespace FourSlashInterface {
|
||||
this.state.baselineSmartSelection();
|
||||
}
|
||||
|
||||
public baselineSyntacticDiagnostics() {
|
||||
this.state.baselineSyntacticDiagnostics();
|
||||
}
|
||||
|
||||
public baselineSyntacticAndSemanticDiagnostics() {
|
||||
this.state.baselineSyntacticAndSemanticDiagnostics();
|
||||
}
|
||||
|
||||
public nameOrDottedNameSpanTextIs(text: string) {
|
||||
this.state.verifyCurrentNameOrDottedNameSpanText(text);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user