add hooks for walking parts of classes

import handling of classes and expressions
This commit is contained in:
Tobias Koppers 2020-04-09 15:53:59 +02:00
parent a1b9f3ecf2
commit b38330754f
3 changed files with 124 additions and 65 deletions

View File

@ -166,6 +166,8 @@
"valign",
"returnfalse",
"return'development",
"datastructures",
"prewalking",
"webassemblyjs",
"fsevents",

View File

@ -18,6 +18,8 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
/** @typedef {import("estree").BinaryExpression} BinaryExpressionNode */
/** @typedef {import("estree").BlockStatement} BlockStatementNode */
/** @typedef {import("estree").CallExpression} CallExpressionNode */
/** @typedef {import("estree").ClassDeclaration} ClassDeclarationNode */
/** @typedef {import("estree").ClassExpression} ClassExpressionNode */
/** @typedef {import("estree").Comment} CommentNode */
/** @typedef {import("estree").ConditionalExpression} ConditionalExpressionNode */
/** @typedef {import("estree").Declaration} DeclarationNode */
@ -28,6 +30,7 @@ const BasicEvaluatedExpression = require("./BasicEvaluatedExpression");
/** @typedef {import("estree").Literal} LiteralNode */
/** @typedef {import("estree").LogicalExpression} LogicalExpressionNode */
/** @typedef {import("estree").MemberExpression} MemberExpressionNode */
/** @typedef {import("estree").MethodDefinition} MethodDefinitionNode */
/** @typedef {import("estree").ModuleDeclaration} ModuleDeclarationNode */
/** @typedef {import("estree").Node} AnyNode */
/** @typedef {import("estree").Program} ProgramNode */
@ -156,6 +159,10 @@ class JavascriptParser extends Parser {
statement: new SyncBailHook(["statement"]),
/** @type {SyncBailHook<[IfStatementNode], boolean | void>} */
statementIf: new SyncBailHook(["statement"]),
/** @type {SyncBailHook<[ExpressionNode, ClassExpressionNode | ClassDeclarationNode], boolean | void>} */
classExtendsExpression: new SyncBailHook(["expression", "statement"]),
/** @type {SyncBailHook<[MethodDefinitionNode, ClassExpressionNode | ClassDeclarationNode], boolean | void>} */
classBodyElement: new SyncBailHook(["element", "statement"]),
/** @type {HookMap<SyncBailHook<[LabeledStatementNode], boolean | void>>} */
label: new HookMap(() => new SyncBailHook(["statement"])),
/** @type {SyncBailHook<[StatementNode, ImportSource], boolean | void>} */
@ -1137,17 +1144,28 @@ class JavascriptParser extends Parser {
}
}
/**
* @param {ClassExpressionNode | ClassDeclarationNode} classy a class node
* @returns {void}
*/
walkClass(classy) {
if (classy.superClass) this.walkExpression(classy.superClass);
if (classy.superClass) {
if (!this.hooks.classExtendsExpression.call(classy.superClass, classy)) {
this.walkExpression(classy.superClass);
}
}
if (classy.body && classy.body.type === "ClassBody") {
const wasTopLevel = this.scope.topLevelScope;
this.scope.topLevelScope = false;
for (const methodDefinition of classy.body.body) {
if (methodDefinition.type === "MethodDefinition") {
this.walkMethodDefinition(methodDefinition);
for (const classElement of classy.body.body) {
if (!this.hooks.classBodyElement.call(classElement, classy)) {
if (classElement.type === "MethodDefinition") {
this.scope.topLevelScope = false;
this.walkMethodDefinition(classElement);
this.scope.topLevelScope = wasTopLevel;
}
// TODO add support for ClassProperty here once acorn supports it
}
}
this.scope.topLevelScope = wasTopLevel;
}
}
@ -1974,47 +1992,6 @@ class JavascriptParser extends Parser {
}
}
walkLeftHandSideExpression(expression) {
switch (expression.type) {
case "Identifier":
this.walkIdentifier(expression);
break;
case "CallExpression":
this.walkCallExpression(expression);
break;
case "NewExpression":
this.walkNewExpression(expression);
break;
case "ClassExpression":
this.walkClassExpression(expression);
break;
case "MemberExpression":
this.walkMemberExpression(expression);
break;
case "AssignmentExpression":
this.walkAssignmentExpression(expression);
break;
case "FunctionExpression":
this.walkFunctionExpression(expression);
break;
case "ConditionalExpression":
this.walkConditionalExpression(expression);
break;
case "LogicalExpression":
this.walkLogicalExpression(expression);
break;
case "SequenceExpression":
this.walkSequenceExpression(expression);
break;
case "ThisExpression":
this.walkThisExpression(expression);
break;
case "TaggedTemplateExpression":
this.walkTaggedTemplateExpression(expression);
break;
}
}
walkAwaitExpression(expression) {
if (this.scope.topLevelScope === true)
this.hooks.topLevelAwait.call(expression);

View File

@ -11,7 +11,10 @@ const {
const PureExpressionDependency = require("../dependencies/PureExpressionDependency");
const InnerGraph = require("./InnerGraph");
/** @typedef {import("estree").ClassDeclaration} ClassDeclarationNode */
/** @typedef {import("estree").ClassExpression} ClassExpressionNode */
/** @typedef {import("estree").Node} Node */
/** @typedef {import("estree").VariableDeclarator} VariableDeclaratorNode */
/** @typedef {import("../Compiler")} Compiler */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../dependencies/HarmonyImportSpecifierDependency")} HarmonyImportSpecifierDependency */
@ -145,11 +148,31 @@ class InnerGraphPlugin {
InnerGraph.inferDependencyUsage(parser.state);
logger.timeAggregate("infer dependency usage");
});
// During prewalking the following datastructures are filled with
// nodes that have a TopLevelSymbol assigned and
// variables are tagged with the assigned TopLevelSymbol
// We differ 3 types of nodes:
// 1. full statements (export default, function declaration)
// 2. classes (class declaration, class expression)
// 3. variable declarators (const x = ...)
/** @type {WeakMap<Node, TopLevelSymbol>} */
const statementWithTopLevelSymbol = new WeakMap();
/** @type {WeakMap<Node, Node>} */
const statementPurePart = new WeakMap();
/** @type {WeakMap<ClassExpressionNode | ClassDeclarationNode, TopLevelSymbol>} */
const classWithTopLevelSymbol = new WeakMap();
/** @type {WeakMap<VariableDeclaratorNode, TopLevelSymbol>} */
const declWithTopLevelSymbol = new WeakMap();
/** @type {WeakSet<VariableDeclaratorNode>} */
const pureDeclarators = new WeakSet();
// The following hooks are used during prewalking:
parser.hooks.preStatement.tap("InnerGraphPlugin", statement => {
if (!InnerGraph.isEnabled(parser.state)) return;
@ -162,6 +185,7 @@ class InnerGraphPlugin {
}
}
});
parser.hooks.blockPreStatement.tap("InnerGraphPlugin", statement => {
if (!InnerGraph.isEnabled(parser.state)) return;
@ -169,14 +193,19 @@ class InnerGraphPlugin {
if (statement.type === "ClassDeclaration") {
const name = statement.id ? statement.id.name : "*default*";
const fn = InnerGraph.tagTopLevelSymbol(parser, name);
statementWithTopLevelSymbol.set(statement, fn);
classWithTopLevelSymbol.set(statement, fn);
return true;
}
if (statement.type === "ExportDefaultDeclaration") {
const name = "*default*";
const fn = InnerGraph.tagTopLevelSymbol(parser, name);
const decl = statement.declaration;
if (isPure(decl, parser, decl.range[1])) {
const name = "*default*";
const fn = InnerGraph.tagTopLevelSymbol(parser, name);
if (
decl.type === "ClassExpression" ||
decl.type === "ClassDeclaration"
) {
classWithTopLevelSymbol.set(decl, fn);
} else if (isPure(decl, parser, decl.range[1])) {
statementWithTopLevelSymbol.set(statement, fn);
if (
!decl.type.endsWith("FunctionExpression") &&
@ -189,9 +218,6 @@ class InnerGraphPlugin {
}
}
});
/** @type {WeakMap<Node, TopLevelSymbol>} */
const declWithTopLevelSymbol = new WeakMap();
const pureDeclarators = new WeakSet();
parser.hooks.preDeclarator.tap(
"InnerGraphPlugin",
@ -202,8 +228,11 @@ class InnerGraphPlugin {
decl.init &&
decl.id.type === "Identifier"
) {
if (isPure(decl.init, parser, decl.id.range[1])) {
const name = decl.id.name;
const name = decl.id.name;
if (decl.init.type === "ClassExpression") {
const fn = InnerGraph.tagTopLevelSymbol(parser, name);
classWithTopLevelSymbol.set(decl.init, fn);
} else if (isPure(decl.init, parser, decl.id.range[1])) {
const fn = InnerGraph.tagTopLevelSymbol(parser, name);
declWithTopLevelSymbol.set(decl, fn);
if (
@ -217,12 +246,29 @@ class InnerGraphPlugin {
}
}
);
// During real walking we set the TopLevelSymbol state to the assigned
// TopLevelSymbol by using the fill datastructures.
// In addition to tracking TopLevelSymbols, we sometimes need to
// add a PureExpressionDependency. This is needed to skip execution
// of pure expressions, even when they are not dropped due to
// minimizing. Otherwise symbols used there might not exist anymore
// as they are removed as unused by this optimization
// When we find a reference to a TopLevelSymbol, we register a
// TopLevelSymbol dependency from TopLevelSymbol in state to the
// referenced TopLevelSymbol. This way we get a graph of all
// TopLevelSymbols.
// The following hooks are called during walking:
parser.hooks.statement.tap("InnerGraphPlugin", statement => {
if (!InnerGraph.isEnabled(parser.state)) return;
if (parser.scope.topLevelScope === true) {
InnerGraph.setTopLevelSymbol(parser.state, undefined);
const fn = statementWithTopLevelSymbol.get(statement);
const fn = statementWithTopLevelSymbol.get(statement);
if (fn) {
InnerGraph.setTopLevelSymbol(parser.state, fn);
const purePart = statementPurePart.get(statement);
@ -244,22 +290,55 @@ class InnerGraphPlugin {
}
});
}
}
}
});
parser.hooks.classExtendsExpression.tap(
"InnerGraphPlugin",
(expr, statement) => {
if (!InnerGraph.isEnabled(parser.state)) return;
if (parser.scope.topLevelScope === true) {
const fn = classWithTopLevelSymbol.get(statement);
if (
statement.type === "ClassDeclaration" &&
statement.superClass
fn &&
isPure(
expr,
parser,
statement.id ? statement.id.range[1] : statement.range[0]
)
) {
if (isPure(statement, parser, statement.id.range[1])) {
onUsageSuper(statement.superClass);
InnerGraph.setTopLevelSymbol(parser.state, fn);
onUsageSuper(expr);
}
}
}
);
parser.hooks.classBodyElement.tap(
"InnerGraphPlugin",
(element, statement) => {
if (!InnerGraph.isEnabled(parser.state)) return;
if (parser.scope.topLevelScope === true) {
const fn = classWithTopLevelSymbol.get(statement);
if (fn) {
if (element.type === "MethodDefinition") {
InnerGraph.setTopLevelSymbol(parser.state, fn);
} else if (
element.type === "ClassProperty" &&
!element.static
) {
// TODO add test case once acorn supports it
// Currently this is not parsable
InnerGraph.setTopLevelSymbol(parser.state, fn);
} else {
statementWithTopLevelSymbol.delete(statement);
InnerGraph.setTopLevelSymbol(parser.state, undefined);
parser.walkLeftHandSideExpression(statement.superClass);
return true;
}
}
}
}
});
);
parser.hooks.declarator.tap("InnerGraphPlugin", (decl, statement) => {
if (!InnerGraph.isEnabled(parser.state)) return;
const fn = declWithTopLevelSymbol.get(decl);
@ -295,6 +374,7 @@ class InnerGraphPlugin {
return true;
}
});
parser.hooks.expression
.for(topLevelSymbolTag)
.tap("InnerGraphPlugin", () => {