Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions .jshintrc

This file was deleted.

8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.editorconfig
.eslintrc
.gitignore
.npmignore
.travis.yml
node_modules/
npm-debug.log
test/
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sudo: false
language: node_js
node_js:
- stable
- lts/*
- 6
- "12"
- "10"
- "8"
- "6"
35 changes: 32 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# gulp-jsonlint [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url]

> jsonlint plugin for [gulp](http://gulpjs.com/)
> [JSON]/[JSON5] file syntax validation plugin for [`gulp`] using [`jsonlint`]

## Usage

Expand Down Expand Up @@ -39,7 +39,30 @@ gulp.src('./src/*.json')

### jsonlint(options)

For now, `options` are not supported yet.
Options can be passed as keys in an object to the `jsonlint` function. The following are their defaults:

jsonlint({
// parsing
mode: 'json',
ignoreComments: false,
ignoreTrailingCommas: false,
allowSingleQuotedStrings: false,
allowDuplicateObjectKeys: true,
// formatting
format: false,
indent: 2,
sortKeys: false
})

* `mode`, when set to "cjson" or "json5", enables some other flags automatically
* `ignoreComments`, when `true` JavaScript-style single-line and multiple-line comments will be recognised and ignored
* `ignoreTrailingCommas`, when `true` trailing commas in objects and arrays will be ignored
* `allowSingleQuotedStrings`, when `true` single quotes will be accepted as alternative delimiters for strings
* `allowDuplicateObjectKeys`, when `false` duplicate keys in objects will be reported as an error

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

### jsonlint.reporter(customReporter)

Expand Down Expand Up @@ -75,7 +98,7 @@ Stop a task/stream if an jsonlint error has been reported for any file, but wait

## License

[MIT License](http://en.wikipedia.org/wiki/MIT_License)
[MIT License]

[npm-url]: https://npmjs.org/package/gulp-jsonlint
[npm-image]: https://badge.fury.io/js/gulp-jsonlint.svg
Expand All @@ -85,3 +108,9 @@ Stop a task/stream if an jsonlint error has been reported for any file, but wait

[depstat-url]: https://david-dm.org/rogeriopvl/gulp-jsonlint
[depstat-image]: https://david-dm.org/rogeriopvl/gulp-jsonlint.svg

[MIT License]: http://en.wikipedia.org/wiki/MIT_License
[`gulp`]: http://gulpjs.com/
[`jsonlint`]: https://prantlf.github.io/jsonlint/
[JSON]: https://tools.ietf.org/html/rfc8259
[JSON5]: https://spec.json5.org
44 changes: 41 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

var mapStream = require('map-stream')
var colors = require('ansi-colors')
var jsonlint = require('jsonlint')
var jsonlint = require('@prantlf/jsonlint')
var sorter = require('@prantlf/jsonlint/lib/sorter')
var through = require('through2')
var PluginError = require('plugin-error')
var log = require('fancy-log')
Expand All @@ -20,13 +21,50 @@ var formatOutput = function(msg) {
}

var jsonLintPlugin = function(options) {
options = options || {}
options = Object.assign(
{
mode: 'json',
ignoreComments: false,
ignoreTrailingCommas: false,
allowSingleQuotedStrings: false,
allowDuplicateObjectKeys: true,
format: false,
indent: 2,
sortKeys: false
},
options
)

return mapStream(function(file, cb) {
var errorMessage = ''

var parserOptions = {
mode: options.mode,
ignoreComments:
options.ignoreComments ||
options.cjson ||
options.mode === 'cjson' ||
options.mode === 'json5',
ignoreTrailingCommas:
options.ignoreTrailingCommas || options.mode === 'json5',
allowSingleQuotedStrings:
options.allowSingleQuotedStrings || options.mode === 'json5',
allowDuplicateObjectKeys: options.allowDuplicateObjectKeys,
limitedErrorInfo: !(
options.ignoreComments ||
options.cjson ||
options.allowSingleQuotedStrings
)
}
try {
jsonlint.parse(String(file.contents))
var parsedData = jsonlint.parse(String(file.contents), parserOptions)
if (options.format) {
if (options.sortKeys) {
parsedData = sorter.sortObject(parsedData)
}
var formatted = JSON.stringify(parsedData, null, options.indent) + '\n'
file.contents = new Buffer(formatted)
}
} catch (err) {
errorMessage = err.message
}
Expand Down
Loading