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
[ts] Allow ...<...>
followed by newline or binary operator
#14636
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ import { | |
type TokenType, | ||
tokenIsTemplate, | ||
tokenCanStartExpression, | ||
tokenIsBinaryOperator, | ||
} from "../../tokenizer/types"; | ||
import { types as tc } from "../../tokenizer/context"; | ||
import * as N from "../../types"; | ||
|
@@ -66,12 +65,6 @@ function assert(x: boolean): void { | |
} | ||
} | ||
|
||
function tsTokenCanStartExpression(token: TokenType) { | ||
// tsc considers binary operators as "can start expression" tokens: | ||
// https://github.com/microsoft/TypeScript/blob/eca1b4/src/compiler/parser.ts#L4260-L4266 | ||
return tokenCanStartExpression(token) || tokenIsBinaryOperator(token); | ||
} | ||
|
||
type ParsingContext = | ||
| "EnumMembers" | ||
| "HeritageClauseElement" | ||
|
@@ -2429,11 +2422,11 @@ export default (superClass: Class<Parser>): Class<Parser> => | |
} | ||
|
||
const typeArguments = this.tsParseTypeArgumentsInExpression(); | ||
if (!typeArguments) throw this.unexpected(); | ||
if (!typeArguments) return; | ||
|
||
if (isOptionalCall && !this.match(tt.parenL)) { | ||
missingParenErrorLoc = this.state.curPosition(); | ||
throw this.unexpected(); | ||
return; | ||
} | ||
|
||
if (tokenIsTemplate(this.state.type)) { | ||
|
@@ -2469,19 +2462,17 @@ export default (superClass: Class<Parser>): Class<Parser> => | |
return this.finishCallExpression(node, state.optionalChainMember); | ||
} | ||
|
||
// TODO: This doesn't exactly match what TS does when it comes to ASI. | ||
// For example, | ||
// a<b> | ||
// if (0); | ||
// is not valid TS code (https://github.com/microsoft/TypeScript/issues/48654) | ||
// However, it should correctly parse anything that is correctly parsed by TS. | ||
const tokenType = this.state.type; | ||
if ( | ||
tsTokenCanStartExpression(this.state.type) && | ||
this.state.type !== tt.parenL | ||
// a<b>>c is not (a<b>)>c, but a<(b>>c) | ||
tokenType === tt.gt || | ||
// a<b>c is (a<b)>c | ||
(tokenType !== tt.parenL && | ||
tokenCanStartExpression(tokenType) && | ||
!this.hasPrecedingLineBreak()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also have this test (still passing) which is relevant: https://github.com/babel/babel/tree/main/packages/babel-parser/test/fixtures/typescript/type-arguments/call-newline |
||
) { | ||
// Bail out. We have something like a<b>c, which is not an expression with | ||
// type arguments but an (a < b) > c comparison. | ||
throw this.unexpected(); | ||
// Bail out. | ||
return; | ||
} | ||
|
||
const node: N.TsInstantiationExpression = this.startNodeAt( | ||
|
40 changes: 40 additions & 0 deletions
40
...abel-parser/test/fixtures/typescript/type-arguments/instantiation-expression-asi/input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const x5 = f<true> | ||
let yy = 0; | ||
|
||
const x6 = f<true> | ||
interface I {} | ||
|
||
let x10 = f<true> | ||
this.bar() | ||
|
||
let x11 = f<true> | ||
function bar() {} | ||
|
||
let x12 = f<true> | ||
class C {} | ||
|
||
let x13 = f<true> | ||
bar() | ||
|
||
let x14 = f<true> | ||
void bar() | ||
|
||
class C1 { | ||
static specialFoo = f<string> | ||
static bar = 123 | ||
} | ||
|
||
class C2 { | ||
public specialFoo = f<string> | ||
public bar = 123 | ||
} | ||
|
||
class C3 { | ||
private specialFoo = f<string> | ||
private bar = 123 | ||
} | ||
|
||
class C4 { | ||
protected specialFoo = f<string> | ||
protected bar = 123 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linked issue has been fixed.