Skip to content

Commit ca7fa7f

Browse files
authored
feat: Ability to force crlf
Config now includes force-crlf boolean option that makes sure, while enabled, json output prints with \r\n as new line characters. Default remains \n. Example usage: { "patterns": ["**/*.json", "!**/node_modules/**/*", "!**/dist/**"], .... "force-crlf": true } Usage with CLI matches other boolean arguments. * [Feature] Ability to force crlf * [Fix] stricter comparison when deciding to use crlf * [Chore] Update remaining docs
1 parent 3d53d3a commit ca7fa7f

5 files changed

Lines changed: 21 additions & 3 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ Usage: `jsonlint [options] [--] [<file, directory, pattern> ...]`
163163
--enforce-double-quotes surrounds all strings with double quotes
164164
--enforce-single-quotes surrounds all strings with single quotes
165165
--trim-trailing-commas omit trailing commas from objects and arrays
166+
--force-crlf makes sure all line breaks are CRLF
166167
--succeed-with-no-files succeed (exit code 0) if no files were found
167168
--[no-]color force or disable colourful output of the diff
168169
-v, --version output the version number
@@ -237,6 +238,7 @@ The configuration is an object with the following properties, described above, w
237238
| enforce-double-quotes | enforceDoubleQuotes |
238239
| enforce-single-quotes | enforceSingleQuotes |
239240
| trim-trailing-commas | trimTrailingCommas |
241+
| force-crlf | forceCrlf |
240242

241243
The parameter `config` will be ignored in configuration files. The extra parameter `patterns` can be set to an array of strings with paths or patterns instead of putting them to the command line.
242244

@@ -347,6 +349,7 @@ The [`print`](#pretty-printing) method accepts an object `options` as the second
347349
| `enforceDoubleQuotes` | will surround all strings with double quotes |
348350
| `enforceSingleQuotes` | will surround all strings with single quotes |
349351
| `trimTrailingCommas` | will omit all trailing commas after the last object entry or array item |
352+
| `forceCrlf` | makes sure all line breaks are CRLF |
350353

351354
```js
352355
// Just concatenate the tokens to produce the same output as was the input.
@@ -371,6 +374,8 @@ print(tokens, {
371374
enforceDoubleQuotes: true,
372375
trimTrailingCommas: true
373376
})
377+
// Same as `print(tokens, {})`, but uses \r\n for line breaks.
378+
print(tokens, { forceCrlf: true })
374379
```
375380

376381
Pretty-printing can be also used to preserve the contents of string literal in the output. For example, the following input:

lib/cli.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Options:
6161
--enforce-double-quotes surrounds all strings with double quotes
6262
--enforce-single-quotes surrounds all strings with single quotes
6363
--trim-trailing-commas omit trailing commas from objects and arrays
64+
--force-crlf makes sure all line breaks are CRLF
6465
--succeed-with-no-files succeed (exit code 0) if no files were found
6566
--[no-]color force or disable colourful output of the diff
6667
-v, --version output the version number
@@ -241,6 +242,9 @@ for (let i = 2, l = argv.length; i < l; ++i) {
241242
case 'trim-trailing-commas':
242243
params.trimTrailingCommas = flag
243244
return
245+
case 'force-crlf':
246+
params.forceCrlf = flag
247+
return
244248
case 'succeed-with-no-files':
245249
params.succeedWithNoFiles = flag
246250
return
@@ -281,6 +285,7 @@ const paramNames = {
281285
'enforce-double-quotes': 'enforceDoubleQuotes',
282286
'enforce-single-quotes': 'enforceSingleQuotes',
283287
'trim-trailing-commas': 'trimTrailingCommas',
288+
'force-crlf': 'forceCrlf',
284289
'sort-keys': 'sortKeys',
285290
'sort-keys-ignore-case': 'sortKeysIgnoreCase',
286291
'sort-keys-locale': 'sortKeysLocale',
@@ -390,7 +395,8 @@ function processContents (source, file) {
390395
stripObjectKeys: params.stripObjectKeys,
391396
enforceDoubleQuotes: params.enforceDoubleQuotes,
392397
enforceSingleQuotes: params.enforceSingleQuotes,
393-
trimTrailingCommas: params.trimTrailingCommas
398+
trimTrailingCommas: params.trimTrailingCommas,
399+
forceCrlf: params.forceCrlf
394400
})
395401
}
396402
const sortOptions = {}
@@ -458,7 +464,7 @@ function ensureLineBreak (parsed, source) {
458464
const newLine = !lines[lines.length - 1]
459465
if (params.trailingNewline === true ||
460466
(params.trailingNewline !== false && newLine)) {
461-
parsed += '\n'
467+
parsed += params.forceCrlf === true ? "\r\n" : "\n"
462468
}
463469
return parsed
464470
}

lib/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,11 @@ declare module '@prantlf/jsonlint/lib/printer' {
506506
* Remove trailing commas after the last item in objects and arrays.
507507
*/
508508
trimTrailingCommas?: boolean
509+
510+
/**
511+
* Makes sure all line breaks are CRLF.
512+
*/
513+
forceCrlf?: boolean
509514
}
510515

511516
/**

lib/printer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
const enforceDoubleQuotes = options.enforceDoubleQuotes
4949
const enforceSingleQuotes = options.enforceSingleQuotes
5050
const trimTrailingCommas = options.trimTrailingCommas
51+
const newLineChar = options.forceCrlf === true ? "\r\n" : "\n"
5152

5253
let outputString = ''
5354
let foundLineBreak
@@ -90,7 +91,7 @@
9091
let addDelayedSpaceOrLineBreak
9192
if (prettyPrint) {
9293
addLineBreak = function () {
93-
outputString += '\n'
94+
outputString += newLineChar
9495
}
9596

9697
addDelayedSpaceOrLineBreak = function () {

test/types.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,6 @@ test('print', () => {
7171
print(tokens, { enforceDoubleQuotes: true })
7272
print(tokens, { enforceSingleQuotes: true })
7373
print(tokens, { trimTrailingCommas: true })
74+
print(tokens, { forceCrlf: true })
7475
assert.ok(true)
7576
})

0 commit comments

Comments
 (0)