Skip to content

[Feature] Ability to force crlf #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ Usage: `jsonlint [options] [--] [<file, directory, pattern> ...]`
--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
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down
10 changes: 8 additions & 2 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 5 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -90,7 +91,7 @@
let addDelayedSpaceOrLineBreak
if (prettyPrint) {
addLineBreak = function () {
outputString += '\n'
outputString += newLineChar
}

addDelayedSpaceOrLineBreak = function () {
Expand Down
1 change: 1 addition & 0 deletions test/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})