Skip to content

Commit

Permalink
feat: Allow setting the line count as diff context
Browse files Browse the repository at this point in the history
  • Loading branch information
prantlf committed May 4, 2022
1 parent 0423a4b commit 9b22843
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Usage: `jsonlint [options] [<file, directory, pattern> ...]`
-V, --validate [file] JSON schema file to use for validation
-e, --environment [env] which specification of JSON Schema the
validation file uses
-x, --context [num] line count used as the diff context (default: 3)
-l, --log-files print only the parsed file names to stdout
-q, --quiet do not print the parsed json to stdout
-n, --continue continue with other files if an error occurs
Expand Down
9 changes: 5 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const commander = require('commander')
.option('-D, --no-duplicate-keys', 'report duplicate object keys as an error')
.option('-V, --validate [file]', 'JSON schema file to use for validation')
.option('-e, --environment [env]', 'which specification of JSON Schema the validation file uses')
.option('-x, --context [num]', 'line count used as the diff context', 3)
.option('-l, --log-files', 'print only the parsed file names to stdout')
.option('-q, --quiet', 'do not print the parsed json to stdout')
.option('-n, --continue', 'continue with other files if an error occurs')
Expand Down Expand Up @@ -218,7 +219,7 @@ function ensureLineBreak (parsed, source) {

function checkContents (file, source, parsed) {
const { createTwoFilesPatch, structuredPatch } = require('diff')
const structured = structuredPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
const structured = structuredPatch(`${file}.orig`, file, source, parsed, '', '', { context: options.context })
const length = structured.hunks && structured.hunks.length
if (length > 0) {
const hunk = length === 1 ? 'hunk differs' : 'hunks differ'
Expand All @@ -231,7 +232,7 @@ function checkContents (file, source, parsed) {
console.error(message)
}
if (!options.quiet) {
const diff = createTwoFilesPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
const diff = createTwoFilesPatch(`${file}.orig`, file, source, parsed, '', '', { context: options.context })
console.log(diff)
}
if (options.continue) {
Expand All @@ -253,10 +254,10 @@ function diffContents(file, source, parsed) {
const compact = options.quiet || options.compact
let diff, length
if (compact) {
diff = structuredPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
diff = structuredPatch(`${file}.orig`, file, source, parsed, '', '', { context: options.context })
length = diff.hunks && diff.hunks.length
} else {
diff = createTwoFilesPatch(`${file}.orig`, file, source, parsed, '', '', { context: 3 })
diff = createTwoFilesPatch(`${file}.orig`, file, source, parsed, '', '', { context: options.context })
length = diff.split(/\r?\n/).length - 4
}
if (length > 0) {
Expand Down

0 comments on commit 9b22843

Please sign in to comment.