Skip to content

factory: getLeftmostExpression needs to handle more kinds #25223

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

Closed
ajafff opened this issue Jun 26, 2018 · 4 comments
Closed

factory: getLeftmostExpression needs to handle more kinds #25223

ajafff opened this issue Jun 26, 2018 · 4 comments
Labels
API Relates to the public API for TypeScript Bug A bug in TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this

Comments

@ajafff
Copy link
Contributor

ajafff commented Jun 26, 2018

TypeScript Version: 3.0.0-dev.20180621

Search Terms:

Code

import * as ts from 'typescript';
const n = ts.createStatement(ts.createAsExpression(ts.createObjectLiteral(), ts.createTypeReferenceNode('any', undefined)));
ts.createPrinter().printNode(ts.EmitHint.Unspecified, n, ts.createSourceFile('dummy.ts', '', ts.ScriptTarget.Latest));

Expected behavior:

({} as any);

The same applies to NonNullExpression.

Actual behavior:

Invalid code, this is a syntax error: {} as any;

@mhegazy mhegazy added Bug A bug in TypeScript Help Wanted You can do this API Relates to the public API for TypeScript labels Jun 30, 2018
@mhegazy mhegazy added this to the Community milestone Jun 30, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Jun 30, 2018

PRs welcomed.

ajafff added a commit to ajafff/TypeScript that referenced this issue Jun 30, 2018
Also parenthesizes AsExpression in `createNew`

Fixes: microsoft#25223
ajafff added a commit to ajafff/TypeScript that referenced this issue Jun 30, 2018
@DanielRosenwasser
Copy link
Member

I'm surprised we're even making transformations with the printer. Are you sure you're not running this through a transform first?

@ajafff
Copy link
Contributor Author

ajafff commented Jul 1, 2018

@DanielRosenwasser the printer doesn't do anything special here. It's the factory function (in this case createStatement) that adds parentheses if necessary. Except for the example above where no parens are added.

Instead of using the printer, one could also just use isParenthesizedExpression(n.expression)

@DanielRosenwasser
Copy link
Member

Gotcha, thanks!

@mhegazy mhegazy modified the milestones: Community, TypeScript 3.0 Jul 5, 2018
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Jul 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Relates to the public API for TypeScript Bug A bug in TypeScript Fixed A PR has been merged for this issue Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

3 participants