Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | 1x 1x 1x 1x 1x 1x 329x 329x 329x 1x 73x 73x 73x 10x 10x 10x 10x 10x 8x 8x 8x 8x 10x 10x 10x 10x 7x 7x 7x 7x 7x 7x 10x 4x 4x 2x 2x 2x 10x 10x 10x 1x 1x 1x 9x 9x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 1x 2x 3x | /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { JSONSchemaService, ResolvedSchema, UnresolvedSchema } from './jsonSchemaService'; import { JSONDocument } from '../parser/jsonParser'; import { TextDocument, ErrorCode, PromiseConstructor, Thenable, LanguageSettings, DocumentLanguageSettings, SeverityLevel, Diagnostic, DiagnosticSeverity, Range } from '../jsonLanguageTypes'; import * as nls from 'vscode-nls'; import { JSONSchemaRef, JSONSchema } from '../jsonSchema'; import { isDefined, isBoolean } from '../utils/objects'; const localize = nls.loadMessageBundle(); export class JSONValidation { private jsonSchemaService: JSONSchemaService; private promise: PromiseConstructor; private validationEnabled: boolean | undefined; private commentSeverity: DiagnosticSeverity | undefined; public constructor(jsonSchemaService: JSONSchemaService, promiseConstructor: PromiseConstructor) { this.jsonSchemaService = jsonSchemaService; this.promise = promiseConstructor; this.validationEnabled = true; } public configure(raw: LanguageSettings) { Eif (raw) { this.validationEnabled = raw.validate; this.commentSeverity = raw.allowComments ? undefined : DiagnosticSeverity.Error; } } public doValidation(textDocument: TextDocument, jsonDocument: JSONDocument, documentSettings?: DocumentLanguageSettings, schema?: JSONSchema): Thenable<Diagnostic[]> { Iif (!this.validationEnabled) { return this.promise.resolve([]); } const diagnostics: Diagnostic[] = []; const added: { [signature: string]: boolean } = {}; const addProblem = (problem: Diagnostic) => { // remove duplicated messages const signature = problem.range.start.line + ' ' + problem.range.start.character + ' ' + problem.message; Eif (!added[signature]) { added[signature] = true; diagnostics.push(problem); } }; const getDiagnostics = (schema: ResolvedSchema | undefined) => { let trailingCommaSeverity = documentSettings ? toDiagnosticSeverity(documentSettings.trailingCommas) : DiagnosticSeverity.Error; let commentSeverity = documentSettings ? toDiagnosticSeverity(documentSettings.comments) : this.commentSeverity; if (schema) { Iif (schema.errors.length && jsonDocument.root) { const astRoot = jsonDocument.root; const property = astRoot.type === 'object' ? astRoot.properties[0] : undefined; if (property && property.keyNode.value === '$schema') { const node = property.valueNode || property; const range = Range.create(textDocument.positionAt(node.offset), textDocument.positionAt(node.offset + node.length)); addProblem(Diagnostic.create(range, schema.errors[0], DiagnosticSeverity.Warning, ErrorCode.SchemaResolveError)); } else { const range = Range.create(textDocument.positionAt(astRoot.offset), textDocument.positionAt(astRoot.offset + 1)); addProblem(Diagnostic.create(range, schema.errors[0], DiagnosticSeverity.Warning, ErrorCode.SchemaResolveError)); } } else { const semanticErrors = jsonDocument.validate(textDocument, schema.schema); Eif (semanticErrors) { semanticErrors.forEach(addProblem); } } Iif (schemaAllowsComments(schema.schema)) { commentSeverity = undefined; } Iif (schemaAllowsTrailingCommas(schema.schema)) { trailingCommaSeverity = undefined; } } for (const p of jsonDocument.syntaxErrors) { Eif (p.code === ErrorCode.TrailingComma) { if (typeof trailingCommaSeverity !== 'number') { continue; } p.severity = trailingCommaSeverity; } addProblem(p); } Iif (typeof commentSeverity === 'number') { const message = localize('InvalidCommentToken', 'Comments are not permitted in JSON.'); jsonDocument.comments.forEach(c => { addProblem(Diagnostic.create(c, message, commentSeverity, ErrorCode.CommentNotPermitted)); }); } return diagnostics; }; if (schema) { const id = schema.id || ('schemaservice://untitled/' + idCounter++); return this.jsonSchemaService.resolveSchemaContent(new UnresolvedSchema(schema), id, {}).then(resolvedSchema => { return getDiagnostics(resolvedSchema); }); } return this.jsonSchemaService.getSchemaForResource(textDocument.uri, jsonDocument).then(schema => { return getDiagnostics(schema); }); } } let idCounter = 0; function schemaAllowsComments(schemaRef: JSONSchemaRef): boolean | undefined { Eif (schemaRef && typeof schemaRef === 'object') { Iif (isBoolean(schemaRef.allowComments)) { return schemaRef.allowComments; } Iif (schemaRef.allOf) { for (const schema of schemaRef.allOf) { const allow = schemaAllowsComments(schema); if (isBoolean(allow)) { return allow; } } } } return undefined; } function schemaAllowsTrailingCommas(schemaRef: JSONSchemaRef): boolean | undefined { Eif (schemaRef && typeof schemaRef === 'object') { Iif (isBoolean(schemaRef.allowTrailingCommas)) { return schemaRef.allowTrailingCommas; } const deprSchemaRef = schemaRef as any; Iif (isBoolean(deprSchemaRef['allowsTrailingCommas'])) { // deprecated return deprSchemaRef['allowsTrailingCommas']; } Iif (schemaRef.allOf) { for (const schema of schemaRef.allOf) { const allow = schemaAllowsTrailingCommas(schema); if (isBoolean(allow)) { return allow; } } } } return undefined; } function toDiagnosticSeverity(severityLevel: SeverityLevel | undefined): DiagnosticSeverity | undefined { switch (severityLevel) { case 'error': return DiagnosticSeverity.Error; case 'warning': return DiagnosticSeverity.Warning; case 'ignore': return undefined; } return undefined; } |