@prantlf/jsonlint 10.2.0 → 11.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +21 -19
- package/lib/cli.js +27 -25
- package/lib/formatter.js +12 -10
- package/lib/jsonlint.js +121 -121
- package/lib/printer.js +32 -32
- package/lib/schema-drafts.js +6 -3
- package/lib/sorter.js +6 -6
- package/lib/validator.js +31 -31
- package/package.json +31 -17
- package/web/ajv.min.js +3 -1
- package/web/ajv.min.js.map +1 -1
- package/web/formatter.min.js +1 -1
- package/web/formatter.min.js.map +1 -1
- package/web/jsonlint.html +333 -0
- package/web/jsonlint.min.js +1 -1
- package/web/jsonlint.min.js.map +1 -1
- package/web/printer.min.js +1 -1
- package/web/printer.min.js.map +1 -1
- package/web/schema-drafts.min.js +1 -1
- package/web/schema-drafts.min.js.map +1 -1
- package/web/sorter.min.js +1 -1
- package/web/sorter.min.js.map +1 -1
- package/web/validator.min.js +1 -1
- package/web/validator.min.js.map +1 -1
- package/CHANGELOG.md +0 -271
package/LICENSE
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
3
|
Copyright (c) 2012-2018 Zachary Carter
|
|
4
|
-
Copyright (c) 2019
|
|
4
|
+
Copyright (c) 2019-2022 Ferdinand Prantl
|
|
5
5
|
|
|
6
6
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
# JSON Lint
|
|
2
2
|
|
|
3
|
-
[](https://david-dm.org/prantlf/jsonlint#info=devDependencies)
|
|
8
|
-
[](https://standardjs.com)
|
|
3
|
+
[
|
|
4
|
+

|
|
5
|
+
](https://www.npmjs.com/package/@prantlf/jsonlint)
|
|
6
|
+
[](https://codecov.io/gh/prantlf/jsonlint)
|
|
9
7
|
|
|
10
|
-
A [JSON]/[JSON5] parser and
|
|
8
|
+
A [JSON]/CJSON/[JSON5] parser, validator and pretty-printer with a command-line client. A [pure JavaScript version] of the service provided at [jsonlint.com].
|
|
11
9
|
|
|
12
10
|
This is a fork of the original package with the following enhancements:
|
|
13
11
|
|
|
14
12
|
* Handles multiple files on the command line (by Greg Inman).
|
|
15
13
|
* Walks directories recursively (by Paul Vollmer).
|
|
16
14
|
* Provides 100% compatible interface to the native `JSON.parse` method.
|
|
17
|
-
* Optionally recognizes JavaScript-style comments and single quoted strings.
|
|
15
|
+
* Optionally recognizes JavaScript-style comments (CJSON) and single quoted strings (JSON5).
|
|
18
16
|
* Optionally ignores trailing commas and reports duplicate object keys as an error.
|
|
19
17
|
* Supports [JSON Schema] drafts 04, 06 and 07.
|
|
20
18
|
* Offers pretty-printing including comment-stripping and object keys without quotes (JSON5).
|
|
@@ -30,6 +28,7 @@ Integration to the favourite task loaders for JSON file validation is provided b
|
|
|
30
28
|
|
|
31
29
|
* [`Grunt`] - see [`@prantlf/grunt-jsonlint`]
|
|
32
30
|
* [`Gulp`] - see [`@prantlf/gulp-jsonlint`]
|
|
31
|
+
* [`Rollup`] - see [`rollup-plugin-jsonlint`]
|
|
33
32
|
|
|
34
33
|
## Synopsis
|
|
35
34
|
|
|
@@ -189,7 +188,7 @@ const validate = compile('string with JSON schema', {
|
|
|
189
188
|
|
|
190
189
|
### Pretty-Printing
|
|
191
190
|
|
|
192
|
-
You can parse a JSON string to an array of tokens and print it back to a string with some changes applied. It can be unification of whitespace or
|
|
191
|
+
You can parse a JSON string to an array of tokens and print it back to a string with some changes applied. It can be unification of whitespace, reformatting or stripping comments, for example. (Raw token values must be enabled when tokenizing the JSON input.)
|
|
193
192
|
|
|
194
193
|
```js
|
|
195
194
|
const { tokenize } = require('@prantlf/jsonlint')
|
|
@@ -206,7 +205,7 @@ The [`print`](#pretty-printing) method accepts an object `options` as the second
|
|
|
206
205
|
| --------------------------- | ------------------------------------------------------- |
|
|
207
206
|
| `indent` | count of spaces or the specific characters to be used as an indentation unit |
|
|
208
207
|
| `pruneComments` | will omit all tokens with comments |
|
|
209
|
-
| `stripObjectKeys`
|
|
208
|
+
| `stripObjectKeys` | will not print quotes around object keys which are JavaScript identifier names |
|
|
210
209
|
| `enforceDoubleQuotes` | will surround all strings with double quotes |
|
|
211
210
|
| `enforceSingleQuotes` | will surround all strings with single quotes |
|
|
212
211
|
| `trimTrailingCommas` | will omit all trailing commas after the last object entry or array item |
|
|
@@ -242,7 +241,10 @@ The method `tokenize` has the same prototype as the method [`parse`](#module-int
|
|
|
242
241
|
|
|
243
242
|
```js
|
|
244
243
|
const { tokenize } = require('@prantlf/jsonlint')
|
|
245
|
-
const tokens = tokenize('{"flag":true /* default */}', {
|
|
244
|
+
const tokens = tokenize('{"flag":true /* default */}', {
|
|
245
|
+
ignoreComments: true,
|
|
246
|
+
rawTokens: true
|
|
247
|
+
}))
|
|
246
248
|
// Returns the following array:
|
|
247
249
|
// [
|
|
248
250
|
// { type: 'symbol', raw: '{', value: '{' },
|
|
@@ -267,15 +269,13 @@ If you want to retain comments or whitespace for pretty-printing, for example, s
|
|
|
267
269
|
|
|
268
270
|
### Performance
|
|
269
271
|
|
|
270
|
-
This is a part of an output from the [parser benchmark], when parsing a 4.2 KB formatted string ([package.json](./package.json)) with Node.js
|
|
272
|
+
This is a part of an output from the [parser benchmark], when parsing a 4.2 KB formatted string ([package.json](./package.json)) with Node.js 12.14.0:
|
|
271
273
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
the tokenizable jju parser x 8,832 ops/sec ±0.92% (89 runs sampled)
|
|
276
|
-
the tokenizing jju parser x 7,911 ops/sec ±1.05% (86 runs sampled)
|
|
274
|
+
jsonlint using native JSON.parse x 97,109 ops/sec ±0.81% (93 runs sampled)
|
|
275
|
+
jsonlint using hand-coded parser x 7,256 ops/sec ±0.54% (90 runs sampled)
|
|
276
|
+
jsonlint using tokenising parser x 6,387 ops/sec ±0.44% (88 runs sampled)
|
|
277
277
|
|
|
278
|
-
A custom JSON parser is [a lot slower] than the built-in one. However, it is more important to have a [clear error reporting] than the highest speed in scenarios like parsing configuration files.
|
|
278
|
+
A custom JSON parser is [a lot slower] than the built-in one. However, it is more important to have a [clear error reporting] than the highest speed in scenarios like parsing configuration files. (For better error-reporting, the speed can be preserved by using the native parser initially and re-parsing with another parser only in case of failure.) Features like comments or JSON5 are also helpful in configuration files. Tokens preserve the complete input and can be used for pretty-printing without losing the comments.
|
|
279
279
|
|
|
280
280
|
### Error Handling
|
|
281
281
|
|
|
@@ -317,7 +317,7 @@ ${reason}`)
|
|
|
317
317
|
|
|
318
318
|
## License
|
|
319
319
|
|
|
320
|
-
Copyright (C) 2012-
|
|
320
|
+
Copyright (C) 2012-2022 Zachary Carter, Ferdinand Prantl
|
|
321
321
|
|
|
322
322
|
Licensed under the [MIT License].
|
|
323
323
|
|
|
@@ -330,8 +330,10 @@ Licensed under the [MIT License].
|
|
|
330
330
|
[UMD]: https://github.com/umdjs/umd
|
|
331
331
|
[`Grunt`]: https://gruntjs.com/
|
|
332
332
|
[`Gulp`]: http://gulpjs.com/
|
|
333
|
+
[`Rollup`]: https://rollupjs.org/
|
|
333
334
|
[`@prantlf/grunt-jsonlint`]: https://www.npmjs.com/package/@prantlf/grunt-jsonlint
|
|
334
335
|
[`@prantlf/gulp-jsonlint`]: https://www.npmjs.com/package/@prantlf/gulp-jsonlint
|
|
336
|
+
[`rollup-plugin-jsonlint`]: https://www.npmjs.com/package/rollup-plugin-jsonlint
|
|
335
337
|
[7x faster than the custom parser]: ./benchmarks/results/performance.md#results
|
|
336
338
|
[parser benchmark]: ./benchmarks#json-parser-comparison
|
|
337
339
|
[a lot slower]: ./benchmarks/results/performance.md#results
|
package/lib/cli.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
3
|
+
const fs = require('fs')
|
|
4
|
+
const path = require('path')
|
|
5
|
+
const parser = require('./jsonlint')
|
|
6
|
+
const formatter = require('./formatter')
|
|
7
|
+
const printer = require('./printer')
|
|
8
|
+
const sorter = require('./sorter')
|
|
9
|
+
const validator = require('./validator')
|
|
10
|
+
const pkg = require('../package')
|
|
11
11
|
|
|
12
12
|
function collectExtensions (extension) {
|
|
13
13
|
return extension.split(',')
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
const commander = require('commander')
|
|
17
17
|
.name('jsonlint')
|
|
18
18
|
.usage('[options] [<file or directory> ...]')
|
|
19
19
|
.description(pkg.description)
|
|
@@ -47,6 +47,8 @@ var options = require('commander')
|
|
|
47
47
|
})
|
|
48
48
|
.parse(process.argv)
|
|
49
49
|
|
|
50
|
+
const options = commander.opts()
|
|
51
|
+
|
|
50
52
|
function logNormalError (error, file) {
|
|
51
53
|
console.log('File:', file)
|
|
52
54
|
console.error(error.message)
|
|
@@ -58,7 +60,7 @@ function logCompactError (error, file) {
|
|
|
58
60
|
}
|
|
59
61
|
|
|
60
62
|
function parse (source, file) {
|
|
61
|
-
|
|
63
|
+
let parserOptions, parsed, formatted
|
|
62
64
|
try {
|
|
63
65
|
parserOptions = {
|
|
64
66
|
mode: options.mode,
|
|
@@ -68,13 +70,13 @@ function parse (source, file) {
|
|
|
68
70
|
allowDuplicateObjectKeys: options.duplicateKeys
|
|
69
71
|
}
|
|
70
72
|
if (options.validate) {
|
|
71
|
-
|
|
73
|
+
let validate
|
|
72
74
|
try {
|
|
73
|
-
|
|
75
|
+
const schema = fs.readFileSync(path.normalize(options.validate), 'utf8')
|
|
74
76
|
parserOptions.environment = options.environment
|
|
75
77
|
validate = validator.compile(schema, parserOptions)
|
|
76
78
|
} catch (error) {
|
|
77
|
-
|
|
79
|
+
const message = 'Loading the JSON schema failed: "' +
|
|
78
80
|
options.validate + '".\n' + error.message
|
|
79
81
|
throw new Error(message)
|
|
80
82
|
}
|
|
@@ -84,7 +86,7 @@ function parse (source, file) {
|
|
|
84
86
|
}
|
|
85
87
|
if (options.prettyPrint) {
|
|
86
88
|
parserOptions.rawTokens = true
|
|
87
|
-
|
|
89
|
+
const tokens = parser.tokenize(source, parserOptions)
|
|
88
90
|
// TODO: Support sorting tor the tokenized input too.
|
|
89
91
|
return printer.print(tokens, {
|
|
90
92
|
indent: options.indent,
|
|
@@ -132,7 +134,7 @@ function parse (source, file) {
|
|
|
132
134
|
|
|
133
135
|
function processFile (file) {
|
|
134
136
|
file = path.normalize(file)
|
|
135
|
-
|
|
137
|
+
const source = parse(fs.readFileSync(file, 'utf8'), file)
|
|
136
138
|
if (options.inPlace) {
|
|
137
139
|
fs.writeFileSync(file, source)
|
|
138
140
|
} else {
|
|
@@ -143,23 +145,23 @@ function processFile (file) {
|
|
|
143
145
|
}
|
|
144
146
|
|
|
145
147
|
function processSources (src, checkExtension) {
|
|
146
|
-
|
|
148
|
+
const extensions = options.extensions.map(function (extension) {
|
|
147
149
|
return '.' + extension
|
|
148
150
|
})
|
|
149
|
-
|
|
151
|
+
let srcStat
|
|
150
152
|
try {
|
|
151
153
|
srcStat = fs.statSync(src)
|
|
152
154
|
if (srcStat.isFile()) {
|
|
153
155
|
if (checkExtension) {
|
|
154
|
-
|
|
156
|
+
const ext = path.extname(src)
|
|
155
157
|
if (extensions.indexOf(ext) < 0) {
|
|
156
158
|
return
|
|
157
159
|
}
|
|
158
160
|
}
|
|
159
161
|
processFile(src)
|
|
160
162
|
} else if (srcStat.isDirectory()) {
|
|
161
|
-
|
|
162
|
-
for (
|
|
163
|
+
const sources = fs.readdirSync(src)
|
|
164
|
+
for (let i = 0; i < sources.length; i++) {
|
|
163
165
|
processSources(path.join(src, sources[i]), true)
|
|
164
166
|
}
|
|
165
167
|
}
|
|
@@ -169,20 +171,20 @@ function processSources (src, checkExtension) {
|
|
|
169
171
|
}
|
|
170
172
|
|
|
171
173
|
function main () {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
+
const files = commander.args
|
|
175
|
+
let source = ''
|
|
174
176
|
if (files.length) {
|
|
175
|
-
for (
|
|
177
|
+
for (let i = 0; i < files.length; i++) {
|
|
176
178
|
processSources(files[i], false)
|
|
177
179
|
}
|
|
178
180
|
} else {
|
|
179
|
-
|
|
181
|
+
const stdin = process.openStdin()
|
|
180
182
|
stdin.setEncoding('utf8')
|
|
181
183
|
stdin.on('data', function (chunk) {
|
|
182
184
|
source += chunk.toString('utf8')
|
|
183
185
|
})
|
|
184
186
|
stdin.on('end', function () {
|
|
185
|
-
|
|
187
|
+
const parsed = parse(source, '<stdin>')
|
|
186
188
|
if (!options.quiet) {
|
|
187
189
|
console.log(parsed)
|
|
188
190
|
}
|
package/lib/formatter.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
// eslint-disable-next-line no-unused-expressions
|
|
2
|
+
// eslint-disable-next-line no-unused-expressions, multiline-ternary
|
|
3
3
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports)
|
|
4
|
-
// eslint-disable-next-line no-undef
|
|
4
|
+
// eslint-disable-next-line no-undef, multiline-ternary
|
|
5
5
|
: typeof define === 'function' && define.amd ? define('jsonlint-formatter', ['exports'], factory)
|
|
6
6
|
// eslint-disable-next-line no-undef
|
|
7
7
|
: (global = global || self, factory(global.jsonlintFormatter = {}))
|
|
@@ -21,15 +21,17 @@
|
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
function format (json, indent) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
let i = 0
|
|
25
|
+
let length = 0
|
|
26
|
+
const indentString = indent !== undefined
|
|
27
27
|
? typeof indent === 'number'
|
|
28
|
-
? new Array(indent + 1).join(' ')
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
? new Array(indent + 1).join(' ')
|
|
29
|
+
: indent
|
|
30
|
+
: ' '
|
|
31
|
+
let outputString = ''
|
|
32
|
+
let indentLevel = 0
|
|
33
|
+
let inString
|
|
34
|
+
let currentChar
|
|
33
35
|
|
|
34
36
|
for (i = 0, length = json.length; i < length; i += 1) {
|
|
35
37
|
currentChar = json.charAt(i)
|