Skip to content

Commit 4ef98a3

Browse files
committed
default parse options
1 parent 499e95b commit 4ef98a3

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/impl/parser.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import {
1010
ModificationOptions, ParseError, ParseErrorCode, Location, Segment, ParseOptions, JSONVisitor
1111
} from '../main';
1212

13+
namespace ParseOptions {
14+
export const DEFAULT = {
15+
allowTrailingComma: false
16+
};
17+
}
18+
1319
interface NodeImpl extends Node {
1420
type: NodeType;
1521
value?: any;
@@ -144,7 +150,7 @@ export function getLocation(text: string, position: number): Location {
144150
* Parses the given text and returns the object the JSON content represents. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
145151
* Therefore always check the errors list to find out if the input was valid.
146152
*/
147-
export function parse(text: string, errors: ParseError[] = [], options?: ParseOptions): any {
153+
export function parse(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): any {
148154
let currentProperty: string | null = null;
149155
let currentParent: any = [];
150156
let previousParents: any[] = [];
@@ -194,7 +200,7 @@ export function parse(text: string, errors: ParseError[] = [], options?: ParseOp
194200
/**
195201
* 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.
196202
*/
197-
export function parseTree(text: string, errors: ParseError[] = [], options?: ParseOptions): Node {
203+
export function parseTree(text: string, errors: ParseError[] = [], options: ParseOptions = ParseOptions.DEFAULT): Node {
198204
let currentParent: NodeImpl = { type: 'array', offset: -1, length: -1, children: [], parent: void 0 }; // artificial root
199205

200206
function ensurePropertyComplete(endOffset: number) {
@@ -366,7 +372,7 @@ export function findNodeAtOffset(node: Node, offset: number, includeRightBound =
366372
/**
367373
* Parses the given text and invokes the visitor functions for each object, array and literal reached.
368374
*/
369-
export function visit(text: string, visitor: JSONVisitor, options?: ParseOptions): any {
375+
export function visit(text: string, visitor: JSONVisitor, options: ParseOptions = ParseOptions.DEFAULT): any {
370376

371377
let _scanner = createScanner(text, false);
372378

src/impl/scanner.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,12 @@ export function createScanner(text: string, ignoreTrivia: boolean = false): JSON
258258
if (text.charCodeAt(pos + 1) === CharacterCodes.asterisk) {
259259
pos += 2;
260260

261+
let safeLength = len - 1; // For lookahead.
261262
let commentClosed = false;
262-
while (pos < len) {
263+
while (pos < safeLength) {
263264
let ch = text.charCodeAt(pos);
264265

265-
if (ch === CharacterCodes.asterisk && (pos + 1 < len) && text.charCodeAt(pos + 1) === CharacterCodes.slash) {
266+
if (ch === CharacterCodes.asterisk && text.charCodeAt(pos + 1) === CharacterCodes.slash) {
266267
pos += 2;
267268
commentClosed = true;
268269
break;

0 commit comments

Comments
 (0)