diff --git a/lib/optimize/InnerGraph.js b/lib/optimize/InnerGraph.js index 066469348..bc199686d 100644 --- a/lib/optimize/InnerGraph.js +++ b/lib/optimize/InnerGraph.js @@ -205,15 +205,24 @@ exports.getExportDependentDependencies = state => { }; /** - * @param {ParserState} state parser state + * @param {TODO} parser parser * @param {string} name symbol name * @returns {TopLevelSymbol} usage data */ -exports.createTopLevelSymbol = (state, name) => { - const innerGraphState = getState(state); +exports.tagTopLevelSymbol = (parser, name) => { + const innerGraphState = getState(parser.state); const { innerGraph } = innerGraphState || {}; - return new TopLevelSymbol(name, innerGraph); + parser.defineVariable(name); + + const existingTag = parser.getTagData(name, topLevelSymbolTag); + if (existingTag) { + return existingTag; + } + + const fn = new TopLevelSymbol(name, innerGraph); + parser.tagVariable(name, topLevelSymbolTag, fn); + return fn; }; class TopLevelSymbol { diff --git a/lib/optimize/InnerGraphPlugin.js b/lib/optimize/InnerGraphPlugin.js index 99a1313b8..0fb6166ee 100644 --- a/lib/optimize/InnerGraphPlugin.js +++ b/lib/optimize/InnerGraphPlugin.js @@ -128,9 +128,7 @@ class InnerGraphPlugin { if (parser.scope.topLevelScope === true) { if (statement.type === "FunctionDeclaration") { const name = statement.id ? statement.id.name : "*default*"; - parser.defineVariable(name); - const fn = InnerGraph.createTopLevelSymbol(parser.state, name); - parser.tagVariable(name, topLevelSymbolTag, fn); + const fn = InnerGraph.tagTopLevelSymbol(parser, name); statementWithTopLevelSymbol.set(statement, fn); return true; } @@ -142,9 +140,7 @@ class InnerGraphPlugin { if (parser.scope.topLevelScope === true) { if (statement.type === "ClassDeclaration") { const name = statement.id ? statement.id.name : "*default*"; - parser.defineVariable(name); - const fn = InnerGraph.createTopLevelSymbol(parser.state, name); - parser.tagVariable(name, topLevelSymbolTag, fn); + const fn = InnerGraph.tagTopLevelSymbol(parser, name); statementWithTopLevelSymbol.set(statement, fn); return true; } @@ -157,28 +153,12 @@ class InnerGraphPlugin { decl.type === "Identifier" ) { const name = "*default*"; - parser.defineVariable(name); - const fn = InnerGraph.createTopLevelSymbol( - parser.state, - name - ); - parser.tagVariable(name, topLevelSymbolTag, fn); + const fn = InnerGraph.tagTopLevelSymbol(parser, name); statementWithTopLevelSymbol.set(statement, fn); } } } }); - const tagVar = name => { - parser.defineVariable(name); - const existingTag = parser.getTagData(name, topLevelSymbolTag); - const fn = - existingTag || - InnerGraph.createTopLevelSymbol(parser.state, name); - if (!existingTag) { - parser.tagVariable(name, topLevelSymbolTag, fn); - } - return fn; - }; /** @type {WeakMap<{}, TopLevelSymbol>} */ const declWithTopLevelSymbol = new WeakMap(); const pureDeclarators = new WeakSet(); @@ -197,13 +177,13 @@ class InnerGraphPlugin { decl.init.type === "ClassExpression" ) { const name = decl.id.name; - const fn = tagVar(name); + const fn = InnerGraph.tagTopLevelSymbol(parser, name); declWithTopLevelSymbol.set(decl, fn); return true; } if (isPure(decl.init, parser, decl.id.range[1])) { const name = decl.id.name; - const fn = tagVar(name); + const fn = InnerGraph.tagTopLevelSymbol(parser, name); declWithTopLevelSymbol.set(decl, fn); pureDeclarators.add(decl); return true;