Skip to content

Commit e03a227

Browse files
authored
Merge pull request webpack#6055 from nikhilgaba/parserFix
Improve Parser to recognize 'new Foo(...)' type expressions
2 parents c71fd05 + 1083dea commit e03a227

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/Parser.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,14 @@ class Parser extends Tapable {
10471047
}
10481048

10491049
walkNewExpression(expression) {
1050+
const callee = this.evaluateExpression(expression.callee);
1051+
if(callee.isIdentifier()) {
1052+
const result = this.applyPluginsBailResult("new " + callee.identifier, expression);
1053+
if(result === true) {
1054+
return;
1055+
}
1056+
}
1057+
10501058
this.walkExpression(expression.callee);
10511059
if(expression.arguments)
10521060
this.walkExpressions(expression.arguments);

test/Parser.unittest.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ describe("Parser", () => {
191191
ijksub: ["test"]
192192
}
193193
],
194+
"new Foo(...)": [
195+
function() {
196+
new xyz("membertest");
197+
}, {
198+
xyz: ["membertest"]
199+
}
200+
],
194201
};
195202

196203
Object.keys(testCases).forEach((name) => {
@@ -237,6 +244,11 @@ describe("Parser", () => {
237244
testParser.state.expressions.push(expr.name);
238245
return true;
239246
});
247+
testParser.plugin("new xyz", (expr) => {
248+
if(!testParser.state.xyz) testParser.state.xyz = [];
249+
testParser.state.xyz.push(testParser.parseString(expr.arguments[0]));
250+
return true;
251+
});
240252
const actual = testParser.parse(source);
241253
should.strictEqual(typeof actual, "object");
242254
actual.should.be.eql(state);

0 commit comments

Comments
 (0)