incapsulate tag variable

This commit is contained in:
Sergey Melyukov 2020-01-27 18:46:23 +03:00
parent 68a61677dc
commit 432d338f9a
2 changed files with 18 additions and 29 deletions

View File

@ -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 {

View File

@ -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;