* 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`
Fixes#4605
The AST here is actually two `html`s and it did follow the [CommonMark spec](https://spec.commonmark.org/0.28/#html-blocks), so I ended up with respecting the blank line between block `html`s.
* Add various tests for ts type annotated function properties.
* Improve format when passing ts arrow function with return type
* Add test for case from #2581.
We landed a change that added a new `InterpreterDirective` AST node type for hashbangs, and no longer add it as a comment/leadingComment.
Ref: https://github.com/babel/babel/pull/7928
I mimicked what we do in `@babel/generator` here, since I found it better than trying to add comments to the ast in `parser-include-shebang.js`). Definitely open to a better/cleaner option though!
Note: I'll follow this up with enabling tests for https://github.com/prettier/prettier/pull/4543 and https://github.com/prettier/prettier/pull/4540 once they land too.
* Support new node types for optionals
* Update babylon
* Fix decorator tests
* fix manual errors
* Update flow
* pretty print
* enable option for flow to support ??
* fix AST_COMPARE=1
* fix lint and explicitly test 1_2_3 syntax for flow
* Don't default parser to babylon
* Different default values according to version
* Fix error logging
* Add tests specifically for parser inference
* Add another test case
* Add API test
* Break link definitions onto multiple lines when needed
* Simplify the conditional for breaking the title
* Don’t print the title if it’s blank
* Revert "Don’t print the title if it’s blank"
This reverts commit 2a8d0dd9995a2e5ab8e6d888c07b88dda058637f.
* Add another test
* Fix title printing
* Second time’s the charm
* Third time’s the charm?
* Fix snapshot
* Prettify
* Don’t break unless `proseWrap` is `always`
* Test `proseWrap: never` on link references
* Fix plugin resolution when path does not start with ./
* Minor consistency fix: use "if (resolvedPluginSearchDir) {...}" in load-plugins
* Use more appropriate quotes in plugin-resolution.js
* Try both local paths and node modules when resolving plugins manually
* Test bespoke plugin loading by node module name
* Simplify getFileInfo() as suggested by @j-f1
https://github.com/prettier/prettier/pull/4341#discussion_r187112707
* Implement prettier.getFileInfo() method and --file-info CLI option
* Add empty line between functions in index.js
* Support --plugin-search-dirs / pluginSearchDirs() in --file-info / getFileInfo()
* Address review comments by @ikatyang
* Fix plugin API in globally installed Prettier and introduce optional --plugin-search-dir
* Use find-parent-dir instead of find-up and test autoloading (with mocked fn)
* Add two test cases where --plugin-search-dir is not .
* Do not mutate pluginSearchDirs argument in load-plugins.js
* Do not test automatic plugin resolution as mocking of "find-parent-dir" does not work due to rollup
* Document --plugin-search-dir / pluginSearchDirs and improve spacing
* Address @ikatyang's review comments
* Fix require path for third-party
* Undo alphabetic sorting of third-party scripts
* Added support for the graph ql comment tag
As described in the issue https://github.com/prettier/prettier/issues/4360
Prettier still adds a space inbetween the comment tag and the template literal.
* Added comment explaining the random spaces around GraphQL
* Fixed failing AST_COMPARE tests
* Add tests showing existing behavior of various TS casts.
* Improve formatting of TS casts with generics and unions.
By adding a soft break between the cast and the expression. Also
includes wrapping parentheses for clarity as suggested in #4171.
Avoids changing behavior at all though if casting an array or object
literal because those already have good behavior where the array or
object literal breaks before the cast does so including them would just
result in a pointless extra layer of parentheses that would add no
clarity.
* Update tests in prepation for updating long cast format.
* Update so nested breaks inside type cast are indented.
* Add tests where expression being cast is too long itself.
* Update cast formatting to only sometimes break after cast.
Specifically it will break after the cast if that makes the cast itself
fit on a single line. If the cast itself won't fit on a single line then
the expression being cast will be placed directly after the `>` at the
end of the cast.
* Add heuristic to format functional composition more nicely
* Regex -> array
* Use object instead of array for faster lookup
* Remove empty lines
* Add redux connect
Also add comments for libraries
* handle cusor position relative to comments correctly
* use more robust method of cursor offset tracking
* add support for cursorOffset with rangeStart/End
* add tests for cursorOffset
* use old JS syntax for node4
* add another cursor offset test case
* remove trailing whitespace in cursor tests
* fix null findNodeAtOffset handling
* remove dead code
* add more test cases
* use indexOf for node4
* revert comments.js
* add more comment tests
* change tests infra
* Fix AST_COMPARE
* move CURSOR to top level of file
* mutate result
* use es6 property shorthand
* simplify diff offset calculation
* put remaining test cases in individual files
* Fix: Always preserve frontmatter, remove dependency
* Move correct test to correct filename
* Add test for malformed frontmatter, finetune regex
* Use other malformed test case
* Update yarn.lock
* fix(glimmer): preserve leading/trailing spaces in text nodes underneath attribute nodes when next to mustache statements.
* fix(glimmer): format printer-glimmer.js
* fix(glimmer): lint fix
* fix(glimmer): removed decomposition assignments to be compatible with Node 4
* fix(glimmer): Array.includes() -> indexOf() for Node 4
* feat(typescript): support generic JSX element
* fix(playground): `buffer is not defined` in `--parser typescript`
* chore(deps): update typescript-eslint-parser
* d3 counts as a constructor
* adds test
* Tweak wording
* docs: fix linting
* updates test
* no $ special case
* renames function
* shorter function name
* removes trailing white space
* feat(markdown): support hasPragma/insertPragma
* test: add require-pragma tests
* feat: support pragma with front matter
* feat: support pragma in multiline comment
* fix: don't lowercase custom selectors variable name #4254
* proper fix for camelCased custom selector
* Move customs-selectors case snapshots to correct place
* Allow new interface style for GraphQL.
This is a breaking change since it will upgrade the old style to the new one.
Closes#3600.
* Allow old interface style.
When in present of mixed style, it updates to the new one.
* feat: enable plugin-specific comment functionality
This change enables language plugins to provide their own "handleComment" function. To do so, we're exposing the addLeadingComment, addTrailingComment, and addDangling comment functions which attach comment nodes correctly in the ast which will get handled by prettier core when printing.
* moving js comment functionality to plugin