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
refactor: introduce lookaheadInLineCharCode
#15510
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ import { | |
isNewLine, | ||
isWhitespace, | ||
skipWhiteSpace, | ||
skipWhiteSpaceInLine, | ||
} from "../util/whitespace"; | ||
import State from "./state"; | ||
import type { LookaheadState, DeferredStrictError } from "./state"; | ||
|
@@ -195,6 +196,34 @@ export default abstract class Tokenizer extends CommentsParser { | |
return this.input.charCodeAt(this.nextTokenStart()); | ||
} | ||
|
||
/** | ||
* Similar to nextToken, but it will stop at line break when it is seen before the next token | ||
* | ||
* @returns {number} position of the next token start or line break, whichever is seen first. | ||
* @memberof Tokenizer | ||
*/ | ||
nextTokenInLineStart(): number { | ||
return this.nextTokenInLineStartSince(this.state.pos); | ||
} | ||
|
||
nextTokenInLineStartSince(pos: number): number { | ||
skipWhiteSpaceInLine.lastIndex = pos; | ||
return skipWhiteSpaceInLine.test(this.input) | ||
? skipWhiteSpaceInLine.lastIndex | ||
: pos; | ||
} | ||
Comment on lines
+199
to
+214
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. nextTokenInLineStart(pos = this.state.pos): number { Would this be better? 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. default parameter adds a new branching. I can inline 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. Ah, |
||
|
||
/** | ||
* Similar to lookaheadCharCode, but it will return the char code of line break if it is | ||
* seen before the next token | ||
* | ||
* @returns {number} char code of the next token start or line break, whichever is seen first. | ||
* @memberof Tokenizer | ||
*/ | ||
lookaheadInLineCharCode(): number { | ||
return this.input.charCodeAt(this.nextTokenInLineStart()); | ||
} | ||
|
||
codePointAtPos(pos: number): number { | ||
// The implementation is based on | ||
// https://source.chromium.org/chromium/chromium/src/+/master:v8/src/builtins/builtins-string-gen.cc;l=1455;drc=221e331b49dfefadbc6fa40b0c68e6f97606d0b3;bpv=0;bpt=1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,7 @@ export function isNewLine(code: number): boolean { | |
export const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g; | ||
|
||
export const skipWhiteSpaceInLine = | ||
/(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/y; | ||
/(?:[^\S\n\r\u2028\u2029]|\/\/.*|\/\*.*?\*\/)*/g; | ||
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. This change has no effect since we are not using |
||
|
||
// Skip whitespace and single-line comments, including /* no newline here */. | ||
// After this RegExp matches, its lastIndex points to a line terminator, or | ||
|
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.
Can we move this check again to after
startsWithUsing
in line 921 below? We don't need to run it whenisLetOrUsing
is true because ofstartsWithLet
.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.
Actually the line break check here is not required.
is valid, and so should
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 proposal spec requires it; @rbuckton is there a reason?
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.
Ah you are right. I overlooked the spec.
Line break is probably safe here.
for (using
requires a trailing semicolon to start a ForStatement, so there won't be ASI issues here fromusing
not being a keyword.As for this PR I will keep the current behaviour.
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.
It's definitely required in a regular
for
, becausefor
reuses LexicalDeclaration, which requires it. I'd have to look into whether removing the restriction fromfor-of
would cause problems, but either way that would be a normative change requiring consensus. I'm leaning towards leaving it as-is, however, to keep the line termination rules forusing
andawait using
consistent in all places.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.
I agree. If there are any strong push back, we can always loose the restriction instead of adding one, which would be breaking.