perf: Move input to state and precalculate length
This also fixes a bug with async functions
This commit is contained in:
@@ -16,7 +16,6 @@ export default class BaseParser {
|
||||
|
||||
// Initialized by Tokenizer
|
||||
state: State;
|
||||
input: string;
|
||||
|
||||
isReservedWord(word: string): boolean {
|
||||
if (word === "await") {
|
||||
|
||||
@@ -1146,7 +1146,11 @@ export default class ExpressionParser extends LValParser {
|
||||
|
||||
const node = this.startNodeAt(startPos, startLoc);
|
||||
this.addExtra(node, "rawValue", value);
|
||||
this.addExtra(node, "raw", this.input.slice(startPos, this.state.end));
|
||||
this.addExtra(
|
||||
node,
|
||||
"raw",
|
||||
this.state.input.slice(startPos, this.state.end),
|
||||
);
|
||||
node.value = value;
|
||||
this.next();
|
||||
return this.finishNode(node, type);
|
||||
@@ -1365,7 +1369,7 @@ export default class ExpressionParser extends LValParser {
|
||||
}
|
||||
}
|
||||
elem.value = {
|
||||
raw: this.input
|
||||
raw: this.state.input
|
||||
.slice(this.state.start, this.state.end)
|
||||
.replace(/\r\n?/g, "\n"),
|
||||
cooked: this.state.value,
|
||||
@@ -1967,7 +1971,8 @@ export default class ExpressionParser extends LValParser {
|
||||
if (
|
||||
(name === "class" || name === "function") &&
|
||||
(this.state.lastTokEnd !== this.state.lastTokStart + 1 ||
|
||||
this.input.charCodeAt(this.state.lastTokStart) !== charCodes.dot)
|
||||
this.state.input.charCodeAt(this.state.lastTokStart) !==
|
||||
charCodes.dot)
|
||||
) {
|
||||
this.state.context.pop();
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ export default class Parser extends StatementParser {
|
||||
|
||||
this.options = options;
|
||||
this.inModule = this.options.sourceType === "module";
|
||||
this.input = input;
|
||||
this.plugins = pluginsMap(this.options.plugins);
|
||||
this.filename = options.sourceFilename;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ export default class LocationParser extends CommentsParser {
|
||||
code?: string,
|
||||
} = {},
|
||||
): empty {
|
||||
const loc = getLineInfo(this.input, pos);
|
||||
const loc = getLineInfo(this.state.input, pos);
|
||||
message += ` (${loc.line}:${loc.column})`;
|
||||
// $FlowIgnore
|
||||
const err: SyntaxError & { pos: number, loc: Position } = new SyntaxError(
|
||||
|
||||
@@ -44,7 +44,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
const directiveLiteral = this.startNodeAt(expr.start, expr.loc.start);
|
||||
const directive = this.startNodeAt(stmt.start, stmt.loc.start);
|
||||
|
||||
const raw = this.input.slice(expr.start, expr.end);
|
||||
const raw = this.state.input.slice(expr.start, expr.end);
|
||||
const val = (directiveLiteral.value = raw.slice(1, -1)); // remove quotes
|
||||
|
||||
this.addExtra(directiveLiteral, "raw", raw);
|
||||
@@ -551,7 +551,9 @@ export default class StatementParser extends ExpressionParser {
|
||||
parseThrowStatement(node: N.ThrowStatement): N.ThrowStatement {
|
||||
this.next();
|
||||
if (
|
||||
lineBreak.test(this.input.slice(this.state.lastTokEnd, this.state.start))
|
||||
lineBreak.test(
|
||||
this.state.input.slice(this.state.lastTokEnd, this.state.start),
|
||||
)
|
||||
) {
|
||||
this.raise(this.state.lastTokEnd, "Illegal newline after throw");
|
||||
}
|
||||
@@ -1521,7 +1523,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
isAsyncFunction() {
|
||||
if (!this.isContextual("async")) return false;
|
||||
|
||||
const { input, pos } = this.state;
|
||||
const { input, pos, length } = this.state;
|
||||
|
||||
skipWhiteSpace.lastIndex = pos;
|
||||
const skip = skipWhiteSpace.exec(input);
|
||||
@@ -1533,7 +1535,7 @@ export default class StatementParser extends ExpressionParser {
|
||||
return (
|
||||
!lineBreak.test(input.slice(pos, next)) &&
|
||||
input.slice(next, next + 8) === "function" &&
|
||||
(next + 8 === input.length || !isIdentifierChar(input.charAt(next + 8)))
|
||||
(next + 8 === length || !isIdentifierChar(input.charCodeAt(next + 8)))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -87,7 +87,7 @@ export default class UtilParser extends Tokenizer {
|
||||
|
||||
hasPrecedingLineBreak(): boolean {
|
||||
return lineBreak.test(
|
||||
this.input.slice(this.state.lastTokEnd, this.state.start),
|
||||
this.state.input.slice(this.state.lastTokEnd, this.state.start),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user