Skip to content
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

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

Closed
donaldpipowitch opened this issue May 31, 2018 · 5 comments
Closed
Labels

Comments

@donaldpipowitch
Copy link

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.

@dsherret dsherret added the bug label May 31, 2018
@dsherret
Copy link
Owner

Hi @donaldpipowitch, yes this is a bug and thanks for reporting this! That comment is not owned by the interface. In the beginning of this library comments were an afterthought so there are still a few areas like this that aren't correct.

@cancerberoSgx
Copy link
Contributor

@dsherret - just in case, this is the difference between ts.Node getText vs getFullText - and the same for other methods like getStart() getFullStart() getFullWidth(), etc - if internally the library use "Full" methods instead of non full then those trailing comments (tribia content as they call it) will be considered. - if not it will be discarded. My two cents.

@dsherret
Copy link
Owner

@cancerberoSgx thanks, but no it's not related to that. I fixed a similar issue to this before so I'll have this fixed soon.

@dsherret
Copy link
Owner

dsherret commented Jun 1, 2018

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

@donaldpipowitch
Copy link
Author

donaldpipowitch commented Jun 1, 2018

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

dsherret added a commit that referenced this issue May 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants