diff --git a/README.md b/README.md index 51f20ae..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 @@ -233,6 +234,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 +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 | ```js // Just concatenate the tokens to produce the same output as was the input. @@ -361,6 +364,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..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 @@ -217,6 +218,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 +261,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 +370,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 +439,7 @@ function ensureLineBreak (parsed, source) { const newLine = !lines[lines.length - 1] if (params.trailingNewline === true || (params.trailingNewline !== false && newLine)) { - parsed += '\n' + parsed += params.forceCrlf === true ? "\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..fbfa8df 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 === true ? "\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) })