Skip to content

Commit 9b22843

Browse files
committed
feat: Allow setting the line count as diff context
1 parent 0423a4b commit 9b22843

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ Usage: `jsonlint [options] [<file, directory, pattern> ...]`
136136
-V, --validate [file] JSON schema file to use for validation
137137
-e, --environment [env] which specification of JSON Schema the
138138
validation file uses
139+
-x, --context [num] line count used as the diff context (default: 3)
139140
-l, --log-files print only the parsed file names to stdout
140141
-q, --quiet do not print the parsed json to stdout
141142
-n, --continue continue with other files if an error occurs

lib/cli.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const commander = require('commander')
3232
.option('-D, --no-duplicate-keys', 'report duplicate object keys as an error')
3333
.option('-V, --validate [file]', 'JSON schema file to use for validation')
3434
.option('-e, --environment [env]', 'which specification of JSON Schema the validation file uses')
35+
.option('-x, --context [num]', 'line count used as the diff context', 3)
3536
.option('-l, --log-files', 'print only the parsed file names to stdout')
3637
.option('-q, --quiet', 'do not print the parsed json to stdout')
3738
.option('-n, --continue', 'continue with other files if an error occurs')
@@ -218,7 +219,7 @@ function ensureLineBreak (parsed, source) {
218219

219220
function checkContents (file, source, parsed) {
220221
const { createTwoFilesPatch, structuredPatch } = require('diff')
221-
const structured = structuredPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
222+
const structured = structuredPatch(`${file}.orig`, file, source, parsed, '', '', { context: options.context })
222223
const length = structured.hunks && structured.hunks.length
223224
if (length > 0) {
224225
const hunk = length === 1 ? 'hunk differs' : 'hunks differ'
@@ -231,7 +232,7 @@ function checkContents (file, source, parsed) {
231232
console.error(message)
232233
}
233234
if (!options.quiet) {
234-
const diff = createTwoFilesPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
235+
const diff = createTwoFilesPatch(`${file}.orig`, file, source, parsed, '', '', { context: options.context })
235236
console.log(diff)
236237
}
237238
if (options.continue) {
@@ -253,10 +254,10 @@ function diffContents(file, source, parsed) {
253254
const compact = options.quiet || options.compact
254255
let diff, length
255256
if (compact) {
256-
diff = structuredPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
257+
diff = structuredPatch(`${file}.orig`, file, source, parsed, '', '', { context: options.context })
257258
length = diff.hunks && diff.hunks.length
258259
} else {
259-
diff = createTwoFilesPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
260+
diff = createTwoFilesPatch(`${file}.orig`, file, source, parsed, '', '', { context: options.context })
260261
length = diff.split(/\r?\n/).length - 4
261262
}
262263
if (length > 0) {

0 commit comments

Comments
 (0)