From 1e0e5ebb548ef2d4382b57debe1f21a96022ecd7 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Mon, 17 Dec 2018 16:27:46 +0800 Subject: [PATCH] fix: fix error type issues --- package.json | 9 ++---- src/languageservice/parser/yamlParser.ts | 27 ++++++++++++----- .../services/yamlValidation.ts | 29 ++++++++++--------- src/languageservice/yamlLanguageTypes.ts | 6 ++-- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 9a01c1a..2bb47d9 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "watch": "tsc -p ./src --watch", "prepublish": "rimraf ./out && rimraf ./release && yarn compile:umd && node ./scripts/bundle && mcopy ./src/monaco.d.ts ./release/monaco.d.ts", "lint": "prettier \"{src,test}/**/*.{json,scss,html,ts}\" --write", - "test": "jest" + "test": "jest --verbose" }, "author": "Kevin Decker (http://incaseofstairs.com)", "maintainers": [ @@ -51,14 +51,11 @@ }, "lint-staged": { "linters": { - "*.{json,scss,html}": [ + "*.{json,scss,html,ts}": [ "prettier --write", "git add" ] - }, - "ignore": [ - "src/backend/vendor/**/*" - ] + } }, "husky": { "hooks": { diff --git a/src/languageservice/parser/yamlParser.ts b/src/languageservice/parser/yamlParser.ts index 56deaca..db97af9 100644 --- a/src/languageservice/parser/yamlParser.ts +++ b/src/languageservice/parser/yamlParser.ts @@ -25,6 +25,7 @@ import { StringASTNodeImpl, } from './jsonParser'; import { parseYamlBoolean } from './scalar-type'; +import { DiagnosticSeverity } from 'vscode-languageserver-types'; function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode { if (!node) { @@ -99,10 +100,10 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode { const itemNode = item === null ? new NullASTNodeImpl( - parent, - instance.startPosition, - instance.endPosition - instance.startPosition - ) + parent, + instance.startPosition, + instance.endPosition - instance.startPosition + ) : recursivelyBuildAst(result, item); result.items.push(itemNode); @@ -225,10 +226,16 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode { function convertError(e: Yaml.Error) { return { message: `${e.reason}`, + // TODO: YAML ast parser does not give a length for validation error, + // TODO: thus we treat the range from line start to the target position. location: { - offset: e.mark.position, - code: ErrorCode.Undefined, + offset: e.mark.position - e.mark.column, + length: e.mark.column, }, + code: ErrorCode.Undefined, + severity: e.isWarning + ? DiagnosticSeverity.Warning + : DiagnosticSeverity.Error, }; } @@ -248,14 +255,18 @@ function createJSONDocument( 'Expected a YAML object, array or literal' ), code: ErrorCode.Undefined, - location: { start: yamlDoc.startPosition, end: yamlDoc.endPosition }, + location: { + offset: yamlDoc.startPosition, + length: yamlDoc.endPosition - yamlDoc.startPosition, + }, + severity: DiagnosticSeverity.Error, }); } const duplicateKeyReason = 'duplicate key'; // Patch ontop of yaml-ast-parser to disable duplicate key message on merge key - const isDuplicateAndNotMergeKey = function ( + const isDuplicateAndNotMergeKey = function( error: Yaml.Error, yamlText: string ) { diff --git a/src/languageservice/services/yamlValidation.ts b/src/languageservice/services/yamlValidation.ts index 96bf333..e01b3e8 100644 --- a/src/languageservice/services/yamlValidation.ts +++ b/src/languageservice/services/yamlValidation.ts @@ -5,10 +5,11 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import { DiagnosticSeverity, TextDocument } from 'vscode-languageserver-types'; +import { DiagnosticSeverity, TextDocument, Diagnostic } from 'vscode-languageserver-types'; import { LanguageSettings } from '../yamlLanguageService'; import { YAMLDocument } from '../yamlLanguageTypes'; import { JSONSchemaService, ResolvedSchema } from './jsonSchemaService'; +import { Thenable } from '../jsonLanguageTypes'; export class YAMLValidation { private validationEnabled: boolean; @@ -22,14 +23,14 @@ export class YAMLValidation { } } - public doValidation(textDocument: TextDocument, yamlDocument: YAMLDocument) { + public doValidation(textDocument: TextDocument, yamlDocument: YAMLDocument): Thenable { if (!this.validationEnabled) { return Promise.resolve([]); } return this.jsonSchemaService .getSchemaForResource(textDocument.uri) - .then(function(schema) { - const diagnostics = []; + .then(function (schema) { + const diagnostics: Diagnostic[] = []; const added = {}; let newSchema = schema; if (schema) { @@ -53,10 +54,11 @@ export class YAMLValidation { const curDiagnostic = diagnostics[diag]; currentDoc.errors.push({ location: { - start: curDiagnostic.range.start, - end: curDiagnostic.range.end, + offset: textDocument.offsetAt(curDiagnostic.range.start), + length: textDocument.offsetAt(curDiagnostic.range.end) - textDocument.offsetAt(curDiagnostic.range.start), }, message: curDiagnostic.message, + severity: curDiagnostic.severity, }); } documentIndex++; @@ -84,26 +86,25 @@ export class YAMLValidation { const currentDoc = yamlDocument.documents[currentYAMLDoc]; currentDoc.errors .concat(currentDoc.warnings) - .forEach(function(error, idx) { + .forEach(function (error, idx) { // remove duplicated messages const signature = - error.location.start + + error.location.offset + ' ' + - error.location.end + + error.location.length + ' ' + error.message; if (!added[signature]) { added[signature] = true; - const range = { - start: textDocument.positionAt(error.location.start), - end: textDocument.positionAt(error.location.end), - }; diagnostics.push({ severity: idx >= currentDoc.errors.length ? DiagnosticSeverity.Warning : DiagnosticSeverity.Error, - range, + range: { + start: textDocument.positionAt(error.location.offset), + end: textDocument.positionAt(error.location.offset + error.location.length), + }, message: error.message, }); } diff --git a/src/languageservice/yamlLanguageTypes.ts b/src/languageservice/yamlLanguageTypes.ts index aa1d2e9..40c8f24 100644 --- a/src/languageservice/yamlLanguageTypes.ts +++ b/src/languageservice/yamlLanguageTypes.ts @@ -1,10 +1,10 @@ import { ASTNode } from './jsonLanguageTypes'; -import { JSONDocument } from './parser/jsonParser'; +import { JSONDocument, IProblem } from './parser/jsonParser'; export class SingleYAMLDocument extends JSONDocument { public lines; - public errors; - public warnings; + public errors: IProblem[]; + public warnings: IProblem[]; constructor(lines: number[]) { super(null, []);