Calling `.remove()` removes comment which shouldn't be removed
See original GitHub issueHi there! This is my first try to use ts-simple-ast
. We would like to convert certain interfaces to types. But in some cases “trailing” comments are removed and we don’t know why.
Given the following example:
{
"name": "ts-ast-test",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "ts-node index.ts"
},
"dependencies": {
"ts-simple-ast": "^11.2.1"
},
"devDependencies": {
"ts-node": "^6.0.5"
}
}
import Project, { TypeAliasDeclarationStructure } from 'ts-simple-ast';
const project = new Project();
const example = `interface Foo {
foo: string;
}
// foo
const foo = 1;`;
const file = project.createSourceFile('example.ts', example);
const interfaces = file.getInterfaces();
interfaces.forEach((myInterface) => {
const typeAlias: TypeAliasDeclarationStructure = {
name: myInterface.getName(),
type:
'{' +
myInterface.getMembers().map(p => p.getText()).join(' ') +
'}'
};
file.insertTypeAlias(myInterface.getChildIndex(), typeAlias);
myInterface.remove();
});
console.log(file.getText());
The output is this:
type Foo = {foo: string;};
const foo = 1;
As you can see the comment // foo
was removed. I wonder why? When I paste the same example to https://astexplorer.net/ with TypeScript selected as the parser it shows that the comment is not a trailing comment of interface Foo
, but a leading comment to const foo = 1;
.
So is this a bug that the comment will be removed when myInterface.remove();
is called?
Thank you.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
Removing Comments - DEV Community 👩💻👨💻
I tend to err on the side of eliminating comments, for several reasons. First, if the code is clear, and uses good type...
Read more >Removing all types of comments in java file - Stack Overflow
I have a java project and i have used comments in many location in various java files in the project. Now i need...
Read more >Insert or delete a comment - Microsoft Support
Right-click the comment, and choose Delete Comment. To delete all the comments in the document, go to the Review tab, click the down-arrow...
Read more >Why is it wrong to comment out code and then gradually ...
Leaving commented code in your code base is a bad practice but that is not what you are doing if you work through...
Read more >How To Delete All Comments in Excel - Alphr
Open a context menu with right-click, then click Delete Comment. Using the “Go To” option removes all comments from your current worksheet ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is awesome. I’ll test it. Thank you very much for the quick fix and the release.
Fixed in 11.2.2. Let me know if you run into any other issues.