From cb10912b7ea740172b548607438816fefaacf7bd Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Fri, 14 Dec 2018 14:51:36 +0800 Subject: [PATCH 1/5] test: add jest for testing --- src/languageservice/parser/yamlParser.ts | 2 +- .../services/jsonSchemaService.ts | 3 +- .../services/yamlCompletion.ts | 15 ++- src/languageservice/services/yamlFormatter.ts | 114 ++++++++++-------- src/languageservice/services/yamlHover.ts | 20 ++- src/languageservice/yamlLanguageService.ts | 12 +- test/autoCompletion.test.ts | 3 +- test/formatter.test.ts | 2 +- test/hover3.test.ts | 2 +- test/tsconfig.json | 1 - 10 files changed, 103 insertions(+), 71 deletions(-) diff --git a/src/languageservice/parser/yamlParser.ts b/src/languageservice/parser/yamlParser.ts index dce2327..56deaca 100644 --- a/src/languageservice/parser/yamlParser.ts +++ b/src/languageservice/parser/yamlParser.ts @@ -72,7 +72,7 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode { // TODO: calculate the correct NULL range. const valueNode = instance.value ? recursivelyBuildAst(result, instance.value) - : new NullASTNodeImpl(parent, instance.endPosition); + : new NullASTNodeImpl(result, instance.endPosition); result.keyNode = keyNode; result.valueNode = valueNode; diff --git a/src/languageservice/services/jsonSchemaService.ts b/src/languageservice/services/jsonSchemaService.ts index 0ffcb12..eac57b3 100644 --- a/src/languageservice/services/jsonSchemaService.ts +++ b/src/languageservice/services/jsonSchemaService.ts @@ -556,7 +556,8 @@ export class JSONSchemaService implements IJSONSchemaService { next.anyOf, next.allOf, next.oneOf, - next.items as JSONSchema[] + next.items as JSONSchema[], + next.schemaSequence, ); }; diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index b88b67e..1ca6fd8 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -35,22 +35,19 @@ import { ClientCapabilities, ObjectASTNode, PropertyASTNode, - StringASTNode, } from '../jsonLanguageTypes'; import { matchOffsetToDocument } from '../utils/arrUtils'; import { stringifyObject } from '../utils/json'; import { isDefined } from '../utils/objects'; import { endsWith } from '../utils/strings'; import { YAMLDocument } from '../yamlLanguageTypes'; +import { LanguageSettings } from '../yamlLanguageService'; const localize = nls.loadMessageBundle(); -// !! FIXME: this implementation is buggy - export class YAMLCompletion { + private completionEnabled = true; private supportsMarkdown: boolean | undefined; - private templateVarIdCounter = 0; - constructor( private schemaService: SchemaService.IJSONSchemaService, private contributions: JSONWorkerContribution[] = [], @@ -60,6 +57,12 @@ export class YAMLCompletion { this.contributions = contributions; } + public configure(settings: LanguageSettings) { + if (settings) { + this.completionEnabled = settings.completion; + } + } + public doResolve(item: CompletionItem): Thenable { for (let i = this.contributions.length - 1; i >= 0; i--) { if (this.contributions[i].resolveCompletion) { @@ -88,7 +91,7 @@ export class YAMLCompletion { const currentDocIndex = doc.documents.indexOf(currentDoc); - if (currentDoc === null) { + if (currentDoc === null || !this.completionEnabled) { return Promise.resolve(result); } diff --git a/src/languageservice/services/yamlFormatter.ts b/src/languageservice/services/yamlFormatter.ts index 86f230b..1be9dcd 100644 --- a/src/languageservice/services/yamlFormatter.ts +++ b/src/languageservice/services/yamlFormatter.ts @@ -15,60 +15,70 @@ import { } from 'vscode-languageserver-types'; import { EOL } from '../../fillers/os'; import * as Yaml from '../../yaml-ast-parser/index'; +import { LanguageSettings } from '../yamlLanguageService'; -export function format( - document: TextDocument, - options: FormattingOptions, - customTags: String[] -): TextEdit[] { - const text = document.getText(); - customTags = customTags || []; +export class YamlFormatter { + private customTags: string[]; - const schemaWithAdditionalTags = jsyaml.Schema.create( - customTags.map(tag => { - const typeInfo = tag.split(' '); - return new jsyaml.Type(typeInfo[0], { kind: typeInfo[1] || 'scalar' }); - }) - ); - - // We need compiledTypeMap to be available from schemaWithAdditionalTags before we add the new custom properties - customTags.map(tag => { - const typeInfo = tag.split(' '); - schemaWithAdditionalTags.compiledTypeMap[typeInfo[0]] = new jsyaml.Type( - typeInfo[0], - { kind: typeInfo[1] || 'scalar' } - ); - }); - - const additionalOptions: Yaml.LoadOptions = { - schema: schemaWithAdditionalTags, - }; - - const documents = []; - jsyaml.loadAll(text, doc => documents.push(doc), additionalOptions); - - const dumpOptions = { indent: options.tabSize, noCompatMode: true }; - - let newText; - if (documents.length == 1) { - const yaml = documents[0]; - newText = jsyaml.safeDump(yaml, dumpOptions); - } else { - const formatted = documents.map(d => jsyaml.safeDump(d, dumpOptions)); - newText = - '%YAML 1.2' + - EOL + - '---' + - EOL + - formatted.join('...' + EOL + '---' + EOL) + - '...' + - EOL; + configure(settings: LanguageSettings) { + if (settings) { + this.customTags = settings.customTags || []; + } } - return [ - TextEdit.replace( - Range.create(Position.create(0, 0), document.positionAt(text.length)), - newText - ), - ]; + doFormat( + document: TextDocument, + options: FormattingOptions, + ): TextEdit[] { + const text = document.getText(); + const customTags = this.customTags || []; + + const schemaWithAdditionalTags = jsyaml.Schema.create( + customTags.map(tag => { + const typeInfo = tag.split(' '); + return new jsyaml.Type(typeInfo[0], { kind: typeInfo[1] || 'scalar' }); + }) + ); + + // We need compiledTypeMap to be available from schemaWithAdditionalTags before we add the new custom properties + customTags.map(tag => { + const typeInfo = tag.split(' '); + schemaWithAdditionalTags.compiledTypeMap[typeInfo[0]] = new jsyaml.Type( + typeInfo[0], + { kind: typeInfo[1] || 'scalar' } + ); + }); + + const additionalOptions: Yaml.LoadOptions = { + schema: schemaWithAdditionalTags, + }; + + const documents = []; + jsyaml.loadAll(text, doc => documents.push(doc), additionalOptions); + + const dumpOptions = { indent: options.tabSize, noCompatMode: true }; + + let newText; + if (documents.length == 1) { + const yaml = documents[0]; + newText = jsyaml.safeDump(yaml, dumpOptions); + } else { + const formatted = documents.map(d => jsyaml.safeDump(d, dumpOptions)); + newText = + '%YAML 1.2' + + EOL + + '---' + + EOL + + formatted.join('...' + EOL + '---' + EOL) + + '...' + + EOL; + } + + return [ + TextEdit.replace( + Range.create(Position.create(0, 0), document.positionAt(text.length)), + newText + ), + ]; + } } diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index f316a99..7b1c9b9 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -19,13 +19,22 @@ import { } from 'vscode-languageserver-types'; import { matchOffsetToDocument } from '../utils/arrUtils'; import { YAMLDocument } from '../yamlLanguageTypes'; +import { LanguageSettings } from '../yamlLanguageService'; export class YAMLHover { + private shouldHover = true; + constructor( private schemaService: SchemaService.IJSONSchemaService, private contributions: JSONWorkerContribution[] = [] ) {} + public configure(languageSettings: LanguageSettings){ + if(languageSettings){ + this.shouldHover = !!languageSettings.hover; + } + } + public doHover( document: TextDocument, position: Position, @@ -33,11 +42,11 @@ export class YAMLHover { ): Thenable { const offset = document.offsetAt(position); const currentDoc = matchOffsetToDocument(offset, doc); - if (currentDoc === null) { + if (currentDoc === null || !this.shouldHover) { return Promise.resolve(void 0); } - const currentDocIndex = doc.documents.indexOf(currentDoc); let node = currentDoc.getNodeFromOffset(offset); + const currentDocIndex = doc.documents.indexOf(currentDoc); if ( !node || ((node.type === 'object' || node.type === 'array') && @@ -85,8 +94,13 @@ export class YAMLHover { .getSchemaForResource(document.uri, currentDoc) .then(schema => { if (schema) { + let newSchema = schema; + if (schema.schema && schema.schema.schemaSequence && schema.schema.schemaSequence[currentDocIndex]) { + newSchema = new SchemaService.ResolvedSchema(schema.schema.schemaSequence[currentDocIndex]); + } + const matchingSchemas = currentDoc.getMatchingSchemas( - schema.schema, + newSchema.schema, node.offset ); diff --git a/src/languageservice/yamlLanguageService.ts b/src/languageservice/yamlLanguageService.ts index 0662b2d..486a71d 100644 --- a/src/languageservice/yamlLanguageService.ts +++ b/src/languageservice/yamlLanguageService.ts @@ -27,7 +27,7 @@ import { JSONSchemaService, } from './services/jsonSchemaService'; import { YAMLCompletion } from './services/yamlCompletion'; -import { format } from './services/yamlFormatter'; +import { YamlFormatter } from './services/yamlFormatter'; import { YAMLHover } from './services/yamlHover'; import { YAMLValidation } from './services/yamlValidation'; import { YAMLDocument } from './yamlLanguageTypes'; @@ -38,7 +38,7 @@ export interface LanguageSettings { completion?: boolean; // Setting for whether we want to have completion results isKubernetes?: boolean; // If true then its validating against kubernetes schemas?: any[]; // List of schemas, - customTags?: String[]; // Array of Custom Tags + customTags?: string[]; // Array of Custom Tags } export interface Thenable { @@ -134,6 +134,7 @@ export function getLanguageService( const hover = new YAMLHover(schemaService, contributions); const yamlDocumentSymbols = new YAMLDocumentSymbols(schemaService); const yamlValidation = new YAMLValidation(schemaService); + const yamlFormatter = new YamlFormatter(); return { configure: settings => { @@ -147,6 +148,11 @@ export function getLanguageService( ); }); } + + yamlValidation.configure(settings); + hover.configure(settings); + completer.configure(settings); + yamlFormatter.configure(settings); }, registerCustomSchemaProvider: (schemaProvider: CustomSchemaProvider) => { schemaService.registerCustomSchemaProvider(schemaProvider); @@ -165,7 +171,7 @@ export function getLanguageService( yamlDocumentSymbols ), resetSchema: (uri: string) => schemaService.onResourceChange(uri), - doFormat: format, + doFormat: yamlFormatter.doFormat.bind(yamlFormatter), parseYAMLDocument: (document: TextDocument) => parseYAML(document.getText()), }; diff --git a/test/autoCompletion.test.ts b/test/autoCompletion.test.ts index 46496bf..6792de3 100644 --- a/test/autoCompletion.test.ts +++ b/test/autoCompletion.test.ts @@ -38,7 +38,6 @@ describe('Auto Completion Tests', () => { function parseSetup(content: string, position) { const testTextDocument = setup(content); - const yDoc = parseYAML(testTextDocument.getText()); return completionHelper( testTextDocument, testTextDocument.positionAt(position), @@ -70,7 +69,7 @@ describe('Auto Completion Tests', () => { const completion = parseSetup(content, 12); completion .then(function (result) { - expect(result.items.length).toEqual(0); + expect(result.items.length).toEqual(1); }) .then(done, done); }); diff --git a/test/formatter.test.ts b/test/formatter.test.ts index 4c86757..adb9fbc 100644 --- a/test/formatter.test.ts +++ b/test/formatter.test.ts @@ -48,7 +48,7 @@ describe('Formatter Tests', () => { assert.equal(edits[0].newText, 'cwd: test\n'); }); - it('Formatting works without custom tags', () => { + it('Formatting works with custom tags', () => { const content = `cwd: !Test test`; const testTextDocument = setup(content); const edits = languageService.doFormat( diff --git a/test/hover3.test.ts b/test/hover3.test.ts index 131312d..2a46174 100644 --- a/test/hover3.test.ts +++ b/test/hover3.test.ts @@ -50,7 +50,7 @@ describe('Hover Setting Tests', () => { const hover = parseSetup(content, 1); hover .then(function (result) { - assert.notEqual(result, undefined); + assert.equal(result, undefined); }) .then(done, done); }); diff --git a/test/tsconfig.json b/test/tsconfig.json index 9e3ad39..d051c4a 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -7,7 +7,6 @@ "esModuleInterop": true, "sourceMap": true, "lib": ["es2016"], - "outDir": "./out/server", "downlevelIteration": true }, "exclude": ["node_modules", "out"] From 1e0e5ebb548ef2d4382b57debe1f21a96022ecd7 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Mon, 17 Dec 2018 16:27:46 +0800 Subject: [PATCH 2/5] 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, []); From 84e82b52f74d69e402665fbfbb792dcb3d493e34 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Mon, 17 Dec 2018 16:40:31 +0800 Subject: [PATCH 3/5] style: lint files --- src/languageservice/parser/jsonParser.ts | 26 ++-- src/languageservice/parser/yamlParser.ts | 7 +- .../services/jsonSchemaService.ts | 2 +- src/languageservice/services/yamlFormatter.ts | 5 +- src/languageservice/services/yamlHover.ts | 20 ++- .../services/yamlValidation.ts | 23 ++- test/arrUtils.test.ts | 10 +- test/autoCompletion.test.ts | 66 ++++----- test/autoCompletion2.test.ts | 28 ++-- test/autoCompletion3.test.ts | 12 +- test/documentSymbols.test.ts | 26 ++-- test/formatter.test.ts | 8 +- test/hover.test.ts | 45 +++--- test/hover2.test.ts | 10 +- test/hover3.test.ts | 19 ++- test/integration.test.ts | 56 +++---- test/mulipleDocuments.test.ts | 30 ++-- test/schema.test.ts | 92 ++++++------ test/schemaNotFound.ts | 8 +- test/schemaValidation.test.ts | 138 +++++++++--------- test/testHelper.ts | 18 +-- test/uri.test.ts | 6 +- 22 files changed, 338 insertions(+), 317 deletions(-) diff --git a/src/languageservice/parser/jsonParser.ts b/src/languageservice/parser/jsonParser.ts index 5e50967..c5ce19a 100644 --- a/src/languageservice/parser/jsonParser.ts +++ b/src/languageservice/parser/jsonParser.ts @@ -213,7 +213,7 @@ export interface ISchemaCollector { class SchemaCollector implements ISchemaCollector { public schemas: IApplicableSchema[] = []; - constructor(private focusOffset = -1, private exclude: ASTNode = null) { } + constructor(private focusOffset = -1, private exclude: ASTNode = null) {} public add(schema: IApplicableSchema) { this.schemas.push(schema); } @@ -237,9 +237,9 @@ class NoOpSchemaCollector implements ISchemaCollector { } public static instance = new NoOpSchemaCollector(); - private constructor() { } - public add(schema: IApplicableSchema) { } - public merge(other: ISchemaCollector) { } + private constructor() {} + public add(schema: IApplicableSchema) {} + public merge(other: ISchemaCollector) {} public include(node: ASTNode) { return true; } @@ -389,7 +389,7 @@ export class JSONDocument { public root: ASTNode, public readonly syntaxErrors: Diagnostic[] = [], public readonly comments: Range[] = [] - ) { } + ) {} public getNodeFromOffset( offset: number, @@ -1110,13 +1110,15 @@ function validate( break; } case 'array': { - propertyNode.valueNode.items.forEach((sequenceNode: ObjectASTNode) => { - sequenceNode.properties.forEach(propASTNode => { - const seqKey = propASTNode.keyNode.value; - seenKeys[seqKey] = propASTNode.valueNode; - unprocessedProperties.push(seqKey); - }); - }); + propertyNode.valueNode.items.forEach( + (sequenceNode: ObjectASTNode) => { + sequenceNode.properties.forEach(propASTNode => { + const seqKey = propASTNode.keyNode.value; + seenKeys[seqKey] = propASTNode.valueNode; + unprocessedProperties.push(seqKey); + }); + } + ); break; } default: { diff --git a/src/languageservice/parser/yamlParser.ts b/src/languageservice/parser/yamlParser.ts index db97af9..b15fe25 100644 --- a/src/languageservice/parser/yamlParser.ts +++ b/src/languageservice/parser/yamlParser.ts @@ -226,11 +226,10 @@ 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. + // TODO: YAML ast parser does not give a length for validation error. location: { - offset: e.mark.position - e.mark.column, - length: e.mark.column, + offset: e.mark.position, + length: 0, }, code: ErrorCode.Undefined, severity: e.isWarning diff --git a/src/languageservice/services/jsonSchemaService.ts b/src/languageservice/services/jsonSchemaService.ts index eac57b3..1f50b02 100644 --- a/src/languageservice/services/jsonSchemaService.ts +++ b/src/languageservice/services/jsonSchemaService.ts @@ -557,7 +557,7 @@ export class JSONSchemaService implements IJSONSchemaService { next.allOf, next.oneOf, next.items as JSONSchema[], - next.schemaSequence, + next.schemaSequence ); }; diff --git a/src/languageservice/services/yamlFormatter.ts b/src/languageservice/services/yamlFormatter.ts index 1be9dcd..bc98201 100644 --- a/src/languageservice/services/yamlFormatter.ts +++ b/src/languageservice/services/yamlFormatter.ts @@ -26,10 +26,7 @@ export class YamlFormatter { } } - doFormat( - document: TextDocument, - options: FormattingOptions, - ): TextEdit[] { + doFormat(document: TextDocument, options: FormattingOptions): TextEdit[] { const text = document.getText(); const customTags = this.customTags || []; diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index 7b1c9b9..427a869 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -29,11 +29,11 @@ export class YAMLHover { private contributions: JSONWorkerContribution[] = [] ) {} - public configure(languageSettings: LanguageSettings){ - if(languageSettings){ - this.shouldHover = !!languageSettings.hover; - } - } + public configure(languageSettings: LanguageSettings) { + if (languageSettings) { + this.shouldHover = !!languageSettings.hover; + } + } public doHover( document: TextDocument, @@ -95,8 +95,14 @@ export class YAMLHover { .then(schema => { if (schema) { let newSchema = schema; - if (schema.schema && schema.schema.schemaSequence && schema.schema.schemaSequence[currentDocIndex]) { - newSchema = new SchemaService.ResolvedSchema(schema.schema.schemaSequence[currentDocIndex]); + if ( + schema.schema && + schema.schema.schemaSequence && + schema.schema.schemaSequence[currentDocIndex] + ) { + newSchema = new SchemaService.ResolvedSchema( + schema.schema.schemaSequence[currentDocIndex] + ); } const matchingSchemas = currentDoc.getMatchingSchemas( diff --git a/src/languageservice/services/yamlValidation.ts b/src/languageservice/services/yamlValidation.ts index e01b3e8..6ff703c 100644 --- a/src/languageservice/services/yamlValidation.ts +++ b/src/languageservice/services/yamlValidation.ts @@ -5,7 +5,11 @@ *--------------------------------------------------------------------------------------------*/ 'use strict'; -import { DiagnosticSeverity, TextDocument, Diagnostic } 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'; @@ -23,13 +27,16 @@ export class YAMLValidation { } } - public doValidation(textDocument: TextDocument, yamlDocument: YAMLDocument): Thenable { + public doValidation( + textDocument: TextDocument, + yamlDocument: YAMLDocument + ): Thenable { if (!this.validationEnabled) { return Promise.resolve([]); } return this.jsonSchemaService .getSchemaForResource(textDocument.uri) - .then(function (schema) { + .then(function(schema) { const diagnostics: Diagnostic[] = []; const added = {}; let newSchema = schema; @@ -55,7 +62,9 @@ export class YAMLValidation { currentDoc.errors.push({ location: { offset: textDocument.offsetAt(curDiagnostic.range.start), - length: textDocument.offsetAt(curDiagnostic.range.end) - textDocument.offsetAt(curDiagnostic.range.start), + length: + textDocument.offsetAt(curDiagnostic.range.end) - + textDocument.offsetAt(curDiagnostic.range.start), }, message: curDiagnostic.message, severity: curDiagnostic.severity, @@ -86,7 +95,7 @@ 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.offset + @@ -103,7 +112,9 @@ export class YAMLValidation { : DiagnosticSeverity.Error, range: { start: textDocument.positionAt(error.location.offset), - end: textDocument.positionAt(error.location.offset + error.location.length), + end: textDocument.positionAt( + error.location.offset + error.location.length + ), }, message: error.message, }); diff --git a/test/arrUtils.test.ts b/test/arrUtils.test.ts index ed70ccf..2b393e1 100644 --- a/test/arrUtils.test.ts +++ b/test/arrUtils.test.ts @@ -10,8 +10,8 @@ import { const assert = require('assert'); describe('Array Utils Tests', () => { - describe('Server - Array Utils', function () { - describe('removeDuplicates', function () { + describe('Server - Array Utils', function() { + describe('removeDuplicates', function() { it('Remove one duplicate with property', () => { const obj1 = { test_key: 'test_value', @@ -69,7 +69,7 @@ describe('Array Utils Tests', () => { }); }); - describe('getLineOffsets', function () { + describe('getLineOffsets', function() { it('No offset', () => { const offsets = getLineOffsets(''); assert.equal(offsets.length, 0); @@ -89,7 +89,7 @@ describe('Array Utils Tests', () => { it('Multiple offsets', () => { const offsets = getLineOffsets( - 'first_offset\n second_offset\n third_offset', + 'first_offset\n second_offset\n third_offset' ); assert.equal(offsets.length, 3); assert.equal(offsets[0], 0); @@ -98,7 +98,7 @@ describe('Array Utils Tests', () => { }); }); - describe('removeDuplicatesObj', function () { + describe('removeDuplicatesObj', function() { it('Remove one duplicate with property', () => { const obj1 = { test_key: 'test_value', diff --git a/test/autoCompletion.test.ts b/test/autoCompletion.test.ts index 6792de3..5e9a27d 100644 --- a/test/autoCompletion.test.ts +++ b/test/autoCompletion.test.ts @@ -12,7 +12,7 @@ const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = 'http://json.schemastore.org/bowerrc'; @@ -25,14 +25,14 @@ languageSettings.schemas.push({ uri, fileMatch }); languageService.configure(languageSettings); describe('Auto Completion Tests', () => { - describe('yamlCompletion with bowerrc', function () { - describe('doComplete', function () { + describe('yamlCompletion with bowerrc', function() { + describe('doComplete', function() { function setup(content: string) { return TextDocument.create( 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -40,145 +40,145 @@ describe('Auto Completion Tests', () => { const testTextDocument = setup(content); return completionHelper( testTextDocument, - testTextDocument.positionAt(position), + testTextDocument.positionAt(position) ); } - it('Autocomplete on root node without word', (done) => { + it('Autocomplete on root node without word', done => { const content = ''; const completion = parseSetup(content, 0); completion - .then(function (result) { + .then(function(result) { assert.notEqual(result.items.length, 0); }) .then(done, done); }); - it('Autocomplete on root node with word', (done) => { + it('Autocomplete on root node with word', done => { const content = 'analyt'; const completion = parseSetup(content, 6); completion - .then(function (result) { + .then(function(result) { assert.notEqual(result.items.length, 0); }) .then(done, done); }); - it('Autocomplete on default value (without value content)', (done) => { + it('Autocomplete on default value (without value content)', done => { const content = 'directory: '; const completion = parseSetup(content, 12); completion - .then(function (result) { + .then(function(result) { expect(result.items.length).toEqual(1); }) .then(done, done); }); - it('Autocomplete on default value (with value content)', (done) => { + it('Autocomplete on default value (with value content)', done => { const content = 'directory: bow'; const completion = parseSetup(content, 15); completion - .then(function (result) { + .then(function(result) { assert.notEqual(result.items.length, 0); }) .then(done, done); }); - it('Autocomplete on boolean value (without value content)', (done) => { + it('Autocomplete on boolean value (without value content)', done => { const content = 'analytics: '; const completion = parseSetup(content, 11); completion - .then(function (result) { + .then(function(result) { expect(result.items.length).toEqual(2); }) .then(done, done); }); - it('Autocomplete on boolean value (with value content)', (done) => { + it('Autocomplete on boolean value (with value content)', done => { const content = 'analytics: fal'; const completion = parseSetup(content, 11); completion - .then(function (result) { + .then(function(result) { assert.equal(result.items.length, 2); }) .then(done, done); }); - it('Autocomplete on number value (without value content)', (done) => { + it('Autocomplete on number value (without value content)', done => { const content = 'timeout: '; const completion = parseSetup(content, 9); completion - .then(function (result) { + .then(function(result) { expect(result.items.length).toEqual(1); }) .then(done, done); }); - it('Autocomplete on number value (with value content)', (done) => { + it('Autocomplete on number value (with value content)', done => { const content = 'timeout: 6'; const completion = parseSetup(content, 10); completion - .then(function (result) { + .then(function(result) { assert.equal(result.items.length, 1); }) .then(done, done); }); - it('Autocomplete key in middle of file', (done) => { + it('Autocomplete key in middle of file', done => { const content = 'scripts:\n post'; const completion = parseSetup(content, 11); completion - .then(function (result) { + .then(function(result) { assert.notEqual(result.items.length, 0); }) .then(done, done); }); - it('Autocomplete key in middle of file 2', (done) => { + it('Autocomplete key in middle of file 2', done => { const content = 'scripts:\n postinstall: /test\n preinsta'; const completion = parseSetup(content, 31); completion - .then(function (result) { + .then(function(result) { assert.notEqual(result.items.length, 0); }) .then(done, done); }); - it('Autocomplete does not happen right after :', (done) => { + it('Autocomplete does not happen right after :', done => { const content = 'analytics:'; const completion = parseSetup(content, 9); completion - .then(function (result) { + .then(function(result) { assert.notEqual(result.items.length, 0); }) .then(done, done); }); - it('Autocomplete does not happen right after : under an object', (done) => { + it('Autocomplete does not happen right after : under an object', done => { const content = 'scripts:\n postinstall:'; const completion = parseSetup(content, 21); completion - .then(function (result) { + .then(function(result) { assert.notEqual(result.items.length, 0); }) .then(done, done); }); - it('Autocomplete on multi yaml documents in a single file on root', (done) => { + it('Autocomplete on multi yaml documents in a single file on root', done => { const content = `---\nanalytics: true\n...\n---\n...`; const completion = parseSetup(content, 28); completion - .then(function (result) { + .then(function(result) { assert.notEqual(result.items.length, 0); }) .then(done, done); }); - it('Autocomplete on multi yaml documents in a single file on scalar', (done) => { + it('Autocomplete on multi yaml documents in a single file on scalar', done => { const content = `---\nanalytics: true\n...\n---\njson: \n...`; const completion = parseSetup(content, 34); completion - .then(function (result) { + .then(function(result) { assert.notEqual(result.items.length, 0); }) .then(done, done); diff --git a/test/autoCompletion2.test.ts b/test/autoCompletion2.test.ts index 6c2841b..f2fb120 100644 --- a/test/autoCompletion2.test.ts +++ b/test/autoCompletion2.test.ts @@ -12,7 +12,7 @@ const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = 'http://json.schemastore.org/composer'; @@ -30,7 +30,7 @@ describe('Auto Completion Tests', () => { 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -38,13 +38,13 @@ describe('Auto Completion Tests', () => { const testTextDocument = setup(content); return completionHelper( testTextDocument, - testTextDocument.positionAt(position), + testTextDocument.positionAt(position) ); } describe('yamlCompletion with composer', function() { describe('doComplete', function() { - it('Array autocomplete without word', (done) => { + it('Array autocomplete without word', done => { const content = 'authors:\n - '; const completion = parseSetup(content, 14); completion @@ -54,7 +54,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Array autocomplete without word on array symbol', (done) => { + it('Array autocomplete without word on array symbol', done => { const content = 'authors:\n -'; const completion = parseSetup(content, 13); completion @@ -64,7 +64,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Array autocomplete without word on space before array symbol', (done) => { + it('Array autocomplete without word on space before array symbol', done => { const content = 'authors:\n - name: test\n '; const completion = parseSetup(content, 24); completion @@ -74,7 +74,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Array autocomplete with letter', (done) => { + it('Array autocomplete with letter', done => { const content = 'authors:\n - n'; const completion = parseSetup(content, 14); completion @@ -84,7 +84,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Array autocomplete without word (second item)', (done) => { + it('Array autocomplete without word (second item)', done => { const content = 'authors:\n - name: test\n '; const completion = parseSetup(content, 32); completion @@ -94,7 +94,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Array autocomplete with letter (second item)', (done) => { + it('Array autocomplete with letter (second item)', done => { const content = 'authors:\n - name: test\n e'; const completion = parseSetup(content, 27); completion @@ -104,7 +104,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Autocompletion after array', (done) => { + it('Autocompletion after array', done => { const content = 'authors:\n - name: test\n'; const completion = parseSetup(content, 24); completion @@ -114,7 +114,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Autocompletion after array with depth', (done) => { + it('Autocompletion after array with depth', done => { const content = 'archive:\n exclude:\n - test\n'; const completion = parseSetup(content, 29); completion @@ -124,7 +124,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Autocompletion after array with depth', (done) => { + it('Autocompletion after array with depth', done => { const content = 'autoload:\n classmap:\n - test\n exclude-from-classmap:\n - test\n '; const completion = parseSetup(content, 70); @@ -137,7 +137,7 @@ describe('Auto Completion Tests', () => { }); describe('Failure tests', function() { - it('Autocompletion has no results on value when they are not available', (done) => { + it('Autocompletion has no results on value when they are not available', done => { const content = 'time: '; const completion = parseSetup(content, 6); completion @@ -147,7 +147,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Autocompletion has no results on value when they are not available (with depth)', (done) => { + it('Autocompletion has no results on value when they are not available (with depth)', done => { const content = 'archive:\n exclude:\n - test\n '; const completion = parseSetup(content, 33); completion diff --git a/test/autoCompletion3.test.ts b/test/autoCompletion3.test.ts index 28e9598..7e6433a 100644 --- a/test/autoCompletion3.test.ts +++ b/test/autoCompletion3.test.ts @@ -12,7 +12,7 @@ const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = 'http://json.schemastore.org/asmdef'; @@ -32,7 +32,7 @@ describe('Auto Completion Tests', () => { 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -40,11 +40,11 @@ describe('Auto Completion Tests', () => { const testTextDocument = setup(content); return completionHelper( testTextDocument, - testTextDocument.positionAt(position), + testTextDocument.positionAt(position) ); } - it('Array of enum autocomplete without word on array symbol', (done) => { + it('Array of enum autocomplete without word on array symbol', done => { const content = 'optionalUnityReferences:\n -'; const completion = parseSetup(content, 29); completion @@ -54,7 +54,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Array of enum autocomplete without word', (done) => { + it('Array of enum autocomplete without word', done => { const content = 'optionalUnityReferences:\n - '; const completion = parseSetup(content, 30); completion @@ -64,7 +64,7 @@ describe('Auto Completion Tests', () => { .then(done, done); }); - it('Array of enum autocomplete with letter', (done) => { + it('Array of enum autocomplete with letter', done => { const content = 'optionalUnityReferences:\n - T'; const completion = parseSetup(content, 31); completion diff --git a/test/documentSymbols.test.ts b/test/documentSymbols.test.ts index f602327..eddf55a 100644 --- a/test/documentSymbols.test.ts +++ b/test/documentSymbols.test.ts @@ -12,7 +12,7 @@ const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); // TODO: this suite is outdated and should be updated. @@ -24,7 +24,7 @@ xdescribe('Document Symbols Tests', () => { 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -33,67 +33,67 @@ xdescribe('Document Symbols Tests', () => { const jsonDocument = parseYAML(testTextDocument.getText()); return languageService.findDocumentSymbols( testTextDocument, - jsonDocument, + jsonDocument ); } - it('Document is empty', (done) => { + it('Document is empty', done => { const content = ''; const symbols = parseSetup(content); assert.equal(symbols, null); done(); }); - it('Simple document symbols', (done) => { + it('Simple document symbols', done => { const content = 'cwd: test'; const symbols = parseSetup(content); assert.equal(symbols.length, 1); done(); }); - it('Document Symbols with number', (done) => { + it('Document Symbols with number', done => { const content = 'node1: 10000'; const symbols = parseSetup(content); assert.equal(symbols.length, 1); done(); }); - it('Document Symbols with boolean', (done) => { + it('Document Symbols with boolean', done => { const content = 'node1: False'; const symbols = parseSetup(content); assert.equal(symbols.length, 1); done(); }); - it('Document Symbols with object', (done) => { + it('Document Symbols with object', done => { const content = 'scripts:\n node1: test\n node2: test'; const symbols = parseSetup(content); assert.equal(symbols.length, 3); done(); }); - it('Document Symbols with null', (done) => { + it('Document Symbols with null', done => { const content = 'apiVersion: null'; const symbols = parseSetup(content); assert.equal(symbols.length, 1); done(); }); - it('Document Symbols with array of strings', (done) => { + it('Document Symbols with array of strings', done => { const content = 'items:\n - test\n - test'; const symbols = parseSetup(content); assert.equal(symbols.length, 1); done(); }); - it('Document Symbols with array', (done) => { + it('Document Symbols with array', done => { const content = 'authors:\n - name: Josh\n - email: jp'; const symbols = parseSetup(content); assert.equal(symbols.length, 3); done(); }); - it('Document Symbols with object and array', (done) => { + it('Document Symbols with object and array', done => { const content = 'scripts:\n node1: test\n node2: test\nauthors:\n - name: Josh\n - email: jp'; const symbols = parseSetup(content); @@ -101,7 +101,7 @@ xdescribe('Document Symbols Tests', () => { done(); }); - it('Document Symbols with multi documents', (done) => { + it('Document Symbols with multi documents', done => { const content = '---\nanalytics: true\n...\n---\njson: test\n...'; const symbols = parseSetup(content); assert.equal(symbols.length, 2); diff --git a/test/formatter.test.ts b/test/formatter.test.ts index adb9fbc..28ba1ac 100644 --- a/test/formatter.test.ts +++ b/test/formatter.test.ts @@ -10,7 +10,7 @@ const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = 'http://json.schemastore.org/bowerrc'; @@ -32,7 +32,7 @@ describe('Formatter Tests', () => { 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -42,7 +42,7 @@ describe('Formatter Tests', () => { const testTextDocument = setup(content); const edits = languageService.doFormat( testTextDocument, - {} as FormattingOptions, + {} as FormattingOptions ); assert.notEqual(edits.length, 0); assert.equal(edits[0].newText, 'cwd: test\n'); @@ -53,7 +53,7 @@ describe('Formatter Tests', () => { const testTextDocument = setup(content); const edits = languageService.doFormat( testTextDocument, - {} as FormattingOptions, + {} as FormattingOptions ); assert.notEqual(edits.length, 0); }); diff --git a/test/hover.test.ts b/test/hover.test.ts index 305f6c8..bace1fa 100644 --- a/test/hover.test.ts +++ b/test/hover.test.ts @@ -4,14 +4,17 @@ *--------------------------------------------------------------------------------------------*/ import { TextDocument } from 'vscode-languageserver'; import { parse as parseYAML } from '../src/languageservice/parser/yamlParser'; -import { getLanguageService, LanguageSettings } from '../src/languageservice/yamlLanguageService'; +import { + getLanguageService, + LanguageSettings, +} from '../src/languageservice/yamlLanguageService'; import { schemaRequestService, workspaceContext } from './testHelper'; const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = 'http://json.schemastore.org/bowerrc'; @@ -24,14 +27,14 @@ languageSettings.schemas.push({ uri, fileMatch }); languageService.configure(languageSettings); describe('Hover Tests', () => { - describe('Yaml Hover with bowerrc', function () { - describe('doComplete', function () { + describe('Yaml Hover with bowerrc', function() { + describe('doComplete', function() { function setup(content: string) { return TextDocument.create( 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -41,81 +44,81 @@ describe('Hover Tests', () => { return languageService.doHover( testTextDocument, testTextDocument.positionAt(position), - jsonDocument, + jsonDocument ); } - it('Hover on key on root', (done) => { + it('Hover on key on root', done => { const content = 'cwd: test'; const hover = parseSetup(content, 1); hover - .then(function (result) { + .then(function(result) { assert.notEqual(result.contents.length, 0); }) .then(done, done); }); - it('Hover on value on root', (done) => { + it('Hover on value on root', done => { const content = 'cwd: test'; const hover = parseSetup(content, 6); hover - .then(function (result) { + .then(function(result) { assert.notEqual(result.contents.length, 0); }) .then(done, done); }); - it('Hover on key with depth', (done) => { + it('Hover on key with depth', done => { const content = 'scripts:\n postinstall: test'; const hover = parseSetup(content, 15); hover - .then(function (result) { + .then(function(result) { assert.notEqual(result.contents.length, 0); }) .then(done, done); }); - it('Hover on value with depth', (done) => { + it('Hover on value with depth', done => { const content = 'scripts:\n postinstall: test'; const hover = parseSetup(content, 26); hover - .then(function (result) { + .then(function(result) { assert.notEqual(result.contents.length, 0); }) .then(done, done); }); - it('Hover works on both root node and child nodes works', (done) => { + it('Hover works on both root node and child nodes works', done => { const content = 'scripts:\n postinstall: test'; const firstHover = parseSetup(content, 3); - firstHover.then(function (result) { + firstHover.then(function(result) { assert.notEqual(result.contents.length, 0); }); const secondHover = parseSetup(content, 15); secondHover - .then(function (result) { + .then(function(result) { assert.notEqual(result.contents.length, 0); }) .then(done, done); }); - it('Hover does not show results when there isnt description field', (done) => { + it('Hover does not show results when there isnt description field', done => { const content = 'analytics: true'; const hover = parseSetup(content, 3); hover - .then(function (result) { + .then(function(result) { assert.notEqual(result.contents.length, 0); }) .then(done, done); }); - it('Hover on multi document', (done) => { + it('Hover on multi document', done => { const content = '---\nanalytics: true\n...\n---\njson: test\n...'; const hover = parseSetup(content, 30); hover - .then(function (result) { + .then(function(result) { assert.notEqual(result.contents.length, 0); }) .then(done, done); diff --git a/test/hover2.test.ts b/test/hover2.test.ts index 02324a7..f4af3d9 100644 --- a/test/hover2.test.ts +++ b/test/hover2.test.ts @@ -14,7 +14,7 @@ const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = 'http://json.schemastore.org/composer'; @@ -34,7 +34,7 @@ describe('Hover Tests', () => { 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -44,11 +44,11 @@ describe('Hover Tests', () => { return languageService.doHover( testTextDocument, testTextDocument.positionAt(position), - jsonDocument, + jsonDocument ); } - it('Hover works on array nodes', (done) => { + it('Hover works on array nodes', done => { const content = 'authors:\n - name: Josh'; const hover = parseSetup(content, 14); hover @@ -58,7 +58,7 @@ describe('Hover Tests', () => { .then(done, done); }); - it('Hover works on array nodes 2', (done) => { + it('Hover works on array nodes 2', done => { const content = 'authors:\n - name: Josh\n - email: jp'; const hover = parseSetup(content, 28); hover diff --git a/test/hover3.test.ts b/test/hover3.test.ts index 2a46174..5de2fc4 100644 --- a/test/hover3.test.ts +++ b/test/hover3.test.ts @@ -4,14 +4,17 @@ *--------------------------------------------------------------------------------------------*/ import { TextDocument } from 'vscode-languageserver'; import { parse as parseYAML } from '../src/languageservice/parser/yamlParser'; -import { getLanguageService, LanguageSettings } from '../src/languageservice/yamlLanguageService'; +import { + getLanguageService, + LanguageSettings, +} from '../src/languageservice/yamlLanguageService'; import { schemaRequestService, workspaceContext } from './testHelper'; const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = 'http://json.schemastore.org/bowerrc'; @@ -24,14 +27,14 @@ languageSettings.schemas.push({ uri, fileMatch }); languageService.configure(languageSettings); describe('Hover Setting Tests', () => { - describe('Yaml Hover with bowerrc', function () { - describe('doComplete', function () { + describe('Yaml Hover with bowerrc', function() { + describe('doComplete', function() { function setup(content: string) { return TextDocument.create( 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -41,15 +44,15 @@ describe('Hover Setting Tests', () => { return languageService.doHover( testTextDocument, testTextDocument.positionAt(position), - jsonDocument, + jsonDocument ); } - it('Hover should not return anything', (done) => { + it('Hover should not return anything', done => { const content = 'cwd: test'; const hover = parseSetup(content, 1); hover - .then(function (result) { + .then(function(result) { assert.equal(result, undefined); }) .then(done, done); diff --git a/test/integration.test.ts b/test/integration.test.ts index bc79259..02cba58 100644 --- a/test/integration.test.ts +++ b/test/integration.test.ts @@ -12,7 +12,7 @@ const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = @@ -34,7 +34,7 @@ xdescribe('Kubernetes Integration Tests', () => { 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -46,7 +46,7 @@ xdescribe('Kubernetes Integration Tests', () => { // Validating basic nodes describe('Test that validation does not throw errors', function() { - it('Basic test', (done) => { + it('Basic test', done => { const content = `apiVersion: v1`; const validator = parseSetup(content); validator @@ -56,7 +56,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Basic test on nodes with children', (done) => { + it('Basic test on nodes with children', done => { const content = `metadata:\n name: hello`; const validator = parseSetup(content); validator @@ -66,7 +66,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Advanced test on nodes with children', (done) => { + it('Advanced test on nodes with children', done => { const content = `apiVersion: v1\nmetadata:\n name: test1`; const validator = parseSetup(content); validator @@ -76,7 +76,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Type string validates under children', (done) => { + it('Type string validates under children', done => { const content = `apiVersion: v1\nkind: Pod\nmetadata:\n resourceVersion: test`; const validator = parseSetup(content); validator @@ -87,7 +87,7 @@ xdescribe('Kubernetes Integration Tests', () => { }); describe('Type tests', function() { - it('Type String does not error on valid node', (done) => { + it('Type String does not error on valid node', done => { const content = `apiVersion: v1`; const validator = parseSetup(content); validator @@ -97,7 +97,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Type Boolean does not error on valid node', (done) => { + it('Type Boolean does not error on valid node', done => { const content = `readOnlyRootFilesystem: false`; const validator = parseSetup(content); validator @@ -107,7 +107,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Type Number does not error on valid node', (done) => { + it('Type Number does not error on valid node', done => { const content = `generation: 5`; const validator = parseSetup(content); validator @@ -117,7 +117,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Type Object does not error on valid node', (done) => { + it('Type Object does not error on valid node', done => { const content = `metadata:\n clusterName: tes`; const validator = parseSetup(content); validator @@ -127,7 +127,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Type Array does not error on valid node', (done) => { + it('Type Array does not error on valid node', done => { const content = `items:\n - apiVersion: v1`; const validator = parseSetup(content); validator @@ -140,7 +140,7 @@ xdescribe('Kubernetes Integration Tests', () => { }); describe('Test that validation DOES throw errors', function() { - it('Error when theres no value for a node', (done) => { + it('Error when theres no value for a node', done => { const content = `apiVersion:`; const validator = parseSetup(content); validator @@ -150,7 +150,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Error on incorrect value type (number)', (done) => { + it('Error on incorrect value type (number)', done => { const content = `apiVersion: 1000`; const validator = parseSetup(content); validator @@ -160,7 +160,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Error on incorrect value type (boolean)', (done) => { + it('Error on incorrect value type (boolean)', done => { const content = `apiVersion: False`; const validator = parseSetup(content); validator @@ -170,7 +170,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Error on incorrect value type (string)', (done) => { + it('Error on incorrect value type (string)', done => { const content = `isNonResourceURL: hello_world`; const validator = parseSetup(content); validator @@ -180,7 +180,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Error on incorrect value type (object)', (done) => { + it('Error on incorrect value type (object)', done => { const content = `apiVersion: v1\nkind: Pod\nmetadata:\n name: False`; const validator = parseSetup(content); validator @@ -190,7 +190,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Error on incorrect value type in multiple yaml documents', (done) => { + it('Error on incorrect value type in multiple yaml documents', done => { const content = `---\napiVersion: v1\n...\n---\napiVersion: False\n...`; const validator = parseSetup(content); validator @@ -200,7 +200,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Property error message should be "Unexpected property {$property_name}" when property is not allowed ', (done) => { + it('Property error message should be "Unexpected property {$property_name}" when property is not allowed ', done => { const content = `unknown_node: test`; const validator = parseSetup(content); validator @@ -220,7 +220,7 @@ xdescribe('Kubernetes Integration Tests', () => { 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -229,11 +229,11 @@ xdescribe('Kubernetes Integration Tests', () => { const yDoc = parseYAML(testTextDocument.getText()); return completionHelper( testTextDocument, - testTextDocument.positionAt(position), + testTextDocument.positionAt(position) ); } - it('Autocomplete on root node without word', (done) => { + it('Autocomplete on root node without word', done => { const content = ''; const completion = parseSetup(content, 0); completion @@ -243,7 +243,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Autocomplete on root node with word', (done) => { + it('Autocomplete on root node with word', done => { const content = 'api'; const completion = parseSetup(content, 6); completion @@ -253,7 +253,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Autocomplete on default value (without value content)', (done) => { + it('Autocomplete on default value (without value content)', done => { const content = 'apiVersion: '; const completion = parseSetup(content, 10); completion @@ -263,7 +263,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Autocomplete on default value (with value content)', (done) => { + it('Autocomplete on default value (with value content)', done => { const content = 'apiVersion: v1\nkind: Bin'; const completion = parseSetup(content, 19); completion @@ -273,7 +273,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Autocomplete on boolean value (without value content)', (done) => { + it('Autocomplete on boolean value (without value content)', done => { const content = 'isNonResourceURL: '; const completion = parseSetup(content, 18); completion @@ -283,7 +283,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Autocomplete on boolean value (with value content)', (done) => { + it('Autocomplete on boolean value (with value content)', done => { const content = 'isNonResourceURL: fal'; const completion = parseSetup(content, 21); completion @@ -293,7 +293,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Autocomplete key in middle of file', (done) => { + it('Autocomplete key in middle of file', done => { const content = 'metadata:\n nam'; const completion = parseSetup(content, 14); completion @@ -303,7 +303,7 @@ xdescribe('Kubernetes Integration Tests', () => { .then(done, done); }); - it('Autocomplete key in middle of file 2', (done) => { + it('Autocomplete key in middle of file 2', done => { const content = 'metadata:\n name: test\n cluster'; const completion = parseSetup(content, 31); completion diff --git a/test/mulipleDocuments.test.ts b/test/mulipleDocuments.test.ts index 41da741..f37d528 100644 --- a/test/mulipleDocuments.test.ts +++ b/test/mulipleDocuments.test.ts @@ -34,7 +34,7 @@ function toFsPath(str): string { } const uri = toFsPath( - path.join(__dirname, './fixtures/customMultipleSchemaSequences.json'), + path.join(__dirname, './fixtures/customMultipleSchemaSequences.json') ); const languageSettings: LanguageSettings = { schemas: [], @@ -50,13 +50,13 @@ languageService.configure(languageSettings); describe('Multiple Documents Validation Tests', () => { // Tests for validator - describe('Multiple Documents Validation', function () { + describe('Multiple Documents Validation', function() { function setup(content: string) { return TextDocument.create( 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -64,7 +64,7 @@ describe('Multiple Documents Validation Tests', () => { const testTextDocument = setup(content); const yDoc = parseYAML( testTextDocument.getText(), - languageSettings.customTags, + languageSettings.customTags ); return languageService.doValidation(testTextDocument, yDoc); } @@ -75,11 +75,11 @@ describe('Multiple Documents Validation Tests', () => { return languageService.doHover( testTextDocument, testTextDocument.positionAt(position), - jsonDocument, + jsonDocument ); } - it('Should validate multiple documents', (done) => { + it('Should validate multiple documents', done => { const content = ` name: jack age: 22 @@ -88,56 +88,56 @@ analytics: true `; const validator = validatorSetup(content); validator - .then((result) => { + .then(result => { assert.equal(result.length, 0); }) .then(done, done); }); - it('Should find errors in both documents', (done) => { + it('Should find errors in both documents', done => { const content = `name1: jack age: asd --- cwd: False`; const validator = validatorSetup(content); validator - .then(function (result) { + .then(function(result) { assert.equal(result.length, 3); }) .then(done, done); }); - it('Should find errors in first document', (done) => { + it('Should find errors in first document', done => { const content = `name: jack age: age --- analytics: true`; const validator = validatorSetup(content); validator - .then(function (result) { + .then(function(result) { assert.equal(result.length, 1); }) .then(done, done); }); - it('Should find errors in second document', (done) => { + it('Should find errors in second document', done => { const content = `name: jack age: 22 --- cwd: False`; const validator = validatorSetup(content); validator - .then(function (result) { + .then(function(result) { assert.equal(result.length, 1); }) .then(done, done); }); - it('Should hover in first document', (done) => { + it('Should hover in first document', done => { const content = `name: jack\nage: 22\n---\ncwd: False`; const hover = hoverSetup(content, 1 + content.indexOf('age')); hover - .then(function (result) { + .then(function(result) { assert.notEqual(result.contents.length, 0); assert.equal(result.contents[0], 'The age of this person'); }) diff --git a/test/schema.test.ts b/test/schema.test.ts index b635d61..7eb360e 100644 --- a/test/schema.test.ts +++ b/test/schema.test.ts @@ -25,7 +25,7 @@ const fixtureDocuments = { 'http://schema.management.azure.com/schemas/2015-08-01/Microsoft.Compute.json': 'Microsoft.Compute.json', }; -const requestServiceMock = function (uri: string): Promise { +const requestServiceMock = function(uri: string): Promise { if (uri.length && uri[uri.length - 1] === '#') { uri = uri.substr(0, uri.length - 1); } @@ -48,10 +48,10 @@ const workspaceContext = { }; describe('JSON Schema', () => { - test('Resolving $refs', function (testDone) { + test('Resolving $refs', function(testDone) { const service = new SchemaService.JSONSchemaService( requestServiceMock, - workspaceContext, + workspaceContext ); service.setSchemaContributions({ schemas: { @@ -74,7 +74,7 @@ describe('JSON Schema', () => { service .getResolvedSchema('https://myschemastore/main') - .then((solvedSchema) => { + .then(solvedSchema => { assert.deepEqual(solvedSchema.schema.properties.child, { id: 'https://myschemastore/child', type: 'bool', @@ -83,16 +83,16 @@ describe('JSON Schema', () => { }) .then( () => testDone(), - (error) => { + error => { testDone(error); - }, + } ); }); - test('Resolving $refs 2', function (testDone) { + test('Resolving $refs 2', function(testDone) { const service = new SchemaService.JSONSchemaService( requestServiceMock, - workspaceContext, + workspaceContext ); service.setSchemaContributions({ schemas: { @@ -121,7 +121,7 @@ describe('JSON Schema', () => { service .getResolvedSchema('http://json.schemastore.org/swagger-2.0') - .then((fs) => { + .then(fs => { assert.deepEqual(fs.schema.properties.responseValue, { type: 'object', required: ['$ref'], @@ -130,16 +130,16 @@ describe('JSON Schema', () => { }) .then( () => testDone(), - (error) => { + error => { testDone(error); - }, + } ); }); - test('Resolving $refs 3', function (testDone) { + test('Resolving $refs 3', function(testDone) { const service = new SchemaService.JSONSchemaService( requestServiceMock, - workspaceContext, + workspaceContext ); service.setSchemaContributions({ schemas: { @@ -172,7 +172,7 @@ describe('JSON Schema', () => { service .getResolvedSchema('https://myschemastore/main/schema1.json') - .then((fs) => { + .then(fs => { assert.deepEqual(fs.schema.properties.p1, { type: 'string', enum: ['object'], @@ -188,16 +188,16 @@ describe('JSON Schema', () => { }) .then( () => testDone(), - (error) => { + error => { testDone(error); - }, + } ); }); - test('FileSchema', function (testDone) { + test('FileSchema', function(testDone) { const service = new SchemaService.JSONSchemaService( requestServiceMock, - workspaceContext, + workspaceContext ); service.setSchemaContributions({ @@ -222,22 +222,22 @@ describe('JSON Schema', () => { service .getResolvedSchema('main') - .then((fs) => { + .then(fs => { const section = fs.getSection(['child', 'grandchild']); assert.equal(section.description, 'Meaning of Life'); }) .then( () => testDone(), - (error) => { + error => { testDone(error); - }, + } ); }); - test('Array FileSchema', function (testDone) { + test('Array FileSchema', function(testDone) { const service = new SchemaService.JSONSchemaService( requestServiceMock, - workspaceContext, + workspaceContext ); service.setSchemaContributions({ @@ -265,22 +265,22 @@ describe('JSON Schema', () => { service .getResolvedSchema('main') - .then((fs) => { + .then(fs => { const section = fs.getSection(['child', '0', 'grandchild']); assert.equal(section.description, 'Meaning of Life'); }) .then( () => testDone(), - (error) => { + error => { testDone(error); - }, + } ); }); - test('Missing subschema', function (testDone) { + test('Missing subschema', function(testDone) { const service = new SchemaService.JSONSchemaService( requestServiceMock, - workspaceContext, + workspaceContext ); service.setSchemaContributions({ @@ -299,22 +299,22 @@ describe('JSON Schema', () => { service .getResolvedSchema('main') - .then((fs) => { + .then(fs => { const section = fs.getSection(['child', 'grandchild']); assert.strictEqual(section, null); }) .then( () => testDone(), - (error) => { + error => { testDone(error); - }, + } ); }); - test('Preloaded Schema', function (testDone) { + test('Preloaded Schema', function(testDone) { const service = new SchemaService.JSONSchemaService( requestServiceMock, - workspaceContext, + workspaceContext ); const id = 'https://myschemastore/test1'; const schema: JsonSchema.JSONSchema = { @@ -336,53 +336,53 @@ describe('JSON Schema', () => { service .getSchemaForResource('test.json') - .then((schema) => { + .then(schema => { const section = schema.getSection(['child', 'grandchild']); assert.equal(section.description, 'Meaning of Life'); }) .then( () => testDone(), - (error) => { + error => { testDone(error); - }, + } ); }); - test('Null Schema', function (testDone) { + test('Null Schema', function(testDone) { const service = new SchemaService.JSONSchemaService( requestServiceMock, - workspaceContext, + workspaceContext ); service .getSchemaForResource('test.json') - .then((schema) => { + .then(schema => { assert.equal(schema, null); }) .then( () => testDone(), - (error) => { + error => { testDone(error); - }, + } ); }); - test('Schema not found', function (testDone) { + test('Schema not found', function(testDone) { const service = new SchemaService.JSONSchemaService( requestServiceMock, - workspaceContext, + workspaceContext ); service .loadSchema('test.json') - .then((schema) => { + .then(schema => { assert.notEqual(schema.errors.length, 0); }) .then( () => testDone(), - (error) => { + error => { testDone(error); - }, + } ); }); }); diff --git a/test/schemaNotFound.ts b/test/schemaNotFound.ts index 5090a82..d159866 100644 --- a/test/schemaNotFound.ts +++ b/test/schemaNotFound.ts @@ -11,7 +11,7 @@ const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = 'SchemaDoesNotExist'; @@ -32,7 +32,7 @@ describe('Validation Tests', () => { 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -40,14 +40,14 @@ describe('Validation Tests', () => { const testTextDocument = setup(content); const yDoc = parseYAML( testTextDocument.getText(), - languageSettings.customTags, + languageSettings.customTags ); return languageService.doValidation(testTextDocument, yDoc); } // Validating basic nodes describe('Test that validation throws error when schema is not found', function() { - it('Basic test', (done) => { + it('Basic test', done => { const content = `testing: true`; const validator = parseSetup(content); validator diff --git a/test/schemaValidation.test.ts b/test/schemaValidation.test.ts index 59c4692..01d4a3d 100644 --- a/test/schemaValidation.test.ts +++ b/test/schemaValidation.test.ts @@ -11,7 +11,7 @@ const assert = require('assert'); const languageService = getLanguageService( schemaRequestService, workspaceContext, - [], + [] ); const uri = 'http://json.schemastore.org/bowerrc'; @@ -28,13 +28,13 @@ languageService.configure(languageSettings); describe('Validation Tests', () => { // Tests for validator - describe('Validation', function () { + describe('Validation', function() { function setup(content: string) { return TextDocument.create( 'file://~/Desktop/vscode-k8s/test.yaml', 'yaml', 0, - content, + content ); } @@ -42,154 +42,154 @@ describe('Validation Tests', () => { const testTextDocument = setup(content); const yDoc = parseYAML( testTextDocument.getText(), - languageSettings.customTags, + languageSettings.customTags ); return languageService.doValidation(testTextDocument, yDoc); } // Validating basic nodes - describe('Test that validation does not throw errors', function () { - it('Basic test', (done) => { + describe('Test that validation does not throw errors', function() { + it('Basic test', done => { const content = `analytics: true`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Test that boolean value in quotations is not interpreted as boolean i.e. it errors', (done) => { + it('Test that boolean value in quotations is not interpreted as boolean i.e. it errors', done => { const content = `analytics: "no"`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { assert.notEqual(result.length, 0); }) .then(done, done); }); - it('Test that boolean value without quotations is valid', (done) => { + it('Test that boolean value without quotations is valid', done => { const content = `analytics: no`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Test that boolean is valid when inside strings', (done) => { + it('Test that boolean is valid when inside strings', done => { const content = `cwd: "no"`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Test that boolean is invalid when no strings present and schema wants string', (done) => { + it('Test that boolean is invalid when no strings present and schema wants string', done => { const content = `cwd: no`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { assert.notEqual(result.length, 0); }) .then(done, done); }); - it('Basic test', (done) => { + it('Basic test', done => { const content = `analytics: true`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Basic test on nodes with children', (done) => { + it('Basic test on nodes with children', done => { const content = `scripts:\n preinstall: test1\n postinstall: test2`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Advanced test on nodes with children', (done) => { + it('Advanced test on nodes with children', done => { const content = `analytics: true\ncwd: this\nscripts:\n preinstall: test1\n postinstall: test2`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Type string validates under children', (done) => { + it('Type string validates under children', done => { const content = `registry:\n register: http://test_url.com`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Include with value should not error', (done) => { + it('Include with value should not error', done => { const content = `customize: !include customize.yaml`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Null scalar value should be treated as null', (done) => { + it('Null scalar value should be treated as null', done => { const content = `cwd: Null`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(1); }) .then(done, done); }); - it('Anchor should not not error', (done) => { + it('Anchor should not not error', done => { const content = `default: &DEFAULT\n name: Anchor\nanchor_test:\n <<: *DEFAULT`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Anchor with multiple references should not not error', (done) => { + it('Anchor with multiple references should not not error', done => { const content = `default: &DEFAULT\n name: Anchor\nanchor_test:\n <<: *DEFAULT\nanchor_test2:\n <<: *DEFAULT`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Multiple Anchor in array of references should not not error', (done) => { + it('Multiple Anchor in array of references should not not error', done => { const content = `default: &DEFAULT\n name: Anchor\ncustomname: &CUSTOMNAME\n custom_name: Anchor\nanchor_test:\n <<: [*DEFAULT, *CUSTOMNAME]`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Multiple Anchors being referenced in same level at same time', (done) => { + it('Multiple Anchors being referenced in same level at same time', done => { const content = ` default: &DEFAULT name: Anchor @@ -201,87 +201,87 @@ describe('Validation Tests', () => { `; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Custom Tags without type', (done) => { + it('Custom Tags without type', done => { const content = `analytics: !Test false`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Custom Tags with type', (done) => { + it('Custom Tags with type', done => { const content = `resolvers: !Ref\n - test`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - describe('Type tests', function () { - it('Type String does not error on valid node', (done) => { + describe('Type tests', function() { + it('Type String does not error on valid node', done => { const content = `cwd: this`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Type Boolean does not error on valid node', (done) => { + it('Type Boolean does not error on valid node', done => { const content = `analytics: true`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Type Number does not error on valid node', (done) => { + it('Type Number does not error on valid node', done => { const content = `timeout: 60000`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Type Object does not error on valid node', (done) => { + it('Type Object does not error on valid node', done => { const content = `registry:\n search: http://test_url.com`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Type Array does not error on valid node', (done) => { + it('Type Array does not error on valid node', done => { const content = `resolvers:\n - test\n - test\n - test`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(0); }) .then(done, done); }); - it('Do not error when there are multiple types in schema and theyre valid', (done) => { + it('Do not error when there are multiple types in schema and theyre valid', done => { const content = `license: MIT`; const validator = parseSetup(content); - validator.then(function (result) { + validator.then(function(result) { expect(result.length).toEqual(0); }); done(); @@ -289,82 +289,82 @@ describe('Validation Tests', () => { }); }); - describe('Test that validation DOES throw errors', function () { - it('Error when theres a finished untyped item', (done) => { + describe('Test that validation DOES throw errors', function() { + it('Error when theres a finished untyped item', done => { const content = `cwd: hello\nan`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { assert.notEqual(result.length, 0); }) .then(done, done); }); - it('Error when theres no value for a node', (done) => { + it('Error when theres no value for a node', done => { const content = `cwd:`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { assert.notEqual(result.length, 0); }) .then(done, done); }); - it('Error on incorrect value type (number)', (done) => { + it('Error on incorrect value type (number)', done => { const content = `cwd: 100000`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { assert.notEqual(result.length, 0); }) .then(done, done); }); - it('Error on incorrect value type (boolean)', (done) => { + it('Error on incorrect value type (boolean)', done => { const content = `cwd: False`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { assert.notEqual(result.length, 0); }) .then(done, done); }); - it('Error on incorrect value type (string)', (done) => { + it('Error on incorrect value type (string)', done => { const content = `analytics: hello`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { assert.notEqual(result.length, 0); }) .then(done, done); }); - it('Error on incorrect value type (object)', (done) => { + it('Error on incorrect value type (object)', done => { const content = `scripts: test`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { assert.notEqual(result.length, 0); }) .then(done, done); }); - it('Error on incorrect value type (array)', (done) => { + it('Error on incorrect value type (array)', done => { const content = `resolvers: test`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { assert.notEqual(result.length, 0); }) .then(done, done); }); - it('Include without value should error', (done) => { + it('Include without value should error', done => { const content = `customize: !include`; const validator = parseSetup(content); validator - .then(function (result) { + .then(function(result) { expect(result.length).toEqual(1); }) .then(done, done); diff --git a/test/testHelper.ts b/test/testHelper.ts index d21983b..7077533 100644 --- a/test/testHelper.ts +++ b/test/testHelper.ts @@ -19,7 +19,7 @@ import URI from '../src/languageservice/utils/uri'; namespace VSCodeContentRequest { export const type: RequestType<{}, {}, {}, {}> = new RequestType( - 'vscode/content', + 'vscode/content' ); } @@ -28,7 +28,7 @@ let connection: IConnection = null; if (process.argv.indexOf('--stdio') === -1) { connection = createConnection( new IPCMessageReader(process), - new IPCMessageWriter(process), + new IPCMessageWriter(process) ); } else { connection = createConnection(); @@ -50,7 +50,7 @@ connection.onInitialize( }, }, }; - }, + } ); export let workspaceContext = { @@ -69,24 +69,24 @@ export let schemaRequestService = (uri: string): Thenable => { }); } else if (Strings.startsWith(uri, 'vscode://')) { return connection.sendRequest(VSCodeContentRequest.type, uri).then( - (responseText) => { + responseText => { return responseText; }, - (error) => { + error => { return error.message; - }, + } ); } return xhr({ url: uri, followRedirects: 5 }).then( - (response) => { + response => { return response.responseText; }, (error: XHRResponse) => { return Promise.reject( error.responseText || getErrorStatusDescription(error.status) || - error.toString(), + error.toString() ); - }, + } ); }; diff --git a/test/uri.test.ts b/test/uri.test.ts index 67c485c..b773613 100644 --- a/test/uri.test.ts +++ b/test/uri.test.ts @@ -26,7 +26,7 @@ describe('URI Tests', () => { 'www.foo.com', '/bar.html', 'name=hello', - '123', + '123' ); assert.equal(result.authority, 'www.foo.com'); assert.equal(result.fragment, '123'); @@ -69,14 +69,14 @@ describe('URI Tests', () => { describe('URI toString', function() { it('toString with encoding', () => { const result = URI.parse( - 'http://www.foo.com:8080/bar.html?name=hello#123', + 'http://www.foo.com:8080/bar.html?name=hello#123' ).toString(); assert.equal('http://www.foo.com:8080/bar.html?name%3Dhello#123', result); }); it('toString without encoding', () => { const result = URI.parse( - 'http://www.foo.com/bar.html?name=hello#123', + 'http://www.foo.com/bar.html?name=hello#123' ).toString(true); assert.equal('http://www.foo.com/bar.html?name=hello#123', result); }); From ed907a90964689d68cacf88bafd48f3e40fa3fc4 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Tue, 18 Dec 2018 18:49:13 +0800 Subject: [PATCH 4/5] fix: add colon position to key node --- .vscode/launch.json | 18 +++ src/languageservice/parser/jsonParser.ts | 1 + src/languageservice/parser/yamlParser.ts | 3 + .../services/yamlCompletion.ts | 2 +- src/languageservice/services/yamlHover.ts | 2 +- .../services/yamlValidation.ts | 2 +- src/languageservice/yamlLanguageTypes.ts | 117 +++++++++++++++++- src/yaml-ast-parser/loader.ts | 11 +- src/yaml-ast-parser/yamlAST.ts | 4 +- test/yamlDocument.test.ts | 62 ++++++++++ 10 files changed, 206 insertions(+), 16 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 test/yamlDocument.test.ts diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..4768b35 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "name": "vscode-jest-tests", + "request": "launch", + "args": ["--runInBand"], + "cwd": "${workspaceFolder}", + "console": "integratedTerminal", + "internalConsoleOptions": "neverOpen", + "program": "${workspaceFolder}/node_modules/jest/bin/jest" + } + ] +} diff --git a/src/languageservice/parser/jsonParser.ts b/src/languageservice/parser/jsonParser.ts index c5ce19a..8ab60fc 100644 --- a/src/languageservice/parser/jsonParser.ts +++ b/src/languageservice/parser/jsonParser.ts @@ -142,6 +142,7 @@ export class NumberASTNodeImpl extends ASTNodeImpl implements NumberASTNode { export class StringASTNodeImpl extends ASTNodeImpl implements StringASTNode { public type: 'string' = 'string'; public value: string; + public isKey: boolean; constructor(parent: ASTNode, offset: number, length?: number) { super(parent, offset, length); diff --git a/src/languageservice/parser/yamlParser.ts b/src/languageservice/parser/yamlParser.ts index b15fe25..16ed99d 100644 --- a/src/languageservice/parser/yamlParser.ts +++ b/src/languageservice/parser/yamlParser.ts @@ -61,6 +61,8 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode { instance.endPosition - key.startPosition ); + result.colonOffset = key.colonPosition; + // Technically, this is an arbitrary node in YAML // I doubt we would get a better string representation by parsing it const keyNode = new StringASTNodeImpl( @@ -69,6 +71,7 @@ function recursivelyBuildAst(parent: ASTNode, node: Yaml.YAMLNode): ASTNode { key.endPosition - key.startPosition ); keyNode.value = key.value; + keyNode.isKey = true; // TODO: calculate the correct NULL range. const valueNode = instance.value diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 1ca6fd8..b10d15e 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -59,7 +59,7 @@ export class YAMLCompletion { public configure(settings: LanguageSettings) { if (settings) { - this.completionEnabled = settings.completion; + this.completionEnabled = settings.completion !== false; } } diff --git a/src/languageservice/services/yamlHover.ts b/src/languageservice/services/yamlHover.ts index 427a869..a2f6110 100644 --- a/src/languageservice/services/yamlHover.ts +++ b/src/languageservice/services/yamlHover.ts @@ -31,7 +31,7 @@ export class YAMLHover { public configure(languageSettings: LanguageSettings) { if (languageSettings) { - this.shouldHover = !!languageSettings.hover; + this.shouldHover = languageSettings.hover !== false; } } diff --git a/src/languageservice/services/yamlValidation.ts b/src/languageservice/services/yamlValidation.ts index 6ff703c..d429a53 100644 --- a/src/languageservice/services/yamlValidation.ts +++ b/src/languageservice/services/yamlValidation.ts @@ -23,7 +23,7 @@ export class YAMLValidation { public configure(raw: LanguageSettings) { if (raw) { - this.validationEnabled = raw.validate; + this.validationEnabled = raw.validate !== false; } } diff --git a/src/languageservice/yamlLanguageTypes.ts b/src/languageservice/yamlLanguageTypes.ts index 40c8f24..4eeb95c 100644 --- a/src/languageservice/yamlLanguageTypes.ts +++ b/src/languageservice/yamlLanguageTypes.ts @@ -1,8 +1,9 @@ import { ASTNode } from './jsonLanguageTypes'; import { JSONDocument, IProblem } from './parser/jsonParser'; +import { getPosition } from './utils/documentPositionCalculator'; export class SingleYAMLDocument extends JSONDocument { - public lines; + public lines: number[]; public errors: IProblem[]; public warnings: IProblem[]; @@ -23,6 +24,116 @@ export class SingleYAMLDocument extends JSONDocument { return super.getNodeFromOffset(offset, true); } + // getNodeFromOffsetEndInclusive(offset: number): ASTNode { + // if (!this.root) { + // return; + // } + // if ( + // offset < this.root.offset || + // offset > this.root.offset + this.root.length + // ) { + // // We somehow are completely outside the document + // // This is unexpected + // console.log('Attempting to resolve node outside of document'); + // return null; + // } + + // function* sliding2(nodes: ASTNode[]) { + // for (let i = 0; i < nodes.length; i ++) { + // yield [nodes[i], i === nodes.length ? null : nodes[i + 1]]; + // } + // } + + // const onLaterLine = (offset: number, node: ASTNode) => { + // const { line: actualLine } = getPosition(offset, this.lines); + // const { line: nodeEndLine } = getPosition( + // node.offset + node.length, + // this.lines + // ); + + // return actualLine > nodeEndLine; + // }; + + // let findNode = (nodes: ASTNode[]): ASTNode => { + // if (nodes.length === 0) { + // return null; + // } + + // const gen = sliding2(nodes); + + // for (let [first, second] of gen) { + // const end = second + // ? second.offset + // : first.parent.offset + first.parent.length; + // if (offset >= first.offset && offset < end) { + // const children = first.children; + + // const foundChild = findNode(children); + + // if (!foundChild && onLaterLine(offset, first)) { + // return this.getNodeByIndent(this.lines, offset, this.root); + // } + + // return foundChild || first; + // } + // } + + // return null; + // }; + + // return findNode(this.root.children) || this.root; + // } + + // private getNodeByIndent = ( + // lines: number[], + // offset: number, + // node: ASTNode + // ) => { + // const { line, column: indent } = getPosition(offset, this.lines); + + // const children = node.children; + + // function findNode(children: ASTNode[]) { + // if (children.length > 0) { + + // } + // for (let idx = 0; idx < children.length; idx++) { + // const child = children[idx]; + + // const { line: childLine, column: childCol } = getPosition( + // child.offset, + // lines + // ); + + // if (childCol > indent) { + // return null; + // } + + // const newChildren = child.children; + // const foundNode = findNode(newChildren); + + // if (foundNode) { + // return foundNode; + // } + + // // We have the right indentation, need to return based on line + // if (childLine == line) { + // return child; + // } + // if (childLine > line) { + // // Get previous + // idx - 1 >= 0 ? children[idx - 1] : child; + // } + // // Else continue loop to try next element + // } + + // // Special case, we found the correct + // return children[children.length - 1]; + // } + + // return findNode(children) || node; + // }; + public getNodeFromOffsetEndInclusive(offset: number): ASTNode { const collector: ASTNode[] = []; const findNode = (node: ASTNode): ASTNode => { @@ -60,12 +171,8 @@ export class SingleYAMLDocument extends JSONDocument { export class YAMLDocument { public documents: SingleYAMLDocument[]; - public errors; - public warnings; constructor(documents: SingleYAMLDocument[]) { this.documents = documents; - this.errors = []; - this.warnings = []; } } diff --git a/src/yaml-ast-parser/loader.ts b/src/yaml-ast-parser/loader.ts index 17769d4..16a76d8 100644 --- a/src/yaml-ast-parser/loader.ts +++ b/src/yaml-ast-parser/loader.ts @@ -492,11 +492,7 @@ function storeMappingPair( }); _result.mappings.push(mapping); - _result.endPosition = valueNode - ? valueNode.endPosition - : keyNode.endPosition + 1; // FIXME.workaround should be position of ':' indeed - // } - + _result.endPosition = mapping.endPosition; return _result; } @@ -646,7 +642,8 @@ function readPlainScalar(state: State, nodeIndent, withinFlowCollection) { _lineIndent, _kind = state.kind, _result = state.result, - ch; + ch, + _colonPosition = -1; const state_result = ast.newScalar(); state_result.plainScalar = true; state.result = state_result; @@ -694,6 +691,7 @@ function readPlainScalar(state: State, nodeIndent, withinFlowCollection) { is_WS_OR_EOL(following) || (withinFlowCollection && is_FLOW_INDICATOR(following)) ) { + _colonPosition = state.position; break; } } else if (0x23 /* # */ === ch) { @@ -750,6 +748,7 @@ function readPlainScalar(state: State, nodeIndent, withinFlowCollection) { state_result.startPosition, state_result.endPosition ); + state_result.colonPosition = _colonPosition; return true; } diff --git a/src/yaml-ast-parser/yamlAST.ts b/src/yaml-ast-parser/yamlAST.ts index 1eb4613..b803198 100644 --- a/src/yaml-ast-parser/yamlAST.ts +++ b/src/yaml-ast-parser/yamlAST.ts @@ -20,6 +20,7 @@ export interface YAMLNode extends YAMLDocument { startPosition: number; endPosition: number; kind: Kind; + colonPosition?: number; // Nearest colon position. anchorId?: string; valueObject?: any; parent: YAMLNode; @@ -63,8 +64,7 @@ export interface YamlMap extends YAMLNode { mappings: YAMLMapping[]; } export function newMapping(key: YAMLScalar, value: YAMLNode): YAMLMapping { - const end = value ? value.endPosition : key.endPosition + 1; // FIXME.workaround, end should be defied by position of ':' - // console.log('key: ' + key.value + ' ' + key.startPosition + '..' + key.endPosition + ' ' + value + ' end: ' + end); + const end = value ? value.endPosition : key.colonPosition + 1; const node = { key, value, diff --git a/test/yamlDocument.test.ts b/test/yamlDocument.test.ts new file mode 100644 index 0000000..7a20e78 --- /dev/null +++ b/test/yamlDocument.test.ts @@ -0,0 +1,62 @@ +import { TextDocument } from 'vscode-languageserver'; +import { parse as parseYAML } from '../src/languageservice/parser/yamlParser'; + +describe('SingleYAMLDocument tests', () => { + function setup(content: string) { + return TextDocument.create( + 'file://~/Desktop/vscode-k8s/test.yaml', + 'yaml', + 0, + content + ); + } + + describe('getNodeFromOffsetEndInclusive', () => { + const content = `a : + b: + + `; + function parseSetup(offset: number) { + const yamlDocs = parseYAML(content); + + // Should be one doc only + expect(yamlDocs.documents.length).toBe(1); + return yamlDocs.documents[0].getNodeFromOffsetEndInclusive(offset); + } + + it('0', () => { + const node = parseSetup(0); + expect(node.value).toBe('a'); + expect(node.type).toBe('string'); + }); + + it('1', () => { + const node = parseSetup(1); + expect(node.value).toBe('a'); + expect(node.type).toBe('string'); + }); + + it('2', () => { + const node = parseSetup(2); + expect(node.type).toBe('property'); + }); + + it('6', () => { + const node = parseSetup(6); + expect(node.value).toBe('b'); + expect(node.type).toBe('string'); + }); + + it('7', () => { + const node = parseSetup(7); + expect(node.value).toBe('b'); + expect(node.type).toBe('string'); + }); + + it('8', () => { + const node = parseSetup(8); + expect(node.value).toBe('b'); + expect(node.type).toBe('string'); + }); + }); +}); From 1fea33bd600c65881583519b05702c1b3c7fea93 Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Fri, 21 Dec 2018 17:23:40 +0800 Subject: [PATCH 5/5] test: some more fix to unit tests --- src/languageservice/yamlLanguageTypes.ts | 119 ++--------------------- test/documentSymbols.test.ts | 33 +++++-- test/yamlDocument.test.ts | 54 ++-------- 3 files changed, 44 insertions(+), 162 deletions(-) diff --git a/src/languageservice/yamlLanguageTypes.ts b/src/languageservice/yamlLanguageTypes.ts index 4eeb95c..7ea953c 100644 --- a/src/languageservice/yamlLanguageTypes.ts +++ b/src/languageservice/yamlLanguageTypes.ts @@ -24,116 +24,6 @@ export class SingleYAMLDocument extends JSONDocument { return super.getNodeFromOffset(offset, true); } - // getNodeFromOffsetEndInclusive(offset: number): ASTNode { - // if (!this.root) { - // return; - // } - // if ( - // offset < this.root.offset || - // offset > this.root.offset + this.root.length - // ) { - // // We somehow are completely outside the document - // // This is unexpected - // console.log('Attempting to resolve node outside of document'); - // return null; - // } - - // function* sliding2(nodes: ASTNode[]) { - // for (let i = 0; i < nodes.length; i ++) { - // yield [nodes[i], i === nodes.length ? null : nodes[i + 1]]; - // } - // } - - // const onLaterLine = (offset: number, node: ASTNode) => { - // const { line: actualLine } = getPosition(offset, this.lines); - // const { line: nodeEndLine } = getPosition( - // node.offset + node.length, - // this.lines - // ); - - // return actualLine > nodeEndLine; - // }; - - // let findNode = (nodes: ASTNode[]): ASTNode => { - // if (nodes.length === 0) { - // return null; - // } - - // const gen = sliding2(nodes); - - // for (let [first, second] of gen) { - // const end = second - // ? second.offset - // : first.parent.offset + first.parent.length; - // if (offset >= first.offset && offset < end) { - // const children = first.children; - - // const foundChild = findNode(children); - - // if (!foundChild && onLaterLine(offset, first)) { - // return this.getNodeByIndent(this.lines, offset, this.root); - // } - - // return foundChild || first; - // } - // } - - // return null; - // }; - - // return findNode(this.root.children) || this.root; - // } - - // private getNodeByIndent = ( - // lines: number[], - // offset: number, - // node: ASTNode - // ) => { - // const { line, column: indent } = getPosition(offset, this.lines); - - // const children = node.children; - - // function findNode(children: ASTNode[]) { - // if (children.length > 0) { - - // } - // for (let idx = 0; idx < children.length; idx++) { - // const child = children[idx]; - - // const { line: childLine, column: childCol } = getPosition( - // child.offset, - // lines - // ); - - // if (childCol > indent) { - // return null; - // } - - // const newChildren = child.children; - // const foundNode = findNode(newChildren); - - // if (foundNode) { - // return foundNode; - // } - - // // We have the right indentation, need to return based on line - // if (childLine == line) { - // return child; - // } - // if (childLine > line) { - // // Get previous - // idx - 1 >= 0 ? children[idx - 1] : child; - // } - // // Else continue loop to try next element - // } - - // // Special case, we found the correct - // return children[children.length - 1]; - // } - - // return findNode(children) || node; - // }; - public getNodeFromOffsetEndInclusive(offset: number): ASTNode { const collector: ASTNode[] = []; const findNode = (node: ASTNode): ASTNode => { @@ -167,6 +57,15 @@ export class SingleYAMLDocument extends JSONDocument { } return currMinNode || foundNode; } + + // Returns a node at offset which will be used as a hint for completion + // + public getCompletionNodeFromOffset(offset: number): ASTNode { + if (!this.root) { + return; + } + // TODO + } } export class YAMLDocument { diff --git a/test/documentSymbols.test.ts b/test/documentSymbols.test.ts index eddf55a..0faeb57 100644 --- a/test/documentSymbols.test.ts +++ b/test/documentSymbols.test.ts @@ -15,9 +15,28 @@ const languageService = getLanguageService( [] ); +function traverse( + rootSymbols: DocumentSymbol[], + fn: (doc: DocumentSymbol) => void +) { + if (!rootSymbols || rootSymbols.length === 0) { + return; + } + rootSymbols.forEach(symbol => { + fn(symbol); + traverse(symbol.children, fn); + }); +} + +function allNodes(rootSymbols: DocumentSymbol[]): DocumentSymbol[] { + const res: DocumentSymbol[] = []; + traverse(rootSymbols, doc => res.push(doc)); + return res; +} + // TODO: this suite is outdated and should be updated. // https://github.com/Microsoft/vscode-json-languageservice/blob/master/src/test/documentSymbols.test.ts -xdescribe('Document Symbols Tests', () => { +describe('Document Symbols Tests', () => { describe('Document Symbols Tests', function() { function setup(content: string) { return TextDocument.create( @@ -31,16 +50,18 @@ xdescribe('Document Symbols Tests', () => { function parseSetup(content: string) { const testTextDocument = setup(content); const jsonDocument = parseYAML(testTextDocument.getText()); - return languageService.findDocumentSymbols( + const rootSymbols = languageService.findDocumentSymbols( testTextDocument, jsonDocument ); + + return allNodes(rootSymbols); } it('Document is empty', done => { const content = ''; const symbols = parseSetup(content); - assert.equal(symbols, null); + assert.equal(symbols.length, 0); done(); }); @@ -82,14 +103,14 @@ xdescribe('Document Symbols Tests', () => { it('Document Symbols with array of strings', done => { const content = 'items:\n - test\n - test'; const symbols = parseSetup(content); - assert.equal(symbols.length, 1); + assert.equal(symbols.length, 3); done(); }); it('Document Symbols with array', done => { const content = 'authors:\n - name: Josh\n - email: jp'; const symbols = parseSetup(content); - assert.equal(symbols.length, 3); + assert.equal(symbols.length, 5); done(); }); @@ -97,7 +118,7 @@ xdescribe('Document Symbols Tests', () => { const content = 'scripts:\n node1: test\n node2: test\nauthors:\n - name: Josh\n - email: jp'; const symbols = parseSetup(content); - assert.equal(symbols.length, 6); + assert.equal(symbols.length, 8); done(); }); diff --git a/test/yamlDocument.test.ts b/test/yamlDocument.test.ts index 7a20e78..85c7e07 100644 --- a/test/yamlDocument.test.ts +++ b/test/yamlDocument.test.ts @@ -3,60 +3,22 @@ import { parse as parseYAML } from '../src/languageservice/parser/yamlParser'; describe('SingleYAMLDocument tests', () => { function setup(content: string) { - return TextDocument.create( - 'file://~/Desktop/vscode-k8s/test.yaml', - 'yaml', - 0, - content - ); - } - - describe('getNodeFromOffsetEndInclusive', () => { - const content = `a : - b: - - `; - function parseSetup(offset: number) { + return (offset: number) => { const yamlDocs = parseYAML(content); // Should be one doc only expect(yamlDocs.documents.length).toBe(1); - return yamlDocs.documents[0].getNodeFromOffsetEndInclusive(offset); - } + return yamlDocs.documents[0].getCompletionNodeFromOffset(offset); + }; + } - it('0', () => { - const node = parseSetup(0); - expect(node.value).toBe('a'); - expect(node.type).toBe('string'); - }); - - it('1', () => { + describe('getCompletionNodeFromOffset for simple null value mapping', () => { + const content = 'a : \n '; + const parseSetup = setup(content); + it('within value of `a`', () => { const node = parseSetup(1); expect(node.value).toBe('a'); expect(node.type).toBe('string'); }); - - it('2', () => { - const node = parseSetup(2); - expect(node.type).toBe('property'); - }); - - it('6', () => { - const node = parseSetup(6); - expect(node.value).toBe('b'); - expect(node.type).toBe('string'); - }); - - it('7', () => { - const node = parseSetup(7); - expect(node.value).toBe('b'); - expect(node.type).toBe('string'); - }); - - it('8', () => { - const node = parseSetup(8); - expect(node.value).toBe('b'); - expect(node.type).toBe('string'); - }); }); });