Coder Social home page Coder Social logo

Comments (1)

nene avatar nene commented on May 22, 2024

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 lines, 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.