From d73fe3b512943ca5fe07a1d113c6721b6f1b8c0d Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Sun, 21 May 2017 00:12:56 +0300 Subject: [PATCH 1/5] 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 --- src/format-files.js | 20 ++++++- src/format-files.test.js | 8 +-- src/parser.js | 120 ++++++++++++++++++++++++++++----------- 3 files changed, 108 insertions(+), 40 deletions(-) diff --git a/src/format-files.js b/src/format-files.js index c6b497c..0c67266 100644 --- a/src/format-files.js +++ b/src/format-files.js @@ -37,15 +37,31 @@ function formatFilesFromArgv({ ignore: ignoreGlobs = [], eslintIgnore: applyEslintIgnore = true, prettierLast, - prettier, + ...prettierOptions }) { + /* + // Wanna be more strict? + const { + useTabs, + bracketSpacing, + jsxBracketSameLine, + semi, + printWidth, + tabWidth, + trailingComma, + parser + } = prettierOptions + */ + console.log('actual') + console.log(prettierOptions) + logger.setLevel(logLevel) const prettierESLintOptions = { logLevel, eslintPath, prettierPath, prettierLast, - prettierOptions: prettier, + prettierOptions, } const cliOptions = {write, listDifferent} if (stdin) { diff --git a/src/format-files.test.js b/src/format-files.test.js index 54e389d..f96bd1b 100644 --- a/src/format-files.test.js +++ b/src/format-files.test.js @@ -116,11 +116,7 @@ test('logs errors to the console if something goes wrong', async () => { const errorPrefix = expect.stringMatching(/prettier-eslint-cli.*ERROR/) const cliError = expect.stringContaining('eslint-config-error') const errorOutput = expect.stringContaining('Some weird eslint config error') - expect(console.error).toHaveBeenCalledWith( - errorPrefix, - cliError, - errorOutput, - ) + expect(console.error).toHaveBeenCalledWith(errorPrefix, cliError, errorOutput) }) test('forwards logLevel onto prettier-eslint', async () => { @@ -139,7 +135,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'}}), diff --git a/src/parser.js b/src/parser.js index db3e01b..8874e74 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,17 +1,22 @@ -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 arrify from 'arrify' -import camelcaseKeys from 'camelcase-keys' -import chalk from 'chalk' -import boolify from 'boolify' +const path = require('path') +const getLogger = require('loglevel-colored-level-prefix') +const findUp = require('find-up') +const yargs = require('yargs') +const {oneLine} = require('common-tags') +const arrify = require('arrify') +// const camelcaseKeys = require('camelcase-keys') +// const chalk = require('chalk') +// const boolify = require('boolify') const logger = getLogger({prefix: 'prettier-eslint-cli'}) const parser = yargs - .usage('Usage: $0 ... [--option-1 option-1-value --option-2]') + .usage( + `Usage: $0 ... [--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() @@ -21,7 +26,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', @@ -39,12 +48,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, @@ -68,13 +79,72 @@ 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.', + }, + // typos of fast fingers? + // because `yargs` don't have `recommendOptions`, huh? + 'trailing-comma': { + default: 'none', + type: 'string', + describe: `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: `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: `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. @@ -84,23 +154,9 @@ 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 +module.exports = parser function getPathInHostNodeModules(module) { logger.debug(`Looking for a local installation of the module "${module}"`) From d4f5904b64d01ae5aef2b95c98f8d657a2e35474 Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Sun, 21 May 2017 00:18:08 +0300 Subject: [PATCH 2/5] chore(cleanup): misc stuff --- src/format-files.js | 2 -- src/parser.js | 19 +++++++------------ 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/format-files.js b/src/format-files.js index 0c67266..26683e6 100644 --- a/src/format-files.js +++ b/src/format-files.js @@ -52,8 +52,6 @@ function formatFilesFromArgv({ parser } = prettierOptions */ - console.log('actual') - console.log(prettierOptions) logger.setLevel(logLevel) const prettierESLintOptions = { diff --git a/src/parser.js b/src/parser.js index 8874e74..4553410 100644 --- a/src/parser.js +++ b/src/parser.js @@ -1,12 +1,9 @@ -const path = require('path') -const getLogger = require('loglevel-colored-level-prefix') -const findUp = require('find-up') -const yargs = require('yargs') -const {oneLine} = require('common-tags') -const arrify = require('arrify') -// const camelcaseKeys = require('camelcase-keys') -// const chalk = require('chalk') -// const boolify = require('boolify') +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 arrify from 'arrify' const logger = getLogger({prefix: 'prettier-eslint-cli'}) @@ -94,8 +91,6 @@ semicolons and --no-eslint-ignore to disable default ignores.`, type: 'number', describe: 'Specify the number of spaces per indentation-level.', }, - // typos of fast fingers? - // because `yargs` don't have `recommendOptions`, huh? 'trailing-comma': { default: 'none', type: 'string', @@ -156,7 +151,7 @@ that may introduce ASI failures }) .strict() -module.exports = parser +export default parser function getPathInHostNodeModules(module) { logger.debug(`Looking for a local installation of the module "${module}"`) From 0e5fd6acd25e87143269b60f0761adc46c4b6da2 Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Sun, 21 May 2017 00:24:07 +0300 Subject: [PATCH 3/5] fix(tests): update cli tests, not help output is a bit bigger Because we have described all the prettier's options in our help output --- cli-test/tests/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/cli-test/tests/index.js b/cli-test/tests/index.js index cee8234..decb88f 100644 --- a/cli-test/tests/index.js +++ b/cli-test/tests/index.js @@ -27,10 +27,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...', + ` + We probably have a problem. + The --help output is probably too long (${stdout.length})... + `, ) } }) @@ -45,9 +48,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)) } @@ -58,7 +61,7 @@ test('formats files and outputs to stdout', async () => { stripIndent( ` export default foo - + function foo(thing) { return thing } From f152ddeb2026ca69d8b802ac06b7ce06917f840f Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Sun, 21 May 2017 08:17:17 +0300 Subject: [PATCH 4/5] chore(add): run all contributors --- .all-contributorsrc | 10 ++++++++++ README.md | 4 ++-- src/format-files.test.js | 6 +++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index 1cc7fbd..aae37f7 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -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" + ] } ] } diff --git a/README.md b/README.md index 36e4f53..158e4b2 100644 --- a/README.md +++ b/README.md @@ -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] @@ -183,7 +183,7 @@ Thanks goes to these people ([emoji key][emojis]): | [
Kent C. Dodds](https://kentcdodds.com)
[๐Ÿ’ป](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") | [
Adam Harris](https://github.com/aharris88)
[๐Ÿ’ป](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") | [
Eric McCormick](https://ericmccormick.io)
[๐Ÿ‘€](#review-edm00se "Reviewed Pull Requests") | [
Joel Sequeira](https://github.com/joelseq)
[๐Ÿ“–](https://github.com/prettier/prettier-eslint-cli/commits?author=joelseq "Documentation") | [
Frank Taillandier](https://frank.taillandier.me)
| [
Adam Stankiewicz](http://sheerun.net)
[๐Ÿ’ป](https://github.com/prettier/prettier-eslint-cli/commits?author=sheerun "Code") | [
Stephen John Sorensen](http://www.stephenjohnsorensen.com/)
[๐Ÿ’ป](https://github.com/prettier/prettier-eslint-cli/commits?author=spudly "Code") | | :---: | :---: | :---: | :---: | :---: | :---: | :---: | -| [
Gandem](https://github.com/Gandem)
[๐Ÿ’ป](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Code") [โš ๏ธ](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Tests") | [
Matteo Ronchi](https://github.com/cef62)
[๐Ÿ›](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Acef62 "Bug reports") [๐Ÿ’ป](https://github.com/prettier/prettier-eslint-cli/commits?author=cef62 "Code") | [
Benoรฎt Zugmeyer](https://github.com/BenoitZugmeyer)
[๐Ÿ’ป](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Code") [โš ๏ธ](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Tests") | +| [
Gandem](https://github.com/Gandem)
[๐Ÿ’ป](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Code") [โš ๏ธ](https://github.com/prettier/prettier-eslint-cli/commits?author=Gandem "Tests") | [
Matteo Ronchi](https://github.com/cef62)
[๐Ÿ›](https://github.com/prettier/prettier-eslint-cli/issues?q=author%3Acef62 "Bug reports") [๐Ÿ’ป](https://github.com/prettier/prettier-eslint-cli/commits?author=cef62 "Code") | [
Benoรฎt Zugmeyer](https://github.com/BenoitZugmeyer)
[๐Ÿ’ป](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Code") [โš ๏ธ](https://github.com/prettier/prettier-eslint-cli/commits?author=BenoitZugmeyer "Tests") | [
Charlike Mike Reagent](https://i.am.charlike.online)
[๐Ÿ’ป](https://github.com/prettier/prettier-eslint-cli/commits?author=tunnckoCore "Code") [โš ๏ธ](https://github.com/prettier/prettier-eslint-cli/commits?author=tunnckoCore "Tests") | This project follows the [all-contributors][all-contributors] specification. Contributions of any kind welcome! diff --git a/src/format-files.test.js b/src/format-files.test.js index f96bd1b..edb1dc6 100644 --- a/src/format-files.test.js +++ b/src/format-files.test.js @@ -116,7 +116,11 @@ test('logs errors to the console if something goes wrong', async () => { const errorPrefix = expect.stringMatching(/prettier-eslint-cli.*ERROR/) const cliError = expect.stringContaining('eslint-config-error') const errorOutput = expect.stringContaining('Some weird eslint config error') - expect(console.error).toHaveBeenCalledWith(errorPrefix, cliError, errorOutput) + expect(console.error).toHaveBeenCalledWith( + errorPrefix, + cliError, + errorOutput, + ) }) test('forwards logLevel onto prettier-eslint', async () => { From 62b1b9c7949525378e0816e40fe6bb9070879402 Mon Sep 17 00:00:00 2001 From: tunnckoCore Date: Sun, 21 May 2017 23:43:50 +0300 Subject: [PATCH 5/5] fix(tweaks): commented fixes about tagged template strings --- cli-test/tests/index.js | 9 ++++---- src/format-files.js | 14 ------------- src/parser.js | 46 +++++++++++++++++++++-------------------- 3 files changed, 29 insertions(+), 40 deletions(-) diff --git a/cli-test/tests/index.js b/cli-test/tests/index.js index decb88f..7653c6e 100644 --- a/cli-test/tests/index.js +++ b/cli-test/tests/index.js @@ -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) @@ -30,10 +31,10 @@ test('help outputs usage information and flags', async () => { if (stdout.length > 4100) { console.error(stdout) throw new Error( - ` - We probably have a problem. - The --help output is probably too long (${stdout.length})... - `, + oneLine` + We probably have a problem. + The --help output is probably too long (${stdout.length})... + `, ) } }) diff --git a/src/format-files.js b/src/format-files.js index 26683e6..5b9805e 100644 --- a/src/format-files.js +++ b/src/format-files.js @@ -39,20 +39,6 @@ function formatFilesFromArgv({ prettierLast, ...prettierOptions }) { - /* - // Wanna be more strict? - const { - useTabs, - bracketSpacing, - jsxBracketSameLine, - semi, - printWidth, - tabWidth, - trailingComma, - parser - } = prettierOptions - */ - logger.setLevel(logLevel) const prettierESLintOptions = { logLevel, diff --git a/src/parser.js b/src/parser.js index 4553410..e77cb21 100644 --- a/src/parser.js +++ b/src/parser.js @@ -2,17 +2,18 @@ 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' const logger = getLogger({prefix: 'prettier-eslint-cli'}) const parser = yargs .usage( - `Usage: $0 ... [--option-1 option-1-value --option-2] + stripIndent`Usage: $0 ... [--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.`, + 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') @@ -94,25 +95,26 @@ semicolons and --no-eslint-ignore to disable default ignores.`, 'trailing-comma': { default: 'none', type: 'string', - describe: `Print trailing commas wherever possible. + 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) + 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: `Print spaces between brackets in object literals. -Can use --no-bracket-spacing for "false" to disable it. + 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} -`, + Valid options: + - true - Example: { foo: bar } + - false - Example: {foo: bar} + `, }, 'jsx-bracket-same-line': { default: false, @@ -131,14 +133,14 @@ Valid options: semi: { default: true, type: 'boolean', - describe: `Print semicolons at the ends of statements. -Can use --no-semi to be compatible with StandardJS. + 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 -`, + 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