From a93ac3cb65a030e85ec7a2754011d164e5ad1330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dibl=C3=ADk?= Date: Wed, 19 Feb 2025 11:58:21 +0100 Subject: [PATCH 1/3] [Feature] Ability to force crlf --- README.md | 4 ++++ lib/cli.js | 9 +++++++-- lib/index.d.ts | 5 +++++ lib/printer.js | 3 ++- test/types.test.ts | 1 + 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 51f20ae..17f00de 100644 --- a/README.md +++ b/README.md @@ -233,6 +233,7 @@ The configuration is an object with the following properties, described above, w | enforce-double-quotes | enforceDoubleQuotes | | enforce-single-quotes | enforceSingleQuotes | | trim-trailing-commas | trimTrailingCommas | +| force-crlf | forceCrlf | 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. @@ -337,6 +338,7 @@ The [`print`](#pretty-printing) method accepts an object `options` as the second | `enforceDoubleQuotes` | will surround all strings with double quotes | | `enforceSingleQuotes` | will surround all strings with single quotes | | `trimTrailingCommas` | will omit all trailing commas after the last object entry or array item | +| `forceCrlf` | makes sure all line breaks are CRLF. | ```js // Just concatenate the tokens to produce the same output as was the input. @@ -361,6 +363,8 @@ print(tokens, { enforceDoubleQuotes: true, trimTrailingCommas: true }) +// Same as `print(tokens, {})`, but uses \r\n for line breaks. +print(tokens, { forceCrlf: true }) ``` ### Tokenizing diff --git a/lib/cli.js b/lib/cli.js index a41930f..7f7beee 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -217,6 +217,9 @@ for (let i = 2, l = argv.length; i < l; ++i) { case 'trim-trailing-commas': params.trimTrailingCommas = flag return + case 'force-crlf': + params.forceCrlf = flag + return case 'succeed-with-no-files': params.succeedWithNoFiles = flag return @@ -257,6 +260,7 @@ const paramNames = { 'enforce-double-quotes': 'enforceDoubleQuotes', 'enforce-single-quotes': 'enforceSingleQuotes', 'trim-trailing-commas': 'trimTrailingCommas', + 'force-crlf': 'forceCrlf', 'sort-keys': 'sortKeys', 'sort-keys-ignore-case': 'sortKeysIgnoreCase', 'sort-keys-locale': 'sortKeysLocale', @@ -365,7 +369,8 @@ function processContents (source, file) { stripObjectKeys: params.stripObjectKeys, enforceDoubleQuotes: params.enforceDoubleQuotes, enforceSingleQuotes: params.enforceSingleQuotes, - trimTrailingCommas: params.trimTrailingCommas + trimTrailingCommas: params.trimTrailingCommas, + forceCrlf: params.forceCrlf }) } const sortOptions = {} @@ -433,7 +438,7 @@ function ensureLineBreak (parsed, source) { const newLine = !lines[lines.length - 1] if (params.trailingNewline === true || (params.trailingNewline !== false && newLine)) { - parsed += '\n' + parsed += params.forceCrlf ? "\r\n" : "\n" } return parsed } diff --git a/lib/index.d.ts b/lib/index.d.ts index fc3487f..81a7beb 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -499,6 +499,11 @@ declare module '@prantlf/jsonlint/lib/printer' { * Remove trailing commas after the last item in objects and arrays. */ trimTrailingCommas?: boolean + + /** + * Makes sure all line breaks are CRLF. + */ + forceCrlf?: boolean } /** diff --git a/lib/printer.js b/lib/printer.js index 1a9c868..72818a1 100644 --- a/lib/printer.js +++ b/lib/printer.js @@ -48,6 +48,7 @@ const enforceDoubleQuotes = options.enforceDoubleQuotes const enforceSingleQuotes = options.enforceSingleQuotes const trimTrailingCommas = options.trimTrailingCommas + const newLineChar = options.forceCrlf ? "\r\n" : "\n" let outputString = '' let foundLineBreak @@ -90,7 +91,7 @@ let addDelayedSpaceOrLineBreak if (prettyPrint) { addLineBreak = function () { - outputString += '\n' + outputString += newLineChar } addDelayedSpaceOrLineBreak = function () { diff --git a/test/types.test.ts b/test/types.test.ts index 90c07c7..b7fc5ec 100644 --- a/test/types.test.ts +++ b/test/types.test.ts @@ -71,5 +71,6 @@ test('print', () => { print(tokens, { enforceDoubleQuotes: true }) print(tokens, { enforceSingleQuotes: true }) print(tokens, { trimTrailingCommas: true }) + print(tokens, { forceCrlf: true }) assert.ok(true) }) From e37b53a89c4e2a055b885ddbffa0d6fdc0421d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dibl=C3=ADk?= Date: Wed, 19 Feb 2025 12:17:47 +0100 Subject: [PATCH 2/3] [Fix] stricter comparison when deciding to use crlf --- lib/cli.js | 2 +- lib/printer.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index 7f7beee..567cf75 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -438,7 +438,7 @@ function ensureLineBreak (parsed, source) { const newLine = !lines[lines.length - 1] if (params.trailingNewline === true || (params.trailingNewline !== false && newLine)) { - parsed += params.forceCrlf ? "\r\n" : "\n" + parsed += params.forceCrlf === true ? "\r\n" : "\n" } return parsed } diff --git a/lib/printer.js b/lib/printer.js index 72818a1..fbfa8df 100644 --- a/lib/printer.js +++ b/lib/printer.js @@ -48,7 +48,7 @@ const enforceDoubleQuotes = options.enforceDoubleQuotes const enforceSingleQuotes = options.enforceSingleQuotes const trimTrailingCommas = options.trimTrailingCommas - const newLineChar = options.forceCrlf ? "\r\n" : "\n" + const newLineChar = options.forceCrlf === true ? "\r\n" : "\n" let outputString = '' let foundLineBreak From ef22d72f1f5266d431906fc804322c2359bcd27c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dibl=C3=ADk?= Date: Wed, 19 Feb 2025 12:26:42 +0100 Subject: [PATCH 3/3] [Chore] Update remaining docs --- README.md | 3 ++- lib/cli.js | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 17f00de..9579152 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,7 @@ Usage: `jsonlint [options] [--] [ ...]` --enforce-double-quotes surrounds all strings with double quotes --enforce-single-quotes surrounds all strings with single quotes --trim-trailing-commas omit trailing commas from objects and arrays + --force-crlf makes sure all line breaks are CRLF --succeed-with-no-files succeed (exit code 0) if no files were found -v, --version output the version number -h, --help display help for command @@ -338,7 +339,7 @@ The [`print`](#pretty-printing) method accepts an object `options` as the second | `enforceDoubleQuotes` | will surround all strings with double quotes | | `enforceSingleQuotes` | will surround all strings with single quotes | | `trimTrailingCommas` | will omit all trailing commas after the last object entry or array item | -| `forceCrlf` | makes sure all line breaks are CRLF. | +| `forceCrlf` | makes sure all line breaks are CRLF | ```js // Just concatenate the tokens to produce the same output as was the input. diff --git a/lib/cli.js b/lib/cli.js index 567cf75..e01848d 100755 --- a/lib/cli.js +++ b/lib/cli.js @@ -59,6 +59,7 @@ Options: --enforce-double-quotes surrounds all strings with double quotes --enforce-single-quotes surrounds all strings with single quotes --trim-trailing-commas omit trailing commas from objects and arrays + --force-crlf makes sure all line breaks are CRLF --succeed-with-no-files succeed (exit code 0) if no files were found -v, --version output the version number -h, --help display help for command