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"
51 changes: 48 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,45 @@ 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)

#### Schema Validation

You can validate JSON files using JSON Schema drafts 04, 06 or 07, if you specify the schema in addition to other options:

jsonlint({
schema: {
src: 'some/manifest-schema.json',
environment: 'json-schema-draft-04'
}
})

* `schema`, when set the source file will be validated using ae JSON Schema in addition to the syntax checks
* `src`, when filled with a file path, the file will be used as a source of the JSON Schema
* `environment`, can specify the version of the JSON Schema draft to use for validation: "json-schema-draft-04", "json-schema-draft-06" or "json-schema-draft-07" (if not set, the schema draft version will be inferred automatically)

### jsonlint.reporter(customReporter)

Expand Down Expand Up @@ -75,7 +113,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 +123,10 @@ 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
[JSON Schema]: https://json-schema.org
116 changes: 98 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,118 @@
'use strict'

var fs = require('fs')
var mapStream = require('map-stream')
var colors = require('ansi-colors')
var jsonlint = require('jsonlint')
var jsonlint = require('@prantlf/jsonlint')
var validator = require('@prantlf/jsonlint/lib/validator')
var sorter = require('@prantlf/jsonlint/lib/sorter')
var through = require('through2')
var PluginError = require('plugin-error')
var log = require('fancy-log')

var formatOutput = function(msg) {
var output = {}

if (msg) {
output.message = msg
var jsonLintPlugin = function(options) {
options = Object.assign(
{
mode: 'json',
ignoreComments: false,
ignoreTrailingCommas: false,
allowSingleQuotedStrings: false,
allowDuplicateObjectKeys: true,
schema: {},
format: false,
indent: 2,
sortKeys: false
},
options
)
var schema = options.schema
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,
environment: schema.environment,
limitedErrorInfo: !(
options.ignoreComments ||
options.cjson ||
options.allowSingleQuotedStrings
)
}
var schemaContent

function createResult(message) {
var result = {}
if (message) {
result.message = message
result.success = false
} else {
result.success = true
}
return result
}

output.success = msg ? false : true
function formatOutput(parsedData, file) {
if (options.format) {
if (options.sortKeys) {
parsedData = sorter.sortObject(parsedData)
}
var formatted = JSON.stringify(parsedData, null, options.indent) + '\n'
file.contents = new Buffer(formatted)
}
}

return output
}
function validateSchema(parsedData, file, finish) {
var errorMessage
try {
var validate = validator.compile(schemaContent, parserOptions)
validate(parsedData)
formatOutput(parsedData, file)
} catch (error) {
errorMessage = error.message
}
finish(errorMessage)
}

var jsonLintPlugin = function(options) {
options = options || {}
function loadAndValidateSchema(parsedData, file, finish) {
if (schemaContent) {
validateSchema(parsedData, finish)
} else {
fs.readFile(schema.src, 'utf-8', function(error, fileContent) {
if (error) {
finish(error.message)
} else {
schemaContent = fileContent
validateSchema(parsedData, file, finish)
}
})
}
}

return mapStream(function(file, cb) {
var errorMessage = ''
var errorMessage
function finish(errorMessage) {
file.jsonlint = createResult(errorMessage)
cb(null, file)
}

try {
jsonlint.parse(String(file.contents))
} catch (err) {
errorMessage = err.message
var parsedData = jsonlint.parse(String(file.contents), parserOptions)
if (schema.src) {
loadAndValidateSchema(parsedData, file, finish)
return
}
formatOutput(parsedData, file)
} catch (error) {
errorMessage = error.message
}
file.jsonlint = formatOutput(errorMessage)

cb(null, file)
finish(errorMessage)
})
}

Expand Down
Loading