Skip to content

Commit

Permalink
Breaking Change: Be compatible with Prettier CLI (#59)
Browse files Browse the repository at this point in the history
* feat(compatibility): directly pass all flags/options to prettier

for example instead of passing --prettier.singleQuote, you now can just use --single-quote" or
--singleQuote

BREAKING CHANGE: no more need for --prettier prefixing for Prettier's options
  • Loading branch information
tunnckoCore authored and Kent C. Dodds committed May 21, 2017
1 parent 628c8fd commit 9e11635
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 36 deletions.
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@
"code",
"test"
]
},
{
"login": "tunnckoCore",
"name": "Charlike Mike Reagent",
"avatar_url": "https://avatars0.githubusercontent.com/u/5038030?v=3",
"profile": "https://i.am.charlike.online",
"contributions": [
"code",
"test"
]
}
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CLI for [`prettier-eslint`][prettier-eslint]
[![downloads][downloads-badge]][npm-stat]
[![MIT License][license-badge]][LICENSE]

[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-11-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -183,7 +183,7 @@ Thanks goes to these people ([emoji key][emojis]):
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](https://kentcdodds.com)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=kentcdodds "Code") [📖](https://github.com/prettier/prettier-eslint-cli/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=kentcdodds "Tests") | [<img src="https://avatars3.githubusercontent.com/u/3266363?v=3" width="100px;"/><br /><sub>Adam Harris</sub>](https://github.com/aharris88)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=aharris88 "Code") [📖](https://github.com/prettier/prettier-eslint-cli/commits?author=aharris88 "Documentation") [👀](#review-aharris88 "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/622118?v=3" width="100px;"/><br /><sub>Eric McCormick</sub>](https://ericmccormick.io)<br />[👀](#review-edm00se "Reviewed Pull Requests") | [<img src="https://avatars.githubusercontent.com/u/12389411?v=3" width="100px;"/><br /><sub>Joel Sequeira</sub>](https://github.com/joelseq)<br />[📖](https://github.com/prettier/prettier-eslint-cli/commits?author=joelseq "Documentation") | [<img src="https://avatars.githubusercontent.com/u/103008?v=3" width="100px;"/><br /><sub>Frank Taillandier</sub>](https://frank.taillandier.me)<br /> | [<img src="https://avatars3.githubusercontent.com/u/292365?v=3" width="100px;"/><br /><sub>Adam Stankiewicz</sub>](http://sheerun.net)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=sheerun "Code") | [<img src="https://avatars3.githubusercontent.com/u/487068?v=3" width="100px;"/><br /><sub>Stephen John Sorensen</sub>](http://www.stephenjohnsorensen.com/)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=spudly "Code") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars0.githubusercontent.com/u/11560964?v=3" width="100px;"/><br /><sub>Gandem</sub>](https://github.com/Gandem)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Tests") | [<img src="https://avatars0.githubusercontent.com/u/129991?v=3" width="100px;"/><br /><sub>Matteo Ronchi</sub>](https://github.com/cef62)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Acef62 "Bug reports") [💻](https://github.com/prettier/prettier-eslint-cli/commits?author=cef62 "Code") | [<img src="https://avatars2.githubusercontent.com/u/61787?v=3" width="100px;"/><br /><sub>Benoît Zugmeyer</sub>](https://github.com/BenoitZugmeyer)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Tests") |
| [<img src="https://avatars0.githubusercontent.com/u/11560964?v=3" width="100px;"/><br /><sub>Gandem</sub>](https://github.com/Gandem)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Tests") | [<img src="https://avatars0.githubusercontent.com/u/129991?v=3" width="100px;"/><br /><sub>Matteo Ronchi</sub>](https://github.com/cef62)<br />[🐛](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Acef62 "Bug reports") [💻](https://github.com/prettier/prettier-eslint-cli/commits?author=cef62 "Code") | [<img src="https://avatars2.githubusercontent.com/u/61787?v=3" width="100px;"/><br /><sub>Benoît Zugmeyer</sub>](https://github.com/BenoitZugmeyer)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Tests") | [<img src="https://avatars0.githubusercontent.com/u/5038030?v=3" width="100px;"/><br /><sub>Charlike Mike Reagent</sub>](https://i.am.charlike.online)<br />[💻](https://github.com/prettier/prettier-eslint-cli/commits?author=tunnckoCore "Code") [⚠️](https://github.com/prettier/prettier-eslint-cli/commits?author=tunnckoCore "Tests") |
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome!
Expand Down
14 changes: 9 additions & 5 deletions cli-test/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path'
import fs from 'fs'
import spawn from 'spawn-command'
import pify from 'pify'
import {oneLine} from 'common-tags'
import stripIndent from 'strip-indent'

const pWriteFile = pify(fs.writeFile)
Expand All @@ -27,10 +28,13 @@ test('help outputs usage information and flags', async () => {
expect(stdout).toContain('Options:\n')
// just a sanity check.
// If it's ever longer than 2000 then we've probably got a problem...
if (stdout.length > 2000) {
if (stdout.length > 4100) {
console.error(stdout)
throw new Error(
'We probably have a problem. The --help output is probably too long...',
oneLine`
We probably have a problem.
The --help output is probably too long (${stdout.length})...
`,
)
}
})
Expand All @@ -45,9 +49,9 @@ test('formats files and outputs to stdout', async () => {
stripIndent(
`
import baz, {stuff} from 'fdjakfdlfw-baz'
export {bazzy}
function bazzy(something) {
return baz(stuff(something))
}
Expand All @@ -58,7 +62,7 @@ test('formats files and outputs to stdout', async () => {
stripIndent(
`
export default foo
function foo(thing) {
return thing
}
Expand Down
4 changes: 2 additions & 2 deletions src/format-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ function formatFilesFromArgv({
ignore: ignoreGlobs = [],
eslintIgnore: applyEslintIgnore = true,
prettierLast,
prettier,
...prettierOptions
}) {
logger.setLevel(logLevel)
const prettierESLintOptions = {
logLevel,
eslintPath,
prettierPath,
prettierLast,
prettierOptions: prettier,
prettierOptions,
}
const cliOptions = {write, listDifferent}
if (stdin) {
Expand Down
2 changes: 1 addition & 1 deletion src/format-files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ test('forwards prettierLast onto prettier-eslint', async () => {
test('forwards prettierOptions onto prettier-eslint', async () => {
await formatFiles({
_: ['src/**/1*.js'],
prettier: {trailingComma: 'es5'},
trailingComma: 'es5',
})
expect(formatMock).toHaveBeenCalledWith(
expect.objectContaining({prettierOptions: {trailingComma: 'es5'}}),
Expand Down
105 changes: 79 additions & 26 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import path from 'path'
import getLogger from 'loglevel-colored-level-prefix'
import findUp from 'find-up'
import yargs from 'yargs'
import {oneLine} from 'common-tags'
import {oneLine, stripIndent} from 'common-tags'
import arrify from 'arrify'
import camelcaseKeys from 'camelcase-keys'
import chalk from 'chalk'
import boolify from 'boolify'

const logger = getLogger({prefix: 'prettier-eslint-cli'})

const parser = yargs
.usage('Usage: $0 <globs>... [--option-1 option-1-value --option-2]')
.usage(
stripIndent`Usage: $0 <globs>... [--option-1 option-1-value --option-2]
Prefix an option with "no-" to set it to false, such as --no-semi to
disable semicolons and --no-eslint-ignore to disable default ignores.
`,
)
.help('h')
.alias('h', 'help')
.version()
Expand All @@ -21,7 +24,11 @@ const parser = yargs
describe: 'Edit the file in-place (beware!)',
type: 'boolean',
},
stdin: {default: false, describe: 'Read input via stdin', type: 'boolean'},
stdin: {
default: false,
describe: 'Read input via stdin',
type: 'boolean',
},
'eslint-ignore': {
default: true,
type: 'boolean',
Expand All @@ -39,12 +46,14 @@ const parser = yargs
from Prettier + Eslint formatting.
`,
},
eslintPath: {
// allow `--eslint-path` and `--eslintPath`
'eslint-path': {
default: getPathInHostNodeModules('eslint'),
describe: 'The path to the eslint module to use',
coerce: coercePath,
},
prettierPath: {
// allow `--prettier-path` and `--prettierPath`
'prettier-path': {
describe: 'The path to the prettier module to use',
default: getPathInHostNodeModules('prettier'),
coerce: coercePath,
Expand All @@ -68,13 +77,71 @@ const parser = yargs
default: false,
type: 'boolean',
},
prettier: {
'use-tabs': {
default: false,
type: 'boolean',
describe: 'Indent lines with tabs instead of spaces.',
},
'print-width': {
default: 80,
type: 'number',
describe: 'Specify the length of line that the printer will wrap on.',
},
'tab-width': {
default: 2,
type: 'number',
describe: 'Specify the number of spaces per indentation-level.',
},
'trailing-comma': {
default: 'none',
type: 'string',
describe: stripIndent`
Print trailing commas wherever possible.
Valid options:
- "none" - no trailing commas
- "es5" - trailing commas where valid in ES5 (objects, arrays, etc)
- "all" - trailing commas wherever possible (function arguments)
`,
},

'bracket-spacing': {
default: true,
type: 'boolean',
describe: stripIndent`Print spaces between brackets in object literals.
Can use --no-bracket-spacing for "false" to disable it.
Valid options:
- true - Example: { foo: bar }
- false - Example: {foo: bar}
`,
},
'jsx-bracket-same-line': {
default: false,
type: 'boolean',
describe: oneLine`
Prettier configuration options
to be passed to prettier-eslint
using dot-notation
Put the > of a multi-line JSX element at
the end of the last line instead of
being alone on the next line
`,
},
parser: {
default: 'babylon',
type: 'string',
describe: 'Specify which parser to use.',
},
semi: {
default: true,
type: 'boolean',
describe: stripIndent`Print semicolons at the ends of statements.
Can use --no-semi to be compatible with StandardJS.
Valid options:
- true - add a semicolon at the end of every statement
- false - only add semicolons at the beginning of lines
that may introduce ASI failures
`,
},
// TODO: if we allow people to to specify a config path,
// we need to read that somehow. These can come invarious
// formats and we'd have to work out `extends` somehow as well.
Expand All @@ -84,20 +151,6 @@ const parser = yargs
// describe: 'Path to the eslint config to use for eslint --fix',
// },
})
.coerce('prettier', config => {
if (typeof config === 'object') {
return boolify(camelcaseKeys(config))
} else {
throw Error(
chalk.red(
oneLine`
You should use dot-notation with
the --prettier flag, for example, --prettier.singleQuote
`,
),
)
}
})
.strict()

export default parser
Expand Down

0 comments on commit 9e11635

Please sign in to comment.