HarmonyImportDependencyParserPlugin.js
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
"use strict";
const HarmonyImportDependency = require("./HarmonyImportDependency");
const HarmonyImportSpecifierDependency = require("./HarmonyImportSpecifierDependency");
const HarmonyAcceptImportDependency = require("./HarmonyAcceptImportDependency");
const HarmonyAcceptDependency = require("./HarmonyAcceptDependency");
const HarmonyModulesHelpers = require("./HarmonyModulesHelpers");
module.exports = class HarmonyImportDependencyParserPlugin {
constructor(moduleOptions) {
this.strictExportPresence = moduleOptions.strictExportPresence;
this.strictThisContextOnImports = moduleOptions.strictThisContextOnImports;
}
apply(parser) {
parser.plugin("import", (statement, source) => {
const dep = new HarmonyImportDependency(source, HarmonyModulesHelpers.getNewModuleVar(parser.state, source), statement.range);
dep.loc = statement.loc;
parser.state.current.addDependency(dep);
parser.state.lastHarmonyImport = dep;
return true;
});
parser.plugin("import specifier", (statement, source, id, name) => {
parser.scope.definitions.length--;
parser.scope.renames[`$${name}`] = "imported var";
if(!parser.state.harmonySpecifier) parser.state.harmonySpecifier = {};
parser.state.harmonySpecifier[`$${name}`] = [parser.state.lastHarmonyImport, HarmonyModulesHelpers.getModuleVar(parser.state, source), id];
return true;
});
parser.plugin("expression imported var", (expr) => {
const name = expr.name;
const settings = parser.state.harmonySpecifier[`$${name}`];
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range, this.strictExportPresence);
dep.shorthand = parser.scope.inShorthand;
dep.directImport = true;
dep.loc = expr.loc;
parser.state.current.addDependency(dep);
return true;
});
parser.plugin("expression imported var.*", (expr) => {
const name = expr.object.name;
const settings = parser.state.harmonySpecifier[`$${name}`];
if(settings[2] !== null)
return false;
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], expr.property.name || expr.property.value, name, expr.range, this.strictExportPresence);
dep.shorthand = parser.scope.inShorthand;
dep.directImport = false;
dep.loc = expr.loc;
parser.state.current.addDependency(dep);
return true;
});
if(this.strictThisContextOnImports) {
// only in case when we strictly follow the spec we need a special case here
parser.plugin("call imported var.*", (expr) => {
if(expr.callee.type !== "MemberExpression") return;
if(expr.callee.object.type !== "Identifier") return;
const name = expr.callee.object.name;
const settings = parser.state.harmonySpecifier[`$${name}`];
if(settings[2] !== null)
return false;
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], expr.callee.property.name || expr.callee.property.value, name, expr.callee.range, this.strictExportPresence);
dep.shorthand = parser.scope.inShorthand;
dep.directImport = false;
dep.namespaceObjectAsContext = true;
dep.loc = expr.callee.loc;
parser.state.current.addDependency(dep);
if(expr.arguments)
parser.walkExpressions(expr.arguments);
return true;
});
}
parser.plugin("call imported var", (expr) => {
const args = expr.arguments;
const fullExpr = expr;
expr = expr.callee;
if(expr.type !== "Identifier") return;
const name = expr.name;
const settings = parser.state.harmonySpecifier[`$${name}`];
const dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range, this.strictExportPresence);
dep.directImport = true;
dep.callArgs = args;
dep.call = fullExpr;
dep.loc = expr.loc;
parser.state.current.addDependency(dep);
if(args)
parser.walkExpressions(args);
return true;
});
parser.plugin("hot accept callback", (expr, requests) => {
const dependencies = requests
.filter(request => HarmonyModulesHelpers.checkModuleVar(parser.state, request))
.map(request => {
const dep = new HarmonyAcceptImportDependency(request, HarmonyModulesHelpers.getModuleVar(parser.state, request), expr.range);
dep.loc = expr.loc;
parser.state.current.addDependency(dep);
return dep;
});
if(dependencies.length > 0) {
const dep = new HarmonyAcceptDependency(expr.range, dependencies, true);
dep.loc = expr.loc;
parser.state.current.addDependency(dep);
}
});
parser.plugin("hot accept without callback", (expr, requests) => {
const dependencies = requests
.filter(request => HarmonyModulesHelpers.checkModuleVar(parser.state, request))
.map(request => {
const dep = new HarmonyAcceptImportDependency(request, HarmonyModulesHelpers.getModuleVar(parser.state, request), expr.range);
dep.loc = expr.loc;
parser.state.current.addDependency(dep);
return dep;
});
if(dependencies.length > 0) {
const dep = new HarmonyAcceptDependency(expr.range, dependencies, false);
dep.loc = expr.loc;
parser.state.current.addDependency(dep);
}
});
}
};