Skip to content

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

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

Open
aomarks opened this issue Sep 14, 2021 · 0 comments
Open

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

aomarks opened this issue Sep 14, 2021 · 0 comments

Comments

@aomarks
Copy link
Member

aomarks commented Sep 14, 2021

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 */
@aomarks aomarks self-assigned this Sep 14, 2021
aomarks added a commit to lit/lit.dev that referenced this issue Sep 14, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
- Replace home-page-specific language toggle with the new global one.

- Replace the separate JavaScript vs TypeScript "Hello World" Playground examples with a unified one. I'm keeping the JS output hand-rolled for this example for now, because the `static properties` block isn't in the perfect position, and this is a very prominent example. Will fix in lit/lit#2159 as followup.

- Fix home page highlight positions when in JS mode. This actually broke in #495 where I forgot to update the ranges for the yellow-highlight effect.

- Added a check for generated samples that have lost or re-ordered playground fold/hide comments. This should help detect weird cases that might need hand-rolling going forward.

- Hand-roll two examples that are losing fold comments, and move one comment to be trailing so that it's not lost, that I found via the above check. Filed lit/lit#2158 to hopefully address in the transformers in the future.

- Remove some unnecessary TypeScript vs JavaScript language, and add a few more switchable samples.

- Remove `jsSamples` mod feature flag to launch JS docs.

Fixes #332
Fixes #490
@aomarks aomarks moved this to Todo in Lit Project Board Jan 24, 2022
@aomarks aomarks moved this from Todo to Needs Discussion in Lit Project Board Jan 24, 2022
@aomarks aomarks moved this from Needs Discussion to Todo in Lit Project Board Jan 24, 2022
@aomarks aomarks moved this from 🔥 Front Burner to 🧊 Icebox in Lit Project Board Feb 10, 2022
@aomarks aomarks moved this from 🧊 Icebox to 📋 Triaged in Lit Project Board Jun 16, 2022
@aomarks aomarks removed their assignment Jun 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 📋 Triaged
Development

No branches or pull requests

1 participant