Skip to content

Commit 50e06aa

Browse files
committed
feat: Add jsonlint options for formatting and object key sorting
1 parent 1505d14 commit 50e06aa

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This is a fork of the original package with the following enhancements:
1313
* Optionally recognizes JavaScript-style comments and single quoted strings.
1414
* Optionally ignores trailing commas and reports duplicate object keys as an error.
1515
* Prefers using the 8x faster native JSON parser, if possible.
16+
* Optionally reformats the output JSON and sorts object keys alphabetically.
1617
* Depends on up-to-date npm modules with no installation warnings.
1718

1819
## Usage
@@ -55,11 +56,16 @@ gulp.src('./src/*.json')
5556
Options can be passed as keys in an object to the `jsonlint` function. The following are their defaults:
5657

5758
jsonlint({
59+
// parsing
5860
mode: 'json',
5961
ignoreComments: false,
6062
ignoreTrailingCommas: false,
6163
allowSingleQuotedStrings: false,
6264
allowDuplicateObjectKeys: true,
65+
// formatting
66+
format: false,
67+
indent: 2,
68+
sortKeys: false
6369
})
6470

6571
* `mode`, when set to "cjson" or "json5", enables some other flags automatically
@@ -68,6 +74,10 @@ Options can be passed as keys in an object to the `jsonlint` function. The follo
6874
* `allowSingleQuotedStrings`, when `true` single quotes will be accepted as alternative delimiters for strings
6975
* `allowDuplicateObjectKeys`, when `false` duplicate keys in objects will be reported as an error
7076

77+
* `format`, when `true` `JSON.stringify` will be used to format the JavaScript (if it is valid)
78+
* `indent`, the value passed to `JSON.stringify`, it can be the number of spaces, or string like "\t"
79+
* `sortKeys`, when `true` keys of objects in the output JSON will be sorted alphabetically (`format` has to be set to `true` too)
80+
7181
### jsonlint.reporter(customReporter)
7282

7383
#### customReporter(file)

index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var mapStream = require('map-stream');
44
var colors = require('ansi-colors');
55
var jsonlint = require('@prantlf/jsonlint');
6+
var sorter = require('@prantlf/jsonlint/lib/sorter');
67
var through = require('through2');
78
var PluginError = require('plugin-error');
89
var log = require('fancy-log');
@@ -23,7 +24,10 @@ var jsonLintPlugin = function (options) {
2324
ignoreComments: false,
2425
ignoreTrailingCommas: false,
2526
allowSingleQuotedStrings: false,
26-
allowDuplicateObjectKeys: true
27+
allowDuplicateObjectKeys: true,
28+
format: false,
29+
indent: 2,
30+
sortKeys: false
2731
}, options);
2832

2933
return mapStream(function (file, cb) {
@@ -35,11 +39,17 @@ var jsonLintPlugin = function (options) {
3539
options.mode === 'cjson' || options.mode === 'json5',
3640
ignoreTrailingCommas: options.ignoreTrailingCommas || options.mode === 'json5',
3741
allowSingleQuotedStrings: options.allowSingleQuotedStrings || options.mode === 'json5',
38-
allowDuplicateObjectKeys: options.allowDuplicateObjectKeys,
39-
limitedErrorInfo: !(options.ignoreComments || options.cjson || options.allowSingleQuotedStrings)
42+
allowDuplicateObjectKeys: options.allowDuplicateObjectKeys
4043
};
4144
try {
42-
jsonlint.parse(String(file.contents), parserOptions);
45+
var parsedData = jsonlint.parse(String(file.contents), parserOptions);
46+
if (options.format) {
47+
if (options.sortKeys) {
48+
parsedData = sorter.sortObject(parsedData);
49+
}
50+
var formatted = JSON.stringify(parsedData, null, options.indent) + '\n';
51+
file.contents = new Buffer(formatted);
52+
}
4353
}
4454
catch (err) {
4555
errorMessage = err.message;

test/fixtures/comments.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* Commented configuration */
22
{
3+
"key2": 2,
34
// String parameter
4-
"key": "value"
5+
"key1": 1
56
}

test/main.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ function parseInvalidFile (filePath, done, options) {
4646
stream.end();
4747
}
4848

49+
function formatValidFile (filePath, done, options, newContent) {
50+
var file = getFile(filePath);
51+
var stream = jsonLintPlugin(options);
52+
53+
stream.on('data', function (f) {
54+
should.exist(f);
55+
should.exist(f.contents);
56+
String(f.contents).should.equal(newContent + '\n');
57+
});
58+
59+
stream.once('end', done);
60+
stream.write(file);
61+
stream.end();
62+
}
63+
4964
describe('gulp-jsonlint', function () {
5065
it('should pass file through', function (done) {
5166
var cbCounter = 0;
@@ -172,4 +187,20 @@ describe('gulp-jsonlint', function () {
172187
mode: 'json5'
173188
});
174189
});
190+
191+
it('can format the output', function (done) {
192+
formatValidFile('fixtures/json5.json', done, {
193+
mode: 'json5',
194+
format: true
195+
}, '{\n "key": "value"\n}');
196+
});
197+
198+
it('can sort object keys in the output', function (done) {
199+
formatValidFile('fixtures/comments.json', done, {
200+
ignoreComments: true,
201+
format: true,
202+
indent: 0,
203+
sortKeys: true
204+
}, '{"key1":1,"key2":2}');
205+
});
175206
});

0 commit comments

Comments
 (0)