Skip to content

Commit d9e4b96

Browse files
committed
parseTree() returns undefined on empty string input. Fixes #40
1 parent cd4bdf6 commit d9e4b96

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

src/impl/parser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export function parse(text: string, errors: ParseError[] = [], options: ParseOpt
209209
/**
210210
* 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.
211211
*/
212-
export function parseTree(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): Node {
212+
export function parseTree(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): Node | undefined {
213213
let currentParent: NodeImpl = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root
214214

215215
function ensurePropertyComplete(endOffset: number) {
@@ -276,7 +276,7 @@ export function parseTree(text: string, errors: ParseError[] = [], options: Pars
276276
/**
277277
* Finds the node at the given path in a JSON DOM.
278278
*/
279-
export function findNodeAtLocation(root: Node, path: JSONPath): Node | undefined {
279+
export function findNodeAtLocation(root: Node | undefined, path: JSONPath): Node | undefined {
280280
if (!root) {
281281
return undefined;
282282
}

src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export const parse: (text: string, errors?: ParseError[], options?: ParseOptions
106106
/**
107107
* 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.
108108
*/
109-
export const parseTree: (text: string, errors?: ParseError[], options?: ParseOptions) => Node = parser.parseTree;
109+
export const parseTree: (text: string, errors?: ParseError[], options?: ParseOptions) => Node | undefined = parser.parseTree;
110110

111111
/**
112112
* Finds the node at the given path in a JSON DOM.
@@ -167,7 +167,7 @@ export const enum ParseErrorCode {
167167

168168
export function printParseErrorCode(code: ParseErrorCode) {
169169
switch (code) {
170-
case ParseErrorCode.InvalidSymbol: return 'InvalidSymbol'; case ParseErrorCode.InvalidNumberFormat: return 'InvalidNumberFormat';
170+
case ParseErrorCode.InvalidSymbol: return 'InvalidSymbol'; case ParseErrorCode.InvalidNumberFormat: return 'InvalidNumberFormat';
171171
case ParseErrorCode.PropertyNameExpected: return 'PropertyNameExpected';
172172
case ParseErrorCode.ValueExpected: return 'ValueExpected';
173173
case ParseErrorCode.ColonExpected: return 'ColonExpected';

src/test/json.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ function assertTree(input: string, expected: any, expectedErrors: ParseError[] =
5353
var actual = parseTree(input, errors);
5454

5555
assert.deepEqual(errors, expectedErrors);
56-
let checkParent = (node: Node) => {
57-
if (node.children) {
56+
let checkParent = (node: Node | undefined) => {
57+
if (node?.children) {
5858
for (let child of node.children) {
5959
assert.equal(node, child.parent);
6060
delete (<any>child).parent; // delete to avoid recursion in deep equal
@@ -103,8 +103,8 @@ function assertVisit(input: string, expected: VisitorCallback[], expectedErrors:
103103
assert.deepEqual(actuals, expected, JSON.stringify(actuals));
104104
}
105105

106-
function assertNodeAtLocation(input: Node, segments: Segment[], expected: any) {
107-
let actual = findNodeAtLocation(input, segments);
106+
function assertNodeAtLocation(input: Node | undefined, segments: Segment[], expected: any) {
107+
let actual = input && findNodeAtLocation(input, segments);
108108
assert.deepEqual(actual ? getNodeValue(actual) : void 0, expected);
109109
if (actual) {
110110
assert.deepEqual(getNodePath(actual), segments);

0 commit comments

Comments
 (0)