@prantlf/jsonlint 11.2.0 → 11.3.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/README.md +127 -59
- package/lib/cli.js +56 -3
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -17,10 +17,11 @@ This is a fork of the original project ([zaach/jsonlint](https://github.com/zaac
|
|
|
17
17
|
* Supports [JSON Schema] drafts 04, 06 and 07.
|
|
18
18
|
* Offers pretty-printing including comment-stripping and object keys without quotes (JSON5).
|
|
19
19
|
* Prefers the native JSON parser if possible to run [7x faster than the custom parser].
|
|
20
|
-
* Reports errors with rich additional information. From the
|
|
21
|
-
*
|
|
20
|
+
* Reports errors with rich additional information. From the JSON Schema validation too.
|
|
21
|
+
* Consumes configuration from both command line and [configuration files](configuration).
|
|
22
|
+
* Implements JavaScript modules using [UMD] to work in Node.js, in a browser, everywhere.
|
|
22
23
|
* Depends on up-to-date npm modules with no installation warnings.
|
|
23
|
-
* Small size - 18.
|
|
24
|
+
* Small size - 18.7 kB minified, 6.54 kB gzipped, 5.16 kB brotlied.
|
|
24
25
|
|
|
25
26
|
**Note:** In comparison with the original project, this package exports only the `parse` method; not the `Parser` object.
|
|
26
27
|
|
|
@@ -52,9 +53,11 @@ Example of an error message:
|
|
|
52
53
|
|
|
53
54
|
## Command-line Interface
|
|
54
55
|
|
|
55
|
-
Install `jsonlint` with `npm
|
|
56
|
+
Install `jsonlint` with `npm`, `pnpm` or `yarn` globally to be able to use the command-line interface in any directory:
|
|
56
57
|
|
|
57
|
-
npm i @prantlf/jsonlint
|
|
58
|
+
npm i -g @prantlf/jsonlint
|
|
59
|
+
pnpm i -g @prantlf/jsonlint
|
|
60
|
+
yarn add --global @prantlf/jsonlint
|
|
58
61
|
|
|
59
62
|
Validate a single file:
|
|
60
63
|
|
|
@@ -64,64 +67,129 @@ or pipe the JSON input into `stdin`:
|
|
|
64
67
|
|
|
65
68
|
cat myfile.json | jsonlint
|
|
66
69
|
|
|
67
|
-
or process all `.json` files in a directory:
|
|
70
|
+
or process all `.json` files in a directory and rewriting them with the pretty-printed output:
|
|
68
71
|
|
|
69
|
-
jsonlint mydir
|
|
72
|
+
jsonlint --in-place --pretty-print mydir
|
|
70
73
|
|
|
71
74
|
By default, `jsonlint` will either report a syntax error with details or pretty-print the source if it is valid.
|
|
72
75
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
76
|
+
A more complex example: check all JSON files in a Node.js project, except for dependencies in `node_modules`, allow comments (CJSON) and trailing commas, forbid duplicated object keys, print processed files names on the console, print errors on a single line and if an error occurs, continue with other files:
|
|
77
|
+
|
|
78
|
+
jsonlint --comments --trailing-commas --no-duplicate-keys \
|
|
79
|
+
--log-files --compact --continue '**/*.json' '!**/node_modules'
|
|
80
|
+
|
|
81
|
+
The same parameters can be passed from a configuration file:
|
|
82
|
+
|
|
83
|
+
```json
|
|
84
|
+
{
|
|
85
|
+
"comments": true,
|
|
86
|
+
"trailing-commas": true,
|
|
87
|
+
"duplicate-keys": false,
|
|
88
|
+
"log-files": true,
|
|
89
|
+
"compact": true,
|
|
90
|
+
"continue": true,
|
|
91
|
+
"patterns": ["**/*.json", "!**/node_modules"]
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Usage
|
|
96
|
+
|
|
97
|
+
Usage: `jsonlint [options] [<file, directory, pattern> ...]`
|
|
98
|
+
|
|
99
|
+
#### Options
|
|
100
|
+
|
|
101
|
+
-f, --config [file] read options from a custom configuration file
|
|
102
|
+
-F, --no-config disable searching for configuration file
|
|
103
|
+
-s, --sort-keys sort object keys (not when prettifying)
|
|
104
|
+
-E, --extensions [ext] file extensions to process for directory walk
|
|
105
|
+
(default: ["json","JSON"])
|
|
106
|
+
-i, --in-place overwrite the input files
|
|
107
|
+
-t, --indent [num|char] number of spaces or specific characters
|
|
108
|
+
to use for indentation (default: 2)
|
|
109
|
+
-c, --compact compact error display
|
|
110
|
+
-M, --mode [mode] set other parsing flags according to a format
|
|
111
|
+
type (default: "json")
|
|
112
|
+
-C, --comments recognize and ignore JavaScript-style comments
|
|
113
|
+
-S, --single-quoted-strings support single quotes as string delimiters
|
|
114
|
+
-T, --trailing-commas ignore trailing commas in objects and arrays
|
|
115
|
+
-D, --no-duplicate-keys report duplicate object keys as an error
|
|
116
|
+
-V, --validate [file] JSON schema file to use for validation
|
|
117
|
+
-e, --environment [env] which specification of JSON Schema the
|
|
118
|
+
validation file uses
|
|
119
|
+
-l, --log-files print only the parsed file names to stdout
|
|
120
|
+
-q, --quiet do not print the parsed json to stdout
|
|
121
|
+
-n, --continue continue with other files if an error occurs
|
|
122
|
+
-p, --pretty-print prettify the input instead of stringifying
|
|
123
|
+
the parsed object
|
|
124
|
+
-P, --pretty-print-invalid force pretty-printing even for invalid input
|
|
125
|
+
-r, --trailing-newline ensure a line break at the end of the output
|
|
126
|
+
-R, --no-trailing-newline ensure no line break at the end of the output
|
|
127
|
+
--prune-comments omit comments from the prettified output
|
|
128
|
+
--strip-object-keys strip quotes from object keys if possible
|
|
129
|
+
(JSON5)
|
|
130
|
+
--enforce-double-quotes surrounds all strings with double quotes
|
|
131
|
+
--enforce-single-quotes surrounds all strings with single quotes
|
|
132
|
+
(JSON5)
|
|
133
|
+
--trim-trailing-commas omit trailing commas from objects and arrays
|
|
134
|
+
(JSON5)
|
|
135
|
+
-v, --version output the version number
|
|
136
|
+
-h, --help output usage information
|
|
137
|
+
|
|
138
|
+
You can use BASH patterns for including and excluding files (only files).
|
|
139
|
+
Patterns are case-sensitive and have to use slashes as a path separators.
|
|
140
|
+
A pattern to exclude from processing starts with "!".
|
|
141
|
+
|
|
142
|
+
Parsing mode can be "cjson" or "json5" to enable other flags automatically.
|
|
143
|
+
If no files or directories are specified, stdin will be parsed. Environments
|
|
144
|
+
for JSON schema validation are "json-schema-draft-04", "json-schema-draft-06"
|
|
145
|
+
or "json-schema-draft-07". If not specified, it will be auto-detected.
|
|
146
|
+
|
|
147
|
+
### Configuration
|
|
148
|
+
|
|
149
|
+
In addition to the command line parameters, the options can be supplied from the following files:
|
|
150
|
+
|
|
151
|
+
package.json, key jsonlint
|
|
152
|
+
.jsonlintrc
|
|
153
|
+
.jsonlintrc.json
|
|
154
|
+
.jsonlintrc.yaml
|
|
155
|
+
.jsonlintrc.yml
|
|
156
|
+
.jsonlintrc.js
|
|
157
|
+
.jsonlintrc.cjs
|
|
158
|
+
jsonlint.config.js
|
|
159
|
+
jsonlint.config.cjs
|
|
160
|
+
|
|
161
|
+
The automatic search for one of the following locations above can be disabled by the command-line parameter `-F|--no-config`. A concrete configuration file can be specified by the command-line parameter `-f|--config [file]`. Parameters from the command line will have higher priority than parameters from a configuration file.
|
|
162
|
+
|
|
163
|
+
The configuration is an object with the following properties, described above, which can be entered either in the kebab-case or in the camel-case:
|
|
164
|
+
|
|
165
|
+
| Parameter | Alias |
|
|
166
|
+
| --------- | ----- |
|
|
167
|
+
| patterns | |
|
|
168
|
+
| sort-keys | sortKeys |
|
|
169
|
+
| extensions | |
|
|
170
|
+
| in-place | inPlace |
|
|
171
|
+
| indent | |
|
|
172
|
+
| compact | |
|
|
173
|
+
| mode | |
|
|
174
|
+
| comments | |
|
|
175
|
+
| single-quoted-strings | singleQuotedStrings |
|
|
176
|
+
| trailing-commas | trailingCommas |
|
|
177
|
+
| duplicate-keys | duplicateKeys |
|
|
178
|
+
| validate | |
|
|
179
|
+
| environment | |
|
|
180
|
+
| log-files | logFiles |
|
|
181
|
+
| quiet | |
|
|
182
|
+
| continue | |
|
|
183
|
+
| pretty-print | prettyPrint |
|
|
184
|
+
| pretty-print-invalid | prettyPrintInvalid |
|
|
185
|
+
| trailing-newline | trailingNewline'
|
|
186
|
+
| prune-comments | pruneComments |
|
|
187
|
+
| strip-object-keys | stripObjectKeys |
|
|
188
|
+
| enforce-double-quotes | enforceDoubleQuotes |
|
|
189
|
+
| enforce-single-quotes | enforceSingleQuotes |
|
|
190
|
+
| trim-trailing-commas | trimTrailingCommas |
|
|
191
|
+
|
|
192
|
+
The parameter `config` will be ignored in configuration files. The extra parameter `patterns` can be set to an array of strings with paths or patterns instead of putting them to the command line.
|
|
125
193
|
|
|
126
194
|
## Module Interface
|
|
127
195
|
|
package/lib/cli.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const { readdirSync, readFileSync, statSync, writeFileSync } = require('fs')
|
|
4
4
|
const { extname, join, normalize } = require('path')
|
|
5
5
|
const { isDynamicPattern, sync } = require('fast-glob')
|
|
6
|
+
const { cosmiconfigSync } = require('cosmiconfig')
|
|
6
7
|
const { parse, tokenize } = require('./jsonlint')
|
|
7
8
|
const { format } = require('./formatter')
|
|
8
9
|
const { print } = require('./printer')
|
|
@@ -16,6 +17,8 @@ const commander = require('commander')
|
|
|
16
17
|
.name('jsonlint')
|
|
17
18
|
.usage('[options] [<file, directory, pattern> ...]')
|
|
18
19
|
.description(description)
|
|
20
|
+
.option('-f, --config [file]', 'read options from a custom configuration file')
|
|
21
|
+
.option('-F, --no-config', 'disable searching for configuration files')
|
|
19
22
|
.option('-s, --sort-keys', 'sort object keys (not when prettifying)')
|
|
20
23
|
.option('-E, --extensions [ext]', 'file extensions to process for directory walk', collectValues, ['json', 'JSON'])
|
|
21
24
|
.option('-i, --in-place', 'overwrite the input files')
|
|
@@ -54,9 +57,56 @@ const commander = require('commander')
|
|
|
54
57
|
})
|
|
55
58
|
.parse(process.argv)
|
|
56
59
|
|
|
57
|
-
const
|
|
60
|
+
const paramNames = {
|
|
61
|
+
'trailing-commas': 'trailingCommas',
|
|
62
|
+
'single-quoted-strings': 'singleQuotedStrings',
|
|
63
|
+
'duplicate-keys': 'duplicateKeys',
|
|
64
|
+
'pretty-print': 'prettyPrint',
|
|
65
|
+
'prune-comments': 'pruneComments',
|
|
66
|
+
'strip-object-keys': 'stripObjectKeys',
|
|
67
|
+
'enforce-double-quotes': 'enforceDoubleQuotes',
|
|
68
|
+
'enforce-single-quotes': 'enforceSingleQuotes',
|
|
69
|
+
'trim-trailing-commas': 'trimTrailingCommas',
|
|
70
|
+
'sort-keys': 'sortKeys',
|
|
71
|
+
'pretty-print-invalid': 'prettyPrintInvalid',
|
|
72
|
+
'log-files': 'logFiles',
|
|
73
|
+
'in-place': 'inPlace',
|
|
74
|
+
'trailing-newline': 'trailingNewline'
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const params = commander.opts()
|
|
78
|
+
let options
|
|
79
|
+
if (params.config === false) {
|
|
80
|
+
options = params
|
|
81
|
+
} else {
|
|
82
|
+
const configurator = cosmiconfigSync('jsonlint')
|
|
83
|
+
const { config = {} } = (params.config && configurator.load(params.config)) ||
|
|
84
|
+
configurator.search() || {}
|
|
85
|
+
options = mergeOptions({}, convertConfig(config), params)
|
|
86
|
+
}
|
|
87
|
+
|
|
58
88
|
const extensions = options.extensions.map(extension => '.' + extension)
|
|
59
89
|
|
|
90
|
+
function convertConfig (config) {
|
|
91
|
+
const result = {}
|
|
92
|
+
for (const key in config) {
|
|
93
|
+
const name = paramNames[key] || key
|
|
94
|
+
result[name] = config[key]
|
|
95
|
+
}
|
|
96
|
+
return result
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function mergeOptions (target, ...sources) {
|
|
100
|
+
for (const source of sources) {
|
|
101
|
+
for (const key in source) {
|
|
102
|
+
if (target[key] == null) {
|
|
103
|
+
target[key] = source[key]
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return target
|
|
108
|
+
}
|
|
109
|
+
|
|
60
110
|
function logNormalError (error, file) {
|
|
61
111
|
if (process.exitCode > 0) {
|
|
62
112
|
console.log()
|
|
@@ -155,7 +205,7 @@ function processFile (file) {
|
|
|
155
205
|
const original = readFileSync(file, 'utf8')
|
|
156
206
|
let source = processContents(original, file)
|
|
157
207
|
if (options.inPlace) {
|
|
158
|
-
const lines = original.split(
|
|
208
|
+
const lines = original.split(/\r?\n/)
|
|
159
209
|
const newLine = !lines[lines.length - 1]
|
|
160
210
|
if (options.trailingNewline === true ||
|
|
161
211
|
(options.trailingNewline !== false && newLine)) {
|
|
@@ -207,7 +257,10 @@ function processPatterns (patterns) {
|
|
|
207
257
|
}
|
|
208
258
|
|
|
209
259
|
function main () {
|
|
210
|
-
|
|
260
|
+
let { args: files } = commander
|
|
261
|
+
if (!files.length) {
|
|
262
|
+
files = options.patterns || []
|
|
263
|
+
}
|
|
211
264
|
if (files.length) {
|
|
212
265
|
const dynamic = files.some(file => isDynamicPattern(file))
|
|
213
266
|
if (dynamic) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prantlf/jsonlint",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.3.0",
|
|
4
4
|
"description": "JSON/CJSON/JSON5 parser, syntax and schema validator and pretty-printer.",
|
|
5
5
|
"author": "Ferdinand Prantl <prantlf@gmail.com> (http://prantl.tk)",
|
|
6
6
|
"contributors": [
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"build": "npm run compile && npm run compile:tests",
|
|
46
46
|
"compile": "node scripts/bundle-jsonlint && terser -o web/jsonlint.min.js --source-map \"filename='jsonlint.js',url='jsonlint.min.js.map',includeSources=true\" lib/jsonlint.js && terser -o web/validator.min.js --source-map \"filename='validator.js',url='validator.min.js.map',includeSources=true\" lib/validator.js && terser -o web/formatter.min.js --source-map \"filename='formatter.js',url='formatter.min.js.map',includeSources=true\" lib/formatter.js && terser -o web/sorter.min.js --source-map \"filename='sorter.js',url='sorter.min.js.map',includeSources=true\" lib/sorter.js && terser -o web/printer.min.js --source-map \"filename='printer.js',url='printer.min.js.map',includeSources=true\" lib/printer.js && node scripts/bundle-schema-drafts && terser -o web/schema-drafts.min.js --source-map \"filename='schema-drafts.js',url='schema-drafts.min.js.map',includeSources=true\" lib/schema-drafts.js && terser -o web/ajv.min.js --source-map \"filename='ajv.js',url='ajv.min.js.map',includeSources=true\" node_modules/ajv/dist/ajv.bundle.js",
|
|
47
47
|
"compile:tests": "tsc --lib es6 test/typings.test.ts",
|
|
48
|
-
"test": "nyc --silent node test/typings.test.js && nyc --silent --no-clean node test/parse1 && nyc --silent --no-clean node test/parse1 --native-parser && nyc --silent --no-clean node test/parse2 && nyc --silent --no-clean node test/parse3 && nyc --silent --no-clean node test/parse4 && nyc --silent --no-clean node test/parse5 && nyc --silent --no-clean node test/portable && nyc --silent --no-clean node test/tokenize && nyc --silent --no-clean node test/print && nyc --silent --no-clean node lib/cli package.json test/recursive && nyc --silent --no-clean node lib/cli -sq test/passes/hasOwnProperty.json && nyc --silent --no-clean node lib/cli -s -e json-schema-draft-04 -V test/passes/3.schema.json test/passes/3.json && nyc --silent --no-clean node lib/cli -C test/passes/comments.txt && nyc --silent --no-clean node lib/cli -pS test/passes/strings.txt && nyc --silent --no-clean node lib/cli -M json5 test/passes/json5.text && nyc --silent --no-clean node lib/cli -v && nyc --silent --no-clean node lib/cli -h && nyc --silent --no-clean node lib/cli -Pc test/fails/10.json || nyc report",
|
|
48
|
+
"test": "nyc --silent node test/typings.test.js && nyc --silent --no-clean node test/parse1 && nyc --silent --no-clean node test/parse1 --native-parser && nyc --silent --no-clean node test/parse2 && nyc --silent --no-clean node test/parse3 && nyc --silent --no-clean node test/parse4 && nyc --silent --no-clean node test/parse5 && nyc --silent --no-clean node test/portable && nyc --silent --no-clean node test/tokenize && nyc --silent --no-clean node test/print && nyc --silent --no-clean node lib/cli package.json test/recursive && nyc --silent --no-clean node lib/cli -sq test/passes/hasOwnProperty.json && nyc --silent --no-clean node lib/cli -s -e json-schema-draft-04 -V test/passes/3.schema.json test/passes/3.json && nyc --silent --no-clean node lib/cli -C test/passes/comments.txt && nyc --silent --no-clean node lib/cli -pS test/passes/strings.txt && nyc --silent --no-clean node lib/cli -M json5 test/passes/json5.text && nyc --silent --no-clean node lib/cli -v && nyc --silent --no-clean node lib/cli -h && nyc --silent --no-clean node lib/cli -Pc test/fails/10.json || nyc --silent --no-clean node lib/cli -f test/.jsonrc.yml 'test/**/*.json' '!**/fails' && nyc report",
|
|
49
49
|
"start": "http-server -c 5",
|
|
50
50
|
"web": "npm run web:sync && npm run web:deploy",
|
|
51
51
|
"web:clone": "test ! -d ../jsonlint-pages && git clone --single-branch --branch gh-pages `git remote get-url origin` ../jsonlint-pages",
|
|
@@ -77,6 +77,7 @@
|
|
|
77
77
|
"dependencies": {
|
|
78
78
|
"ajv": "6.12.6",
|
|
79
79
|
"commander": "9.2.0",
|
|
80
|
+
"cosmiconfig": "^7.0.1",
|
|
80
81
|
"fast-glob": "3.2.11"
|
|
81
82
|
},
|
|
82
83
|
"devDependencies": {
|