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

parseTree - Option to keep comments #28

Open
dsherret opened this issue Oct 28, 2019 · 5 comments
Open

parseTree - Option to keep comments #28

dsherret opened this issue Oct 28, 2019 · 5 comments
Assignees
Labels
feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities
Milestone

Comments

@dsherret
Copy link

Thanks for this library! I wrote a jsonc code formatter using it.

Right now I'm using the scanner to get the comments around the nodes in the tree. It would be slightly faster to not rescan these out and instead get all the comments when calling parseTree.

A simple solution would may be to have an option that returns all the comments in an array. Maybe something like...

export function parseTree(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): Node;

Goes to:

export function parseTree(text: string, options: ParseOptions = ParseOptions.DEFAULT): ParseResult;

interface ParseResult {
	file: Node;
	errors: ParseError[];
	/** Included when `options.includeComments` is true. **/
	comments?: Comment[];
};

Then:

export interface ParseOptions {
	disallowComments?: boolean;
	allowTrailingComma?: boolean;
	allowEmptyContent?: boolean;
}

Goes to:

export interface ParseOptions {
	disallowComments?: boolean;
	includeComments?: boolean;
	allowTrailingComma?: boolean;
	allowEmptyContent?: boolean;
}

Thoughts? I could submit a PR for this, but will just need some direction on how the public API should look.

@aeschli
Copy link
Contributor

aeschli commented Nov 13, 2020

Proposal looks fine with, if you want to make a PR.

@aeschli aeschli added the feature-request Request for new features or functionality label Nov 13, 2020
@aeschli aeschli added this to the Backlog milestone Nov 13, 2020
@aeschli aeschli added the help wanted Issues identified as good community contribution opportunities label Nov 13, 2020
@aeschli aeschli changed the title parseTree - Option to keep comments? parseTree - Option to keep comments Nov 13, 2020
@tjx666
Copy link

tjx666 commented Mar 31, 2023

I want to make a vscode extension to sort the json fileds. I try this package, but can't find a good way to get the comment node info

@aeschli
Copy link
Contributor

aeschli commented Apr 3, 2023

@tjx666 You can use the scanner. Also, the JSONVisitor has a onComment callback. It's still up to you to associate a comment with a node.
Note that we just recently added a JSON: Sort Document command to VS Code

@tjx666
Copy link

tjx666 commented Apr 3, 2023

@aeschli I know that and had make a feature request: microsoft/vscode#175343

@felixhaeberle
Copy link

Hey, I also want to simply keep the comments in the parsed jsonc, modify a property and insert the content back while preserving the comments. Can somebody help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality help wanted Issues identified as good community contribution opportunities
Projects
None yet
Development

No branches or pull requests

4 participants