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() returns undefined on empty string input #40

Closed
ivanhernandez13 opened this issue Jul 28, 2020 · 1 comment
Closed

parseTree() returns undefined on empty string input #40

ivanhernandez13 opened this issue Jul 28, 2020 · 1 comment
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug
Milestone

Comments

@ivanhernandez13
Copy link

I ran into an issue with jsonc.parseTree when my input file was an empty file thus causing the text parameter to be an empty string. My code did something very similar to the following:

const rawFileContents = (await readFile(myFilePath)).toString();
const root = jsonc.parseTree(rawFileContents, parseErrors);

if (root.type === MY_EXPECTED_TYPE) {/* do something. */}
else { /* handle invalid file */ }

This causes an exception at root.type when the file at myFilePath is empty because root is undefined.

Is this WAI or should the function signature below include undefined or the function itself return an empty Node when text is the empty string?

/**
 * Parses the given text and returns a tree representation the JSON content. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
 */
export declare function parseTree(text: string, errors?: ParseError[], options?: ParseOptions): Node;
@aeschli
Copy link
Contributor

aeschli commented Nov 13, 2020

It's a bug in the spec. parseTree can return undefined for empty inputs.

@aeschli aeschli added this to the November 2020 milestone Nov 13, 2020
@aeschli aeschli added the bug Issue identified by VS Code Team member as probable bug label Nov 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

2 participants