question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling `.remove()` removes comment which shouldn't be removed

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
donaldpipowitchcommented, Jun 1, 2018

This is awesome. I’ll test it. Thank you very much for the quick fix and the release.

2reactions
dsherretcommented, Jun 1, 2018

Fixed in 11.2.2. Let me know if you run into any other issues.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found