Comments (1)
Looking into the Doc object produced, I see the following (I've discarded extra []
-brackets for clarity):
{
"type": "fill",
"parts": [
"DROP",
{ "type": "line" },
"TABLE",
{ "type": "line-suffix", "contents": [" ", "-- comment 1"] },
{ "type": "break-parent" },
{ "type": "line" },
"-- comment 2",
{ "type": "line", "hard": true },
{ "type": "break-parent" },
"table1",
", ",
"table2"
]
}
This explains why the comments order ends up changing:
- the first comment is added as a line-suffix, so it ends up floating to the end of the line
- the second comment is added normally.
Although there are multiple line
s, only the last hardline
ends up causing an actual newline.
Another example which just uses join(" ", ...)
:
[
"DROP",
" ",
"TABLE",
{ "type": "line-suffix", "contents": [" ", "-- comment 1"] },
{ "type": "break-parent" },
" ",
"-- comment 2",
{ "type": "line", "hard": true },
{ "type": "break-parent" },
"table1",
", ",
"table2"
]
Here there's only one line
which occurs after the second comment, so there's no other place for the comments to go than to the end of this one line.
I tried another test input of:
DROP -- comment 1
TABLE -- comment 2
table1 -- comment 3
which produces the following Doc:
[
"DROP",
{ "type": "line-suffix", "contents": [" ", "-- comment 1"] },
{ "type": "break-parent" },
" ",
"TABLE",
{ "type": "line-suffix", "contents": [" ", "-- comment 2"] },
{ "type": "break-parent" },
" ",
"table1"
{ "type": "line-suffix", "contents": [" ", "-- comment 3"] },
{ "type": "break-parent" },
]
and the following output:
DROP TABLE table1 -- comment 1 -- comment 2 -- comment 3
I think I now understand what the automatic comment-insertion expects:
- every node, to which a comment is attached, must be able to break so that there's a newline after it, to the end of which a comment could be placed.
I'll need to either limit the nodes to which comments can be attached, or I need to make every node breakable when there's a comment on it.
from prettier.
Related Issues (20)
- Non-idempotent and otherwise buggy behavior around intersections and line comments
- Feature request: dark theme for https://prettier.io/ HOT 3
- Add support for `#graphql` comment for formatting GraphQL HOT 3
- Non-idempotent formatting of Angular templates (new lines being inserted on each run) HOT 1
- yaml with folded multiline string and "--prose-wrap always" improperly wraps (and is not idempotent) HOT 3
- documentation request: yaml and prose-wrap
- Enforce consistent use of file extensions in import statements HOT 1
- Unstable formatting of javascript method chains HOT 1
- Dependencies required by scripts/build/utils.js are not reflected in package.json HOT 1
- Give us the option to only apply a plugin on a file HOT 1
- Prettier breaks vue2 syntax when using filters in template
- [MDX] Does not keep indentation in multiline strings HOT 1
- Issue when running prettier for any APEX class in my project HOT 1
- Range formatting doesn't work
- VSCode prettier extension and the `prettier --write .` giving two different results HOT 2
- In less file, automatically insert space between ) and ` HOT 2
- prettier leaves "prettier-ignore" statements in code HOT 3
- CLI: Exit code is 0 when --check is specified and "no parser could be inferred" (since version 3.1) HOT 1
- Ignore CDATA in HTML or option to ignore it HOT 3
- Prettier adding trailing comma after implicit returns and after last param in function call in vue 2 projects HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prettier.