Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
SLdragon opened this issue Jan 8, 2025 · 4 comments
Open

Comments

@SLdragon
Copy link

SLdragon commented Jan 8, 2025

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 SLdragon added the bug Something isn't working label Jan 8, 2025
@eemeli
Copy link
Owner

eemeli commented Jan 13, 2025

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

@eemeli eemeli removed the bug Something isn't working label Jan 13, 2025
@SLdragon
Copy link
Author

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

@eemeli
Copy link
Owner

eemeli commented Jan 16, 2025

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.

@SLdragon
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants