Skip to content

Commit

Permalink
Proper collect comments from document
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Oct 26, 2021
1 parent de4e8fd commit 3143910
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"timeout": "5000",
"ui": "bdd",
"extension": ["ts"],
"package": "./package.json",
"reporter": "spec",
"watch-files": ["./test/*.test.ts"],
"spec": ["./test/*.test.ts"]
}
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ coverage/
.eslintrc.js
.prettierrc.json
.travis.yml
.mocharc.json
8 changes: 8 additions & 0 deletions src/languageservice/parser/yaml-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/yamlSchemaService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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#');
});
});
});

0 comments on commit 3143910

Please sign in to comment.