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 */

Activity

self-assigned this
on Sep 14, 2021
added a commit that references this issue on Sep 14, 2021
moved this from Todo to Needs Discussion in Lit Project Boardon Jan 24, 2022
moved this from Needs Discussion to Todo in Lit Project Boardon Jan 24, 2022
moved this from 🔥 Front Burner to 🧊 Icebox in Lit Project Boardon Feb 10, 2022
moved this from 🧊 Icebox to 📋 Triaged in Lit Project Boardon Jun 16, 2022
removed their assignment
on Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @aomarks

        Issue actions

          [ts-transformers] Some comments are not preserved · Issue #2158 · lit/lit