jsonc-parser
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -0,4 +1,10 @@ | ||
3.2.0 2022-08-30 | ||
================= | ||
- update the version of the bundled Javascript files to `es2020`. | ||
- include all `const enum` values in the bundled JavaScript files (`ScanError`, `SyntaxKind`, `ParseErrorCode`). | ||
3.1.0 2022-07-07 | ||
================== | ||
* added new API `FormattingOptions.keepLines` : It leaves the initial line positions in the formatting | ||
* added new API `FormattingOptions.keepLines` : It leaves the initial line positions in the formatting. | ||
@@ -5,0 +11,0 @@ 3.0.0 2020-11-13 |
@@ -6,13 +6,2 @@ /*--------------------------------------------------------------------------------------------- | ||
'use strict'; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
import { format, isEOL } from './format'; | ||
@@ -24,8 +13,7 @@ import { parseTree, findNodeAtLocation } from './parser'; | ||
export function setProperty(text, originalPath, value, options) { | ||
var _a; | ||
var path = originalPath.slice(); | ||
var errors = []; | ||
var root = parseTree(text, errors); | ||
var parent = void 0; | ||
var lastSegment = void 0; | ||
const path = originalPath.slice(); | ||
const errors = []; | ||
const root = parseTree(text, errors); | ||
let parent = void 0; | ||
let lastSegment = void 0; | ||
while (path.length > 0) { | ||
@@ -36,3 +24,3 @@ lastSegment = path.pop(); | ||
if (typeof lastSegment === 'string') { | ||
value = (_a = {}, _a[lastSegment] = value, _a); | ||
value = { [lastSegment]: value }; | ||
} | ||
@@ -55,3 +43,3 @@ else { | ||
else if (parent.type === 'object' && typeof lastSegment === 'string' && Array.isArray(parent.children)) { | ||
var existing = findNodeAtLocation(parent, [lastSegment]); | ||
const existing = findNodeAtLocation(parent, [lastSegment]); | ||
if (existing !== void 0) { | ||
@@ -62,8 +50,8 @@ if (value === void 0) { // delete | ||
} | ||
var propertyIndex = parent.children.indexOf(existing.parent); | ||
var removeBegin = void 0; | ||
var removeEnd = existing.parent.offset + existing.parent.length; | ||
const propertyIndex = parent.children.indexOf(existing.parent); | ||
let removeBegin; | ||
let removeEnd = existing.parent.offset + existing.parent.length; | ||
if (propertyIndex > 0) { | ||
// remove the comma of the previous node | ||
var previous = parent.children[propertyIndex - 1]; | ||
let previous = parent.children[propertyIndex - 1]; | ||
removeBegin = previous.offset + previous.length; | ||
@@ -75,3 +63,3 @@ } | ||
// remove the comma of the next node | ||
var next = parent.children[1]; | ||
let next = parent.children[1]; | ||
removeEnd = next.offset; | ||
@@ -91,7 +79,7 @@ } | ||
} | ||
var newProperty = "".concat(JSON.stringify(lastSegment), ": ").concat(JSON.stringify(value)); | ||
var index = options.getInsertionIndex ? options.getInsertionIndex(parent.children.map(function (p) { return p.children[0].value; })) : parent.children.length; | ||
var edit = void 0; | ||
const newProperty = `${JSON.stringify(lastSegment)}: ${JSON.stringify(value)}`; | ||
const index = options.getInsertionIndex ? options.getInsertionIndex(parent.children.map(p => p.children[0].value)) : parent.children.length; | ||
let edit; | ||
if (index > 0) { | ||
var previous = parent.children[index - 1]; | ||
let previous = parent.children[index - 1]; | ||
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty }; | ||
@@ -109,7 +97,7 @@ } | ||
else if (parent.type === 'array' && typeof lastSegment === 'number' && Array.isArray(parent.children)) { | ||
var insertIndex = lastSegment; | ||
const insertIndex = lastSegment; | ||
if (insertIndex === -1) { | ||
// Insert | ||
var newProperty = "".concat(JSON.stringify(value)); | ||
var edit = void 0; | ||
const newProperty = `${JSON.stringify(value)}`; | ||
let edit; | ||
if (parent.children.length === 0) { | ||
@@ -119,3 +107,3 @@ edit = { offset: parent.offset + 1, length: 0, content: newProperty }; | ||
else { | ||
var previous = parent.children[parent.children.length - 1]; | ||
const previous = parent.children[parent.children.length - 1]; | ||
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty }; | ||
@@ -127,5 +115,5 @@ } | ||
// Removal | ||
var removalIndex = lastSegment; | ||
var toRemove = parent.children[removalIndex]; | ||
var edit = void 0; | ||
const removalIndex = lastSegment; | ||
const toRemove = parent.children[removalIndex]; | ||
let edit; | ||
if (parent.children.length === 1) { | ||
@@ -137,6 +125,6 @@ // only item | ||
// last item | ||
var previous = parent.children[removalIndex - 1]; | ||
var offset = previous.offset + previous.length; | ||
var parentEndOffset = parent.offset + parent.length; | ||
edit = { offset: offset, length: parentEndOffset - 2 - offset, content: '' }; | ||
let previous = parent.children[removalIndex - 1]; | ||
let offset = previous.offset + previous.length; | ||
let parentEndOffset = parent.offset + parent.length; | ||
edit = { offset, length: parentEndOffset - 2 - offset, content: '' }; | ||
} | ||
@@ -149,6 +137,6 @@ else { | ||
else if (value !== void 0) { | ||
var edit = void 0; | ||
var newProperty = "".concat(JSON.stringify(value)); | ||
let edit; | ||
const newProperty = `${JSON.stringify(value)}`; | ||
if (!options.isArrayInsertion && parent.children.length > lastSegment) { | ||
var toModify = parent.children[lastSegment]; | ||
const toModify = parent.children[lastSegment]; | ||
edit = { offset: toModify.offset, length: toModify.length, content: newProperty }; | ||
@@ -160,4 +148,4 @@ } | ||
else { | ||
var index = lastSegment > parent.children.length ? parent.children.length : lastSegment; | ||
var previous = parent.children[index - 1]; | ||
const index = lastSegment > parent.children.length ? parent.children.length : lastSegment; | ||
const previous = parent.children[index - 1]; | ||
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty }; | ||
@@ -168,7 +156,7 @@ } | ||
else { | ||
throw new Error("Can not ".concat(value === void 0 ? 'remove' : (options.isArrayInsertion ? 'insert' : 'modify'), " Array index ").concat(insertIndex, " as length is not sufficient")); | ||
throw new Error(`Can not ${value === void 0 ? 'remove' : (options.isArrayInsertion ? 'insert' : 'modify')} Array index ${insertIndex} as length is not sufficient`); | ||
} | ||
} | ||
else { | ||
throw new Error("Can not add ".concat(typeof lastSegment !== 'number' ? 'index' : 'property', " to parent of type ").concat(parent.type)); | ||
throw new Error(`Can not add ${typeof lastSegment !== 'number' ? 'index' : 'property'} to parent of type ${parent.type}`); | ||
} | ||
@@ -181,6 +169,6 @@ } | ||
// apply the edit | ||
var newText = applyEdit(text, edit); | ||
let newText = applyEdit(text, edit); | ||
// format the new text | ||
var begin = edit.offset; | ||
var end = edit.offset + edit.content.length; | ||
let begin = edit.offset; | ||
let end = edit.offset + edit.content.length; | ||
if (edit.length === 0 || edit.content.length === 0) { // insert or remove | ||
@@ -194,13 +182,13 @@ while (begin > 0 && !isEOL(newText, begin - 1)) { | ||
} | ||
var edits = format(newText, { offset: begin, length: end - begin }, __assign(__assign({}, options.formattingOptions), { keepLines: false })); | ||
const edits = format(newText, { offset: begin, length: end - begin }, { ...options.formattingOptions, keepLines: false }); | ||
// apply the formatting edits and track the begin and end offsets of the changes | ||
for (var i = edits.length - 1; i >= 0; i--) { | ||
var edit_1 = edits[i]; | ||
newText = applyEdit(newText, edit_1); | ||
begin = Math.min(begin, edit_1.offset); | ||
end = Math.max(end, edit_1.offset + edit_1.length); | ||
end += edit_1.content.length - edit_1.length; | ||
for (let i = edits.length - 1; i >= 0; i--) { | ||
const edit = edits[i]; | ||
newText = applyEdit(newText, edit); | ||
begin = Math.min(begin, edit.offset); | ||
end = Math.max(end, edit.offset + edit.length); | ||
end += edit.content.length - edit.length; | ||
} | ||
// create a single edit with all changes | ||
var editLength = text.length - (newText.length - end) - begin; | ||
const editLength = text.length - (newText.length - end) - begin; | ||
return [{ offset: begin, length: editLength, content: newText.substring(begin, end) }]; | ||
@@ -207,0 +195,0 @@ } |
@@ -8,7 +8,7 @@ /*--------------------------------------------------------------------------------------------- | ||
export function format(documentText, range, options) { | ||
var initialIndentLevel; | ||
var formatText; | ||
var formatTextStart; | ||
var rangeStart; | ||
var rangeEnd; | ||
let initialIndentLevel; | ||
let formatText; | ||
let formatTextStart; | ||
let rangeStart; | ||
let rangeEnd; | ||
if (range) { | ||
@@ -21,3 +21,3 @@ rangeStart = range.offset; | ||
} | ||
var endOffset = rangeEnd; | ||
let endOffset = rangeEnd; | ||
while (endOffset < documentText.length && !isEOL(documentText, endOffset)) { | ||
@@ -36,6 +36,6 @@ endOffset++; | ||
} | ||
var eol = getEOL(options, documentText); | ||
var numberLineBreaks = 0; | ||
var indentLevel = 0; | ||
var indentValue; | ||
const eol = getEOL(options, documentText); | ||
let numberLineBreaks = 0; | ||
let indentLevel = 0; | ||
let indentValue; | ||
if (options.insertSpaces) { | ||
@@ -47,4 +47,4 @@ indentValue = repeat(' ', options.tabSize || 4); | ||
} | ||
var scanner = createScanner(formatText, false); | ||
var hasError = false; | ||
let scanner = createScanner(formatText, false); | ||
let hasError = false; | ||
function newLinesAndIndent() { | ||
@@ -59,3 +59,3 @@ if (numberLineBreaks > 1) { | ||
function scanNext() { | ||
var token = scanner.scan(); | ||
let token = scanner.scan(); | ||
numberLineBreaks = 0; | ||
@@ -74,3 +74,3 @@ while (token === 15 /* SyntaxKind.Trivia */ || token === 14 /* SyntaxKind.LineBreakTrivia */) { | ||
} | ||
var editOperations = []; | ||
const editOperations = []; | ||
function addEdit(text, startOffset, endOffset) { | ||
@@ -81,3 +81,3 @@ if (!hasError && (!range || (startOffset < rangeEnd && endOffset > rangeStart)) && documentText.substring(startOffset, endOffset) !== text) { | ||
} | ||
var firstToken = scanNext(); | ||
let firstToken = scanNext(); | ||
if (options.keepLines && numberLineBreaks > 0) { | ||
@@ -87,13 +87,13 @@ addEdit(repeat(eol, numberLineBreaks), 0, 0); | ||
if (firstToken !== 17 /* SyntaxKind.EOF */) { | ||
var firstTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
var initialIndent = repeat(indentValue, initialIndentLevel); | ||
let firstTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
let initialIndent = repeat(indentValue, initialIndentLevel); | ||
addEdit(initialIndent, formatTextStart, firstTokenStart); | ||
} | ||
while (firstToken !== 17 /* SyntaxKind.EOF */) { | ||
var firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; | ||
var secondToken = scanNext(); | ||
var replaceContent = ''; | ||
var needsLineBreak = false; | ||
let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; | ||
let secondToken = scanNext(); | ||
let replaceContent = ''; | ||
let needsLineBreak = false; | ||
while (numberLineBreaks === 0 && (secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */)) { | ||
var commentTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
let commentTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
addEdit(' ', firstTokenEnd, commentTokenStart); | ||
@@ -210,3 +210,3 @@ firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; | ||
} | ||
var secondTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
const secondTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
addEdit(replaceContent, firstTokenEnd, secondTokenStart); | ||
@@ -218,4 +218,4 @@ firstToken = secondToken; | ||
function repeat(s, count) { | ||
var result = ''; | ||
for (var i = 0; i < count; i++) { | ||
let result = ''; | ||
for (let i = 0; i < count; i++) { | ||
result += s; | ||
@@ -226,7 +226,7 @@ } | ||
function computeIndentLevel(content, options) { | ||
var i = 0; | ||
var nChars = 0; | ||
var tabSize = options.tabSize || 4; | ||
let i = 0; | ||
let nChars = 0; | ||
const tabSize = options.tabSize || 4; | ||
while (i < content.length) { | ||
var ch = content.charAt(i); | ||
let ch = content.charAt(i); | ||
if (ch === ' ') { | ||
@@ -246,4 +246,4 @@ nChars++; | ||
function getEOL(options, text) { | ||
for (var i = 0; i < text.length; i++) { | ||
var ch = text.charAt(i); | ||
for (let i = 0; i < text.length; i++) { | ||
const ch = text.charAt(i); | ||
if (ch === '\r') { | ||
@@ -250,0 +250,0 @@ if (i + 1 < text.length && text.charAt(i + 1) === '\n') { |
@@ -17,6 +17,6 @@ /*--------------------------------------------------------------------------------------------- | ||
export function getLocation(text, position) { | ||
var segments = []; // strings or numbers | ||
var earlyReturnException = new Object(); | ||
var previousNode = undefined; | ||
var previousNodeInst = { | ||
const segments = []; // strings or numbers | ||
const earlyReturnException = new Object(); | ||
let previousNode = undefined; | ||
const previousNodeInst = { | ||
value: {}, | ||
@@ -28,3 +28,3 @@ offset: 0, | ||
}; | ||
var isAtPropertyKey = false; | ||
let isAtPropertyKey = false; | ||
function setPreviousNode(value, offset, length, type) { | ||
@@ -40,3 +40,3 @@ previousNodeInst.value = value; | ||
visit(text, { | ||
onObjectBegin: function (offset, length) { | ||
onObjectBegin: (offset, length) => { | ||
if (position <= offset) { | ||
@@ -49,3 +49,3 @@ throw earlyReturnException; | ||
}, | ||
onObjectProperty: function (name, offset, length) { | ||
onObjectProperty: (name, offset, length) => { | ||
if (position < offset) { | ||
@@ -60,3 +60,3 @@ throw earlyReturnException; | ||
}, | ||
onObjectEnd: function (offset, length) { | ||
onObjectEnd: (offset, length) => { | ||
if (position <= offset) { | ||
@@ -68,3 +68,3 @@ throw earlyReturnException; | ||
}, | ||
onArrayBegin: function (offset, length) { | ||
onArrayBegin: (offset, length) => { | ||
if (position <= offset) { | ||
@@ -76,3 +76,3 @@ throw earlyReturnException; | ||
}, | ||
onArrayEnd: function (offset, length) { | ||
onArrayEnd: (offset, length) => { | ||
if (position <= offset) { | ||
@@ -84,3 +84,3 @@ throw earlyReturnException; | ||
}, | ||
onLiteralValue: function (value, offset, length) { | ||
onLiteralValue: (value, offset, length) => { | ||
if (position < offset) { | ||
@@ -94,3 +94,3 @@ throw earlyReturnException; | ||
}, | ||
onSeparator: function (sep, offset, length) { | ||
onSeparator: (sep, offset, length) => { | ||
if (position <= offset) { | ||
@@ -105,3 +105,3 @@ throw earlyReturnException; | ||
else if (sep === ',') { | ||
var last = segments[segments.length - 1]; | ||
const last = segments[segments.length - 1]; | ||
if (typeof last === 'number') { | ||
@@ -126,7 +126,7 @@ segments[segments.length - 1] = last + 1; | ||
path: segments, | ||
previousNode: previousNode, | ||
isAtPropertyKey: isAtPropertyKey, | ||
matches: function (pattern) { | ||
var k = 0; | ||
for (var i = 0; k < pattern.length && i < segments.length; i++) { | ||
previousNode, | ||
isAtPropertyKey, | ||
matches: (pattern) => { | ||
let k = 0; | ||
for (let i = 0; k < pattern.length && i < segments.length; i++) { | ||
if (pattern[k] === segments[i] || pattern[k] === '*') { | ||
@@ -147,8 +147,6 @@ k++; | ||
*/ | ||
export function parse(text, errors, options) { | ||
if (errors === void 0) { errors = []; } | ||
if (options === void 0) { options = ParseOptions.DEFAULT; } | ||
var currentProperty = null; | ||
var currentParent = []; | ||
var previousParents = []; | ||
export function parse(text, errors = [], options = ParseOptions.DEFAULT) { | ||
let currentProperty = null; | ||
let currentParent = []; | ||
const previousParents = []; | ||
function onValue(value) { | ||
@@ -162,5 +160,5 @@ if (Array.isArray(currentParent)) { | ||
} | ||
var visitor = { | ||
onObjectBegin: function () { | ||
var object = {}; | ||
const visitor = { | ||
onObjectBegin: () => { | ||
const object = {}; | ||
onValue(object); | ||
@@ -171,10 +169,10 @@ previousParents.push(currentParent); | ||
}, | ||
onObjectProperty: function (name) { | ||
onObjectProperty: (name) => { | ||
currentProperty = name; | ||
}, | ||
onObjectEnd: function () { | ||
onObjectEnd: () => { | ||
currentParent = previousParents.pop(); | ||
}, | ||
onArrayBegin: function () { | ||
var array = []; | ||
onArrayBegin: () => { | ||
const array = []; | ||
onValue(array); | ||
@@ -185,8 +183,8 @@ previousParents.push(currentParent); | ||
}, | ||
onArrayEnd: function () { | ||
onArrayEnd: () => { | ||
currentParent = previousParents.pop(); | ||
}, | ||
onLiteralValue: onValue, | ||
onError: function (error, offset, length) { | ||
errors.push({ error: error, offset: offset, length: length }); | ||
onError: (error, offset, length) => { | ||
errors.push({ error, offset, length }); | ||
} | ||
@@ -200,6 +198,4 @@ }; | ||
*/ | ||
export function parseTree(text, errors, options) { | ||
if (errors === void 0) { errors = []; } | ||
if (options === void 0) { options = ParseOptions.DEFAULT; } | ||
var currentParent = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root | ||
export function parseTree(text, errors = [], options = ParseOptions.DEFAULT) { | ||
let currentParent = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root | ||
function ensurePropertyComplete(endOffset) { | ||
@@ -215,11 +211,11 @@ if (currentParent.type === 'property') { | ||
} | ||
var visitor = { | ||
onObjectBegin: function (offset) { | ||
currentParent = onValue({ type: 'object', offset: offset, length: -1, parent: currentParent, children: [] }); | ||
const visitor = { | ||
onObjectBegin: (offset) => { | ||
currentParent = onValue({ type: 'object', offset, length: -1, parent: currentParent, children: [] }); | ||
}, | ||
onObjectProperty: function (name, offset, length) { | ||
currentParent = onValue({ type: 'property', offset: offset, length: -1, parent: currentParent, children: [] }); | ||
currentParent.children.push({ type: 'string', value: name, offset: offset, length: length, parent: currentParent }); | ||
onObjectProperty: (name, offset, length) => { | ||
currentParent = onValue({ type: 'property', offset, length: -1, parent: currentParent, children: [] }); | ||
currentParent.children.push({ type: 'string', value: name, offset, length, parent: currentParent }); | ||
}, | ||
onObjectEnd: function (offset, length) { | ||
onObjectEnd: (offset, length) => { | ||
ensurePropertyComplete(offset + length); // in case of a missing value for a property: make sure property is complete | ||
@@ -230,6 +226,6 @@ currentParent.length = offset + length - currentParent.offset; | ||
}, | ||
onArrayBegin: function (offset, length) { | ||
currentParent = onValue({ type: 'array', offset: offset, length: -1, parent: currentParent, children: [] }); | ||
onArrayBegin: (offset, length) => { | ||
currentParent = onValue({ type: 'array', offset, length: -1, parent: currentParent, children: [] }); | ||
}, | ||
onArrayEnd: function (offset, length) { | ||
onArrayEnd: (offset, length) => { | ||
currentParent.length = offset + length - currentParent.offset; | ||
@@ -239,7 +235,7 @@ currentParent = currentParent.parent; | ||
}, | ||
onLiteralValue: function (value, offset, length) { | ||
onValue({ type: getNodeType(value), offset: offset, length: length, parent: currentParent, value: value }); | ||
onLiteralValue: (value, offset, length) => { | ||
onValue({ type: getNodeType(value), offset, length, parent: currentParent, value }); | ||
ensurePropertyComplete(offset + length); | ||
}, | ||
onSeparator: function (sep, offset, length) { | ||
onSeparator: (sep, offset, length) => { | ||
if (currentParent.type === 'property') { | ||
@@ -254,8 +250,8 @@ if (sep === ':') { | ||
}, | ||
onError: function (error, offset, length) { | ||
errors.push({ error: error, offset: offset, length: length }); | ||
onError: (error, offset, length) => { | ||
errors.push({ error, offset, length }); | ||
} | ||
}; | ||
visit(text, visitor, options); | ||
var result = currentParent.children[0]; | ||
const result = currentParent.children[0]; | ||
if (result) { | ||
@@ -273,5 +269,4 @@ delete result.parent; | ||
} | ||
var node = root; | ||
for (var _i = 0, path_1 = path; _i < path_1.length; _i++) { | ||
var segment = path_1[_i]; | ||
let node = root; | ||
for (let segment of path) { | ||
if (typeof segment === 'string') { | ||
@@ -281,5 +276,4 @@ if (node.type !== 'object' || !Array.isArray(node.children)) { | ||
} | ||
var found = false; | ||
for (var _a = 0, _b = node.children; _a < _b.length; _a++) { | ||
var propertyNode = _b[_a]; | ||
let found = false; | ||
for (const propertyNode of node.children) { | ||
if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment && propertyNode.children.length === 2) { | ||
@@ -296,3 +290,3 @@ node = propertyNode.children[1]; | ||
else { | ||
var index = segment; | ||
const index = segment; | ||
if (node.type !== 'array' || index < 0 || !Array.isArray(node.children) || index >= node.children.length) { | ||
@@ -313,9 +307,9 @@ return undefined; | ||
} | ||
var path = getNodePath(node.parent); | ||
const path = getNodePath(node.parent); | ||
if (node.parent.type === 'property') { | ||
var key = node.parent.children[0].value; | ||
const key = node.parent.children[0].value; | ||
path.push(key); | ||
} | ||
else if (node.parent.type === 'array') { | ||
var index = node.parent.children.indexOf(node); | ||
const index = node.parent.children.indexOf(node); | ||
if (index !== -1) { | ||
@@ -335,6 +329,5 @@ path.push(index); | ||
case 'object': | ||
var obj = Object.create(null); | ||
for (var _i = 0, _a = node.children; _i < _a.length; _i++) { | ||
var prop = _a[_i]; | ||
var valueNode = prop.children[1]; | ||
const obj = Object.create(null); | ||
for (let prop of node.children) { | ||
const valueNode = prop.children[1]; | ||
if (valueNode) { | ||
@@ -354,4 +347,3 @@ obj[prop.children[0].value] = getNodeValue(valueNode); | ||
} | ||
export function contains(node, offset, includeRightBound) { | ||
if (includeRightBound === void 0) { includeRightBound = false; } | ||
export function contains(node, offset, includeRightBound = false) { | ||
return (offset >= node.offset && offset < (node.offset + node.length)) || includeRightBound && (offset === (node.offset + node.length)); | ||
@@ -362,9 +354,8 @@ } | ||
*/ | ||
export function findNodeAtOffset(node, offset, includeRightBound) { | ||
if (includeRightBound === void 0) { includeRightBound = false; } | ||
export function findNodeAtOffset(node, offset, includeRightBound = false) { | ||
if (contains(node, offset, includeRightBound)) { | ||
var children = node.children; | ||
const children = node.children; | ||
if (Array.isArray(children)) { | ||
for (var i = 0; i < children.length && children[i].offset <= offset; i++) { | ||
var item = findNodeAtOffset(children[i], offset, includeRightBound); | ||
for (let i = 0; i < children.length && children[i].offset <= offset; i++) { | ||
const item = findNodeAtOffset(children[i], offset, includeRightBound); | ||
if (item) { | ||
@@ -382,26 +373,25 @@ return item; | ||
*/ | ||
export function visit(text, visitor, options) { | ||
if (options === void 0) { options = ParseOptions.DEFAULT; } | ||
var _scanner = createScanner(text, false); | ||
export function visit(text, visitor, options = ParseOptions.DEFAULT) { | ||
const _scanner = createScanner(text, false); | ||
// Important: Only pass copies of this to visitor functions to prevent accidental modification, and | ||
// to not affect visitor functions which stored a reference to a previous JSONPath | ||
var _jsonPath = []; | ||
const _jsonPath = []; | ||
function toNoArgVisit(visitFunction) { | ||
return visitFunction ? function () { return visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()); } : function () { return true; }; | ||
return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true; | ||
} | ||
function toNoArgVisitWithPath(visitFunction) { | ||
return visitFunction ? function () { return visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), function () { return _jsonPath.slice(); }); } : function () { return true; }; | ||
return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true; | ||
} | ||
function toOneArgVisit(visitFunction) { | ||
return visitFunction ? function (arg) { return visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()); } : function () { return true; }; | ||
return visitFunction ? (arg) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true; | ||
} | ||
function toOneArgVisitWithPath(visitFunction) { | ||
return visitFunction ? function (arg) { return visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), function () { return _jsonPath.slice(); }); } : function () { return true; }; | ||
return visitFunction ? (arg) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true; | ||
} | ||
var onObjectBegin = toNoArgVisitWithPath(visitor.onObjectBegin), onObjectProperty = toOneArgVisitWithPath(visitor.onObjectProperty), onObjectEnd = toNoArgVisit(visitor.onObjectEnd), onArrayBegin = toNoArgVisitWithPath(visitor.onArrayBegin), onArrayEnd = toNoArgVisit(visitor.onArrayEnd), onLiteralValue = toOneArgVisitWithPath(visitor.onLiteralValue), onSeparator = toOneArgVisit(visitor.onSeparator), onComment = toNoArgVisit(visitor.onComment), onError = toOneArgVisit(visitor.onError); | ||
var disallowComments = options && options.disallowComments; | ||
var allowTrailingComma = options && options.allowTrailingComma; | ||
const onObjectBegin = toNoArgVisitWithPath(visitor.onObjectBegin), onObjectProperty = toOneArgVisitWithPath(visitor.onObjectProperty), onObjectEnd = toNoArgVisit(visitor.onObjectEnd), onArrayBegin = toNoArgVisitWithPath(visitor.onArrayBegin), onArrayEnd = toNoArgVisit(visitor.onArrayEnd), onLiteralValue = toOneArgVisitWithPath(visitor.onLiteralValue), onSeparator = toOneArgVisit(visitor.onSeparator), onComment = toNoArgVisit(visitor.onComment), onError = toOneArgVisit(visitor.onError); | ||
const disallowComments = options && options.disallowComments; | ||
const allowTrailingComma = options && options.allowTrailingComma; | ||
function scanNext() { | ||
while (true) { | ||
var token = _scanner.scan(); | ||
const token = _scanner.scan(); | ||
switch (_scanner.getTokenError()) { | ||
@@ -450,8 +440,6 @@ case 4 /* ScanError.InvalidUnicode */: | ||
} | ||
function handleError(error, skipUntilAfter, skipUntil) { | ||
if (skipUntilAfter === void 0) { skipUntilAfter = []; } | ||
if (skipUntil === void 0) { skipUntil = []; } | ||
function handleError(error, skipUntilAfter = [], skipUntil = []) { | ||
onError(error); | ||
if (skipUntilAfter.length + skipUntil.length > 0) { | ||
var token = _scanner.getToken(); | ||
let token = _scanner.getToken(); | ||
while (token !== 17 /* SyntaxKind.EOF */) { | ||
@@ -470,3 +458,3 @@ if (skipUntilAfter.indexOf(token) !== -1) { | ||
function parseString(isValue) { | ||
var value = _scanner.getTokenValue(); | ||
const value = _scanner.getTokenValue(); | ||
if (isValue) { | ||
@@ -486,4 +474,4 @@ onLiteralValue(value); | ||
case 11 /* SyntaxKind.NumericLiteral */: | ||
var tokenValue = _scanner.getTokenValue(); | ||
var value = Number(tokenValue); | ||
const tokenValue = _scanner.getTokenValue(); | ||
let value = Number(tokenValue); | ||
if (isNaN(value)) { | ||
@@ -532,3 +520,3 @@ handleError(2 /* ParseErrorCode.InvalidNumberFormat */); | ||
scanNext(); // consume open brace | ||
var needsComma = false; | ||
let needsComma = false; | ||
while (_scanner.getToken() !== 2 /* SyntaxKind.CloseBraceToken */ && _scanner.getToken() !== 17 /* SyntaxKind.EOF */) { | ||
@@ -565,4 +553,4 @@ if (_scanner.getToken() === 5 /* SyntaxKind.CommaToken */) { | ||
scanNext(); // consume open bracket | ||
var isFirstElement = true; | ||
var needsComma = false; | ||
let isFirstElement = true; | ||
let needsComma = false; | ||
while (_scanner.getToken() !== 4 /* SyntaxKind.CloseBracketToken */ && _scanner.getToken() !== 17 /* SyntaxKind.EOF */) { | ||
@@ -641,3 +629,3 @@ if (_scanner.getToken() === 5 /* SyntaxKind.CommaToken */) { | ||
export function stripComments(text, replaceCh) { | ||
var _scanner = createScanner(text), parts = [], kind, offset = 0, pos; | ||
let _scanner = createScanner(text), parts = [], kind, offset = 0, pos; | ||
do { | ||
@@ -644,0 +632,0 @@ pos = _scanner.getPosition(); |
@@ -10,11 +10,10 @@ /*--------------------------------------------------------------------------------------------- | ||
*/ | ||
export function createScanner(text, ignoreTrivia) { | ||
if (ignoreTrivia === void 0) { ignoreTrivia = false; } | ||
var len = text.length; | ||
var pos = 0, value = '', tokenOffset = 0, token = 16 /* SyntaxKind.Unknown */, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0 /* ScanError.None */; | ||
export function createScanner(text, ignoreTrivia = false) { | ||
const len = text.length; | ||
let pos = 0, value = '', tokenOffset = 0, token = 16 /* SyntaxKind.Unknown */, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0 /* ScanError.None */; | ||
function scanHexDigits(count, exact) { | ||
var digits = 0; | ||
var value = 0; | ||
let digits = 0; | ||
let value = 0; | ||
while (digits < count || !exact) { | ||
var ch = text.charCodeAt(pos); | ||
let ch = text.charCodeAt(pos); | ||
if (ch >= 48 /* CharacterCodes._0 */ && ch <= 57 /* CharacterCodes._9 */) { | ||
@@ -48,3 +47,3 @@ value = value * 16 + ch - 48 /* CharacterCodes._0 */; | ||
function scanNumber() { | ||
var start = pos; | ||
let start = pos; | ||
if (text.charCodeAt(pos) === 48 /* CharacterCodes._0 */) { | ||
@@ -72,3 +71,3 @@ pos++; | ||
} | ||
var end = pos; | ||
let end = pos; | ||
if (pos < text.length && (text.charCodeAt(pos) === 69 /* CharacterCodes.E */ || text.charCodeAt(pos) === 101 /* CharacterCodes.e */)) { | ||
@@ -93,3 +92,3 @@ pos++; | ||
function scanString() { | ||
var result = '', start = pos; | ||
let result = '', start = pos; | ||
while (true) { | ||
@@ -101,3 +100,3 @@ if (pos >= len) { | ||
} | ||
var ch = text.charCodeAt(pos); | ||
const ch = text.charCodeAt(pos); | ||
if (ch === 34 /* CharacterCodes.doubleQuote */) { | ||
@@ -115,3 +114,3 @@ result += text.substring(start, pos); | ||
} | ||
var ch2 = text.charCodeAt(pos++); | ||
const ch2 = text.charCodeAt(pos++); | ||
switch (ch2) { | ||
@@ -143,3 +142,3 @@ case 34 /* CharacterCodes.doubleQuote */: | ||
case 117 /* CharacterCodes.u */: | ||
var ch3 = scanHexDigits(4, true); | ||
const ch3 = scanHexDigits(4, true); | ||
if (ch3 >= 0) { | ||
@@ -184,3 +183,3 @@ result += String.fromCharCode(ch3); | ||
} | ||
var code = text.charCodeAt(pos); | ||
let code = text.charCodeAt(pos); | ||
// trivia: whitespace | ||
@@ -234,3 +233,3 @@ if (isWhiteSpace(code)) { | ||
case 47 /* CharacterCodes.slash */: | ||
var start = pos - 1; | ||
const start = pos - 1; | ||
// Single-line comment | ||
@@ -251,6 +250,6 @@ if (text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) { | ||
pos += 2; | ||
var safeLength = len - 1; // For lookahead. | ||
var commentClosed = false; | ||
const safeLength = len - 1; // For lookahead. | ||
let commentClosed = false; | ||
while (pos < safeLength) { | ||
var ch = text.charCodeAt(pos); | ||
const ch = text.charCodeAt(pos); | ||
if (ch === 42 /* CharacterCodes.asterisk */ && text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) { | ||
@@ -344,3 +343,3 @@ pos += 2; | ||
function scanNextNonTrivia() { | ||
var result; | ||
let result; | ||
do { | ||
@@ -353,11 +352,11 @@ result = scanNext(); | ||
setPosition: setPosition, | ||
getPosition: function () { return pos; }, | ||
getPosition: () => pos, | ||
scan: ignoreTrivia ? scanNextNonTrivia : scanNext, | ||
getToken: function () { return token; }, | ||
getTokenValue: function () { return value; }, | ||
getTokenOffset: function () { return tokenOffset; }, | ||
getTokenLength: function () { return pos - tokenOffset; }, | ||
getTokenStartLine: function () { return lineStartOffset; }, | ||
getTokenStartCharacter: function () { return tokenOffset - prevTokenLineStartOffset; }, | ||
getTokenError: function () { return scanError; }, | ||
getToken: () => token, | ||
getTokenValue: () => value, | ||
getTokenOffset: () => tokenOffset, | ||
getTokenLength: () => pos - tokenOffset, | ||
getTokenStartLine: () => lineStartOffset, | ||
getTokenStartCharacter: () => tokenOffset - prevTokenLineStartOffset, | ||
getTokenError: () => scanError, | ||
}; | ||
@@ -374,1 +373,84 @@ } | ||
} | ||
var CharacterCodes; | ||
(function (CharacterCodes) { | ||
CharacterCodes[CharacterCodes["lineFeed"] = 10] = "lineFeed"; | ||
CharacterCodes[CharacterCodes["carriageReturn"] = 13] = "carriageReturn"; | ||
CharacterCodes[CharacterCodes["space"] = 32] = "space"; | ||
CharacterCodes[CharacterCodes["_0"] = 48] = "_0"; | ||
CharacterCodes[CharacterCodes["_1"] = 49] = "_1"; | ||
CharacterCodes[CharacterCodes["_2"] = 50] = "_2"; | ||
CharacterCodes[CharacterCodes["_3"] = 51] = "_3"; | ||
CharacterCodes[CharacterCodes["_4"] = 52] = "_4"; | ||
CharacterCodes[CharacterCodes["_5"] = 53] = "_5"; | ||
CharacterCodes[CharacterCodes["_6"] = 54] = "_6"; | ||
CharacterCodes[CharacterCodes["_7"] = 55] = "_7"; | ||
CharacterCodes[CharacterCodes["_8"] = 56] = "_8"; | ||
CharacterCodes[CharacterCodes["_9"] = 57] = "_9"; | ||
CharacterCodes[CharacterCodes["a"] = 97] = "a"; | ||
CharacterCodes[CharacterCodes["b"] = 98] = "b"; | ||
CharacterCodes[CharacterCodes["c"] = 99] = "c"; | ||
CharacterCodes[CharacterCodes["d"] = 100] = "d"; | ||
CharacterCodes[CharacterCodes["e"] = 101] = "e"; | ||
CharacterCodes[CharacterCodes["f"] = 102] = "f"; | ||
CharacterCodes[CharacterCodes["g"] = 103] = "g"; | ||
CharacterCodes[CharacterCodes["h"] = 104] = "h"; | ||
CharacterCodes[CharacterCodes["i"] = 105] = "i"; | ||
CharacterCodes[CharacterCodes["j"] = 106] = "j"; | ||
CharacterCodes[CharacterCodes["k"] = 107] = "k"; | ||
CharacterCodes[CharacterCodes["l"] = 108] = "l"; | ||
CharacterCodes[CharacterCodes["m"] = 109] = "m"; | ||
CharacterCodes[CharacterCodes["n"] = 110] = "n"; | ||
CharacterCodes[CharacterCodes["o"] = 111] = "o"; | ||
CharacterCodes[CharacterCodes["p"] = 112] = "p"; | ||
CharacterCodes[CharacterCodes["q"] = 113] = "q"; | ||
CharacterCodes[CharacterCodes["r"] = 114] = "r"; | ||
CharacterCodes[CharacterCodes["s"] = 115] = "s"; | ||
CharacterCodes[CharacterCodes["t"] = 116] = "t"; | ||
CharacterCodes[CharacterCodes["u"] = 117] = "u"; | ||
CharacterCodes[CharacterCodes["v"] = 118] = "v"; | ||
CharacterCodes[CharacterCodes["w"] = 119] = "w"; | ||
CharacterCodes[CharacterCodes["x"] = 120] = "x"; | ||
CharacterCodes[CharacterCodes["y"] = 121] = "y"; | ||
CharacterCodes[CharacterCodes["z"] = 122] = "z"; | ||
CharacterCodes[CharacterCodes["A"] = 65] = "A"; | ||
CharacterCodes[CharacterCodes["B"] = 66] = "B"; | ||
CharacterCodes[CharacterCodes["C"] = 67] = "C"; | ||
CharacterCodes[CharacterCodes["D"] = 68] = "D"; | ||
CharacterCodes[CharacterCodes["E"] = 69] = "E"; | ||
CharacterCodes[CharacterCodes["F"] = 70] = "F"; | ||
CharacterCodes[CharacterCodes["G"] = 71] = "G"; | ||
CharacterCodes[CharacterCodes["H"] = 72] = "H"; | ||
CharacterCodes[CharacterCodes["I"] = 73] = "I"; | ||
CharacterCodes[CharacterCodes["J"] = 74] = "J"; | ||
CharacterCodes[CharacterCodes["K"] = 75] = "K"; | ||
CharacterCodes[CharacterCodes["L"] = 76] = "L"; | ||
CharacterCodes[CharacterCodes["M"] = 77] = "M"; | ||
CharacterCodes[CharacterCodes["N"] = 78] = "N"; | ||
CharacterCodes[CharacterCodes["O"] = 79] = "O"; | ||
CharacterCodes[CharacterCodes["P"] = 80] = "P"; | ||
CharacterCodes[CharacterCodes["Q"] = 81] = "Q"; | ||
CharacterCodes[CharacterCodes["R"] = 82] = "R"; | ||
CharacterCodes[CharacterCodes["S"] = 83] = "S"; | ||
CharacterCodes[CharacterCodes["T"] = 84] = "T"; | ||
CharacterCodes[CharacterCodes["U"] = 85] = "U"; | ||
CharacterCodes[CharacterCodes["V"] = 86] = "V"; | ||
CharacterCodes[CharacterCodes["W"] = 87] = "W"; | ||
CharacterCodes[CharacterCodes["X"] = 88] = "X"; | ||
CharacterCodes[CharacterCodes["Y"] = 89] = "Y"; | ||
CharacterCodes[CharacterCodes["Z"] = 90] = "Z"; | ||
CharacterCodes[CharacterCodes["asterisk"] = 42] = "asterisk"; | ||
CharacterCodes[CharacterCodes["backslash"] = 92] = "backslash"; | ||
CharacterCodes[CharacterCodes["closeBrace"] = 125] = "closeBrace"; | ||
CharacterCodes[CharacterCodes["closeBracket"] = 93] = "closeBracket"; | ||
CharacterCodes[CharacterCodes["colon"] = 58] = "colon"; | ||
CharacterCodes[CharacterCodes["comma"] = 44] = "comma"; | ||
CharacterCodes[CharacterCodes["dot"] = 46] = "dot"; | ||
CharacterCodes[CharacterCodes["doubleQuote"] = 34] = "doubleQuote"; | ||
CharacterCodes[CharacterCodes["minus"] = 45] = "minus"; | ||
CharacterCodes[CharacterCodes["openBrace"] = 123] = "openBrace"; | ||
CharacterCodes[CharacterCodes["openBracket"] = 91] = "openBracket"; | ||
CharacterCodes[CharacterCodes["plus"] = 43] = "plus"; | ||
CharacterCodes[CharacterCodes["slash"] = 47] = "slash"; | ||
CharacterCodes[CharacterCodes["formFeed"] = 12] = "formFeed"; | ||
CharacterCodes[CharacterCodes["tab"] = 9] = "tab"; | ||
})(CharacterCodes || (CharacterCodes = {})); |
@@ -14,7 +14,37 @@ /*--------------------------------------------------------------------------------------------- | ||
*/ | ||
export var createScanner = scanner.createScanner; | ||
export const createScanner = scanner.createScanner; | ||
export var ScanError; | ||
(function (ScanError) { | ||
ScanError[ScanError["None"] = 0] = "None"; | ||
ScanError[ScanError["UnexpectedEndOfComment"] = 1] = "UnexpectedEndOfComment"; | ||
ScanError[ScanError["UnexpectedEndOfString"] = 2] = "UnexpectedEndOfString"; | ||
ScanError[ScanError["UnexpectedEndOfNumber"] = 3] = "UnexpectedEndOfNumber"; | ||
ScanError[ScanError["InvalidUnicode"] = 4] = "InvalidUnicode"; | ||
ScanError[ScanError["InvalidEscapeCharacter"] = 5] = "InvalidEscapeCharacter"; | ||
ScanError[ScanError["InvalidCharacter"] = 6] = "InvalidCharacter"; | ||
})(ScanError || (ScanError = {})); | ||
export var SyntaxKind; | ||
(function (SyntaxKind) { | ||
SyntaxKind[SyntaxKind["OpenBraceToken"] = 1] = "OpenBraceToken"; | ||
SyntaxKind[SyntaxKind["CloseBraceToken"] = 2] = "CloseBraceToken"; | ||
SyntaxKind[SyntaxKind["OpenBracketToken"] = 3] = "OpenBracketToken"; | ||
SyntaxKind[SyntaxKind["CloseBracketToken"] = 4] = "CloseBracketToken"; | ||
SyntaxKind[SyntaxKind["CommaToken"] = 5] = "CommaToken"; | ||
SyntaxKind[SyntaxKind["ColonToken"] = 6] = "ColonToken"; | ||
SyntaxKind[SyntaxKind["NullKeyword"] = 7] = "NullKeyword"; | ||
SyntaxKind[SyntaxKind["TrueKeyword"] = 8] = "TrueKeyword"; | ||
SyntaxKind[SyntaxKind["FalseKeyword"] = 9] = "FalseKeyword"; | ||
SyntaxKind[SyntaxKind["StringLiteral"] = 10] = "StringLiteral"; | ||
SyntaxKind[SyntaxKind["NumericLiteral"] = 11] = "NumericLiteral"; | ||
SyntaxKind[SyntaxKind["LineCommentTrivia"] = 12] = "LineCommentTrivia"; | ||
SyntaxKind[SyntaxKind["BlockCommentTrivia"] = 13] = "BlockCommentTrivia"; | ||
SyntaxKind[SyntaxKind["LineBreakTrivia"] = 14] = "LineBreakTrivia"; | ||
SyntaxKind[SyntaxKind["Trivia"] = 15] = "Trivia"; | ||
SyntaxKind[SyntaxKind["Unknown"] = 16] = "Unknown"; | ||
SyntaxKind[SyntaxKind["EOF"] = 17] = "EOF"; | ||
})(SyntaxKind || (SyntaxKind = {})); | ||
/** | ||
* For a given offset, evaluate the location in the JSON document. Each segment in the location path is either a property name or an array index. | ||
*/ | ||
export var getLocation = parser.getLocation; | ||
export const getLocation = parser.getLocation; | ||
/** | ||
@@ -24,27 +54,27 @@ * 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. | ||
*/ | ||
export var parse = parser.parse; | ||
export const parse = parser.parse; | ||
/** | ||
* 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 var parseTree = parser.parseTree; | ||
export const parseTree = parser.parseTree; | ||
/** | ||
* Finds the node at the given path in a JSON DOM. | ||
*/ | ||
export var findNodeAtLocation = parser.findNodeAtLocation; | ||
export const findNodeAtLocation = parser.findNodeAtLocation; | ||
/** | ||
* Finds the innermost node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset. | ||
*/ | ||
export var findNodeAtOffset = parser.findNodeAtOffset; | ||
export const findNodeAtOffset = parser.findNodeAtOffset; | ||
/** | ||
* Gets the JSON path of the given JSON DOM node | ||
*/ | ||
export var getNodePath = parser.getNodePath; | ||
export const getNodePath = parser.getNodePath; | ||
/** | ||
* Evaluates the JavaScript object of the given JSON DOM node | ||
*/ | ||
export var getNodeValue = parser.getNodeValue; | ||
export const getNodeValue = parser.getNodeValue; | ||
/** | ||
* Parses the given text and invokes the visitor functions for each object, array and literal reached. | ||
*/ | ||
export var visit = parser.visit; | ||
export const visit = parser.visit; | ||
/** | ||
@@ -55,3 +85,22 @@ * Takes JSON with JavaScript-style comments and remove | ||
*/ | ||
export var stripComments = parser.stripComments; | ||
export const stripComments = parser.stripComments; | ||
export var ParseErrorCode; | ||
(function (ParseErrorCode) { | ||
ParseErrorCode[ParseErrorCode["InvalidSymbol"] = 1] = "InvalidSymbol"; | ||
ParseErrorCode[ParseErrorCode["InvalidNumberFormat"] = 2] = "InvalidNumberFormat"; | ||
ParseErrorCode[ParseErrorCode["PropertyNameExpected"] = 3] = "PropertyNameExpected"; | ||
ParseErrorCode[ParseErrorCode["ValueExpected"] = 4] = "ValueExpected"; | ||
ParseErrorCode[ParseErrorCode["ColonExpected"] = 5] = "ColonExpected"; | ||
ParseErrorCode[ParseErrorCode["CommaExpected"] = 6] = "CommaExpected"; | ||
ParseErrorCode[ParseErrorCode["CloseBraceExpected"] = 7] = "CloseBraceExpected"; | ||
ParseErrorCode[ParseErrorCode["CloseBracketExpected"] = 8] = "CloseBracketExpected"; | ||
ParseErrorCode[ParseErrorCode["EndOfFileExpected"] = 9] = "EndOfFileExpected"; | ||
ParseErrorCode[ParseErrorCode["InvalidCommentToken"] = 10] = "InvalidCommentToken"; | ||
ParseErrorCode[ParseErrorCode["UnexpectedEndOfComment"] = 11] = "UnexpectedEndOfComment"; | ||
ParseErrorCode[ParseErrorCode["UnexpectedEndOfString"] = 12] = "UnexpectedEndOfString"; | ||
ParseErrorCode[ParseErrorCode["UnexpectedEndOfNumber"] = 13] = "UnexpectedEndOfNumber"; | ||
ParseErrorCode[ParseErrorCode["InvalidUnicode"] = 14] = "InvalidUnicode"; | ||
ParseErrorCode[ParseErrorCode["InvalidEscapeCharacter"] = 15] = "InvalidEscapeCharacter"; | ||
ParseErrorCode[ParseErrorCode["InvalidCharacter"] = 16] = "InvalidCharacter"; | ||
})(ParseErrorCode || (ParseErrorCode = {})); | ||
export function printParseErrorCode(code) { | ||
@@ -113,6 +162,21 @@ switch (code) { | ||
export function applyEdits(text, edits) { | ||
for (var i = edits.length - 1; i >= 0; i--) { | ||
text = edit.applyEdit(text, edits[i]); | ||
let sortedEdits = edits.slice(0).sort((a, b) => { | ||
const diff = a.offset - b.offset; | ||
if (diff === 0) { | ||
return a.length - b.length; | ||
} | ||
return diff; | ||
}); | ||
let lastModifiedOffset = text.length; | ||
for (let i = sortedEdits.length - 1; i >= 0; i--) { | ||
let e = sortedEdits[i]; | ||
if (e.offset + e.length <= lastModifiedOffset) { | ||
text = edit.applyEdit(text, e); | ||
} | ||
else { | ||
throw new Error('Overlapping edit'); | ||
} | ||
lastModifiedOffset = e.offset; | ||
} | ||
return text; | ||
} |
@@ -1,12 +0,1 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
(function (factory) { | ||
@@ -28,4 +17,4 @@ if (typeof module === "object" && typeof module.exports === "object") { | ||
exports.isWS = exports.applyEdit = exports.setProperty = exports.removeProperty = void 0; | ||
var format_1 = require("./format"); | ||
var parser_1 = require("./parser"); | ||
const format_1 = require("./format"); | ||
const parser_1 = require("./parser"); | ||
function removeProperty(text, path, options) { | ||
@@ -36,8 +25,7 @@ return setProperty(text, path, void 0, options); | ||
function setProperty(text, originalPath, value, options) { | ||
var _a; | ||
var path = originalPath.slice(); | ||
var errors = []; | ||
var root = (0, parser_1.parseTree)(text, errors); | ||
var parent = void 0; | ||
var lastSegment = void 0; | ||
const path = originalPath.slice(); | ||
const errors = []; | ||
const root = (0, parser_1.parseTree)(text, errors); | ||
let parent = void 0; | ||
let lastSegment = void 0; | ||
while (path.length > 0) { | ||
@@ -48,3 +36,3 @@ lastSegment = path.pop(); | ||
if (typeof lastSegment === 'string') { | ||
value = (_a = {}, _a[lastSegment] = value, _a); | ||
value = { [lastSegment]: value }; | ||
} | ||
@@ -67,3 +55,3 @@ else { | ||
else if (parent.type === 'object' && typeof lastSegment === 'string' && Array.isArray(parent.children)) { | ||
var existing = (0, parser_1.findNodeAtLocation)(parent, [lastSegment]); | ||
const existing = (0, parser_1.findNodeAtLocation)(parent, [lastSegment]); | ||
if (existing !== void 0) { | ||
@@ -74,8 +62,8 @@ if (value === void 0) { // delete | ||
} | ||
var propertyIndex = parent.children.indexOf(existing.parent); | ||
var removeBegin = void 0; | ||
var removeEnd = existing.parent.offset + existing.parent.length; | ||
const propertyIndex = parent.children.indexOf(existing.parent); | ||
let removeBegin; | ||
let removeEnd = existing.parent.offset + existing.parent.length; | ||
if (propertyIndex > 0) { | ||
// remove the comma of the previous node | ||
var previous = parent.children[propertyIndex - 1]; | ||
let previous = parent.children[propertyIndex - 1]; | ||
removeBegin = previous.offset + previous.length; | ||
@@ -87,3 +75,3 @@ } | ||
// remove the comma of the next node | ||
var next = parent.children[1]; | ||
let next = parent.children[1]; | ||
removeEnd = next.offset; | ||
@@ -103,7 +91,7 @@ } | ||
} | ||
var newProperty = "".concat(JSON.stringify(lastSegment), ": ").concat(JSON.stringify(value)); | ||
var index = options.getInsertionIndex ? options.getInsertionIndex(parent.children.map(function (p) { return p.children[0].value; })) : parent.children.length; | ||
var edit = void 0; | ||
const newProperty = `${JSON.stringify(lastSegment)}: ${JSON.stringify(value)}`; | ||
const index = options.getInsertionIndex ? options.getInsertionIndex(parent.children.map(p => p.children[0].value)) : parent.children.length; | ||
let edit; | ||
if (index > 0) { | ||
var previous = parent.children[index - 1]; | ||
let previous = parent.children[index - 1]; | ||
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty }; | ||
@@ -121,7 +109,7 @@ } | ||
else if (parent.type === 'array' && typeof lastSegment === 'number' && Array.isArray(parent.children)) { | ||
var insertIndex = lastSegment; | ||
const insertIndex = lastSegment; | ||
if (insertIndex === -1) { | ||
// Insert | ||
var newProperty = "".concat(JSON.stringify(value)); | ||
var edit = void 0; | ||
const newProperty = `${JSON.stringify(value)}`; | ||
let edit; | ||
if (parent.children.length === 0) { | ||
@@ -131,3 +119,3 @@ edit = { offset: parent.offset + 1, length: 0, content: newProperty }; | ||
else { | ||
var previous = parent.children[parent.children.length - 1]; | ||
const previous = parent.children[parent.children.length - 1]; | ||
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty }; | ||
@@ -139,5 +127,5 @@ } | ||
// Removal | ||
var removalIndex = lastSegment; | ||
var toRemove = parent.children[removalIndex]; | ||
var edit = void 0; | ||
const removalIndex = lastSegment; | ||
const toRemove = parent.children[removalIndex]; | ||
let edit; | ||
if (parent.children.length === 1) { | ||
@@ -149,6 +137,6 @@ // only item | ||
// last item | ||
var previous = parent.children[removalIndex - 1]; | ||
var offset = previous.offset + previous.length; | ||
var parentEndOffset = parent.offset + parent.length; | ||
edit = { offset: offset, length: parentEndOffset - 2 - offset, content: '' }; | ||
let previous = parent.children[removalIndex - 1]; | ||
let offset = previous.offset + previous.length; | ||
let parentEndOffset = parent.offset + parent.length; | ||
edit = { offset, length: parentEndOffset - 2 - offset, content: '' }; | ||
} | ||
@@ -161,6 +149,6 @@ else { | ||
else if (value !== void 0) { | ||
var edit = void 0; | ||
var newProperty = "".concat(JSON.stringify(value)); | ||
let edit; | ||
const newProperty = `${JSON.stringify(value)}`; | ||
if (!options.isArrayInsertion && parent.children.length > lastSegment) { | ||
var toModify = parent.children[lastSegment]; | ||
const toModify = parent.children[lastSegment]; | ||
edit = { offset: toModify.offset, length: toModify.length, content: newProperty }; | ||
@@ -172,4 +160,4 @@ } | ||
else { | ||
var index = lastSegment > parent.children.length ? parent.children.length : lastSegment; | ||
var previous = parent.children[index - 1]; | ||
const index = lastSegment > parent.children.length ? parent.children.length : lastSegment; | ||
const previous = parent.children[index - 1]; | ||
edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty }; | ||
@@ -180,7 +168,7 @@ } | ||
else { | ||
throw new Error("Can not ".concat(value === void 0 ? 'remove' : (options.isArrayInsertion ? 'insert' : 'modify'), " Array index ").concat(insertIndex, " as length is not sufficient")); | ||
throw new Error(`Can not ${value === void 0 ? 'remove' : (options.isArrayInsertion ? 'insert' : 'modify')} Array index ${insertIndex} as length is not sufficient`); | ||
} | ||
} | ||
else { | ||
throw new Error("Can not add ".concat(typeof lastSegment !== 'number' ? 'index' : 'property', " to parent of type ").concat(parent.type)); | ||
throw new Error(`Can not add ${typeof lastSegment !== 'number' ? 'index' : 'property'} to parent of type ${parent.type}`); | ||
} | ||
@@ -194,6 +182,6 @@ } | ||
// apply the edit | ||
var newText = applyEdit(text, edit); | ||
let newText = applyEdit(text, edit); | ||
// format the new text | ||
var begin = edit.offset; | ||
var end = edit.offset + edit.content.length; | ||
let begin = edit.offset; | ||
let end = edit.offset + edit.content.length; | ||
if (edit.length === 0 || edit.content.length === 0) { // insert or remove | ||
@@ -207,13 +195,13 @@ while (begin > 0 && !(0, format_1.isEOL)(newText, begin - 1)) { | ||
} | ||
var edits = (0, format_1.format)(newText, { offset: begin, length: end - begin }, __assign(__assign({}, options.formattingOptions), { keepLines: false })); | ||
const edits = (0, format_1.format)(newText, { offset: begin, length: end - begin }, { ...options.formattingOptions, keepLines: false }); | ||
// apply the formatting edits and track the begin and end offsets of the changes | ||
for (var i = edits.length - 1; i >= 0; i--) { | ||
var edit_1 = edits[i]; | ||
newText = applyEdit(newText, edit_1); | ||
begin = Math.min(begin, edit_1.offset); | ||
end = Math.max(end, edit_1.offset + edit_1.length); | ||
end += edit_1.content.length - edit_1.length; | ||
for (let i = edits.length - 1; i >= 0; i--) { | ||
const edit = edits[i]; | ||
newText = applyEdit(newText, edit); | ||
begin = Math.min(begin, edit.offset); | ||
end = Math.max(end, edit.offset + edit.length); | ||
end += edit.content.length - edit.length; | ||
} | ||
// create a single edit with all changes | ||
var editLength = text.length - (newText.length - end) - begin; | ||
const editLength = text.length - (newText.length - end) - begin; | ||
return [{ offset: begin, length: editLength, content: newText.substring(begin, end) }]; | ||
@@ -220,0 +208,0 @@ } |
@@ -17,9 +17,9 @@ (function (factory) { | ||
exports.isEOL = exports.format = void 0; | ||
var scanner_1 = require("./scanner"); | ||
const scanner_1 = require("./scanner"); | ||
function format(documentText, range, options) { | ||
var initialIndentLevel; | ||
var formatText; | ||
var formatTextStart; | ||
var rangeStart; | ||
var rangeEnd; | ||
let initialIndentLevel; | ||
let formatText; | ||
let formatTextStart; | ||
let rangeStart; | ||
let rangeEnd; | ||
if (range) { | ||
@@ -32,3 +32,3 @@ rangeStart = range.offset; | ||
} | ||
var endOffset = rangeEnd; | ||
let endOffset = rangeEnd; | ||
while (endOffset < documentText.length && !isEOL(documentText, endOffset)) { | ||
@@ -47,6 +47,6 @@ endOffset++; | ||
} | ||
var eol = getEOL(options, documentText); | ||
var numberLineBreaks = 0; | ||
var indentLevel = 0; | ||
var indentValue; | ||
const eol = getEOL(options, documentText); | ||
let numberLineBreaks = 0; | ||
let indentLevel = 0; | ||
let indentValue; | ||
if (options.insertSpaces) { | ||
@@ -58,4 +58,4 @@ indentValue = repeat(' ', options.tabSize || 4); | ||
} | ||
var scanner = (0, scanner_1.createScanner)(formatText, false); | ||
var hasError = false; | ||
let scanner = (0, scanner_1.createScanner)(formatText, false); | ||
let hasError = false; | ||
function newLinesAndIndent() { | ||
@@ -70,3 +70,3 @@ if (numberLineBreaks > 1) { | ||
function scanNext() { | ||
var token = scanner.scan(); | ||
let token = scanner.scan(); | ||
numberLineBreaks = 0; | ||
@@ -85,3 +85,3 @@ while (token === 15 /* SyntaxKind.Trivia */ || token === 14 /* SyntaxKind.LineBreakTrivia */) { | ||
} | ||
var editOperations = []; | ||
const editOperations = []; | ||
function addEdit(text, startOffset, endOffset) { | ||
@@ -92,3 +92,3 @@ if (!hasError && (!range || (startOffset < rangeEnd && endOffset > rangeStart)) && documentText.substring(startOffset, endOffset) !== text) { | ||
} | ||
var firstToken = scanNext(); | ||
let firstToken = scanNext(); | ||
if (options.keepLines && numberLineBreaks > 0) { | ||
@@ -98,13 +98,13 @@ addEdit(repeat(eol, numberLineBreaks), 0, 0); | ||
if (firstToken !== 17 /* SyntaxKind.EOF */) { | ||
var firstTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
var initialIndent = repeat(indentValue, initialIndentLevel); | ||
let firstTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
let initialIndent = repeat(indentValue, initialIndentLevel); | ||
addEdit(initialIndent, formatTextStart, firstTokenStart); | ||
} | ||
while (firstToken !== 17 /* SyntaxKind.EOF */) { | ||
var firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; | ||
var secondToken = scanNext(); | ||
var replaceContent = ''; | ||
var needsLineBreak = false; | ||
let firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; | ||
let secondToken = scanNext(); | ||
let replaceContent = ''; | ||
let needsLineBreak = false; | ||
while (numberLineBreaks === 0 && (secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */)) { | ||
var commentTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
let commentTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
addEdit(' ', firstTokenEnd, commentTokenStart); | ||
@@ -221,3 +221,3 @@ firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; | ||
} | ||
var secondTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
const secondTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
addEdit(replaceContent, firstTokenEnd, secondTokenStart); | ||
@@ -230,4 +230,4 @@ firstToken = secondToken; | ||
function repeat(s, count) { | ||
var result = ''; | ||
for (var i = 0; i < count; i++) { | ||
let result = ''; | ||
for (let i = 0; i < count; i++) { | ||
result += s; | ||
@@ -238,7 +238,7 @@ } | ||
function computeIndentLevel(content, options) { | ||
var i = 0; | ||
var nChars = 0; | ||
var tabSize = options.tabSize || 4; | ||
let i = 0; | ||
let nChars = 0; | ||
const tabSize = options.tabSize || 4; | ||
while (i < content.length) { | ||
var ch = content.charAt(i); | ||
let ch = content.charAt(i); | ||
if (ch === ' ') { | ||
@@ -258,4 +258,4 @@ nChars++; | ||
function getEOL(options, text) { | ||
for (var i = 0; i < text.length; i++) { | ||
var ch = text.charAt(i); | ||
for (let i = 0; i < text.length; i++) { | ||
const ch = text.charAt(i); | ||
if (ch === '\r') { | ||
@@ -262,0 +262,0 @@ if (i + 1 < text.length && text.charAt(i + 1) === '\n') { |
@@ -17,3 +17,3 @@ (function (factory) { | ||
exports.getNodeType = exports.stripComments = exports.visit = exports.findNodeAtOffset = exports.contains = exports.getNodeValue = exports.getNodePath = exports.findNodeAtLocation = exports.parseTree = exports.parse = exports.getLocation = void 0; | ||
var scanner_1 = require("./scanner"); | ||
const scanner_1 = require("./scanner"); | ||
var ParseOptions; | ||
@@ -29,6 +29,6 @@ (function (ParseOptions) { | ||
function getLocation(text, position) { | ||
var segments = []; // strings or numbers | ||
var earlyReturnException = new Object(); | ||
var previousNode = undefined; | ||
var previousNodeInst = { | ||
const segments = []; // strings or numbers | ||
const earlyReturnException = new Object(); | ||
let previousNode = undefined; | ||
const previousNodeInst = { | ||
value: {}, | ||
@@ -40,3 +40,3 @@ offset: 0, | ||
}; | ||
var isAtPropertyKey = false; | ||
let isAtPropertyKey = false; | ||
function setPreviousNode(value, offset, length, type) { | ||
@@ -52,3 +52,3 @@ previousNodeInst.value = value; | ||
visit(text, { | ||
onObjectBegin: function (offset, length) { | ||
onObjectBegin: (offset, length) => { | ||
if (position <= offset) { | ||
@@ -61,3 +61,3 @@ throw earlyReturnException; | ||
}, | ||
onObjectProperty: function (name, offset, length) { | ||
onObjectProperty: (name, offset, length) => { | ||
if (position < offset) { | ||
@@ -72,3 +72,3 @@ throw earlyReturnException; | ||
}, | ||
onObjectEnd: function (offset, length) { | ||
onObjectEnd: (offset, length) => { | ||
if (position <= offset) { | ||
@@ -80,3 +80,3 @@ throw earlyReturnException; | ||
}, | ||
onArrayBegin: function (offset, length) { | ||
onArrayBegin: (offset, length) => { | ||
if (position <= offset) { | ||
@@ -88,3 +88,3 @@ throw earlyReturnException; | ||
}, | ||
onArrayEnd: function (offset, length) { | ||
onArrayEnd: (offset, length) => { | ||
if (position <= offset) { | ||
@@ -96,3 +96,3 @@ throw earlyReturnException; | ||
}, | ||
onLiteralValue: function (value, offset, length) { | ||
onLiteralValue: (value, offset, length) => { | ||
if (position < offset) { | ||
@@ -106,3 +106,3 @@ throw earlyReturnException; | ||
}, | ||
onSeparator: function (sep, offset, length) { | ||
onSeparator: (sep, offset, length) => { | ||
if (position <= offset) { | ||
@@ -117,3 +117,3 @@ throw earlyReturnException; | ||
else if (sep === ',') { | ||
var last = segments[segments.length - 1]; | ||
const last = segments[segments.length - 1]; | ||
if (typeof last === 'number') { | ||
@@ -138,7 +138,7 @@ segments[segments.length - 1] = last + 1; | ||
path: segments, | ||
previousNode: previousNode, | ||
isAtPropertyKey: isAtPropertyKey, | ||
matches: function (pattern) { | ||
var k = 0; | ||
for (var i = 0; k < pattern.length && i < segments.length; i++) { | ||
previousNode, | ||
isAtPropertyKey, | ||
matches: (pattern) => { | ||
let k = 0; | ||
for (let i = 0; k < pattern.length && i < segments.length; i++) { | ||
if (pattern[k] === segments[i] || pattern[k] === '*') { | ||
@@ -160,8 +160,6 @@ k++; | ||
*/ | ||
function parse(text, errors, options) { | ||
if (errors === void 0) { errors = []; } | ||
if (options === void 0) { options = ParseOptions.DEFAULT; } | ||
var currentProperty = null; | ||
var currentParent = []; | ||
var previousParents = []; | ||
function parse(text, errors = [], options = ParseOptions.DEFAULT) { | ||
let currentProperty = null; | ||
let currentParent = []; | ||
const previousParents = []; | ||
function onValue(value) { | ||
@@ -175,5 +173,5 @@ if (Array.isArray(currentParent)) { | ||
} | ||
var visitor = { | ||
onObjectBegin: function () { | ||
var object = {}; | ||
const visitor = { | ||
onObjectBegin: () => { | ||
const object = {}; | ||
onValue(object); | ||
@@ -184,10 +182,10 @@ previousParents.push(currentParent); | ||
}, | ||
onObjectProperty: function (name) { | ||
onObjectProperty: (name) => { | ||
currentProperty = name; | ||
}, | ||
onObjectEnd: function () { | ||
onObjectEnd: () => { | ||
currentParent = previousParents.pop(); | ||
}, | ||
onArrayBegin: function () { | ||
var array = []; | ||
onArrayBegin: () => { | ||
const array = []; | ||
onValue(array); | ||
@@ -198,8 +196,8 @@ previousParents.push(currentParent); | ||
}, | ||
onArrayEnd: function () { | ||
onArrayEnd: () => { | ||
currentParent = previousParents.pop(); | ||
}, | ||
onLiteralValue: onValue, | ||
onError: function (error, offset, length) { | ||
errors.push({ error: error, offset: offset, length: length }); | ||
onError: (error, offset, length) => { | ||
errors.push({ error, offset, length }); | ||
} | ||
@@ -214,6 +212,4 @@ }; | ||
*/ | ||
function parseTree(text, errors, options) { | ||
if (errors === void 0) { errors = []; } | ||
if (options === void 0) { options = ParseOptions.DEFAULT; } | ||
var currentParent = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root | ||
function parseTree(text, errors = [], options = ParseOptions.DEFAULT) { | ||
let currentParent = { type: 'array', offset: -1, length: -1, children: [], parent: undefined }; // artificial root | ||
function ensurePropertyComplete(endOffset) { | ||
@@ -229,11 +225,11 @@ if (currentParent.type === 'property') { | ||
} | ||
var visitor = { | ||
onObjectBegin: function (offset) { | ||
currentParent = onValue({ type: 'object', offset: offset, length: -1, parent: currentParent, children: [] }); | ||
const visitor = { | ||
onObjectBegin: (offset) => { | ||
currentParent = onValue({ type: 'object', offset, length: -1, parent: currentParent, children: [] }); | ||
}, | ||
onObjectProperty: function (name, offset, length) { | ||
currentParent = onValue({ type: 'property', offset: offset, length: -1, parent: currentParent, children: [] }); | ||
currentParent.children.push({ type: 'string', value: name, offset: offset, length: length, parent: currentParent }); | ||
onObjectProperty: (name, offset, length) => { | ||
currentParent = onValue({ type: 'property', offset, length: -1, parent: currentParent, children: [] }); | ||
currentParent.children.push({ type: 'string', value: name, offset, length, parent: currentParent }); | ||
}, | ||
onObjectEnd: function (offset, length) { | ||
onObjectEnd: (offset, length) => { | ||
ensurePropertyComplete(offset + length); // in case of a missing value for a property: make sure property is complete | ||
@@ -244,6 +240,6 @@ currentParent.length = offset + length - currentParent.offset; | ||
}, | ||
onArrayBegin: function (offset, length) { | ||
currentParent = onValue({ type: 'array', offset: offset, length: -1, parent: currentParent, children: [] }); | ||
onArrayBegin: (offset, length) => { | ||
currentParent = onValue({ type: 'array', offset, length: -1, parent: currentParent, children: [] }); | ||
}, | ||
onArrayEnd: function (offset, length) { | ||
onArrayEnd: (offset, length) => { | ||
currentParent.length = offset + length - currentParent.offset; | ||
@@ -253,7 +249,7 @@ currentParent = currentParent.parent; | ||
}, | ||
onLiteralValue: function (value, offset, length) { | ||
onValue({ type: getNodeType(value), offset: offset, length: length, parent: currentParent, value: value }); | ||
onLiteralValue: (value, offset, length) => { | ||
onValue({ type: getNodeType(value), offset, length, parent: currentParent, value }); | ||
ensurePropertyComplete(offset + length); | ||
}, | ||
onSeparator: function (sep, offset, length) { | ||
onSeparator: (sep, offset, length) => { | ||
if (currentParent.type === 'property') { | ||
@@ -268,8 +264,8 @@ if (sep === ':') { | ||
}, | ||
onError: function (error, offset, length) { | ||
errors.push({ error: error, offset: offset, length: length }); | ||
onError: (error, offset, length) => { | ||
errors.push({ error, offset, length }); | ||
} | ||
}; | ||
visit(text, visitor, options); | ||
var result = currentParent.children[0]; | ||
const result = currentParent.children[0]; | ||
if (result) { | ||
@@ -288,5 +284,4 @@ delete result.parent; | ||
} | ||
var node = root; | ||
for (var _i = 0, path_1 = path; _i < path_1.length; _i++) { | ||
var segment = path_1[_i]; | ||
let node = root; | ||
for (let segment of path) { | ||
if (typeof segment === 'string') { | ||
@@ -296,5 +291,4 @@ if (node.type !== 'object' || !Array.isArray(node.children)) { | ||
} | ||
var found = false; | ||
for (var _a = 0, _b = node.children; _a < _b.length; _a++) { | ||
var propertyNode = _b[_a]; | ||
let found = false; | ||
for (const propertyNode of node.children) { | ||
if (Array.isArray(propertyNode.children) && propertyNode.children[0].value === segment && propertyNode.children.length === 2) { | ||
@@ -311,3 +305,3 @@ node = propertyNode.children[1]; | ||
else { | ||
var index = segment; | ||
const index = segment; | ||
if (node.type !== 'array' || index < 0 || !Array.isArray(node.children) || index >= node.children.length) { | ||
@@ -329,9 +323,9 @@ return undefined; | ||
} | ||
var path = getNodePath(node.parent); | ||
const path = getNodePath(node.parent); | ||
if (node.parent.type === 'property') { | ||
var key = node.parent.children[0].value; | ||
const key = node.parent.children[0].value; | ||
path.push(key); | ||
} | ||
else if (node.parent.type === 'array') { | ||
var index = node.parent.children.indexOf(node); | ||
const index = node.parent.children.indexOf(node); | ||
if (index !== -1) { | ||
@@ -352,6 +346,5 @@ path.push(index); | ||
case 'object': | ||
var obj = Object.create(null); | ||
for (var _i = 0, _a = node.children; _i < _a.length; _i++) { | ||
var prop = _a[_i]; | ||
var valueNode = prop.children[1]; | ||
const obj = Object.create(null); | ||
for (let prop of node.children) { | ||
const valueNode = prop.children[1]; | ||
if (valueNode) { | ||
@@ -372,4 +365,3 @@ obj[prop.children[0].value] = getNodeValue(valueNode); | ||
exports.getNodeValue = getNodeValue; | ||
function contains(node, offset, includeRightBound) { | ||
if (includeRightBound === void 0) { includeRightBound = false; } | ||
function contains(node, offset, includeRightBound = false) { | ||
return (offset >= node.offset && offset < (node.offset + node.length)) || includeRightBound && (offset === (node.offset + node.length)); | ||
@@ -381,9 +373,8 @@ } | ||
*/ | ||
function findNodeAtOffset(node, offset, includeRightBound) { | ||
if (includeRightBound === void 0) { includeRightBound = false; } | ||
function findNodeAtOffset(node, offset, includeRightBound = false) { | ||
if (contains(node, offset, includeRightBound)) { | ||
var children = node.children; | ||
const children = node.children; | ||
if (Array.isArray(children)) { | ||
for (var i = 0; i < children.length && children[i].offset <= offset; i++) { | ||
var item = findNodeAtOffset(children[i], offset, includeRightBound); | ||
for (let i = 0; i < children.length && children[i].offset <= offset; i++) { | ||
const item = findNodeAtOffset(children[i], offset, includeRightBound); | ||
if (item) { | ||
@@ -402,26 +393,25 @@ return item; | ||
*/ | ||
function visit(text, visitor, options) { | ||
if (options === void 0) { options = ParseOptions.DEFAULT; } | ||
var _scanner = (0, scanner_1.createScanner)(text, false); | ||
function visit(text, visitor, options = ParseOptions.DEFAULT) { | ||
const _scanner = (0, scanner_1.createScanner)(text, false); | ||
// Important: Only pass copies of this to visitor functions to prevent accidental modification, and | ||
// to not affect visitor functions which stored a reference to a previous JSONPath | ||
var _jsonPath = []; | ||
const _jsonPath = []; | ||
function toNoArgVisit(visitFunction) { | ||
return visitFunction ? function () { return visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()); } : function () { return true; }; | ||
return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true; | ||
} | ||
function toNoArgVisitWithPath(visitFunction) { | ||
return visitFunction ? function () { return visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), function () { return _jsonPath.slice(); }); } : function () { return true; }; | ||
return visitFunction ? () => visitFunction(_scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true; | ||
} | ||
function toOneArgVisit(visitFunction) { | ||
return visitFunction ? function (arg) { return visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()); } : function () { return true; }; | ||
return visitFunction ? (arg) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter()) : () => true; | ||
} | ||
function toOneArgVisitWithPath(visitFunction) { | ||
return visitFunction ? function (arg) { return visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), function () { return _jsonPath.slice(); }); } : function () { return true; }; | ||
return visitFunction ? (arg) => visitFunction(arg, _scanner.getTokenOffset(), _scanner.getTokenLength(), _scanner.getTokenStartLine(), _scanner.getTokenStartCharacter(), () => _jsonPath.slice()) : () => true; | ||
} | ||
var onObjectBegin = toNoArgVisitWithPath(visitor.onObjectBegin), onObjectProperty = toOneArgVisitWithPath(visitor.onObjectProperty), onObjectEnd = toNoArgVisit(visitor.onObjectEnd), onArrayBegin = toNoArgVisitWithPath(visitor.onArrayBegin), onArrayEnd = toNoArgVisit(visitor.onArrayEnd), onLiteralValue = toOneArgVisitWithPath(visitor.onLiteralValue), onSeparator = toOneArgVisit(visitor.onSeparator), onComment = toNoArgVisit(visitor.onComment), onError = toOneArgVisit(visitor.onError); | ||
var disallowComments = options && options.disallowComments; | ||
var allowTrailingComma = options && options.allowTrailingComma; | ||
const onObjectBegin = toNoArgVisitWithPath(visitor.onObjectBegin), onObjectProperty = toOneArgVisitWithPath(visitor.onObjectProperty), onObjectEnd = toNoArgVisit(visitor.onObjectEnd), onArrayBegin = toNoArgVisitWithPath(visitor.onArrayBegin), onArrayEnd = toNoArgVisit(visitor.onArrayEnd), onLiteralValue = toOneArgVisitWithPath(visitor.onLiteralValue), onSeparator = toOneArgVisit(visitor.onSeparator), onComment = toNoArgVisit(visitor.onComment), onError = toOneArgVisit(visitor.onError); | ||
const disallowComments = options && options.disallowComments; | ||
const allowTrailingComma = options && options.allowTrailingComma; | ||
function scanNext() { | ||
while (true) { | ||
var token = _scanner.scan(); | ||
const token = _scanner.scan(); | ||
switch (_scanner.getTokenError()) { | ||
@@ -470,8 +460,6 @@ case 4 /* ScanError.InvalidUnicode */: | ||
} | ||
function handleError(error, skipUntilAfter, skipUntil) { | ||
if (skipUntilAfter === void 0) { skipUntilAfter = []; } | ||
if (skipUntil === void 0) { skipUntil = []; } | ||
function handleError(error, skipUntilAfter = [], skipUntil = []) { | ||
onError(error); | ||
if (skipUntilAfter.length + skipUntil.length > 0) { | ||
var token = _scanner.getToken(); | ||
let token = _scanner.getToken(); | ||
while (token !== 17 /* SyntaxKind.EOF */) { | ||
@@ -490,3 +478,3 @@ if (skipUntilAfter.indexOf(token) !== -1) { | ||
function parseString(isValue) { | ||
var value = _scanner.getTokenValue(); | ||
const value = _scanner.getTokenValue(); | ||
if (isValue) { | ||
@@ -506,4 +494,4 @@ onLiteralValue(value); | ||
case 11 /* SyntaxKind.NumericLiteral */: | ||
var tokenValue = _scanner.getTokenValue(); | ||
var value = Number(tokenValue); | ||
const tokenValue = _scanner.getTokenValue(); | ||
let value = Number(tokenValue); | ||
if (isNaN(value)) { | ||
@@ -552,3 +540,3 @@ handleError(2 /* ParseErrorCode.InvalidNumberFormat */); | ||
scanNext(); // consume open brace | ||
var needsComma = false; | ||
let needsComma = false; | ||
while (_scanner.getToken() !== 2 /* SyntaxKind.CloseBraceToken */ && _scanner.getToken() !== 17 /* SyntaxKind.EOF */) { | ||
@@ -585,4 +573,4 @@ if (_scanner.getToken() === 5 /* SyntaxKind.CommaToken */) { | ||
scanNext(); // consume open bracket | ||
var isFirstElement = true; | ||
var needsComma = false; | ||
let isFirstElement = true; | ||
let needsComma = false; | ||
while (_scanner.getToken() !== 4 /* SyntaxKind.CloseBracketToken */ && _scanner.getToken() !== 17 /* SyntaxKind.EOF */) { | ||
@@ -662,3 +650,3 @@ if (_scanner.getToken() === 5 /* SyntaxKind.CommaToken */) { | ||
function stripComments(text, replaceCh) { | ||
var _scanner = (0, scanner_1.createScanner)(text), parts = [], kind, offset = 0, pos; | ||
let _scanner = (0, scanner_1.createScanner)(text), parts = [], kind, offset = 0, pos; | ||
do { | ||
@@ -665,0 +653,0 @@ pos = _scanner.getPosition(); |
@@ -21,11 +21,10 @@ (function (factory) { | ||
*/ | ||
function createScanner(text, ignoreTrivia) { | ||
if (ignoreTrivia === void 0) { ignoreTrivia = false; } | ||
var len = text.length; | ||
var pos = 0, value = '', tokenOffset = 0, token = 16 /* SyntaxKind.Unknown */, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0 /* ScanError.None */; | ||
function createScanner(text, ignoreTrivia = false) { | ||
const len = text.length; | ||
let pos = 0, value = '', tokenOffset = 0, token = 16 /* SyntaxKind.Unknown */, lineNumber = 0, lineStartOffset = 0, tokenLineStartOffset = 0, prevTokenLineStartOffset = 0, scanError = 0 /* ScanError.None */; | ||
function scanHexDigits(count, exact) { | ||
var digits = 0; | ||
var value = 0; | ||
let digits = 0; | ||
let value = 0; | ||
while (digits < count || !exact) { | ||
var ch = text.charCodeAt(pos); | ||
let ch = text.charCodeAt(pos); | ||
if (ch >= 48 /* CharacterCodes._0 */ && ch <= 57 /* CharacterCodes._9 */) { | ||
@@ -59,3 +58,3 @@ value = value * 16 + ch - 48 /* CharacterCodes._0 */; | ||
function scanNumber() { | ||
var start = pos; | ||
let start = pos; | ||
if (text.charCodeAt(pos) === 48 /* CharacterCodes._0 */) { | ||
@@ -83,3 +82,3 @@ pos++; | ||
} | ||
var end = pos; | ||
let end = pos; | ||
if (pos < text.length && (text.charCodeAt(pos) === 69 /* CharacterCodes.E */ || text.charCodeAt(pos) === 101 /* CharacterCodes.e */)) { | ||
@@ -104,3 +103,3 @@ pos++; | ||
function scanString() { | ||
var result = '', start = pos; | ||
let result = '', start = pos; | ||
while (true) { | ||
@@ -112,3 +111,3 @@ if (pos >= len) { | ||
} | ||
var ch = text.charCodeAt(pos); | ||
const ch = text.charCodeAt(pos); | ||
if (ch === 34 /* CharacterCodes.doubleQuote */) { | ||
@@ -126,3 +125,3 @@ result += text.substring(start, pos); | ||
} | ||
var ch2 = text.charCodeAt(pos++); | ||
const ch2 = text.charCodeAt(pos++); | ||
switch (ch2) { | ||
@@ -154,3 +153,3 @@ case 34 /* CharacterCodes.doubleQuote */: | ||
case 117 /* CharacterCodes.u */: | ||
var ch3 = scanHexDigits(4, true); | ||
const ch3 = scanHexDigits(4, true); | ||
if (ch3 >= 0) { | ||
@@ -195,3 +194,3 @@ result += String.fromCharCode(ch3); | ||
} | ||
var code = text.charCodeAt(pos); | ||
let code = text.charCodeAt(pos); | ||
// trivia: whitespace | ||
@@ -245,3 +244,3 @@ if (isWhiteSpace(code)) { | ||
case 47 /* CharacterCodes.slash */: | ||
var start = pos - 1; | ||
const start = pos - 1; | ||
// Single-line comment | ||
@@ -262,6 +261,6 @@ if (text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) { | ||
pos += 2; | ||
var safeLength = len - 1; // For lookahead. | ||
var commentClosed = false; | ||
const safeLength = len - 1; // For lookahead. | ||
let commentClosed = false; | ||
while (pos < safeLength) { | ||
var ch = text.charCodeAt(pos); | ||
const ch = text.charCodeAt(pos); | ||
if (ch === 42 /* CharacterCodes.asterisk */ && text.charCodeAt(pos + 1) === 47 /* CharacterCodes.slash */) { | ||
@@ -355,3 +354,3 @@ pos += 2; | ||
function scanNextNonTrivia() { | ||
var result; | ||
let result; | ||
do { | ||
@@ -364,11 +363,11 @@ result = scanNext(); | ||
setPosition: setPosition, | ||
getPosition: function () { return pos; }, | ||
getPosition: () => pos, | ||
scan: ignoreTrivia ? scanNextNonTrivia : scanNext, | ||
getToken: function () { return token; }, | ||
getTokenValue: function () { return value; }, | ||
getTokenOffset: function () { return tokenOffset; }, | ||
getTokenLength: function () { return pos - tokenOffset; }, | ||
getTokenStartLine: function () { return lineStartOffset; }, | ||
getTokenStartCharacter: function () { return tokenOffset - prevTokenLineStartOffset; }, | ||
getTokenError: function () { return scanError; }, | ||
getToken: () => token, | ||
getTokenValue: () => value, | ||
getTokenOffset: () => tokenOffset, | ||
getTokenLength: () => pos - tokenOffset, | ||
getTokenStartLine: () => lineStartOffset, | ||
getTokenStartCharacter: () => tokenOffset - prevTokenLineStartOffset, | ||
getTokenError: () => scanError, | ||
}; | ||
@@ -386,2 +385,85 @@ } | ||
} | ||
var CharacterCodes; | ||
(function (CharacterCodes) { | ||
CharacterCodes[CharacterCodes["lineFeed"] = 10] = "lineFeed"; | ||
CharacterCodes[CharacterCodes["carriageReturn"] = 13] = "carriageReturn"; | ||
CharacterCodes[CharacterCodes["space"] = 32] = "space"; | ||
CharacterCodes[CharacterCodes["_0"] = 48] = "_0"; | ||
CharacterCodes[CharacterCodes["_1"] = 49] = "_1"; | ||
CharacterCodes[CharacterCodes["_2"] = 50] = "_2"; | ||
CharacterCodes[CharacterCodes["_3"] = 51] = "_3"; | ||
CharacterCodes[CharacterCodes["_4"] = 52] = "_4"; | ||
CharacterCodes[CharacterCodes["_5"] = 53] = "_5"; | ||
CharacterCodes[CharacterCodes["_6"] = 54] = "_6"; | ||
CharacterCodes[CharacterCodes["_7"] = 55] = "_7"; | ||
CharacterCodes[CharacterCodes["_8"] = 56] = "_8"; | ||
CharacterCodes[CharacterCodes["_9"] = 57] = "_9"; | ||
CharacterCodes[CharacterCodes["a"] = 97] = "a"; | ||
CharacterCodes[CharacterCodes["b"] = 98] = "b"; | ||
CharacterCodes[CharacterCodes["c"] = 99] = "c"; | ||
CharacterCodes[CharacterCodes["d"] = 100] = "d"; | ||
CharacterCodes[CharacterCodes["e"] = 101] = "e"; | ||
CharacterCodes[CharacterCodes["f"] = 102] = "f"; | ||
CharacterCodes[CharacterCodes["g"] = 103] = "g"; | ||
CharacterCodes[CharacterCodes["h"] = 104] = "h"; | ||
CharacterCodes[CharacterCodes["i"] = 105] = "i"; | ||
CharacterCodes[CharacterCodes["j"] = 106] = "j"; | ||
CharacterCodes[CharacterCodes["k"] = 107] = "k"; | ||
CharacterCodes[CharacterCodes["l"] = 108] = "l"; | ||
CharacterCodes[CharacterCodes["m"] = 109] = "m"; | ||
CharacterCodes[CharacterCodes["n"] = 110] = "n"; | ||
CharacterCodes[CharacterCodes["o"] = 111] = "o"; | ||
CharacterCodes[CharacterCodes["p"] = 112] = "p"; | ||
CharacterCodes[CharacterCodes["q"] = 113] = "q"; | ||
CharacterCodes[CharacterCodes["r"] = 114] = "r"; | ||
CharacterCodes[CharacterCodes["s"] = 115] = "s"; | ||
CharacterCodes[CharacterCodes["t"] = 116] = "t"; | ||
CharacterCodes[CharacterCodes["u"] = 117] = "u"; | ||
CharacterCodes[CharacterCodes["v"] = 118] = "v"; | ||
CharacterCodes[CharacterCodes["w"] = 119] = "w"; | ||
CharacterCodes[CharacterCodes["x"] = 120] = "x"; | ||
CharacterCodes[CharacterCodes["y"] = 121] = "y"; | ||
CharacterCodes[CharacterCodes["z"] = 122] = "z"; | ||
CharacterCodes[CharacterCodes["A"] = 65] = "A"; | ||
CharacterCodes[CharacterCodes["B"] = 66] = "B"; | ||
CharacterCodes[CharacterCodes["C"] = 67] = "C"; | ||
CharacterCodes[CharacterCodes["D"] = 68] = "D"; | ||
CharacterCodes[CharacterCodes["E"] = 69] = "E"; | ||
CharacterCodes[CharacterCodes["F"] = 70] = "F"; | ||
CharacterCodes[CharacterCodes["G"] = 71] = "G"; | ||
CharacterCodes[CharacterCodes["H"] = 72] = "H"; | ||
CharacterCodes[CharacterCodes["I"] = 73] = "I"; | ||
CharacterCodes[CharacterCodes["J"] = 74] = "J"; | ||
CharacterCodes[CharacterCodes["K"] = 75] = "K"; | ||
CharacterCodes[CharacterCodes["L"] = 76] = "L"; | ||
CharacterCodes[CharacterCodes["M"] = 77] = "M"; | ||
CharacterCodes[CharacterCodes["N"] = 78] = "N"; | ||
CharacterCodes[CharacterCodes["O"] = 79] = "O"; | ||
CharacterCodes[CharacterCodes["P"] = 80] = "P"; | ||
CharacterCodes[CharacterCodes["Q"] = 81] = "Q"; | ||
CharacterCodes[CharacterCodes["R"] = 82] = "R"; | ||
CharacterCodes[CharacterCodes["S"] = 83] = "S"; | ||
CharacterCodes[CharacterCodes["T"] = 84] = "T"; | ||
CharacterCodes[CharacterCodes["U"] = 85] = "U"; | ||
CharacterCodes[CharacterCodes["V"] = 86] = "V"; | ||
CharacterCodes[CharacterCodes["W"] = 87] = "W"; | ||
CharacterCodes[CharacterCodes["X"] = 88] = "X"; | ||
CharacterCodes[CharacterCodes["Y"] = 89] = "Y"; | ||
CharacterCodes[CharacterCodes["Z"] = 90] = "Z"; | ||
CharacterCodes[CharacterCodes["asterisk"] = 42] = "asterisk"; | ||
CharacterCodes[CharacterCodes["backslash"] = 92] = "backslash"; | ||
CharacterCodes[CharacterCodes["closeBrace"] = 125] = "closeBrace"; | ||
CharacterCodes[CharacterCodes["closeBracket"] = 93] = "closeBracket"; | ||
CharacterCodes[CharacterCodes["colon"] = 58] = "colon"; | ||
CharacterCodes[CharacterCodes["comma"] = 44] = "comma"; | ||
CharacterCodes[CharacterCodes["dot"] = 46] = "dot"; | ||
CharacterCodes[CharacterCodes["doubleQuote"] = 34] = "doubleQuote"; | ||
CharacterCodes[CharacterCodes["minus"] = 45] = "minus"; | ||
CharacterCodes[CharacterCodes["openBrace"] = 123] = "openBrace"; | ||
CharacterCodes[CharacterCodes["openBracket"] = 91] = "openBracket"; | ||
CharacterCodes[CharacterCodes["plus"] = 43] = "plus"; | ||
CharacterCodes[CharacterCodes["slash"] = 47] = "slash"; | ||
CharacterCodes[CharacterCodes["formFeed"] = 12] = "formFeed"; | ||
CharacterCodes[CharacterCodes["tab"] = 9] = "tab"; | ||
})(CharacterCodes || (CharacterCodes = {})); | ||
}); |
@@ -16,7 +16,7 @@ (function (factory) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.applyEdits = exports.modify = exports.format = exports.printParseErrorCode = exports.stripComments = exports.visit = exports.getNodeValue = exports.getNodePath = exports.findNodeAtOffset = exports.findNodeAtLocation = exports.parseTree = exports.parse = exports.getLocation = exports.createScanner = void 0; | ||
var formatter = require("./impl/format"); | ||
var edit = require("./impl/edit"); | ||
var scanner = require("./impl/scanner"); | ||
var parser = require("./impl/parser"); | ||
exports.applyEdits = exports.modify = exports.format = exports.printParseErrorCode = exports.ParseErrorCode = exports.stripComments = exports.visit = exports.getNodeValue = exports.getNodePath = exports.findNodeAtOffset = exports.findNodeAtLocation = exports.parseTree = exports.parse = exports.getLocation = exports.SyntaxKind = exports.ScanError = exports.createScanner = void 0; | ||
const formatter = require("./impl/format"); | ||
const edit = require("./impl/edit"); | ||
const scanner = require("./impl/scanner"); | ||
const parser = require("./impl/parser"); | ||
/** | ||
@@ -27,2 +27,32 @@ * Creates a JSON scanner on the given text. | ||
exports.createScanner = scanner.createScanner; | ||
var ScanError; | ||
(function (ScanError) { | ||
ScanError[ScanError["None"] = 0] = "None"; | ||
ScanError[ScanError["UnexpectedEndOfComment"] = 1] = "UnexpectedEndOfComment"; | ||
ScanError[ScanError["UnexpectedEndOfString"] = 2] = "UnexpectedEndOfString"; | ||
ScanError[ScanError["UnexpectedEndOfNumber"] = 3] = "UnexpectedEndOfNumber"; | ||
ScanError[ScanError["InvalidUnicode"] = 4] = "InvalidUnicode"; | ||
ScanError[ScanError["InvalidEscapeCharacter"] = 5] = "InvalidEscapeCharacter"; | ||
ScanError[ScanError["InvalidCharacter"] = 6] = "InvalidCharacter"; | ||
})(ScanError = exports.ScanError || (exports.ScanError = {})); | ||
var SyntaxKind; | ||
(function (SyntaxKind) { | ||
SyntaxKind[SyntaxKind["OpenBraceToken"] = 1] = "OpenBraceToken"; | ||
SyntaxKind[SyntaxKind["CloseBraceToken"] = 2] = "CloseBraceToken"; | ||
SyntaxKind[SyntaxKind["OpenBracketToken"] = 3] = "OpenBracketToken"; | ||
SyntaxKind[SyntaxKind["CloseBracketToken"] = 4] = "CloseBracketToken"; | ||
SyntaxKind[SyntaxKind["CommaToken"] = 5] = "CommaToken"; | ||
SyntaxKind[SyntaxKind["ColonToken"] = 6] = "ColonToken"; | ||
SyntaxKind[SyntaxKind["NullKeyword"] = 7] = "NullKeyword"; | ||
SyntaxKind[SyntaxKind["TrueKeyword"] = 8] = "TrueKeyword"; | ||
SyntaxKind[SyntaxKind["FalseKeyword"] = 9] = "FalseKeyword"; | ||
SyntaxKind[SyntaxKind["StringLiteral"] = 10] = "StringLiteral"; | ||
SyntaxKind[SyntaxKind["NumericLiteral"] = 11] = "NumericLiteral"; | ||
SyntaxKind[SyntaxKind["LineCommentTrivia"] = 12] = "LineCommentTrivia"; | ||
SyntaxKind[SyntaxKind["BlockCommentTrivia"] = 13] = "BlockCommentTrivia"; | ||
SyntaxKind[SyntaxKind["LineBreakTrivia"] = 14] = "LineBreakTrivia"; | ||
SyntaxKind[SyntaxKind["Trivia"] = 15] = "Trivia"; | ||
SyntaxKind[SyntaxKind["Unknown"] = 16] = "Unknown"; | ||
SyntaxKind[SyntaxKind["EOF"] = 17] = "EOF"; | ||
})(SyntaxKind = exports.SyntaxKind || (exports.SyntaxKind = {})); | ||
/** | ||
@@ -67,2 +97,21 @@ * For a given offset, evaluate the location in the JSON document. Each segment in the location path is either a property name or an array index. | ||
exports.stripComments = parser.stripComments; | ||
var ParseErrorCode; | ||
(function (ParseErrorCode) { | ||
ParseErrorCode[ParseErrorCode["InvalidSymbol"] = 1] = "InvalidSymbol"; | ||
ParseErrorCode[ParseErrorCode["InvalidNumberFormat"] = 2] = "InvalidNumberFormat"; | ||
ParseErrorCode[ParseErrorCode["PropertyNameExpected"] = 3] = "PropertyNameExpected"; | ||
ParseErrorCode[ParseErrorCode["ValueExpected"] = 4] = "ValueExpected"; | ||
ParseErrorCode[ParseErrorCode["ColonExpected"] = 5] = "ColonExpected"; | ||
ParseErrorCode[ParseErrorCode["CommaExpected"] = 6] = "CommaExpected"; | ||
ParseErrorCode[ParseErrorCode["CloseBraceExpected"] = 7] = "CloseBraceExpected"; | ||
ParseErrorCode[ParseErrorCode["CloseBracketExpected"] = 8] = "CloseBracketExpected"; | ||
ParseErrorCode[ParseErrorCode["EndOfFileExpected"] = 9] = "EndOfFileExpected"; | ||
ParseErrorCode[ParseErrorCode["InvalidCommentToken"] = 10] = "InvalidCommentToken"; | ||
ParseErrorCode[ParseErrorCode["UnexpectedEndOfComment"] = 11] = "UnexpectedEndOfComment"; | ||
ParseErrorCode[ParseErrorCode["UnexpectedEndOfString"] = 12] = "UnexpectedEndOfString"; | ||
ParseErrorCode[ParseErrorCode["UnexpectedEndOfNumber"] = 13] = "UnexpectedEndOfNumber"; | ||
ParseErrorCode[ParseErrorCode["InvalidUnicode"] = 14] = "InvalidUnicode"; | ||
ParseErrorCode[ParseErrorCode["InvalidEscapeCharacter"] = 15] = "InvalidEscapeCharacter"; | ||
ParseErrorCode[ParseErrorCode["InvalidCharacter"] = 16] = "InvalidCharacter"; | ||
})(ParseErrorCode = exports.ParseErrorCode || (exports.ParseErrorCode = {})); | ||
function printParseErrorCode(code) { | ||
@@ -127,4 +176,19 @@ switch (code) { | ||
function applyEdits(text, edits) { | ||
for (var i = edits.length - 1; i >= 0; i--) { | ||
text = edit.applyEdit(text, edits[i]); | ||
let sortedEdits = edits.slice(0).sort((a, b) => { | ||
const diff = a.offset - b.offset; | ||
if (diff === 0) { | ||
return a.length - b.length; | ||
} | ||
return diff; | ||
}); | ||
let lastModifiedOffset = text.length; | ||
for (let i = sortedEdits.length - 1; i >= 0; i--) { | ||
let e = sortedEdits[i]; | ||
if (e.offset + e.length <= lastModifiedOffset) { | ||
text = edit.applyEdit(text, e); | ||
} | ||
else { | ||
throw new Error('Overlapping edit'); | ||
} | ||
lastModifiedOffset = e.offset; | ||
} | ||
@@ -131,0 +195,0 @@ return text; |
{ | ||
"name": "jsonc-parser", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Scanner and parser for JSON with comments.", | ||
@@ -19,8 +19,8 @@ "main": "./lib/umd/main.js", | ||
"mocha": "^10.0.0", | ||
"typescript": "^4.7.4", | ||
"typescript": "^4.8.2", | ||
"@types/node": "^16.x", | ||
"@types/mocha": "^9.1.1", | ||
"@typescript-eslint/eslint-plugin": "^5.30.5", | ||
"@typescript-eslint/parser": "^5.30.5", | ||
"eslint": "^8.19.0", | ||
"@typescript-eslint/eslint-plugin": "^5.36.0", | ||
"@typescript-eslint/parser": "^5.36.0", | ||
"eslint": "^8.23.0", | ||
"rimraf": "^3.0.2" | ||
@@ -27,0 +27,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
205387
4163