diff --git a/.mocharc.json b/.mocharc.json new file mode 100644 index 00000000..b445eb05 --- /dev/null +++ b/.mocharc.json @@ -0,0 +1,9 @@ +{ + "timeout": "5000", + "ui": "bdd", + "extension": ["ts"], + "package": "./package.json", + "reporter": "spec", + "watch-files": ["./test/*.test.ts"], + "spec": ["./test/*.test.ts"] +} diff --git a/.npmignore b/.npmignore index ad888e54..8103daf5 100644 --- a/.npmignore +++ b/.npmignore @@ -10,3 +10,4 @@ coverage/ .eslintrc.js .prettierrc.json .travis.yml +.mocharc.json diff --git a/src/languageservice/parser/yaml-documents.ts b/src/languageservice/parser/yaml-documents.ts index 7b16fff1..a5c850f6 100644 --- a/src/languageservice/parser/yaml-documents.ts +++ b/src/languageservice/parser/yaml-documents.ts @@ -44,7 +44,15 @@ export class SingleYAMLDocument extends JSONDocument { if (node?.commentBefore) { this._lineComments.push(`#${node.commentBefore}`); } + + if (node?.comment) { + this._lineComments.push(`#${node.comment}`); + } }); + + if (this._internalDocument.comment) { + this._lineComments.push(`#${this._internalDocument.comment}`); + } } set internalDocument(document: Document) { diff --git a/test/yamlSchemaService.test.ts b/test/yamlSchemaService.test.ts index 9b4315e4..ebf7f82b 100644 --- a/test/yamlSchemaService.test.ts +++ b/test/yamlSchemaService.test.ts @@ -91,5 +91,16 @@ describe('YAML Schema Service', () => { expect(schema.schema.required).is.undefined; expect(schema.schema.definitions.bar.type).eqls('string'); }); + + it('should handle modeline schema comment in the middle of file', () => { + const documentContent = `foo:\n bar\n# yaml-language-server: $schema=https://json-schema.org/draft-07/schema#\naa:bbb\n`; + const content = `${documentContent}`; + const yamlDock = parse(content); + + const service = new SchemaService.YAMLSchemaService(requestServiceMock); + service.getSchemaForResource('', yamlDock.documents[0]); + + expect(requestServiceMock).calledOnceWith('https://json-schema.org/draft-07/schema#'); + }); }); });