commentBefore is null for yaml that has the comment at the beginning
Describe the bug
For yaml file
# yaml-language-server: $schema=https://aka.ms/teams-toolkit/v1.7/yaml.schema.json
# Visit https://aka.ms/teamsfx-v5.0-guide for details on this file
# Visit https://aka.ms/teamsfx-actions for details on actions
version: v1.7
Use below js to parse it:
const yaml = require("yaml")
const fs = require("fs");
const yamlContent = fs.readFileSync("example.yaml", "utf8");
const parsedYaml = yaml.parseDocument(yamlContent);
console.log(parsedYaml.commentBefore);
And the commentBefore is null
To Reproduce Just as above
Expected behaviour commentBefore should not be null
Versions (please complete the following information):
- Environment: 18
yaml: 2.7.0.
Additional context No
The comment is getting attached to the key of the key-value pair.
Thank you @eemeli , however, I cannot find any document about key-value pair, so can you share any code snippet about it?
They're documented here: https://eemeli.org/yaml/#collections
In a parsed document, key: K will always be a node of some sort, extending NodeBase -- see the preceding section of the docs for that.
Thanks for your information, based on the document, I tried below code, and still get undefined result:
const yaml = require("yaml")
const fs = require("fs");
const yamlContent = fs.readFileSync("example.yaml", "utf8");
const parsedYaml = yaml.parseDocument(yamlContent);
console.log(parsedYaml.commentBefore); // null
console.log(parsedYaml.get("version", true).commentBefore); // undefined
console.log(parsedYaml.get("version", true).comment); // undefined
I understand why the comment is attached to the key-value pair in the OP’s example, but I don’t understand it in this case:
# yaml-language-server: $schema=https://aka.ms/teams-toolkit/v1.7/yaml.schema.json
# Visit https://aka.ms/teamsfx-v5.0-guide for details on this file
# Visit https://aka.ms/teamsfx-actions for details on actions
version: v1.7