* Account for empty lines in argument list in typical cases
* Fix build errors
* Fix one more build error
* Refactor
* Have better variable names for printing argument list
* Account for feedback on argument list empty lines
* Improve expanion argument logics
* Add a lot more tests
* Improve a test
* Make code cleaner
* Fix a lint error
* Refactor
* Add one last check
* Refactor
* Split Less and SCSS parsing into different parsers
Now, .less files are always parsed with postcss-less, and .scss files
areare always parsed with postcss-scss. This:
- Is less hacky.
- Is meant to avoid issues like #2829.
- Is probably more performant.
`parser: "postcss"` and `--parser postcss` continue to work like before:
First trying postcss-less, and if that fails, postcss-scss, unless a
regex says that we should try in the opposite order. The new values for
the parser option are "postcss-less" and "postcss-scss".
* Remove postcss from package.json since it is not used
* Rename parser-less to less and parser-scss to scss
* Deprecate parser:postcss in favor of parser:css
* Fix CSS tests
Fixes#2820. While that issue is about `@media (--fooBar)` in
particular, this commit changes to never lowercase stuff starting with
`--` in general, as it seems like those `--customStuff` things can pop
up kind of anywhere.
* Add option to require @prettier or @format pragma
Fixes#2397.
Inspired by `eslint-plugin-prettier` and the discussion in #2397, this
implements requiring a special comment pragma to be present in a file's
first comment in order to be formatted.
This will help large codebases gradually transition to prettier over
time without tons of churn or large code reviews.
I implemented this as a standard prettier "option", not just a typical
`argv` flag, as it is relevant in both the cli and the api. This way it
can be provided programmatically, on the command line, or standardized
in a prettierrc file so like the style options, every user can use this
setting consistently and only apply prettier to relevant files, no
mattier their editor integration.
This requires the pragma begin with `@` (in fact it's inserted if the
user doesn't provide it). Currently the usage implies it must be
"prettier" or "format", but it can technically be any value other than
"none", which is similar to the `trailingCommas` option.
cc @vjeux
* Don't quote anything in runPrettier; this is usually handled by a shell
* Make --require-pragma a boolean option
* Use jest-docblock to find pragmas without parsing the ast
* Clarify docs
* includes -> indexOf
* Move test out of integration
Fixes#2775.
This commit will make conditional expressions to match the behavior of
logical expression in method chains:
```js
(a ? b : c).map()
// if the conditional fits in oneline
(a ? b : c)
.map()
// if the conditional doesn't fit
(a
? b
: c)
.map()
```
Fixes#2777
Since we can't break after `return` and don't add `()` around
ConditionalExpressions' tests, we end up with some weird indentation when
breaking.
* Fix comment style when placed after the test of an if statement
* Add tests
* Do a bit of refactoring
* Fix lint errors
* Handle skipping comments
* Account for multiple-line comments
* Add more tests
* Change function name
* Add more tests
* Refactor
* Add some comments
* Do away with getPreviousNonSpaceNonCommentCharacter
* Improve comment and code on handling if statement comments
* Refactor
* Fix a bug
* src/util: Fix negative number handling
* src/util: Handle decimal numbers with leading plus sign
* src/util: Handle sign symbols before numbers in scientific notation
We used to be very naive and just push all the comments at the top of the class, but it's very brute force (and breaks some flow annotations inside of fb) so we can do better :)
parser-postcss parses `@import` at-rules specially, and unfortunately
buggily. This monkey-patches parser-postcss to parse all at-rules the
same way.
Fixes#2584.
postcss-less bug: https://github.com/shellscape/postcss-less/issues/88
* Use printPropertyKey function to print keys in object type property in TS and Flow
* Remove flow test modification
* Add tests for new object key cases
* Add new test file
* Fix some styles
* Handle closure compiler type cast syntax correctly
Fixes https://github.com/prettier/prettier/issues/1445
* Move closure type cast detection to needParens in fast-path.js
* every => some and added additional check for leading comment
* Add comments showing parser arguments
Since this is how they are invoked, the comments make it easier to maintain.
* Use babylon.parseExpression for JSON
See https://github.com/prettier/prettier/issues/2378#issuecomment-314600778
Note that this doesn't yet print comments:
echo '{/*comment*/"K":"V"}' | ./bin/prettier.js --parser json
* Remove parser-json.js
This reduces duplicate code in the built package.
See https://github.com/prettier/prettier/pull/2476#pullrequestreview-50181398
* Add test with block comment in JSON
* Fix test with block comment in JSON
* Add test with line comment in JSON
* Don't build now-redundant parser-json.js
* Now JSX-mode ternaries only break if they need to.
* Only the outermost ternary in a chain of JSX-mode ternaries will become a group.
* We don't wrap null in parens when breaking JSX-mode ternaries.
This commit also improves the test coverage and explanation in tests/jsx/conditional-expression.js.
* Arrow Function Expressions returning JSX will now add parens when the JSX breaks
* Conditional expressions within (or containing) JSX are formatted in a more natural way (for JSX), especiall when chained
* JSX in logical expressions (|| or &&) is always wrapped in parens
Fixes#2208
Fixes#2353. Use the same printing code for both `WhileStatement` and `DoWhileStatement`.
Fix calculation of `isInsideParenthesis` when printing a `LogicalExpression`. There is
no such thing as `DoStatement` -- it should be `DoWhileStatement`.
* styled components now support css keyword
* added support also for keyframes
* added comment to reflect the new keywords
* modified comment
* ignore css and keyframes when testing ast manipulations
* added also injectGlobal
"Daniel Schafer: Yeah, that's definitely what I'd describe as the canonical style; anytime you have two "things" on a line, use commas, but if you have a newline it's not needed."
The tokens are attached to the location via the prototype (so they are not visible when you JSON.stringify) but the various places where we traverse the ast see them. It causes stack overflows and comments that are not printed because they are attached to tokens instead of real nodes.
Fixes#2302
We had two occurrences on fb codebase where people attached comments on `{' '}` which would be swallowed. Instead of dropping the comments, let's make the algorithm a bit less naive.
It outputted which is completely wrong
```js
<FlatList
renderItem={info => <span>{info.item.widget.missingProp // $FlowExpectedError - bad widgetCount type 6, should be Object
}</span>}
/>;
```
* Add caching for printing
For printing the last argument expansion, we need to print the same node a bit differently with a flag. It's not easy to re-print just the node that is different so we end up printing all the sub-tree twice. Since it is often nested, it means that we run into an exponential complexity.
I finally found a simple solution: we can use a Map to store the nodes and their printed values. It is really cheap to do so (I can't notice a time difference with or without on normal code) and allows us to stop going through the two sub-trees in the bad case.
Fixes#1250
* Update printer.js
* Allow JSX lines to be recombined
* Add test case for `<br />` breaking expression
* Fix issue where JSX whitespace could be dropped
* Newlines were incorrectly being preserved when JSX contained text
* save point
* prettier
* Reverting change to check SOF and EOF
* Removing console.log
* Fixing grouping
* Adding comment output
* Basic comments
* Printing comments
* Getting comments to work
* Adding in basic handling of graphql comments
* Removing parser normalization (i assume it'll be added with language support)
* Lint error
* Reverting special cases
* Dealing with off-by-one error
* Format JSON literals like object literals
Following up on commit a42db30 (https://github.com/prettier/prettier/issues/2173),
this change makes it so that literals in JSON files are formatted as
they would be in an object literal. This normalizes trailing zeroes,
exponential notation, unnecessarily escaped characters, etc.
It works by using babylon to parse literals as expressions, and putting
the result in the AST instead of a `json-literal` node. Since JSON
string literals have to be double-quoted, there's a new
`__prettier__isJson` flag on each node, which is used by the printer to
force double-quotes.
* json: Check options.parser when formatting StringLiteral
This is instead of having a custom AST property. See
https://github.com/prettier/prettier/pull/2181#discussion_r122578251
* multiparser: Use babylon.parseExpression
See https://github.com/prettier/prettier/pull/2181#discussion_r122578354
* json: Don't split result AST node into two
See https://github.com/prettier/prettier/pull/2181#discussion_r122578499
* json: Don't use babylon parser to build expression nodes
See https://github.com/prettier/prettier/pull/2181#discussion_r122579052
* Revert "multiparser: Use babylon.parseExpression"
This reverts commit 3d7a4362880d39fac0cea88b5d0186a29538cf6f.
* Remove unused parameter from babylon parser
* json: Remove unused constructorTypes