Skip to content

Add jsonlint parsing options to the gulp plugin options #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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"
26 changes: 23 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,21 @@ 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({
mode: 'json',
ignoreComments: false,
ignoreTrailingCommas: false,
allowSingleQuotedStrings: false,
allowDuplicateObjectKeys: true,
})

* `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

### jsonlint.reporter(customReporter)

Expand Down Expand Up @@ -75,7 +89,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 +99,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
33 changes: 30 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

var jsonLintPlugin = function(options) {
options = options || {}
options = Object.assign(
{
mode: 'json',
ignoreComments: false,
ignoreTrailingCommas: false,
allowSingleQuotedStrings: false,
allowDuplicateObjectKeys: true
},
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))
jsonlint.parse(String(file.contents), parserOptions)
} catch (err) {
errorMessage = err.message
}
Expand Down
Loading