Repository navigation
Description
This looks awesome already. I ran the formatter through the nuclide codebase ( https://github.com/facebook/nuclide/tree/master/pkg ) and here are the things I'd love to get tweaked so that we can use it at Facebook :)
- Make last block expand
When the last argument of a function call is a function, object or array, and all the previous arguments fit in one line, want to inline it.
call(a, b, () => {
});- Put one argument per line when it expands
- Do not put spaces inside of
{}and[]
- Use
,for type object separator instead of;
- Prioritize breaking on
?and:
- Use single quote
- Add trailing commas
- Preserve original \n
I don't think it's possible to automatically add empty lines in the right place most of the time. I think this should be under the developer control to decide where to put them.
- Do not put an extra indent for else if
- Invalid code generated
- Remove trailing whitespace
- Implement chaining for
&and|
- Implement call chains
Something
.call(
...
)
.something.call( ... )
.call( ... );
The way I want it to look is that if it doesn't fit into a single line, every .call() is on its own line and indented two spaces. The way to do it is to look for CallExpression and traverse all the CallExpressions and MemberExpressions and rewrite them in this way.













Activity
jlongster commentedon Dec 31, 2016
(Sigh accidentally posted, editing it to finish)
All of these look really good! None of them changes the few things I'm opinionated about, so I'm really happy to work with you on this. Most of these are pretty easy. Also, I think we should have CLI options to configure a few of them (singe/double-quotes and spaces inside objects/arrays at least). That will help adoption.
It will be interesting to figure this out because the way it formats it now is because of how the AST is structured, which is based on operator precedence. If we force it to be different, we'll probably run into other weird formatting when the ternary operator is involved in a longer expression. We should create a test file with a bunch of different types of expressions involving a ternary.
I have no problems changing this though, hopefully it's not too hard.
Yeah, I think you're right. FWIW refmt does not do this, and I was trying to be as opinionated as it. However, I think this would be another thing that would help adoption.
I think we could at least collapse multiple empty lines into a single empty line. Otherwise, keep single and double newlines in the output, which allows the programmer to group code.
That's not invalid! :) I'm kidding, I know what you mean. It moved the comments up. Comments are hard, and that behavior is from recast, because it doesn't know if the comment is attached to the
current line or the next node. There are a few issues on recast about comments, and this comment (benjamn/recast#191 (comment)) explains the reason for this exact behavior.
Because we've forked it though, we can see if there's a small fix that works for the majority of the cases.
Yep, I introduced this last night with an initial attempt to add blank lines. I thought it's tricky to insert only blank lines (because when you insert a line, it automatically indents it) but I just realized we can easily solve this in the code that actually prints the final string by forcibly removing any spaces on blank lines.
This might be the hardest to implement out of all of these requests. I like the idea and we'll see what we can do. Traversing the AST and printing CallExpressions more manually might work.
vjeux commentedon Dec 31, 2016
jlongster commentedon Jan 2, 2017
Should have more time tomorrow to fix most of these; already finished two. Does the function args change look good?
fa16988
jlongster commentedon Jan 4, 2017
Specializing last argument expansion is finished in 588c8ce (and a follow-up commit, there are a few errors in those snapshot changes).
vjeux commentedon Jan 4, 2017
<3
jlongster commentedon Jan 4, 2017
No more spaces inside objects/arrays in 8326963. You can configure this by passing the
bracketSpacingoption (jscodefmt --bracket-spacingwill turn it on).jlongster commentedon Jan 4, 2017
Uses
,for separating flow type fields in b550f1djlongster commentedon Jan 4, 2017
Single quotes can be enabled in 599b431 with the
--single-quotesoption or thequote: "single"API option (default to double for now).jlongster commentedon Jan 5, 2017
@vjeux A few questions about trailing commas:
jlongster commentedon Jan 5, 2017
In fc186c8 you can add trailing commas to objects split across lines with the
--trailing-commaoption. If there are other expressions that should have a trailing comma, let me know.vjeux commentedon Jan 5, 2017
@jlongster for commas:
That's correct, only print trailing comma when they are split in groups.
For function args, we have a tc39 proposal at stage 4 ("Finished: Indicate that the addition is ready for inclusion in the formal ECMAScript standard") https://github.com/tc39/proposal-trailing-function-commas and a babel transform that we've been using for years https://babeljs.io/docs/plugins/syntax-trailing-function-commas/
We want everything that is separated by commas to have trailing commas. All the ones you mentioned are √. @pieterv probably has an exhaustive list.
jlongster commentedon Jan 5, 2017
Hm... I'm not sure I want to print trailing commas in a place that require a babel transform. I suppose I could do that, since by default it doesn't print trailing commas, and if you opt in you get it everywhere. If users complain maybe we'll have to provide a slightly more fine-grain option.
jlongster commentedon Jan 5, 2017
else if's should be combined in fcd5436
jlongster commentedon Jan 5, 2017
@vjeux Alright, in 0d4d21c it prints trailing commas everywhere. If I missed any place, let me know.
jlongster commentedon Jan 5, 2017
Ternary operators will break across lines first in 7b0ec6d. This actually was easy; I just hadn't implemented any breaking behavior for ternary ops.
jlongster commentedon Jan 5, 2017
Better rules for breaking in union/intersection types landed in 5c39dc0
vjeux commentedon Jan 5, 2017
Omg, you are on a streak <3
jlongster commentedon Jan 8, 2017
Added call chains in #4
jlongster commentedon Jan 9, 2017
Original blank lines are preserverd in bcd44b4. @vjeux I'd be interested in how the nuclide codebase looks now after that; does it work properly?
jlongster commentedon Jan 9, 2017
I improved printing of comments in 994432c. There is still probably more work to do there.
Unfortunately I could not solve this case:
See the issue on recast that I linked to before. It's very difficult because although it's attached as a trailing comment on the right object property AST node, that node is printed before the comma. Therefore a naive printing will do this:
Note how the comma is printed after the comment. Fixing this would involve some really complicated logic for how to interlave comments.
But it's even harder for us than recast. Since we are a flexible printer, we will collapse nodes when they fix. That means that inline comments will be collapsed as well, and even if we fixed the comma, we would output this:
Basically users cannot reliably have inline comments because even if we outputted them correctly, they might be collapsed. We could also try and force parents to break if they have inline comments, but all of this will be really hard.
For now, we do what recast does and simply convert those trailing comments to a leading comment which is much easier to print. We can look into improving this later, although once you apply our printer, it won't convert leading comments back to trailing ones...
jlongster commentedon Jan 9, 2017
I think I have completed this issue as much as I can right now. Feel free to file more issues specific to comments so that we can track them. I'm sure there are other things we can do, but I'd like to go ahead and announce this to get more feedback.
[Site] Margins and paddings (prettier#2)
Revamp landing page (#3718)