jsonc-parser
Advanced tools
Comparing version 3.2.0 to 3.2.1
@@ -7,2 +7,3 @@ /*--------------------------------------------------------------------------------------------- | ||
import { createScanner } from './scanner'; | ||
import { cachedSpaces, cachedBreakLinesWithSpaces, supportedEols } from './string-intern'; | ||
export function format(documentText, range, options) { | ||
@@ -36,2 +37,3 @@ let initialIndentLevel; | ||
const eol = getEOL(options, documentText); | ||
const eolFastPathSupported = supportedEols.includes(eol); | ||
let numberLineBreaks = 0; | ||
@@ -41,3 +43,3 @@ let indentLevel = 0; | ||
if (options.insertSpaces) { | ||
indentValue = repeat(' ', options.tabSize || 4); | ||
indentValue = cachedSpaces[options.tabSize || 4] ?? repeat(cachedSpaces[1], options.tabSize || 4); | ||
} | ||
@@ -47,2 +49,3 @@ else { | ||
} | ||
const indentType = indentValue === '\t' ? '\t' : ' '; | ||
let scanner = createScanner(formatText, false); | ||
@@ -54,5 +57,10 @@ let hasError = false; | ||
} | ||
else { | ||
const amountOfSpaces = indentValue.length * (initialIndentLevel + indentLevel); | ||
if (!eolFastPathSupported || amountOfSpaces > cachedBreakLinesWithSpaces[indentType][eol].length) { | ||
return eol + repeat(indentValue, initialIndentLevel + indentLevel); | ||
} | ||
if (amountOfSpaces <= 0) { | ||
return eol; | ||
} | ||
return cachedBreakLinesWithSpaces[indentType][eol][amountOfSpaces]; | ||
} | ||
@@ -86,3 +94,5 @@ function scanNext() { | ||
let firstTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
let initialIndent = repeat(indentValue, initialIndentLevel); | ||
let initialIndent = (indentValue.length * initialIndentLevel < 20) && options.insertSpaces | ||
? cachedSpaces[indentValue.length * initialIndentLevel] | ||
: repeat(indentValue, initialIndentLevel); | ||
addEdit(initialIndent, formatTextStart, firstTokenStart); | ||
@@ -97,3 +107,3 @@ } | ||
let commentTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
addEdit(' ', firstTokenEnd, commentTokenStart); | ||
addEdit(cachedSpaces[1], firstTokenEnd, commentTokenStart); | ||
firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; | ||
@@ -113,3 +123,3 @@ needsLineBreak = secondToken === 12 /* SyntaxKind.LineCommentTrivia */; | ||
else if (options.keepLines) { | ||
replaceContent = ' '; | ||
replaceContent = cachedSpaces[1]; | ||
} | ||
@@ -126,3 +136,3 @@ } | ||
else if (options.keepLines) { | ||
replaceContent = ' '; | ||
replaceContent = cachedSpaces[1]; | ||
} | ||
@@ -139,3 +149,3 @@ } | ||
else { | ||
replaceContent = ' '; | ||
replaceContent = cachedSpaces[1]; | ||
} | ||
@@ -148,3 +158,3 @@ break; | ||
else { | ||
replaceContent = ' '; | ||
replaceContent = cachedSpaces[1]; | ||
} | ||
@@ -160,3 +170,3 @@ break; | ||
else if (!needsLineBreak) { | ||
replaceContent = ' '; | ||
replaceContent = cachedSpaces[1]; | ||
} | ||
@@ -169,3 +179,3 @@ break; | ||
else if (!needsLineBreak) { | ||
replaceContent = ' '; | ||
replaceContent = cachedSpaces[1]; | ||
} | ||
@@ -192,3 +202,3 @@ break; | ||
if ((secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */) && !needsLineBreak) { | ||
replaceContent = ' '; | ||
replaceContent = cachedSpaces[1]; | ||
} | ||
@@ -235,3 +245,3 @@ else if (secondToken !== 5 /* SyntaxKind.CommaToken */ && secondToken !== 17 /* SyntaxKind.EOF */) { | ||
let ch = content.charAt(i); | ||
if (ch === ' ') { | ||
if (ch === cachedSpaces[1]) { | ||
nChars++; | ||
@@ -238,0 +248,0 @@ } |
@@ -142,3 +142,3 @@ /** | ||
export declare function printParseErrorCode(code: ParseErrorCode): "InvalidSymbol" | "InvalidNumberFormat" | "PropertyNameExpected" | "ValueExpected" | "ColonExpected" | "CommaExpected" | "CloseBraceExpected" | "CloseBracketExpected" | "EndOfFileExpected" | "InvalidCommentToken" | "UnexpectedEndOfComment" | "UnexpectedEndOfString" | "UnexpectedEndOfNumber" | "InvalidUnicode" | "InvalidEscapeCharacter" | "InvalidCharacter" | "<unknown ParseErrorCode>"; | ||
export declare type NodeType = 'object' | 'array' | 'property' | 'string' | 'number' | 'boolean' | 'null'; | ||
export type NodeType = 'object' | 'array' | 'property' | 'string' | 'number' | 'boolean' | 'null'; | ||
export interface Node { | ||
@@ -157,4 +157,4 @@ readonly type: NodeType; | ||
*/ | ||
export declare type Segment = string | number; | ||
export declare type JSONPath = Segment[]; | ||
export type Segment = string | number; | ||
export type JSONPath = Segment[]; | ||
export interface Location { | ||
@@ -247,3 +247,3 @@ /** | ||
*/ | ||
export declare type EditResult = Edit[]; | ||
export type EditResult = Edit[]; | ||
/** | ||
@@ -250,0 +250,0 @@ * Represents a text modification |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./scanner"], factory); | ||
define(["require", "exports", "./scanner", "./string-intern"], factory); | ||
} | ||
@@ -19,2 +19,3 @@ })(function (require, exports) { | ||
const scanner_1 = require("./scanner"); | ||
const string_intern_1 = require("./string-intern"); | ||
function format(documentText, range, options) { | ||
@@ -48,2 +49,3 @@ let initialIndentLevel; | ||
const eol = getEOL(options, documentText); | ||
const eolFastPathSupported = string_intern_1.supportedEols.includes(eol); | ||
let numberLineBreaks = 0; | ||
@@ -53,3 +55,3 @@ let indentLevel = 0; | ||
if (options.insertSpaces) { | ||
indentValue = repeat(' ', options.tabSize || 4); | ||
indentValue = string_intern_1.cachedSpaces[options.tabSize || 4] ?? repeat(string_intern_1.cachedSpaces[1], options.tabSize || 4); | ||
} | ||
@@ -59,2 +61,3 @@ else { | ||
} | ||
const indentType = indentValue === '\t' ? '\t' : ' '; | ||
let scanner = (0, scanner_1.createScanner)(formatText, false); | ||
@@ -66,5 +69,10 @@ let hasError = false; | ||
} | ||
else { | ||
const amountOfSpaces = indentValue.length * (initialIndentLevel + indentLevel); | ||
if (!eolFastPathSupported || amountOfSpaces > string_intern_1.cachedBreakLinesWithSpaces[indentType][eol].length) { | ||
return eol + repeat(indentValue, initialIndentLevel + indentLevel); | ||
} | ||
if (amountOfSpaces <= 0) { | ||
return eol; | ||
} | ||
return string_intern_1.cachedBreakLinesWithSpaces[indentType][eol][amountOfSpaces]; | ||
} | ||
@@ -98,3 +106,5 @@ function scanNext() { | ||
let firstTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
let initialIndent = repeat(indentValue, initialIndentLevel); | ||
let initialIndent = (indentValue.length * initialIndentLevel < 20) && options.insertSpaces | ||
? string_intern_1.cachedSpaces[indentValue.length * initialIndentLevel] | ||
: repeat(indentValue, initialIndentLevel); | ||
addEdit(initialIndent, formatTextStart, firstTokenStart); | ||
@@ -109,3 +119,3 @@ } | ||
let commentTokenStart = scanner.getTokenOffset() + formatTextStart; | ||
addEdit(' ', firstTokenEnd, commentTokenStart); | ||
addEdit(string_intern_1.cachedSpaces[1], firstTokenEnd, commentTokenStart); | ||
firstTokenEnd = scanner.getTokenOffset() + scanner.getTokenLength() + formatTextStart; | ||
@@ -125,3 +135,3 @@ needsLineBreak = secondToken === 12 /* SyntaxKind.LineCommentTrivia */; | ||
else if (options.keepLines) { | ||
replaceContent = ' '; | ||
replaceContent = string_intern_1.cachedSpaces[1]; | ||
} | ||
@@ -138,3 +148,3 @@ } | ||
else if (options.keepLines) { | ||
replaceContent = ' '; | ||
replaceContent = string_intern_1.cachedSpaces[1]; | ||
} | ||
@@ -151,3 +161,3 @@ } | ||
else { | ||
replaceContent = ' '; | ||
replaceContent = string_intern_1.cachedSpaces[1]; | ||
} | ||
@@ -160,3 +170,3 @@ break; | ||
else { | ||
replaceContent = ' '; | ||
replaceContent = string_intern_1.cachedSpaces[1]; | ||
} | ||
@@ -172,3 +182,3 @@ break; | ||
else if (!needsLineBreak) { | ||
replaceContent = ' '; | ||
replaceContent = string_intern_1.cachedSpaces[1]; | ||
} | ||
@@ -181,3 +191,3 @@ break; | ||
else if (!needsLineBreak) { | ||
replaceContent = ' '; | ||
replaceContent = string_intern_1.cachedSpaces[1]; | ||
} | ||
@@ -204,3 +214,3 @@ break; | ||
if ((secondToken === 12 /* SyntaxKind.LineCommentTrivia */ || secondToken === 13 /* SyntaxKind.BlockCommentTrivia */) && !needsLineBreak) { | ||
replaceContent = ' '; | ||
replaceContent = string_intern_1.cachedSpaces[1]; | ||
} | ||
@@ -248,3 +258,3 @@ else if (secondToken !== 5 /* SyntaxKind.CommaToken */ && secondToken !== 17 /* SyntaxKind.EOF */) { | ||
let ch = content.charAt(i); | ||
if (ch === ' ') { | ||
if (ch === string_intern_1.cachedSpaces[1]) { | ||
nChars++; | ||
@@ -251,0 +261,0 @@ } |
@@ -142,3 +142,3 @@ /** | ||
export declare function printParseErrorCode(code: ParseErrorCode): "InvalidSymbol" | "InvalidNumberFormat" | "PropertyNameExpected" | "ValueExpected" | "ColonExpected" | "CommaExpected" | "CloseBraceExpected" | "CloseBracketExpected" | "EndOfFileExpected" | "InvalidCommentToken" | "UnexpectedEndOfComment" | "UnexpectedEndOfString" | "UnexpectedEndOfNumber" | "InvalidUnicode" | "InvalidEscapeCharacter" | "InvalidCharacter" | "<unknown ParseErrorCode>"; | ||
export declare type NodeType = 'object' | 'array' | 'property' | 'string' | 'number' | 'boolean' | 'null'; | ||
export type NodeType = 'object' | 'array' | 'property' | 'string' | 'number' | 'boolean' | 'null'; | ||
export interface Node { | ||
@@ -157,4 +157,4 @@ readonly type: NodeType; | ||
*/ | ||
export declare type Segment = string | number; | ||
export declare type JSONPath = Segment[]; | ||
export type Segment = string | number; | ||
export type JSONPath = Segment[]; | ||
export interface Location { | ||
@@ -247,3 +247,3 @@ /** | ||
*/ | ||
export declare type EditResult = Edit[]; | ||
export type EditResult = Edit[]; | ||
/** | ||
@@ -250,0 +250,0 @@ * Represents a text modification |
@@ -35,3 +35,3 @@ (function (factory) { | ||
ScanError[ScanError["InvalidCharacter"] = 6] = "InvalidCharacter"; | ||
})(ScanError = exports.ScanError || (exports.ScanError = {})); | ||
})(ScanError || (exports.ScanError = ScanError = {})); | ||
var SyntaxKind; | ||
@@ -56,3 +56,3 @@ (function (SyntaxKind) { | ||
SyntaxKind[SyntaxKind["EOF"] = 17] = "EOF"; | ||
})(SyntaxKind = exports.SyntaxKind || (exports.SyntaxKind = {})); | ||
})(SyntaxKind || (exports.SyntaxKind = SyntaxKind = {})); | ||
/** | ||
@@ -115,3 +115,3 @@ * 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. | ||
ParseErrorCode[ParseErrorCode["InvalidCharacter"] = 16] = "InvalidCharacter"; | ||
})(ParseErrorCode = exports.ParseErrorCode || (exports.ParseErrorCode = {})); | ||
})(ParseErrorCode || (exports.ParseErrorCode = ParseErrorCode = {})); | ||
function printParseErrorCode(code) { | ||
@@ -118,0 +118,0 @@ switch (code) { |
{ | ||
"name": "jsonc-parser", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "Scanner and parser for JSON with comments.", | ||
@@ -18,14 +18,13 @@ "main": "./lib/umd/main.js", | ||
"devDependencies": { | ||
"mocha": "^10.0.0", | ||
"typescript": "^4.8.2", | ||
"mocha": "^10.2.0", | ||
"typescript": "^5.3.3", | ||
"@types/node": "^16.x", | ||
"@types/mocha": "^9.1.1", | ||
"@typescript-eslint/eslint-plugin": "^5.36.0", | ||
"@typescript-eslint/parser": "^5.36.0", | ||
"eslint": "^8.23.0", | ||
"rimraf": "^3.0.2" | ||
"@types/mocha": "^10.0.6", | ||
"@typescript-eslint/eslint-plugin": "^6.19.1", | ||
"@typescript-eslint/parser": "^6.19.1", | ||
"eslint": "^8.56.0", | ||
"rimraf": "^5.0.5" | ||
}, | ||
"scripts": { | ||
"prepublishOnly": "npm run clean && npm run compile-esm && npm run test && npm run remove-sourcemap-refs", | ||
"postpublish": "node ./build/post-publish.js", | ||
"prepack": "npm run clean && npm run compile-esm && npm run test && npm run remove-sourcemap-refs", | ||
"compile": "tsc -p ./src && npm run lint", | ||
@@ -37,6 +36,4 @@ "compile-esm": "tsc -p ./src/tsconfig.esm.json", | ||
"test": "npm run compile && mocha ./lib/umd/test", | ||
"lint": "eslint src/**/*.ts", | ||
"preversion": "npm test", | ||
"postversion": "git push && git push --tags" | ||
"lint": "eslint src/**/*.ts" | ||
} | ||
} |
@@ -6,3 +6,4 @@ # jsonc-parser | ||
[![NPM Downloads](https://img.shields.io/npm/dm/jsonc-parser.svg)](https://npmjs.org/package/jsonc-parser) | ||
[![Build Status](https://travis-ci.org/microsoft/node-jsonc-parser.svg?branch=main)](https://travis-ci.org/Microsoft/node-jsonc-parser) | ||
[![Build Status](https://github.com/microsoft/node-jsonc-parser/workflows/Tests/badge.svg)](https://github.com/microsoft/node-jsonc-parser/workflows/Tests) | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | ||
@@ -9,0 +10,0 @@ Why? |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
209943
19
4254
363
1