Skip to content

Commit 2b40491

Browse files
author
chuckd
committed
Code review changes
1 parent c86cc80 commit 2b40491

File tree

2 files changed

+102
-78
lines changed

2 files changed

+102
-78
lines changed

lib/dependencies/AMDDefineDependencyParserPlugin.js

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,25 @@ class AMDDefineDependencyParserPlugin {
4141
}
4242

4343
apply(parser) {
44-
parser.hooks.call.for("define").tap(
45-
"AMDDefineDependencyParserPlugin",
46-
this.processCallDefine.bind(
47-
Object.create(this, {
48-
parser: { value: parser }
49-
})
50-
)
51-
);
44+
parser.hooks.call
45+
.for("define")
46+
.tap(
47+
"AMDDefineDependencyParserPlugin",
48+
this.processCallDefine.bind(this, parser)
49+
);
5250
}
5351

54-
processArray(expr, param, identifiers, namedModule) {
55-
const parser = this.parser;
52+
processArray(parser, expr, param, identifiers, namedModule) {
5653
if (param.isArray()) {
5754
param.items.forEach((param, idx) => {
5855
if (
5956
param.isString() &&
6057
["require", "module", "exports"].includes(param.string)
6158
)
6259
identifiers[idx] = param.string;
63-
const result = this.processItem(expr, param, namedModule);
60+
const result = this.processItem(parser, expr, param, namedModule);
6461
if (result === undefined) {
65-
this.processContext(expr, param);
62+
this.processContext(parser, expr, param);
6663
}
6764
});
6865
return true;
@@ -102,13 +99,12 @@ class AMDDefineDependencyParserPlugin {
10299
return true;
103100
}
104101
}
105-
processItem(expr, param, namedModule) {
106-
const parser = this.parser;
102+
processItem(parser, expr, param, namedModule) {
107103
if (param.isConditional()) {
108104
param.options.forEach(param => {
109-
const result = this.processItem(expr, param);
105+
const result = this.processItem(parser, expr, param);
110106
if (result === undefined) {
111-
this.processContext(expr, param);
107+
this.processContext(parser, expr, param);
112108
}
113109
});
114110
return true;
@@ -136,7 +132,7 @@ class AMDDefineDependencyParserPlugin {
136132
return true;
137133
}
138134
}
139-
processContext(expr, param) {
135+
processContext(parser, expr, param) {
140136
const dep = ContextDependencyHelpers.create(
141137
AMDRequireContextDependency,
142138
param.range,
@@ -146,13 +142,12 @@ class AMDDefineDependencyParserPlugin {
146142
);
147143
if (!dep) return;
148144
dep.loc = expr.loc;
149-
dep.optional = !!this.parser.scope.inTry;
150-
this.parser.state.current.addDependency(dep);
145+
dep.optional = !!parser.scope.inTry;
146+
parser.state.current.addDependency(dep);
151147
return true;
152148
}
153149

154-
processCallDefine(expr) {
155-
const parser = this.parser;
150+
processCallDefine(parser, expr) {
156151
let array, fn, obj, namedModule;
157152
switch (expr.arguments.length) {
158153
case 1:
@@ -232,7 +227,13 @@ class AMDDefineDependencyParserPlugin {
232227
if (array) {
233228
identifiers = {};
234229
const param = parser.evaluateExpression(array);
235-
const result = this.processArray(expr, param, identifiers, namedModule);
230+
const result = this.processArray(
231+
parser,
232+
expr,
233+
param,
234+
identifiers,
235+
namedModule
236+
);
236237
if (!result) return;
237238
if (fnParams)
238239
fnParams = fnParams.slice(fnParamsOffset).filter((param, idx) => {
@@ -302,14 +303,26 @@ class AMDDefineDependencyParserPlugin {
302303
return true;
303304
}
304305

305-
newDefineDependency(...args) {
306-
return new AMDDefineDependency(...args);
306+
newDefineDependency(
307+
range,
308+
arrayRange,
309+
functionRange,
310+
objectRange,
311+
namedModule
312+
) {
313+
return new AMDDefineDependency(
314+
range,
315+
arrayRange,
316+
functionRange,
317+
objectRange,
318+
namedModule
319+
);
307320
}
308-
newRequireArrayDependency(...args) {
309-
return new AMDRequireArrayDependency(...args);
321+
newRequireArrayDependency(depsArray, range) {
322+
return new AMDRequireArrayDependency(depsArray, range);
310323
}
311-
newRequireItemDependency(...args) {
312-
return new AMDRequireItemDependency(...args);
324+
newRequireItemDependency(request, range) {
325+
return new AMDRequireItemDependency(request, range);
313326
}
314327
}
315328
module.exports = AMDDefineDependencyParserPlugin;

lib/dependencies/AMDRequireDependenciesBlockParserPlugin.js

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,20 @@ class AMDRequireDependenciesBlockParserPlugin {
4646
}
4747

4848
apply(parser) {
49-
parser.hooks.call.for("require").tap(
50-
"AMDRequireDependenciesBlockParserPlugin",
51-
this.processCallRequire.bind(
52-
Object.create(this, {
53-
parser: { value: parser }
54-
})
55-
)
56-
);
49+
parser.hooks.call
50+
.for("require")
51+
.tap(
52+
"AMDRequireDependenciesBlockParserPlugin",
53+
this.processCallRequire.bind(this, parser)
54+
);
5755
}
5856

59-
processArray(expr, param) {
60-
const parser = this.parser;
57+
processArray(parser, expr, param) {
6158
if (param.isArray()) {
6259
for (const p of param.items) {
63-
const result = this.processItem(expr, p);
60+
const result = this.processItem(parser, expr, p);
6461
if (result === undefined) {
65-
this.processContext(expr, p);
62+
this.processContext(parser, expr, p);
6663
}
6764
}
6865
return true;
@@ -99,13 +96,12 @@ class AMDRequireDependenciesBlockParserPlugin {
9996
return true;
10097
}
10198
}
102-
processItem(expr, param) {
103-
const parser = this.parser;
99+
processItem(parser, expr, param) {
104100
if (param.isConditional()) {
105101
for (const p of param.options) {
106-
const result = this.processItem(expr, p);
102+
const result = this.processItem(parser, expr, p);
107103
if (result === undefined) {
108-
this.processContext(expr, p);
104+
this.processContext(parser, expr, p);
109105
}
110106
}
111107
return true;
@@ -140,7 +136,7 @@ class AMDRequireDependenciesBlockParserPlugin {
140136
return true;
141137
}
142138
}
143-
processContext(expr, param) {
139+
processContext(parser, expr, param) {
144140
const dep = ContextDependencyHelpers.create(
145141
AMDRequireContextDependency,
146142
param.range,
@@ -150,39 +146,38 @@ class AMDRequireDependenciesBlockParserPlugin {
150146
);
151147
if (!dep) return;
152148
dep.loc = expr.loc;
153-
dep.optional = !!this.parser.scope.inTry;
154-
this.parser.state.current.addDependency(dep);
149+
dep.optional = !!parser.scope.inTry;
150+
parser.state.current.addDependency(dep);
155151
return true;
156152
}
157153

158-
processCallRequire(expr) {
159-
const processArrayForRequestString = param => {
160-
if (param.isArray()) {
161-
const result = param.items.map(item =>
162-
processItemForRequestString(item)
163-
);
164-
if (result.every(Boolean)) return result.join(" ");
165-
} else if (param.isConstArray()) {
166-
return param.array.join(" ");
167-
}
168-
};
154+
processArrayForRequestString(param) {
155+
if (param.isArray()) {
156+
const result = param.items.map(item =>
157+
this.processItemForRequestString(item)
158+
);
159+
if (result.every(Boolean)) return result.join(" ");
160+
} else if (param.isConstArray()) {
161+
return param.array.join(" ");
162+
}
163+
}
169164

170-
const processItemForRequestString = param => {
171-
if (param.isConditional()) {
172-
const result = param.options.map(item =>
173-
processItemForRequestString(item)
174-
);
175-
if (result.every(Boolean)) return result.join("|");
176-
} else if (param.isString()) {
177-
return param.string;
178-
}
179-
};
165+
processItemForRequestString(param) {
166+
if (param.isConditional()) {
167+
const result = param.options.map(item =>
168+
this.processItemForRequestString(item)
169+
);
170+
if (result.every(Boolean)) return result.join("|");
171+
} else if (param.isString()) {
172+
return param.string;
173+
}
174+
}
180175

176+
processCallRequire(parser, expr) {
181177
let param;
182178
let dep;
183179
let result;
184180

185-
const parser = this.parser;
186181
const old = parser.state.current;
187182

188183
if (expr.arguments.length >= 1) {
@@ -194,14 +189,14 @@ class AMDRequireDependenciesBlockParserPlugin {
194189
expr.arguments.length > 2 ? expr.arguments[2].range : null,
195190
parser.state.module,
196191
expr.loc,
197-
processArrayForRequestString(param)
192+
this.processArrayForRequestString(param)
198193
);
199194
parser.state.current = dep;
200195
}
201196

202197
if (expr.arguments.length === 1) {
203198
parser.inScope([], () => {
204-
result = this.processArray(expr, param);
199+
result = this.processArray(parser, expr, param);
205200
});
206201
parser.state.current = old;
207202
if (!result) return;
@@ -212,7 +207,7 @@ class AMDRequireDependenciesBlockParserPlugin {
212207
if (expr.arguments.length === 2 || expr.arguments.length === 3) {
213208
try {
214209
parser.inScope([], () => {
215-
result = this.processArray(expr, param);
210+
result = this.processArray(parser, expr, param);
216211
});
217212
if (!result) {
218213
dep = new UnsupportedDependency("unsupported", expr.range);
@@ -246,14 +241,30 @@ class AMDRequireDependenciesBlockParserPlugin {
246241
}
247242
}
248243

249-
newRequireDependenciesBlock(...args) {
250-
return new AMDRequireDependenciesBlock(...args);
244+
newRequireDependenciesBlock(
245+
expr,
246+
arrayRange,
247+
functionRange,
248+
errorCallbackRange,
249+
module,
250+
loc,
251+
request
252+
) {
253+
return new AMDRequireDependenciesBlock(
254+
expr,
255+
arrayRange,
256+
functionRange,
257+
errorCallbackRange,
258+
module,
259+
loc,
260+
request
261+
);
251262
}
252-
newRequireItemDependency(...args) {
253-
return new AMDRequireItemDependency(...args);
263+
newRequireItemDependency(request, range) {
264+
return new AMDRequireItemDependency(request, range);
254265
}
255-
newRequireArrayDependency(...args) {
256-
return new AMDRequireArrayDependency(...args);
266+
newRequireArrayDependency(depsArray, range) {
267+
return new AMDRequireArrayDependency(depsArray, range);
257268
}
258269
}
259270
module.exports = AMDRequireDependenciesBlockParserPlugin;

0 commit comments

Comments
 (0)