Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mishandling of trailing comments #28

Closed
Conduitry opened this issue Jan 24, 2020 · 3 comments · Fixed by #29
Closed

Mishandling of trailing comments #28

Conduitry opened this issue Jan 24, 2020 · 3 comments · Fixed by #29

Comments

@Conduitry
Copy link
Collaborator

Trailing comments have some buggy handling, which most of the time just results in them being attached to the wrong line in the generated code, which isn't a huge deal, but there are also cases where they can result in syntactically incorrect output.

const cr = require('code-red');
const foo = cr.b`function foo() {
// hey1
/*
hey2
*/
}`;

->

[
  Node {
    type: 'FunctionDeclaration',
    start: 0,
    end: 37,
    id: Node { type: 'Identifier', start: 9, end: 12, name: 'foo' },
    expression: false,
    generator: false,
    async: false,
    params: [],
    body: Node {
      type: 'BlockStatement',
      start: 15,
      end: 37,
      body: [],
      trailingComments: [ { type: 'Line', value: ' hey1', start: 17, end: 24 } ]
    },
    trailingComments: [ { type: 'Block', value: '\nhey2\n', start: 25, end: 35 } ]
  }
]
console.log(cr.print(foo).code);

->

function foo() {

} // hey1 /*
hey2
*/

The comments end up outside the function block, but more seriously, the block comment ends up starting on the same line as the line comment.

Ref. sveltejs/svelte#4312

@vipero07
Copy link

Would it be a bad idea for the compiler to ignore comments? Considering minification results in removal of the comments.

@Conduitry
Copy link
Collaborator Author

The comments can be important - partial support for preserving them was recently added for sveltejs/svelte#4268

Rich-Harris added a commit that referenced this issue Jan 27, 2020
prevent syntax errors when combining comments
@Rich-Harris
Copy link
Owner

Fixed in 0.0.32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants