lit / lit/lit

[ts-transformers] Some comments are not preserved

Open
#2,158 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
21.8k
Forks
1.1k
Avg merge
18h 25m
Merged PRs (30d)
2

Description

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:

```ts
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 https://github.com/microsoft/TypeScript/issues/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 */
```

Contributor guide

Open the contributing guide

Research direction

The issue does not name a file, test, or transformer entry point. Start by reproducing the class example and inspect the TypeScript transformer handling of class-node text and comments; done means trailing comments such as `playground-fold-end` are preserved without hiding following code.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.