fix: fix error type issues

This commit is contained in:
Peng Xiao 2018-12-17 16:27:46 +08:00
parent cb10912b7e
commit 1e0e5ebb54
4 changed files with 40 additions and 31 deletions

View file

@ -9,7 +9,7 @@
"watch": "tsc -p ./src --watch", "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", "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", "lint": "prettier \"{src,test}/**/*.{json,scss,html,ts}\" --write",
"test": "jest" "test": "jest --verbose"
}, },
"author": "Kevin Decker <kpdecker@gmail.com> (http://incaseofstairs.com)", "author": "Kevin Decker <kpdecker@gmail.com> (http://incaseofstairs.com)",
"maintainers": [ "maintainers": [
@ -51,14 +51,11 @@
}, },
"lint-staged": { "lint-staged": {
"linters": { "linters": {
"*.{json,scss,html}": [ "*.{json,scss,html,ts}": [
"prettier --write", "prettier --write",
"git add" "git add"
] ]
}, }
"ignore": [
"src/backend/vendor/**/*"
]
}, },
"husky": { "husky": {
"hooks": { "hooks": {

View file

@ -25,6 +25,7 @@ import {
StringASTNodeImpl, StringASTNodeImpl,
} from './jsonParser'; } from './jsonParser';
import { parseYamlBoolean } from './scalar-type'; import { parseYamlBoolean } from './scalar-type';
import { DiagnosticSeverity } from 'vscode-languageserver-types';
function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode { function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {
if (!node) { if (!node) {
@ -99,10 +100,10 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {
const itemNode = const itemNode =
item === null item === null
? new NullASTNodeImpl( ? new NullASTNodeImpl(
parent, parent,
instance.startPosition, instance.startPosition,
instance.endPosition - instance.startPosition instance.endPosition - instance.startPosition
) )
: recursivelyBuildAst(result, item); : recursivelyBuildAst(result, item);
result.items.push(itemNode); result.items.push(itemNode);
@ -225,10 +226,16 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode {
function convertError(e: Yaml.Error) { function convertError(e: Yaml.Error) {
return { return {
message: `${e.reason}`, 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: { location: {
offset: e.mark.position, offset: e.mark.position - e.mark.column,
code: ErrorCode.Undefined, 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' 'Expected a YAML object, array or literal'
), ),
code: ErrorCode.Undefined, 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'; const duplicateKeyReason = 'duplicate key';
// Patch ontop of yaml-ast-parser to disable duplicate key message on merge key // Patch ontop of yaml-ast-parser to disable duplicate key message on merge key
const isDuplicateAndNotMergeKey = function ( const isDuplicateAndNotMergeKey = function(
error: Yaml.Error, error: Yaml.Error,
yamlText: string yamlText: string
) { ) {

View file

@ -5,10 +5,11 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
'use strict'; 'use strict';
import { DiagnosticSeverity, TextDocument } from 'vscode-languageserver-types'; import { DiagnosticSeverity, TextDocument, Diagnostic } from 'vscode-languageserver-types';
import { LanguageSettings } from '../yamlLanguageService'; import { LanguageSettings } from '../yamlLanguageService';
import { YAMLDocument } from '../yamlLanguageTypes'; import { YAMLDocument } from '../yamlLanguageTypes';
import { JSONSchemaService, ResolvedSchema } from './jsonSchemaService'; import { JSONSchemaService, ResolvedSchema } from './jsonSchemaService';
import { Thenable } from '../jsonLanguageTypes';
export class YAMLValidation { export class YAMLValidation {
private validationEnabled: boolean; private validationEnabled: boolean;
@ -22,14 +23,14 @@ export class YAMLValidation {
} }
} }
public doValidation(textDocument: TextDocument, yamlDocument: YAMLDocument) { public doValidation(textDocument: TextDocument, yamlDocument: YAMLDocument): Thenable<Diagnostic[]> {
if (!this.validationEnabled) { if (!this.validationEnabled) {
return Promise.resolve([]); return Promise.resolve([]);
} }
return this.jsonSchemaService return this.jsonSchemaService
.getSchemaForResource(textDocument.uri) .getSchemaForResource(textDocument.uri)
.then(function(schema) { .then(function (schema) {
const diagnostics = []; const diagnostics: Diagnostic[] = [];
const added = {}; const added = {};
let newSchema = schema; let newSchema = schema;
if (schema) { if (schema) {
@ -53,10 +54,11 @@ export class YAMLValidation {
const curDiagnostic = diagnostics[diag]; const curDiagnostic = diagnostics[diag];
currentDoc.errors.push({ currentDoc.errors.push({
location: { location: {
start: curDiagnostic.range.start, offset: textDocument.offsetAt(curDiagnostic.range.start),
end: curDiagnostic.range.end, length: textDocument.offsetAt(curDiagnostic.range.end) - textDocument.offsetAt(curDiagnostic.range.start),
}, },
message: curDiagnostic.message, message: curDiagnostic.message,
severity: curDiagnostic.severity,
}); });
} }
documentIndex++; documentIndex++;
@ -84,26 +86,25 @@ export class YAMLValidation {
const currentDoc = yamlDocument.documents[currentYAMLDoc]; const currentDoc = yamlDocument.documents[currentYAMLDoc];
currentDoc.errors currentDoc.errors
.concat(currentDoc.warnings) .concat(currentDoc.warnings)
.forEach(function(error, idx) { .forEach(function (error, idx) {
// remove duplicated messages // remove duplicated messages
const signature = const signature =
error.location.start + error.location.offset +
' ' + ' ' +
error.location.end + error.location.length +
' ' + ' ' +
error.message; error.message;
if (!added[signature]) { if (!added[signature]) {
added[signature] = true; added[signature] = true;
const range = {
start: textDocument.positionAt(error.location.start),
end: textDocument.positionAt(error.location.end),
};
diagnostics.push({ diagnostics.push({
severity: severity:
idx >= currentDoc.errors.length idx >= currentDoc.errors.length
? DiagnosticSeverity.Warning ? DiagnosticSeverity.Warning
: DiagnosticSeverity.Error, : DiagnosticSeverity.Error,
range, range: {
start: textDocument.positionAt(error.location.offset),
end: textDocument.positionAt(error.location.offset + error.location.length),
},
message: error.message, message: error.message,
}); });
} }

View file

@ -1,10 +1,10 @@
import { ASTNode } from './jsonLanguageTypes'; import { ASTNode } from './jsonLanguageTypes';
import { JSONDocument } from './parser/jsonParser'; import { JSONDocument, IProblem } from './parser/jsonParser';
export class SingleYAMLDocument extends JSONDocument { export class SingleYAMLDocument extends JSONDocument {
public lines; public lines;
public errors; public errors: IProblem[];
public warnings; public warnings: IProblem[];
constructor(lines: number[]) { constructor(lines: number[]) {
super(null, []); super(null, []);