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

Issue when trying to set the question token on a function parameter with no explicit type #366

Closed
gregjacobs opened this issue Jul 17, 2018 · 3 comments
Labels

Comments

@gregjacobs
Copy link

Hey @dsherret. So my latest utility is to convert a js codebase to ts (and it's open source this time so you can actually see it! https://github.com/gregjacobs/js-to-ts-converter)

I'm basically trying to add the question token to function parameters that I know are optional, but I'm getting an error when trying to do so with parameters that don't yet have an explicit type. Take a look at this minimal repoduction:

import Project, { FunctionDeclaration, Identifier, Node, PropertyAccessExpression, SyntaxKind, TypeGuards } from 'ts-simple-ast';

const project = new Project();
const sourceFile = project.createSourceFile( 'my-file.ts', `
	function myFn( a, b ) {}  // note: no types on these parameters
` );

const myFn: FunctionDeclaration = sourceFile.getDescendants()
	.find( ( node: Node ): node is FunctionDeclaration => {
		return TypeGuards.isFunctionDeclaration( node )
	} )!;

const params = myFn.getParameters();

params[ 0 ].setHasQuestionToken( true );

On that last line, I get this error:

(21:56:00) ~/dev/js-to-ts-converter (master) $ ts-node test.ts

/Users/gjacobs/dev/js-to-ts-converter/node_modules/ts-simple-ast/dist/errors/BaseError.js:7
        var _this = _super.call(this, message) || this;
                           ^
Error: A child of the kind ColonToken was expected.
    at InvalidOperationError.BaseError [as constructor] (/Users/gjacobs/dev/js-to-ts-converter/node_modules/ts-simple-ast/dist/errors/BaseError.js:7:28)
    at new InvalidOperationError (/Users/gjacobs/dev/js-to-ts-converter/node_modules/ts-simple-ast/dist/errors/InvalidOperationError.js:8:28)
    at Object.throwIfNullOrUndefined (/Users/gjacobs/dev/js-to-ts-converter/node_modules/ts-simple-ast/dist/errors/helpers.js:96:15)
    at ParameterDeclaration.Node.getFirstChildByKindOrThrow (/Users/gjacobs/dev/js-to-ts-converter/node_modules/ts-simple-ast/dist/compiler/common/Node.js:1109:23)
    at ParameterDeclaration.QuestionTokenableNode.class_1.setHasQuestionToken (/Users/gjacobs/dev/js-to-ts-converter/node_modules/ts-simple-ast/dist/compiler/base/QuestionTokenableNode.js:32:38)
    at Object.<anonymous> (/Users/gjacobs/dev/js-to-ts-converter/test.ts:15:13)
    at Module._compile (module.js:635:30)
    at Module.m._compile (/usr/local/lib/node_modules/ts-node/src/index.ts:422:23)
    at Module._extensions..js (module.js:646:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/usr/local/lib/node_modules/ts-node/src/index.ts:425:12)

It seems like I can add an explicit any type for the moment as a workaround, but figure this should work correctly too :)

@dsherret
Copy link
Owner

@gregjacobs you're correct! Thanks again for reporting this and sorry about that. Fixed and now published in 12.6.2.

@dsherret
Copy link
Owner

By the way, that project sounds really cool. I will check it out tomorrow though... going to sleep now :)

@gregjacobs
Copy link
Author

Awesome, thanks! Much appreciated! Catch ya tomorrow :)

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