From b71e8a21679d5c0b5020802b8851d109ec0d1a32 Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Mon, 17 Oct 2016 13:11:13 -0700 Subject: [PATCH 1/2] Add isSemantic check to getDiagnosticsWorker --- src/server/session.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/server/session.ts b/src/server/session.ts index 3df6a1acb51..40110d94d53 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -389,9 +389,9 @@ namespace ts.server { }); } - private getDiagnosticsWorker(args: protocol.FileRequestArgs, selector: (project: Project, file: string) => Diagnostic[], includeLinePosition: boolean) { + private getDiagnosticsWorker(args: protocol.FileRequestArgs, isSemantic: boolean, selector: (project: Project, file: string) => Diagnostic[], includeLinePosition: boolean) { const { project, file } = this.getFileAndProject(args); - if (shouldSkipSematicCheck(project)) { + if (isSemantic && shouldSkipSematicCheck(project)) { return []; } const scriptInfo = project.getScriptInfoForNormalizedPath(file); @@ -492,11 +492,11 @@ namespace ts.server { } private getSyntacticDiagnosticsSync(args: protocol.SyntacticDiagnosticsSyncRequestArgs): protocol.Diagnostic[] | protocol.DiagnosticWithLinePosition[] { - return this.getDiagnosticsWorker(args, (project, file) => project.getLanguageService().getSyntacticDiagnostics(file), args.includeLinePosition); + return this.getDiagnosticsWorker(args, /*isSemantic*/ false, (project, file) => project.getLanguageService().getSyntacticDiagnostics(file), args.includeLinePosition); } private getSemanticDiagnosticsSync(args: protocol.SemanticDiagnosticsSyncRequestArgs): protocol.Diagnostic[] | protocol.DiagnosticWithLinePosition[] { - return this.getDiagnosticsWorker(args, (project, file) => project.getLanguageService().getSemanticDiagnostics(file), args.includeLinePosition); + return this.getDiagnosticsWorker(args, /*isSemantic*/ true, (project, file) => project.getLanguageService().getSemanticDiagnostics(file), args.includeLinePosition); } private getDocumentHighlights(args: protocol.DocumentHighlightsRequestArgs, simplifiedResult: boolean): protocol.DocumentHighlightsItem[] | DocumentHighlights[] { From deb344907733d37822bb8e2f56b16f55d64c7c6c Mon Sep 17 00:00:00 2001 From: Jason Ramsay Date: Mon, 17 Oct 2016 14:50:03 -0700 Subject: [PATCH 2/2] Add getSyntacticDiagnostics unit test --- src/server/client.ts | 28 +++++++++++-------- .../getJavaScriptSyntacticDiagnostics01.ts | 23 +++++++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) create mode 100644 tests/cases/fourslash/server/getJavaScriptSyntacticDiagnostics01.ts diff --git a/src/server/client.ts b/src/server/client.ts index b0bc2c5dac0..6d8ef419a7a 100644 --- a/src/server/client.ts +++ b/src/server/client.ts @@ -424,33 +424,39 @@ namespace ts.server { } getSyntacticDiagnostics(fileName: string): Diagnostic[] { - const args: protocol.SyntacticDiagnosticsSyncRequestArgs = { file: fileName }; + const args: protocol.SyntacticDiagnosticsSyncRequestArgs = { file: fileName, includeLinePosition: true }; const request = this.processRequest(CommandNames.SyntacticDiagnosticsSync, args); const response = this.processResponse(request); - return (response.body).map(entry => this.convertDiagnostic(entry, fileName)); + return (response.body).map(entry => this.convertDiagnostic(entry, fileName)); } getSemanticDiagnostics(fileName: string): Diagnostic[] { - const args: protocol.SemanticDiagnosticsSyncRequestArgs = { file: fileName }; + const args: protocol.SemanticDiagnosticsSyncRequestArgs = { file: fileName, includeLinePosition: true }; const request = this.processRequest(CommandNames.SemanticDiagnosticsSync, args); const response = this.processResponse(request); - return (response.body).map(entry => this.convertDiagnostic(entry, fileName)); + return (response.body).map(entry => this.convertDiagnostic(entry, fileName)); } - convertDiagnostic(entry: protocol.Diagnostic, fileName: string): Diagnostic { - const start = this.lineOffsetToPosition(fileName, entry.start); - const end = this.lineOffsetToPosition(fileName, entry.end); + convertDiagnostic(entry: protocol.DiagnosticWithLinePosition, fileName: string): Diagnostic { + let category: DiagnosticCategory; + for (const id in DiagnosticCategory) { + if (typeof id === "string" && entry.category === id.toLowerCase()) { + category = (DiagnosticCategory)[id]; + } + } + + Debug.assert(category !== undefined, "convertDiagnostic: category should not be undefined"); return { file: undefined, - start: start, - length: end - start, - messageText: entry.text, - category: undefined, + start: entry.start, + length: entry.length, + messageText: entry.message, + category: category, code: entry.code }; } diff --git a/tests/cases/fourslash/server/getJavaScriptSyntacticDiagnostics01.ts b/tests/cases/fourslash/server/getJavaScriptSyntacticDiagnostics01.ts new file mode 100644 index 00000000000..0aa88ebd90c --- /dev/null +++ b/tests/cases/fourslash/server/getJavaScriptSyntacticDiagnostics01.ts @@ -0,0 +1,23 @@ +/// + +// @allowJs: true +// @Filename: a.js +//// var ===; + +verify.getSyntacticDiagnostics(`[ + { + "message": "Variable declaration expected.", + "start": 4, + "length": 3, + "category": "error", + "code": 1134 + }, + { + "message": "Expression expected.", + "start": 7, + "length": 1, + "category": "error", + "code": 1109 + } +]`); +verify.getSemanticDiagnostics(`[]`); \ No newline at end of file