Skip to content

[ts-transformers] Some comments are not preserved #2158

Open
@aomarks

Description

@aomarks

There are a few locations for comments where TypeScript's normal behavior is to drop the comment entirely.

One place this can happen is at the end of a class declaration.

For example, in this case the trailing fold-end comment will be dropped, meaning any code after it will be unintentionally hidden when displayed in a Playground:

class MyElement extends LitElement {
  /* playground-fold */
  render() {
    return html`Hello`;
  }
  /* playground-fold-end */
}

// some code that shouldn't be hidden

This is a known issue with TypeScript, but which has been decided should not be fixed (see microsoft/TypeScript#32813).

The core issue is that TypeScript does not represent comments as first-class nodes in the AST, rather they only exist if they are associated as a leading or trailing comment of some node. In this case, the /* playground-fold */ comment is a leading comment of the render method, but the /* playground-fold-end */ comment has no node to be associated with.

There might be something we can do in the transformers to at least partially recover these comments, though. For example, the /* playground-fold-end */ comment above could be detected by manually scanning the text of the class node for a trailing comment, and then re-attaching it as a trailing comment of the render function -- as though it had been written as:

render() { ... } /* playground-fold-end */

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Status

    📋 Triaged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions