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

Adding type to param of non-parenthesized arrow function throws error #374

Closed
gregjacobs opened this issue Jul 24, 2018 · 4 comments
Closed
Labels

Comments

@gregjacobs
Copy link

Now this one might be a bit of a challenge...

I tried to add a type annotation to a parameter of an arrow function where the arrow's single parameter is not surrounded by parenthesis:

myArg => console.log( myArg );

Here's my reproduction of the error:

import Project, { ArrowFunction, SyntaxKind } from 'ts-simple-ast';

const project = new Project();
const sourceFile = project.createSourceFile( 'my-file.ts', `
	myArg => console.log( myArg );
` );


const arrowFunction = sourceFile
	.getDescendantsOfKind( SyntaxKind.ArrowFunction )[ 0 ] as ArrowFunction;

const arrowParam = arrowFunction.getParameters()[ 0 ];
arrowParam.setType( 'any' );

Which results in:

Error: Error replacing tree! Perhaps a syntax error was inserted (Current: ExpressionStatement -- New: LabeledStatement).
-- Details --
Path: my-file.ts
Text: "\n\tmyArg: any => console.log( myArg );\n"

I think it may be ending up with this output text after the replacement:

myArg: any => console.log( myArg );

Rather than:

(myArg: any) => console.log( myArg );
@dsherret dsherret added the bug label Jul 24, 2018
@dsherret
Copy link
Owner

Nice, I like this bug! It shows the benefit of the library and how it can abstract away these problems. I'll fix this tomorrow evening.

@gregjacobs
Copy link
Author

Hehe, sounds good! And yes, that is a good call and a huge benefit of ts-simple-ast!

Thanks for taking a look :)

@dsherret
Copy link
Owner

Hey @gregjacobs, this is fixed in 12.7.2. Also fixed for setInitializer and setHasQuestionToken.

Let me know if you run into or think of any more scenarios. Thanks!

@gregjacobs
Copy link
Author

Very nice, thanks man! Nice implementation too 😄

Appreciate it!

dsherret added a commit that referenced this issue Jul 25, 2018
…forgotten when adding parens.

Fix using the wrong word in an internal comment.
dsherret added a commit that referenced this issue May 14, 2019
…setting its initializer, type, or adding a question token.
dsherret added a commit that referenced this issue May 14, 2019
…forgotten when adding parens.

Fix using the wrong word in an internal comment.
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

2 participants