fix pr comments

This commit is contained in:
Sergey Melyukov 2020-01-21 20:50:27 +03:00
parent 5613a38bb1
commit 533d7fced7
3 changed files with 24 additions and 17 deletions

View File

@ -82,10 +82,12 @@ module.exports = class HarmonyImportDependencyParserPlugin {
const addDepToInnerGraph = dep => {
const innerGraphState = InnerGraph.getState(parser.state);
if (!innerGraphState) return;
const { harmonyAllExportDependentDependencies } = innerGraphState;
const innerGraph = innerGraphState.harmonyInnerGraph;
harmonyAllExportDependentDependencies.add(dep);
const currentTopLevelSymbol = innerGraphState.currentTopLevelSymbol;
const {
innerGraph,
allExportDependentDependencies,
currentTopLevelSymbol
} = innerGraphState;
allExportDependentDependencies.add(dep);
if (!currentTopLevelSymbol) {
innerGraph.set(dep, true);
} else {

View File

@ -11,7 +11,7 @@
/** @typedef {import("../dependencies/PureExpressionDependency")} PureExpressionDependency */
/** @typedef {import("../Parser").ParserState} ParserState */
/** @typedef {Map<TopLevelSymbol | Dependency, Set<string | TopLevelSymbol> | true>} InnerGraph */
/** @typedef {false|{harmonyInnerGraph: InnerGraph, harmonyAllExportDependentDependencies: Set<PureExpressionDependency|HarmonyImportSpecifierDependency>, currentTopLevelSymbol: TopLevelSymbol|void}} State */
/** @typedef {false|{innerGraph: InnerGraph, allExportDependentDependencies: Set<PureExpressionDependency|HarmonyImportSpecifierDependency>, currentTopLevelSymbol: TopLevelSymbol|void}} State */
/** @type {WeakMap<ParserState, State>} */
const parserStateMap = new WeakMap();
@ -37,8 +37,8 @@ exports.enable = parserState => {
return;
}
parserStateMap.set(parserState, {
harmonyInnerGraph: new Map(),
harmonyAllExportDependentDependencies: new Set(),
innerGraph: new Map(),
allExportDependentDependencies: new Set(),
currentTopLevelSymbol: undefined
});
};

View File

@ -119,7 +119,10 @@ class InnerGraphPlugin {
const innerGraphState = InnerGraph.getState(parser.state);
if (!innerGraphState) return;
const innerGraph = innerGraphState.harmonyInnerGraph;
const {
innerGraph,
allExportDependentDependencies
} = innerGraphState;
// flatten graph to terminal nodes (string, undefined or true)
const nonTerminal = new Set(innerGraph.keys());
while (nonTerminal.size > 0) {
@ -164,7 +167,7 @@ class InnerGraphPlugin {
}
}
for (const dep of innerGraphState.harmonyAllExportDependentDependencies) {
for (const dep of allExportDependentDependencies) {
const value = innerGraph.get(dep);
switch (value) {
case undefined:
@ -187,7 +190,7 @@ class InnerGraphPlugin {
if (parser.scope.topLevelScope === true) {
if (statement.type === "FunctionDeclaration") {
const innerGraph = innerGraphState.harmonyInnerGraph;
const { innerGraph } = innerGraphState;
const name = statement.id ? statement.id.name : "*default*";
parser.defineVariable(name);
const fn = new TopLevelSymbol(name, innerGraph);
@ -203,7 +206,7 @@ class InnerGraphPlugin {
if (parser.scope.topLevelScope === true) {
if (statement.type === "ClassDeclaration") {
const innerGraph = innerGraphState.harmonyInnerGraph;
const { innerGraph } = innerGraphState;
const name = statement.id ? statement.id.name : "*default*";
parser.defineVariable(name);
const fn = new TopLevelSymbol(name, innerGraph);
@ -219,7 +222,7 @@ class InnerGraphPlugin {
decl.type === "ClassExpression" ||
decl.type === "Identifier"
) {
const innerGraph = innerGraphState.harmonyInnerGraph;
const { innerGraph } = innerGraphState;
const name = "*default*";
parser.defineVariable(name);
const fn = new TopLevelSymbol(name, innerGraph);
@ -230,7 +233,7 @@ class InnerGraphPlugin {
}
});
const tagVar = (innerGraphState, name) => {
const innerGraph = innerGraphState.harmonyInnerGraph;
const { innerGraph } = innerGraphState;
parser.defineVariable(name);
const existingTag = parser.getTagData(name, topLevelSymbolTag);
const fn = existingTag || new TopLevelSymbol(name, innerGraph);
@ -286,15 +289,18 @@ class InnerGraphPlugin {
parser.hooks.declarator.tap("InnerGraphPlugin", (decl, statement) => {
const innerGraphState = InnerGraph.getState(parser.state);
if (!innerGraphState) return;
const {
innerGraph,
allExportDependentDependencies
} = innerGraphState;
const fn = declWithTopLevelSymbol.get(decl);
if (fn) {
if (pureDeclarators.has(decl)) {
const innerGraph = innerGraphState.harmonyInnerGraph;
const dep = new PureExpressionDependency(decl.init.range);
dep.loc = decl.loc;
parser.state.module.addDependency(dep);
innerGraph.set(dep, new Set([fn]));
innerGraphState.harmonyAllExportDependentDependencies.add(dep);
allExportDependentDependencies.add(dep);
}
innerGraphState.currentTopLevelSymbol = fn;
parser.walkExpression(decl.init);
@ -309,8 +315,7 @@ class InnerGraphPlugin {
if (!innerGraphState) return;
const topLevelSymbol =
/** @type {TopLevelSymbol} */ (parser.currentTagData);
const currentTopLevelSymbol =
innerGraphState.currentTopLevelSymbol;
const { currentTopLevelSymbol } = innerGraphState;
topLevelSymbol.addDependency(currentTopLevelSymbol || true);
});
parser.hooks.assign