babel / babel/babel

Comments are stripped when all code from a file is removed by @babel/plugin-transform-typescript

Open
#11,803 1 comment 0 reactions 0 assignees View on GitHub
i: needs triage
Dominant language
TypeScript
Stars
44k
Forks
6k
Avg merge
5d 15h
Merged PRs (30d)
23

Description

## Bug Report

- [ ] I would like to work on a fix!

**Current behavior**
When processing a file where all its contents is removed during transpilation, comments are also removed. If some code is kept, comments are kept.

- [REPL](https://babeljs.io/repl#?browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=PQKhCgAIUgBAXAngBwKYBNUDNIG8D2ARgFaoDG8AvpAK4DOqATlDLMo_mo0nnfIwEsAdgHNqAbQHoAui2DhUAD2T5ukYfCZYAhmVSQAqg0Z5wASCkB-AFyQ-g0QG5wlIA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=false&presets=typescript&prettier=false&targets=&version=7.10.4&externalPlugins=)

But if you add any additional code to the file it works in the exepcted way:
- [REPL](https://babeljs.io/repl#?browsers=&build=&builtIns=false&spec=false&loose=false&code_lz=PQKhCgAIUgBAXAngBwKYBNUDNIG8D2ARgFaoDG8AvpAK4DOqATlDLMo_mo0nnfIwEsAdgHNqAbQHoAui2DhUAD2T5ukYfCZYAhmVSQAqg0Z5wASCkB-AFyQ-g0QG5wlKJHfgy-IX0iLHQA&debug=false&forceAllTransforms=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=false&presets=typescript&prettier=false&targets=&version=7.10.4&externalPlugins=)

**Input Code**

```ts
/**
* @typedef {object} user
* @property {string} [id]
*/
export interface User {
id?: string;
}
```

**Expected behavior**
Comments are kept even when all the content is stripped.

**Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)**
- Filename: `babel.config.js`

```js
{
plugins: [
'@babel/plugin-transform-typescript',
],
}
```

**Environment**

- Babel version(s): 7.10.4
- How you are using Babel: Reproduced in babel repl

Contributor guide

Open the contributing guide

Research direction

Start with the @babel/plugin-transform-typescript reproduction in the Babel REPL, using the provided TypeScript input and babel.config.js configuration. Trace how comments are handled when the interface is removed, then verify that the comment remains when all code is stripped and that the existing case with retained code still behaves as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
babel, typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.