@stoplight/json
Advanced tools
Comparing version 1.8.0 to 1.9.0
import { GetJsonPathForPosition } from '@stoplight/types'; | ||
import { IJsonASTNode } from './types'; | ||
export declare const getJsonPathForPosition: GetJsonPathForPosition<IJsonASTNode, Map<number, number>>; | ||
export declare const getJsonPathForPosition: GetJsonPathForPosition<IJsonASTNode, number[]>; |
@@ -5,8 +5,8 @@ "use strict"; | ||
exports.getJsonPathForPosition = ({ lineMap, ast }, position) => { | ||
const startOffset = lineMap.get(position.line); | ||
const endOffset = lineMap.get(position.line + 1); | ||
const startOffset = lineMap[position.line]; | ||
const endOffset = lineMap[position.line + 1]; | ||
if (startOffset === undefined) { | ||
return; | ||
} | ||
const node = jsonc_parser_1.findNodeAtOffset(ast, endOffset === undefined ? startOffset + position.character : Math.min(endOffset, startOffset + position.character)); | ||
const node = jsonc_parser_1.findNodeAtOffset(ast, endOffset === undefined ? startOffset + position.character : Math.min(endOffset, startOffset + position.character), true); | ||
if (node === undefined) { | ||
@@ -13,0 +13,0 @@ return; |
import { GetLocationForJsonPath } from '@stoplight/types'; | ||
import { IJsonASTNode } from './types'; | ||
export declare const getLocationForJsonPath: GetLocationForJsonPath<IJsonASTNode, Map<number, number>>; | ||
export declare const getLocationForJsonPath: GetLocationForJsonPath<IJsonASTNode, number[]>; |
@@ -6,18 +6,7 @@ "use strict"; | ||
const node = jsonc_parser_1.findNodeAtLocation(ast, path); | ||
if (node === undefined) { | ||
return node; | ||
if (node === undefined || node.range === undefined) { | ||
return; | ||
} | ||
return { | ||
range: { | ||
start: { | ||
character: node.startColumn, | ||
line: node.startLine, | ||
}, | ||
end: { | ||
character: node.endColumn, | ||
line: node.endLine, | ||
}, | ||
}, | ||
}; | ||
return { range: node.range }; | ||
}; | ||
//# sourceMappingURL=getLocationForJsonPath.js.map |
{ | ||
"name": "@stoplight/json", | ||
"version": "1.8.0", | ||
"version": "1.9.0", | ||
"description": "Useful functions when working with JSON.", | ||
@@ -29,3 +29,3 @@ "keywords": [ | ||
"@stoplight/types": "4.0.x", | ||
"jsonc-parser": "https://github.com/stoplightio/node-jsonc-parser.git#f45703496d842937ec822683550067dfe68d898e", | ||
"jsonc-parser": "2.1.0", | ||
"lodash": "4.x.x" | ||
@@ -32,0 +32,0 @@ }, |
@@ -5,3 +5,3 @@ import { IDiagnostic } from '@stoplight/types/diagnostics'; | ||
import { IJsonASTNode } from './types'; | ||
export declare const parseWithPointers: <T = any>(value: string, options?: ParseOptions) => IParserResult<T, IJsonASTNode, Map<number, number>>; | ||
export declare function parseTree<T>(text: string, errors: IDiagnostic[] | undefined, options: ParseOptions): IParserASTResult<T, Node, Map<number, number>>; | ||
export declare const parseWithPointers: <T = any>(value: string, options?: ParseOptions) => IParserResult<T, IJsonASTNode, number[]>; | ||
export declare function parseTree<T>(text: string, errors: IDiagnostic[] | undefined, options: ParseOptions): IParserASTResult<T, Node, number[]>; |
@@ -16,6 +16,3 @@ "use strict"; | ||
function parseTree(text, errors = [], options) { | ||
const lineMap = new Map([[0, 0]]); | ||
let currentLineNumber = 0; | ||
let currentOffset = 0; | ||
let lastErrorEndOffset = -1; | ||
const lineMap = computeLineMap(text); | ||
let currentParent = { type: 'array', offset: -1, length: -1, children: [], parent: void 0 }; | ||
@@ -31,9 +28,12 @@ let currentParsedProperty = null; | ||
} | ||
function calculateRange(offset, length) { | ||
const startColumn = offset - currentOffset; | ||
function calculateRange(startLine, startCharacter, length) { | ||
return { | ||
startColumn, | ||
startLine: currentLineNumber, | ||
endColumn: startColumn + length, | ||
endLine: currentLineNumber, | ||
start: { | ||
line: startLine, | ||
character: startCharacter, | ||
}, | ||
end: { | ||
line: startLine, | ||
character: startCharacter + length, | ||
}, | ||
}; | ||
@@ -63,4 +63,11 @@ } | ||
const visitor = { | ||
onObjectBegin: (offset) => { | ||
currentParent = onValue(Object.assign({ type: 'object', offset, length: -1, parent: currentParent, children: [] }, calculateRange(offset - 1, -1))); | ||
onObjectBegin: (offset, length, startLine, startCharacter) => { | ||
currentParent = onValue({ | ||
type: 'object', | ||
offset, | ||
length: -1, | ||
parent: currentParent, | ||
children: [], | ||
range: calculateRange(startLine, startCharacter, length), | ||
}); | ||
onParsedComplexBegin({}); | ||
@@ -73,6 +80,8 @@ }, | ||
}, | ||
onObjectEnd: (offset, length) => { | ||
onObjectEnd: (offset, length, startLine, startCharacter) => { | ||
currentParent.length = offset + length - currentParent.offset; | ||
currentParent.endColumn = offset - currentOffset; | ||
currentParent.endLine = currentLineNumber; | ||
if (currentParent.range) { | ||
currentParent.range.end.line = startLine; | ||
currentParent.range.end.character = startCharacter + length; | ||
} | ||
currentParent = currentParent.parent; | ||
@@ -82,10 +91,19 @@ ensurePropertyComplete(offset + length); | ||
}, | ||
onArrayBegin: (offset, length) => { | ||
currentParent = onValue(Object.assign({ type: 'array', offset, length: -1, parent: currentParent, children: [] }, calculateRange(offset - 1, -1))); | ||
onArrayBegin: (offset, length, startLine, startCharacter) => { | ||
currentParent = onValue({ | ||
type: 'array', | ||
offset, | ||
length: -1, | ||
parent: currentParent, | ||
children: [], | ||
range: calculateRange(startLine, startCharacter, length), | ||
}); | ||
onParsedComplexBegin([]); | ||
}, | ||
onArrayEnd: (offset, length) => { | ||
onArrayEnd: (offset, length, startLine, startCharacter) => { | ||
currentParent.length = offset + length - currentParent.offset; | ||
currentParent.endColumn = offset - currentOffset; | ||
currentParent.endLine = currentLineNumber; | ||
if (currentParent.range) { | ||
currentParent.range.end.line = startLine; | ||
currentParent.range.end.character = startCharacter + length; | ||
} | ||
currentParent = currentParent.parent; | ||
@@ -95,5 +113,11 @@ ensurePropertyComplete(offset + length); | ||
}, | ||
onLiteralValue: (value, offset, length) => { | ||
onValue(Object.assign({ type: getLiteralNodeType(value), offset, | ||
length, parent: currentParent, value }, calculateRange(offset, length))); | ||
onLiteralValue: (value, offset, length, startLine, startCharacter) => { | ||
onValue({ | ||
type: getLiteralNodeType(value), | ||
offset, | ||
length, | ||
parent: currentParent, | ||
value, | ||
range: calculateRange(startLine, startCharacter, length), | ||
}); | ||
ensurePropertyComplete(offset + length); | ||
@@ -112,16 +136,5 @@ onParsedValue(value); | ||
}, | ||
onError: (error, offset, length) => { | ||
const range = calculateRange(offset, length); | ||
lastErrorEndOffset = offset + length; | ||
onError: (error, offset, length, startLine, startCharacter) => { | ||
errors.push({ | ||
range: { | ||
start: { | ||
character: range.startColumn, | ||
line: range.startLine, | ||
}, | ||
end: { | ||
character: range.endColumn, | ||
line: range.startLine, | ||
}, | ||
}, | ||
range: calculateRange(startLine, startCharacter, length), | ||
message: jsonc_parser_1.printParseErrorCode(error), | ||
@@ -132,12 +145,2 @@ severity: diagnostics_1.DiagnosticSeverity.Error, | ||
}, | ||
onLineBreak: (lineNumber, offset) => { | ||
lineMap.set(lineNumber, offset); | ||
currentLineNumber = lineNumber; | ||
currentOffset = offset; | ||
if (lastErrorEndOffset !== -1 && offset > lastErrorEndOffset) { | ||
errors[errors.length - 1].range.end.line = lineNumber - 1; | ||
errors[errors.length - 1].range.end.character = offset - lastErrorEndOffset; | ||
lastErrorEndOffset = -1; | ||
} | ||
}, | ||
}; | ||
@@ -168,2 +171,12 @@ jsonc_parser_1.visit(text, visitor, options); | ||
} | ||
const computeLineMap = (input) => { | ||
const lines = input.split(/\n/); | ||
const lineMap = [0]; | ||
let sum = 0; | ||
for (const line of lines) { | ||
sum += line.length + 1; | ||
lineMap.push(sum); | ||
} | ||
return lineMap; | ||
}; | ||
//# sourceMappingURL=parseWithPointers.js.map |
@@ -0,1 +1,2 @@ | ||
import { IRange } from '@stoplight/types'; | ||
import { Node, NodeType } from 'jsonc-parser'; | ||
@@ -7,6 +8,3 @@ export interface IJsonASTNode extends Node { | ||
length: number; | ||
startLine?: number; | ||
startColumn?: number; | ||
endLine?: number; | ||
endColumn?: number; | ||
range?: IRange; | ||
colonOffset?: number; | ||
@@ -13,0 +11,0 @@ parent?: IJsonASTNode; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
HTTP dependency
Supply chain riskContains a dependency which resolves to a remote HTTP URL which could be used to inject untrusted code and reduce overall package reliability.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
0
0
48736
+ Addedjsonc-parser@2.1.0(transitive)
Updatedjsonc-parser@2.1.0