yaml icon indicating copy to clipboard operation
yaml copied to clipboard

commentBefore is null for yaml that has the comment at the beginning

Open SLdragon opened this issue 11 months ago • 5 comments

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

SLdragon avatar Jan 08 '25 08:01 SLdragon

The comment is getting attached to the key of the key-value pair.

eemeli avatar Jan 13 '25 12:01 eemeli

Thank you @eemeli , however, I cannot find any document about key-value pair, so can you share any code snippet about it?

SLdragon avatar Jan 16 '25 05:01 SLdragon

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.

eemeli avatar Jan 16 '25 08:01 eemeli

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

SLdragon avatar Jan 27 '25 06:01 SLdragon

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

jirutka avatar Apr 08 '25 15:04 jirutka