Skip to content

parseTree() returns undefined on empty string input #40

Closed

Description

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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions