The issue here is that less parser somehow included CRs in `comment.raws.content`, but it was hidden by the wrong trailing space elimination previously, which was fixed by #5165.
We already have such tests but it's not reproducible in AppVeyor since they use LF ([`core.autocrlf=input`](https://stackoverflow.com/a/20653073)).
Fixes#5323. Previously we added space after `/*` _on the first line
only,_ but the intent is to only fix the indentation of comments, not
changing their "contents". Besides, this was inconsistent with the
handling of every following line. Finally, it broke `/*!` comments which
some minifiers look for to know which (license) comments to keep.
People can use https://eslint.org/docs/rules/spaced-comment to enforce
when to start comments with spaces.
* fix(css-in-js) Keep newlines in CSS-in-JS Templates (Fixes: #5147)
* Add more tests
* Do not add semicolon when CSS prop is a template variable
* Fix deletion of original semicolon
* Prettify long line
* Add support for flow typecast comments
* Allow whitespace between comment start and colon
Also rename flow-comments.js to utils.js
* fix bug where type annotations could be accidentally created
* use better regex in detecting flow comment whitespace
* fix comment in utils.js
* simplify conditionals and improve comments
* fix lint error
- whitespace-sensitive formatting
- respect css `display: block`/`white-space: pre` (data from [`html-styles`](https://github.com/marionebl/html-styles))
- support magic comment (`<!-- display: block -->`)
- add an option to specify whitespace sensitivity (`--html-whitespace-sensitivity <css|strict|ignore>`)
- magic comments take precedence
- (default) `css`: respect default css style (safe in the most cases)
- `strict`: every node is considered whitespace sensitive (the safest)
- `ignore`: every node is considered whitespace insensitive (dangerous, the original behavior)
- inline inline-tags
- support ie conditional comment (`<!--[if IE]><![endif]-->`)
- indent the script/style content
- no inconsistent output for 2+ attributes
- force break tag if there're multiline attributes
Fixes#5238.
cc @duailibe for the [original change](https://github.com/prettier/prettier/pull/4413). I basically undid that PR because it didn't look to me like `shouldFlatten` made sense as the place to introduce the behavior it was going for, but I might have misunderstood something.
Instead of showing 80 carets, point out just which line the error was
on, and rely on the original message to point out the "column".
I thought about matching the `----^` in the original message with a
regex and use the length of it as the column number, but unfortunately
the original message does not show the original code (in this example
some whitespace is missing), so it wouldn't work.
Before:
```
$ ./bin/prettier.js test.hbs --parser glimmer
[error] test.hbs: SyntaxError: Parse error on line 2:
[error] {{one}}{{
[error] ---------^
[error] Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'EOF' (2:0)
[error] 1 | {{one}}
[error] > 2 | {{
[error] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error] 3 |
```
After:
```
$ ./bin/prettier.js test.hbs --parser glimmer
[error] test.hbs: SyntaxError: Parse error on line 2:
[error] {{one}}{{
[error] ---------^
[error] Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'EOF' (2:0)
[error] 1 | {{one}}
[error] > 2 | {{
[error] 3 |
```
- switch to [`htmlparser2`](https://github.com/fb55/htmlparser2)
- no need to fork
- won't generate pseudo nodes ([`parse5` will](https://github.com/prettier/prettier/issues/5098#issuecomment-423055181))
- should be [faster](https://github.com/fb55/htmlparser2#performance)
- support custom self-closing tags
- support HTML entities
- remove extra trailing newline for `<template>`
- distinguish empty/empty-string attributes (`<tag x>`/`<tag x="">`)
- rename `--parser parse5` with `--parser html`
- enable `html` since 1.15, which means it's enabled by default if you install the dev version from GitHub after this PR merged.
The issue here is that the `stripTrailingHardline` does not work for js/ts, I fixed it and then I realized that we always print trailing newline in every language, so there's no need to strip newline for text in script-like tag.
* feat: html support
* fix: support empty lines between children
* refactor: isScriptTagNode util
* refactor: rename `glimmer` and `handlebars` tests
* fix don't add newline after `directive` node
* fix: support `pre` tag
* fix: don't trim comments content
* fix: `script` and `style` tags output
* feat: indent `head` and `body` inside `html`
* fix: better handle quotes around attribute value
* feat: better handle embed
* tests: svg into html
* fix: better handle boolean and empty attributes
* chore(deps): update `parse5` to latest version
* fix: better handle empty attribute
* tests: fix
* fix: extra newline in multiline node
* fix: print `pre` and `textarea` in right way
* tests: xhtml doctype
* fix: use `>` instead `/>`
* tests: case
* refactor: remove `toLowerCase()`
* feat: support `prettier-ignore`
* tests: empty `script` and `style` tags
* fix: clean
* tests: refactor and more
There're always 2 files that they're source code but not in the `src` directory, which makes it hard to search.
- move the content of `index.js` and `standalone.js` to `src`.
- `/index.js` and `/standalone.js` are preserved as links to not break builds that's installed from git.
Sometimes we need to transform the ast to make it easier to print, but it's currently done in the parser (markdown and yaml), which leads the output ast harder to use for external users (custom parser), adding `printer.preprocess` can solve this issue.
And also this way we could move the trailing newline for json from `ast-to-doc.js` to `language-js`.
* don't inline pipeline operator with leading ownline comment
* Add tests for comments surrounded with binary expressions
* Add babylon test for ownlinecomment with pipeline operator
- upgrade to `yaml@1.0.0-rc.8` and `yaml-unist-parser@1.0.0-rc.4`
- refactor some logic since the AST has slightly changed (ikatyang/yaml-unist-parser#82)
- unmatched aliases are now errors since it may introduce invalid AST from `yaml`
- rewrite the document separator (`...`/`---`) logic, this fixes some cases where it can use `---` but we printed `...`
- removed some unnecessary duplicate trailing newline
- trailing comments on `document` (`... #comment`) and `documentHead` (`--- #comment`) are preserved (i.e. they won't be moved somewhere)
* Add initial support for TOML in configuration files
* Missed brace
* Fix snapshots
* refactor: move loadToml to utils
* Use @iarna/toml
* Add tests for loadToml
* Fix test for CI
* Remove jest-extended
* Create snapshot for load-toml
* Add feat to docs
* lint docs
* Use @iarna/toml/parse-string
* Change path string
If you have
```js
a = b
```
we used to do
```js
group[a = b]
```
which works most of the time but has the unfortunate side effect that if the left part breaks, we're going to break the `=` as well. So you get
```js
{
a
} = \n
b
```
What this PR does it to add a group
```js
group[a group[= b]]
```
so that if the right hand side fits in one line, it should stay that way.
Note that there's a change with a comment being move but I think that it's fine. If I remember correctly, this test was to make sure that we didn't print invalid code, not that it had to be respected (that comment position is not something we really want to support in the first place).
Fixes#4645
Only apply to `alias` and `plain`/`quoteSingle`/`quoteDouble` with
- no backslash newline
- or no literal newline and `proseWrap: preserve`
- or no newline and `proseWrap: never`
- or no whitespace and `proseWrap: always`
in mapping items with implicit key, otherwise it's impossible to do prose wrapping.
* feat(markdown): only align lists if they're already aligned
* fix: handle single item with multiple leading spaces
* fix: empty items are considered not aligned
* Use destructuring for readability and optimzations at for loop length property
* Destructure variables for easy maintainability of code
* PR review fix
* refactor: fix linting
* Use destructures and refactor code for better readability
* Remove merge conflict line
* lint before commit
* Use destructuring for readability and optimzations at for loop length property
* Destructure variables for easy maintainability of code
* PR review fix
* refactor: fix linting
In addition to a tiny performance improvement outlined below,
the CPU profile of traverseDoc is now more readable.
Also anonymous arrow functions changed to named regular functions
so that they are properly displayed in the CPU profile,
and moved to outer scope where there's no closure
so that they aren't re-created (this change's performance is dependent
on JS engine implementation and optimization details).
Before (profile):
```
7129.9 ms 5.43 % 13349.9 ms 10.18 % traverseDocRec
7067.4 ms 5.39 % 11285.5 ms 8.60 % traverseDocRec
31.5 ms 0.02 % 1031.9 ms 0.79 % traverseDoc
23.6 ms 0.02 % 12313.4 ms 9.39 % traverseDoc
2.6 ms 0.00 % 0.3 ms 0.00 % (anonymous)
1.7 ms 0.00 % 1.7 ms 0.00 % call
1.6 ms 0.00 % 1.6 ms 0.00 % call
0.5 ms 0.00 % 0.5 ms 0.00 % conditionalGroup
0.4 ms 0.00 % 0.4 ms 0.00 % printDocToString$1
0.1 ms 0.00 % 0.1 ms 0.00 % printGenerically
0.1 ms 0.00 % 0.1 ms 0.00 % t
0.1 ms 0.00 % 0.1 ms 0.00 % ifBreak
0.1 ms 0.00 % 0.1 ms 0.00 % (anonymous)
0 ms 0 % 0.1 ms 0.00 % forEach
```
After (profile):
```
6937.9 ms 5.37 % 12872.5 ms 9.97 % traverseDoc
5944.0 ms 4.60 % 11047.3 ms 8.55 % propagateBreaks
735.7 ms 0.57 % 1358.3 ms 1.05 % findInDoc
257.9 ms 0.20 % 466.7 ms 0.36 % findInDoc
0.1 ms 0.00 % 0.1 ms 0.00 % has
0.1 ms 0.00 % 0.1 ms 0.00 % printArgumentsList
```
Before (performance):
```
cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null
Debugger listening on ws://127.0.0.1:9229/4b52c027-ef62-49d6-8770-179e805a0f43
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
[debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]}
[debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier-2/LspLanguageService.js'
[debug] loaded options `null`
[debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"}
[debug] '--debug-repeat' option found, running formatWithCursor 1000 times.
[debug] '--debug-repeat' measurements for formatWithCursor: {
[debug] "repeat": 1000,
[debug] "hz": 7.774598830700336,
[debug] "ms": 128.624
[debug] }
```
After (performance):
```
cat ../LspLanguageService.js | NODE_ENV=production node --inspect-brk ./dist/bin-prettier.js --stdin-filepath LspLanguageService.js --loglevel debug --debug-repeat 1000 > /dev/null
Debugger listening on ws://127.0.0.1:9229/aa76e134-a68c-44ed-89a8-efb68bc46baa
For help see https://nodejs.org/en/docs/inspector
Debugger attached.
[debug] normalized argv: {"color":true,"editorconfig":true,"stdin-filepath":"LspLanguageService.js","loglevel":"debug","debug-repeat":1000,"plugin-search-dir":[],"plugin":[],"ignore-path":".prettierignore","config-precedence":"cli-override","_":[]}
[debug] resolve config from '/Users/ivanbabak/_sompylasar/_github/prettier/LspLanguageService.js'
[debug] loaded options `null`
[debug] applied config-precedence (cli-override): {"filepath":"LspLanguageService.js"}
[debug] '--debug-repeat' option found, running formatWithCursor 1000 times.
[debug] '--debug-repeat' measurements for formatWithCursor: {
[debug] "repeat": 1000,
[debug] "hz": 7.888114977163907,
[debug] "ms": 126.773
[debug] }
```
- `--debug-benchmark` uses `benchmark` module to produce statistically significant time measurements.
- `--debug-repeat` uses a naive loop and measures just the average run time, but useful for profiling to highlight hot functions.
* Hande JSX whitespace separately from fbt whitespace
* Extra test cases
* Further tweak to JSX handling
* Possible improvement of single character text
* More tests
* Pull out separator code
* More tests for fbt
This pull request aims to fix the issue of excessive nesting brought up in the following comment: https://github.com/prettier/prettier/issues/737#issuecomment-392541493
This "fix" is very simple. It adds a new possible way to activate JSX mode for ternary operators. If the first non-conditional parent of the expression is a JSX expression container, then JSX mode gets activated.
This branch also contains a test case for said scenario.
This allows `prettier.__debug.formatAST` to not crash when given the AST
for the below code (taken from https://astexplorer.net/), which is
useful when trying to use Prettier as a code generator for an AST
without corresponding source code (https://github.com/prettier/prettier/issues/4675)
```js
let tips = [
"Click on any AST node with a '+' to expand it",
"Hovering over a node highlights the \
corresponding part in the source code",
"Shift click on an AST node expands the whole substree"
];
function printTips() {
tips.forEach((tip, i) => console.log(`Tip ${i}:` + tip));
}
```
- preserve trailing spaces in html, excluding html comment
- `doc-printer` is now unrelated to `options.parser`
- fix some cases that two trailing spaces are mis-considered as `break`