Repository navigation
fix: Align the ASI behaviour with typescript #3053
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
Open
Changqing-JING
wants to merge
2
commits into
AssemblyScript:main
Choose a base branch
from
Changqing-JING:fix/parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -649,7 +649,7 @@ export class Parser extends DiagnosticEmitter { | |
| let parameters: TypeNode[] | null = null; | ||
|
|
||
| // Name<T> | ||
| if (tn.skip(Token.LessThan)) { | ||
| if (!tn.peekOnNewLine() && tn.skip(Token.LessThan)) { | ||
| do { | ||
| let parameter = this.parseType(tn, true, suppressErrors); | ||
| if (!parameter) return null; | ||
|
|
@@ -703,7 +703,7 @@ export class Parser extends DiagnosticEmitter { | |
| } | ||
| } | ||
| // ... [][] | ||
| while (tn.skip(Token.OpenBracket)) { | ||
| while (!tn.peekOnNewLine() && tn.skip(Token.OpenBracket)) { | ||
| let bracketStart = tn.tokenPos; | ||
| if (!tn.skip(Token.CloseBracket)) { | ||
| if (!suppressErrors) { | ||
|
|
@@ -991,7 +991,8 @@ export class Parser extends DiagnosticEmitter { | |
| } while (tn.skip(Token.Comma)); | ||
|
|
||
| let ret = Node.createVariableStatement(decorators, declarations, tn.range(startPos, tn.pos)); | ||
| if (!tn.skip(Token.Semicolon) && !isFor) this.checkASI(tn); | ||
| if (isFor) tn.skip(Token.Semicolon); | ||
| else this.parseSemicolon(tn); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -1019,7 +1020,8 @@ export class Parser extends DiagnosticEmitter { | |
| ); | ||
| } | ||
| let flags = parentFlags; | ||
| if (tn.skip(Token.Exclamation)) { | ||
| if (tn.peek() == Token.Exclamation && !tn.peekOnNewLine()) { | ||
| tn.next(); | ||
| flags |= CommonFlags.DefinitelyAssigned; | ||
| } | ||
|
|
||
|
|
@@ -1175,7 +1177,7 @@ export class Parser extends DiagnosticEmitter { | |
| } | ||
|
|
||
| let ret = Node.createReturnStatement(expr, tn.range(startPos, tn.pos)); | ||
| if (!tn.skip(Token.Semicolon)) this.checkASI(tn); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -1601,7 +1603,8 @@ export class Parser extends DiagnosticEmitter { | |
| tn.range(startPos, tn.pos) | ||
| ); | ||
| ret.overriddenModuleName = this.currentModuleName; | ||
| tn.skip(Token.Semicolon); | ||
| if (body) tn.skip(Token.Semicolon); | ||
| else this.parseSemicolon(tn); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -1669,6 +1672,13 @@ export class Parser extends DiagnosticEmitter { | |
| } | ||
|
|
||
| if (arrowKind) { | ||
| if (tn.peek() == Token.Equals_GreaterThan && tn.peekOnNewLine()) { | ||
| this.error( | ||
| DiagnosticCode.Unexpected_token, | ||
| tn.range(tn.nextTokenPos) | ||
| ); | ||
| return null; | ||
| } | ||
| if (!tn.skip(Token.Equals_GreaterThan)) { | ||
| this.error( | ||
| DiagnosticCode._0_expected, | ||
|
|
@@ -2203,7 +2213,7 @@ export class Parser extends DiagnosticEmitter { | |
| } | ||
| return null; | ||
| } | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return retIndex; | ||
| } | ||
| if (!tn.skipIdentifier(IdentifierHandling.Always)) { | ||
|
|
@@ -2367,8 +2377,10 @@ export class Parser extends DiagnosticEmitter { | |
| body, | ||
| tn.range(startPos, tn.pos) | ||
| ); | ||
| if (!(isInterface && tn.skip(Token.Comma))) { | ||
| tn.skip(Token.Semicolon); | ||
| let hasComma = isInterface && tn.skip(Token.Comma); | ||
| if (!hasComma) { | ||
| if (body) tn.skip(Token.Semicolon); | ||
| else this.parseSemicolon(tn); | ||
| } | ||
| return retMethod; | ||
|
|
||
|
|
@@ -2421,7 +2433,8 @@ export class Parser extends DiagnosticEmitter { | |
| tn.range(startPos, tn.pos) | ||
| ); | ||
| } | ||
| if (tn.skip(Token.Exclamation)) { | ||
| if (tn.peek() == Token.Exclamation && !tn.peekOnNewLine()) { | ||
| tn.next(); | ||
| flags |= CommonFlags.DefinitelyAssigned; | ||
| } | ||
| if (tn.skip(Token.Colon)) { | ||
|
|
@@ -2465,9 +2478,7 @@ export class Parser extends DiagnosticEmitter { | |
| initializer, | ||
| range | ||
| ); | ||
| if (!(isInterface && tn.skip(Token.Comma))) { | ||
| tn.skip(Token.Semicolon); | ||
| } | ||
| if (!(isInterface && tn.skip(Token.Comma))) this.parseSemicolon(tn); | ||
| return retField; | ||
| } | ||
| return null; | ||
|
|
@@ -2656,7 +2667,7 @@ export class Parser extends DiagnosticEmitter { | |
| this.seenlog.add(internalPath); | ||
| } | ||
| } | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } else if (tn.skip(Token.Asterisk)) { | ||
| if (tn.skip(Token.From)) { | ||
|
|
@@ -2672,7 +2683,7 @@ export class Parser extends DiagnosticEmitter { | |
| this.dependees.set(internalPath, new Dependee(currentSource, path)); | ||
| this.backlog.push(internalPath); | ||
| } | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } else { | ||
| this.error( | ||
|
|
@@ -2754,7 +2765,7 @@ export class Parser extends DiagnosticEmitter { | |
| range | ||
| ) | ||
| ], null, false, tn.range(startPos, tn.pos)); | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -2844,7 +2855,7 @@ export class Parser extends DiagnosticEmitter { | |
| this.dependees.set(internalPath, new Dependee(assert(this.currentSource), path)); | ||
| this.backlog.push(internalPath); | ||
| } | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } else { | ||
| this.error( | ||
|
|
@@ -2915,7 +2926,7 @@ export class Parser extends DiagnosticEmitter { | |
| if (tn.skipIdentifier()) { | ||
| let identifier = Node.createIdentifierExpression(tn.readIdentifier(), tn.range()); | ||
| let ret = Node.createExportImportStatement(identifier, asIdentifier, tn.range(startPos, tn.pos)); | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } else { | ||
| this.error( | ||
|
|
@@ -3078,7 +3089,7 @@ export class Parser extends DiagnosticEmitter { | |
| identifier = Node.createIdentifierExpression(tn.readIdentifier(), tn.range()); | ||
| } | ||
| let ret = Node.createBreakStatement(identifier, tn.range()); | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -3094,7 +3105,7 @@ export class Parser extends DiagnosticEmitter { | |
| identifier = Node.createIdentifierExpression(tn.readIdentifier(), tn.range()); | ||
| } | ||
| let ret = Node.createContinueStatement(identifier, tn.range()); | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -3140,7 +3151,8 @@ export class Parser extends DiagnosticEmitter { | |
| } | ||
|
|
||
| parseExpressionStatement( | ||
| tn: Tokenizer | ||
| tn: Tokenizer, | ||
| isFor: bool = false | ||
| ): ExpressionStatement | null { | ||
|
|
||
| // at previous token | ||
|
|
@@ -3149,7 +3161,8 @@ export class Parser extends DiagnosticEmitter { | |
| if (!expr) return null; | ||
|
|
||
| let ret = Node.createExpressionStatement(expr); | ||
| tn.skip(Token.Semicolon); | ||
| if (isFor) tn.skip(Token.Semicolon); | ||
| else this.parseSemicolon(tn); | ||
|
Member
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. Wouldn't export function foo(): i32 {
function bar(): i32 { return 42; } return bar();
}on my end, it panics when a nested function is followed by a statement on the same line as the closing brace. |
||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -3172,7 +3185,7 @@ export class Parser extends DiagnosticEmitter { | |
| initializer = this.parseVariable(tn, CommonFlags.None, null, tn.tokenPos, true); | ||
|
|
||
| } else if (!tn.skip(Token.Semicolon)) { | ||
| initializer = this.parseExpressionStatement(tn); | ||
| initializer = this.parseExpressionStatement(tn, true); | ||
| if (!initializer) return null; | ||
| } | ||
|
|
||
|
|
@@ -3234,7 +3247,7 @@ export class Parser extends DiagnosticEmitter { | |
| if (tn.token == Token.Semicolon) { | ||
| let condition: ExpressionStatement | null = null; | ||
| if (!tn.skip(Token.Semicolon)) { | ||
| condition = this.parseExpressionStatement(tn); | ||
| condition = this.parseExpressionStatement(tn, true); | ||
| if (!condition) return null; | ||
| } | ||
|
|
||
|
|
@@ -3375,7 +3388,6 @@ export class Parser extends DiagnosticEmitter { | |
| switchCases.push(switchCase); | ||
| } | ||
| let ret = Node.createSwitchStatement(condition, switchCases, tn.range(startPos, tn.pos)); | ||
| tn.skip(Token.Semicolon); | ||
| return ret; | ||
| } else { | ||
| this.error( | ||
|
|
@@ -3467,10 +3479,17 @@ export class Parser extends DiagnosticEmitter { | |
| // at 'throw': Expression ';'? | ||
|
|
||
| let startPos = tn.tokenPos; | ||
| if (tn.peekOnNewLine()) { | ||
| this.error( | ||
| DiagnosticCode.Unexpected_token, | ||
| tn.range(tn.nextTokenPos) | ||
| ); | ||
| return null; | ||
| } | ||
| let expression = this.parseExpression(tn); | ||
| if (!expression) return null; | ||
| let ret = Node.createThrowStatement(expression, tn.range(startPos, tn.pos)); | ||
| if (!tn.skip(Token.Semicolon)) this.checkASI(tn); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -3561,7 +3580,6 @@ export class Parser extends DiagnosticEmitter { | |
| finallyStatements, | ||
| tn.range(startPos, tn.pos) | ||
| ); | ||
| tn.skip(Token.Semicolon); | ||
| return ret; | ||
| } else { | ||
| this.error( | ||
|
|
@@ -3608,7 +3626,7 @@ export class Parser extends DiagnosticEmitter { | |
| type, | ||
| tn.range(startPos, tn.pos) | ||
| ); | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| ret.overriddenModuleName = this.currentModuleName; | ||
| return ret; | ||
| } else { | ||
|
|
@@ -3638,7 +3656,7 @@ export class Parser extends DiagnosticEmitter { | |
| let moduleName = tn.readString(); | ||
| let ret = Node.createModuleDeclaration(moduleName, flags, tn.range(startPos, tn.pos)); | ||
| this.currentModuleName = moduleName; | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -3649,10 +3667,10 @@ export class Parser extends DiagnosticEmitter { | |
| // at 'void': Expression ';'? | ||
|
|
||
| let startPos = tn.tokenPos; | ||
| let expression = this.parseExpression(tn, Precedence.Grouping); | ||
| let expression = this.parseExpression(tn, Precedence.UnaryPrefix); | ||
| if (!expression) return null; | ||
| let ret = Node.createVoidStatement(expression, tn.range(startPos, tn.pos)); | ||
| tn.skip(Token.Semicolon); | ||
| this.parseSemicolon(tn); | ||
| return ret; | ||
| } | ||
|
|
||
|
|
@@ -3670,7 +3688,6 @@ export class Parser extends DiagnosticEmitter { | |
| let statement = this.parseStatement(tn); | ||
| if (!statement) return null; | ||
| let ret = Node.createWhileStatement(expression, statement, tn.range(startPos, tn.pos)); | ||
| tn.skip(Token.Semicolon); | ||
| return ret; | ||
| } else { | ||
| this.error( | ||
|
|
@@ -4131,6 +4148,15 @@ export class Parser extends DiagnosticEmitter { | |
| while ( | ||
| (nextPrecedence = determinePrecedence(tn.peek())) >= precedence | ||
| ) { | ||
| let nextToken = tn.peek(); | ||
| if ( | ||
| ( | ||
| nextToken == Token.Plus_Plus || | ||
| nextToken == Token.Minus_Minus || | ||
| nextToken == Token.As || | ||
| nextToken == Token.Exclamation | ||
| ) && tn.peekOnNewLine() | ||
| ) break; | ||
| let token = tn.next(); | ||
| switch (token) { | ||
|
|
||
|
|
@@ -4430,6 +4456,12 @@ export class Parser extends DiagnosticEmitter { | |
| ); | ||
| } | ||
|
|
||
| private parseSemicolon( | ||
| tn: Tokenizer | ||
| ): void { | ||
| if (!tn.skip(Token.Semicolon)) this.checkASI(tn); | ||
| } | ||
|
|
||
| /** Skips over a statement on errors in an attempt to reduce unnecessary diagnostic noise. */ | ||
| skipStatement(tn: Tokenizer): void { | ||
| if (tn.peekOnNewLine()) tn.next(); // if reset() to the previous line | ||
|
|
||
Oops, something went wrong.
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.
This might affect generics (
<token) when it's on a new line. For example,should fail i think?