Skip to content

refactor(cli): centralize options#2767

Merged
suchipi merged 75 commits intoprettier:masterfrom
ikatyang:refactor/cli
Sep 14, 2017
Merged

refactor(cli): centralize options#2767
suchipi merged 75 commits intoprettier:masterfrom
ikatyang:refactor/cli

Conversation

@ikatyang
Copy link
Copy Markdown
Member

@ikatyang ikatyang commented Sep 7, 2017

main:

  • centralize option information
  • auto-generate usage from option information
  • validate choice options from option information
  • use api default options instead of its own cli default options

side:

  • sort usage options
  • rename e/err to error
  • rename resolveConfig to logResolvedConfigPathOrDie

@azz
Copy link
Copy Markdown
Member

azz commented Sep 7, 2017

This looks great!

Comment thread src/cli-constant.js Outdated
);
}

function indent(str, spaces) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, theoretically you could use prettier to format the help 😉

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parser: "text" 😆

@azz
Copy link
Copy Markdown
Member

azz commented Sep 7, 2017

Probably something for a future PR, but I've been thinking about generating the options part of README.md and options.md (which is probably out of date) from code.

We could then support prettier --help trailing-comma and output:

Trailing Commas: Print trailing commas wherever possible.

Valid options:
 --trailing-comma=none  No trailing commas.
 --trailing-comma=es5   Trailing commas where valid in ES5 (objects, arrays, etc.)
 --trailing-comma=all   Trailing commas wherever possible (function arguments). 
                        This requires node 8 or a transform.

Default: none

@ikatyang
Copy link
Copy Markdown
Member Author

ikatyang commented Sep 7, 2017

Something like markdown-magic?

@azz
Copy link
Copy Markdown
Member

azz commented Sep 7, 2017

So long as we use the prettier plugin 👍

Comment thread src/cli-constant.js Outdated
const optionArray = Object.keys(options).reduce(
(current, name) => current.concat(options[name]),
[]
);
Copy link
Copy Markdown
Member

@lydell lydell Sep 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this better written as Object.keys(options).map(name => options[name])? Or am I missing something?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, forgot it's an array too. 😅

@ikatyang
Copy link
Copy Markdown
Member Author

ikatyang commented Sep 8, 2017

I have another idea to further reduce duplicate code in this PR, mark it as WIP now.

@ikatyang ikatyang changed the title refactor(cli): centralize options [WIP] refactor(cli): centralize options Sep 8, 2017
- Move Format options first, since they feel the most important.
- Move --find-config-path to the Config category.
- Merge Command into Other. It wasn't clear what "Command" options are,
  and the Other category isn't too long anyway.
- Move --range-end and --range-start into Other. The aren't Format
  options.
- Move the very important options --write and --list-different to the
  top, in the new Output category. It could be argued that --help and
  --version also belong there, but I don't think they deserve such a
  prominent position, so they are left in the Other category.
  --find-config-path could also be considered an Output option, but I
  think it fits better in the Config category.
- Now that there's an Output category, should there also be an Input
  category? I thought about that, but the only candidates for such a
  category are --stdin and --stdin-filepath and I don't think they are
  important enough to have their own category, so they stay in Other.
- Move --cursor-offset, --range-end and --range-start to a new Editor
  category, since they are mainly for editor integrations.
- Document how Prettier deals with input and output by default.
Comment thread src/cli-util.js
firstCategories.indexOf(category) === -1 &&
lastCategories.indexOf(category) === -1
);
const allCategories = firstCategories.concat(restCategories, lastCategories);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this block? Can't we just do:

const categoryOrder = [
  constant.CATEGORY_OUTPUT,
  constant.CATEGORY_FORMAT,
  constant.CATEGORY_CONFIG,
  constant.CATEGORY_EDITOR,
  constant.CATEGORY_OTHER
];

in cli-constant.js and be done with it? What do you think, @ikatyang?

Copy link
Copy Markdown
Member Author

@ikatyang ikatyang Sep 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My original thought is that we might forget to edit the ordering in cli-util.js, cause unordered category missing thus the snapshot tests passed with no change. We have code review to catch that kind of things, but I'm not sure if we will remember to 😅. Any thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess it makes sense to keep this to help us from making mistakes in the future. It's not so many lines after all.

@lydell
Copy link
Copy Markdown
Member

lydell commented Sep 13, 2017

Let's merge this after 1.6.2 has been released. Just in case.

@suchipi
Copy link
Copy Markdown
Member

suchipi commented Sep 14, 2017

Based on discussion in #2804 (comment), since we are going to make the 1.6.2 release 1.7.0 instead, we are going to merge this

@suchipi suchipi merged commit 655fef1 into prettier:master Sep 14, 2017
@vjeux
Copy link
Copy Markdown
Contributor

vjeux commented Sep 14, 2017

@suchipi could you use Squash & Merge button next time, otherwise the commit history is spammed with the 50 commits from this PR. Thanks :)

@suchipi
Copy link
Copy Markdown
Member

suchipi commented Sep 14, 2017

Will do. I was going to do rebase and merge, but this one said it had merge conflicts (if I did rebase), and so I had to make a merge commit. But I didn't try squash and merge. I'll make that my default for this repo in the future 👍

@ikatyang ikatyang deleted the refactor/cli branch September 14, 2017 23:20
@azz
Copy link
Copy Markdown
Member

azz commented Sep 14, 2017

@vjeux should we disable the other two options so others don't get caught by this?

@lock lock Bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 19, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants