|
Expand Up
|
@@ -168,7 +168,6 @@ const { |
|
|
/** @typedef {(ident: string) => void} OnIdentString */ |
|
|
/** @typedef {(ident: string, identifier: Identifier) => void} OnIdent */ |
|
|
/** @typedef {StatementPathItem[]} StatementPath */ |
|
|
/** @typedef {(Statement | ModuleDeclaration)[]} HoistedDeclarations the declarations one scope hoists, in source order */ |
|
|
/** @typedef {(ModuleDeclaration | Statement | undefined)[]} ModuleDeclarations the program's import and re-export declarations in source order, each followed by the statement written before it */ |
|
|
/** @typedef {number | number[]} BlockDeclarations which statements of one list declare into its block scope: a mask of their indices, or the indices themselves once one is too large to mask */ |
|
|
|
|
Expand All
|
@@ -179,8 +178,6 @@ const { |
|
|
|
|
|
/** @type {string[]} */ |
|
|
const EMPTY_ARRAY = []; |
|
|
/** @type {HoistedDeclarations} */ |
|
|
const EMPTY_HOISTED_DECLARATIONS = []; |
|
|
// what the module pass reports; `export default` is the block pass's |
|
|
// what the parser records per scope, and so what the hoisting pass reaches |
|
|
const RECORDED_STATEMENT_TYPES = new Set([ |
|
Expand Down
Expand Up
|
@@ -269,28 +266,18 @@ const isLeftToEvaluatorOperator = (type, operator) => |
|
|
|
|
|
/** |
|
|
* Whether the evaluator, rather than the rules, judges a node the side-effect |
|
|
* rules leave undecided. |
|
|
* @param {Node | undefined} node the node, read for a unary or binary operator |
|
|
* rules leave undecided; a unary or binary node goes by its operator instead. |
|
|
* @param {number} type its `NodeType` |
|
|
* @returns {boolean} whether the evaluator judges it |
|
|
*/ |
|
|
const isLeftToEvaluator = (node, type) => { |
|
|
switch (type) { |
|
|
case NodeType.UnaryExpression: |
|
|
case NodeType.BinaryExpression: |
|
|
return isLeftToEvaluatorOperator( |
|
|
type, |
|
|
/** @type {UnaryExpression | BinaryExpression} */ (node).operator |
|
|
); |
|
|
default: |
|
|
if (ruledPurityNodeTypes === undefined) { |
|
|
ruledPurityNodeTypes = new Uint8Array(256); |
|
|
for (const name of RULED_PURITY_TYPES) { |
|
|
ruledPurityNodeTypes[NodeType[name]] = 1; |
|
|
} |
|
|
} |
|
|
return ruledPurityNodeTypes[type] !== 1; |
|
|
const isLeftToEvaluator = (type) => { |
|
|
if (ruledPurityNodeTypes === undefined) { |
|
|
ruledPurityNodeTypes = new Uint8Array(256); |
|
|
for (const name of RULED_PURITY_TYPES) { |
|
|
ruledPurityNodeTypes[NodeType[name]] = 1; |
|
|
} |
|
|
} |
|
|
return ruledPurityNodeTypes[type] !== 1; |
|
|
}; |
|
|
|
|
|
/** |
|
Expand Down
Expand Up
|
@@ -1160,99 +1147,6 @@ const evaluateAstCaches = new WeakMap(); |
|
|
// The inner map is strong while the compilation lives, so cap it |
|
|
const EVALUATE_AST_CACHE_LIMIT = 4096; |
|
|
|
|
|
/** |
|
|
* Add one declaration, starting the list at the first — most scopes declare |
|
|
* nothing, and the shared empty list spares them an array. |
|
|
* @param {HoistedDeclarations} hoisted the list so far |
|
|
* @param {Statement | ModuleDeclaration} declaration what to add |
|
|
* @returns {HoistedDeclarations} the list holding it |
|
|
*/ |
|
|
const addHoistedDeclaration = (hoisted, declaration) => { |
|
|
if (hoisted === EMPTY_HOISTED_DECLARATIONS) return [declaration]; |
|
|
hoisted.push(declaration); |
|
|
return hoisted; |
|
|
}; |
|
|
|
|
|
/** |
|
|
* Collect the declarations that hoist to one scope, for an AST that did not |
|
|
* come from webpack's parser and so carries no record of its own. |
|
|
* @param {(Statement | ModuleDeclaration)[]} statements the scope's statements |
|
|
* @param {HoistedDeclarations} hoisted the list so far |
|
|
* @returns {HoistedDeclarations} the list holding what they declare |
|
|
*/ |
|
|
const collectHoistedDeclarations = (statements, hoisted) => { |
|
|
for (let index = 0, len = statements.length; index < len; index++) { |
|
|
hoisted = collectHoistedDeclaration(statements[index], hoisted); |
|
|
} |
|
|
return hoisted; |
|
|
}; |
|
|
|
|
|
/** |
|
|
* Collect from one statement, descending exactly as far as a `var` reaches: |
|
|
* through blocks and loop bodies, never into another function. |
|
|
* @param {Statement | ModuleDeclaration} statement the statement |
|
|
* @param {HoistedDeclarations} hoisted the list so far |
|
|
* @returns {HoistedDeclarations} the list holding what it declares |
|
|
*/ |
|
|
const collectHoistedDeclaration = (statement, hoisted) => { |
|
|
switch (statement.type) { |
|
|
case "VariableDeclaration": |
|
|
if (statement.kind === "var") { |
|
|
hoisted = addHoistedDeclaration(hoisted, statement); |
|
|
} |
|
|
break; |
|
|
case "FunctionDeclaration": |
|
|
case "ClassDeclaration": |
|
|
hoisted = addHoistedDeclaration(hoisted, statement); |
|
|
break; |
|
|
case "BlockStatement": |
|
|
hoisted = collectHoistedDeclarations(statement.body, hoisted); |
|
|
break; |
|
|
case "IfStatement": |
|
|
hoisted = collectHoistedDeclaration(statement.consequent, hoisted); |
|
|
if (statement.alternate) { |
|
|
hoisted = collectHoistedDeclaration(statement.alternate, hoisted); |
|
|
} |
|
|
break; |
|
|
case "ForStatement": |
|
|
if (statement.init && statement.init.type === "VariableDeclaration") { |
|
|
hoisted = collectHoistedDeclaration(statement.init, hoisted); |
|
|
} |
|
|
hoisted = collectHoistedDeclaration(statement.body, hoisted); |
|
|
break; |
|
|
case "ForInStatement": |
|
|
case "ForOfStatement": |
|
|
if (statement.left.type === "VariableDeclaration") { |
|
|
hoisted = collectHoistedDeclaration(statement.left, hoisted); |
|
|
} |
|
|
hoisted = collectHoistedDeclaration(statement.body, hoisted); |
|
|
break; |
|
|
case "WhileStatement": |
|
|
case "DoWhileStatement": |
|
|
case "LabeledStatement": |
|
|
case "WithStatement": |
|
|
hoisted = collectHoistedDeclaration(statement.body, hoisted); |
|
|
break; |
|
|
case "TryStatement": |
|
|
hoisted = collectHoistedDeclaration(statement.block, hoisted); |
|
|
if (statement.handler) { |
|
|
hoisted = collectHoistedDeclaration(statement.handler.body, hoisted); |
|
|
} |
|
|
if (statement.finalizer) { |
|
|
hoisted = collectHoistedDeclaration(statement.finalizer, hoisted); |
|
|
} |
|
|
break; |
|
|
case "SwitchStatement": { |
|
|
const cases = statement.cases; |
|
|
for (let index = 0, len = cases.length; index < len; index++) { |
|
|
hoisted = collectHoistedDeclarations(cases[index].consequent, hoisted); |
|
|
} |
|
|
break; |
|
|
} |
|
|
} |
|
|
return hoisted; |
|
|
}; |
|
|
|
|
|
/** |
|
|
* Parses as the goal asks, an `auto` source as a module and then, where that |
|
|
* fails, as a script, reporting the module parse's error when both fail. |
|
Expand All
|
@@ -1275,7 +1169,8 @@ const parseInGoal = (type, parseAs) => { |
|
|
}; |
|
|
|
|
|
/** |
|
|
* `collectHoistedDeclarations` for a list of statements of the tree, by id. |
|
|
* Collects the declarations a list of statements of the tree hoists to its |
|
|
* scope, by id. |
|
|
* @param {SyntaxTree} tree the tree |
|
|
* @param {number} owner the node holding the list |
|
|
* @param {number} field the `Field` holding the statements |
|
Expand All
|
@@ -1294,7 +1189,8 @@ const collectHoistedDeclarationsNode = (tree, owner, field, hoisted) => { |
|
|
}; |
|
|
|
|
|
/** |
|
|
* `collectHoistedDeclaration` for a statement of the tree, by id. |
|
|
* Collects what one statement of the tree hoists, descending as far as a |
|
|
* `var` reaches: through blocks and loop bodies, never into a function. |
|
|
* @param {SyntaxTree} tree the tree |
|
|
* @param {number} id the statement, or `0` for none |
|
|
* @param {number[]} hoisted the declarations found so far, added to |
|
Expand Down
Expand Up
|
@@ -8794,7 +8690,7 @@ class JavascriptParser extends Parser { |
|
|
if ( |
|
|
type === NodeType.UnaryExpression || type === NodeType.BinaryExpression |
|
|
? isLeftToEvaluatorOperator(type, this._operatorOf(node)) |
|
|
: isLeftToEvaluator(undefined, type) |
|
|
: isLeftToEvaluator(type) |
|
|
) { |
|
|
return this.evaluateExpression(asNode(node)).couldHaveSideEffects(); |
|
|
} |
|
Expand Down
|
|