Skip to content

Commit 85951d3

Browse files
committed
Merge branch 'master' into next
2 parents fd3c802 + e03a227 commit 85951d3

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/Parser.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class Parser extends Tapable {
103103
importCall: new SyncBailHook(["expression"]),
104104
call: new HookMap(() => new SyncBailHook(["expression"])),
105105
callAnyMember: new HookMap(() => new SyncBailHook(["expression"])),
106+
new: new HookMap(() => new SyncBailHook(["expression"])),
106107
expression: new HookMap(() => new SyncBailHook(["expression"])),
107108
expressionAnyMember: new HookMap(() => new SyncBailHook(["expression"])),
108109
program: new SyncBailHook(["ast", "comments"]),
@@ -126,6 +127,7 @@ class Parser extends Tapable {
126127
expressionConditionalOperator: /^expression \?:$/,
127128
callAnyMember: /^call (.+)\.\*$/,
128129
call: /^call (.+)$/,
130+
new: /^new (.+)$/,
129131
expressionAnyMember: /^expression (.+)\.\*$/,
130132
expression: /^expression (.+)$/,
131133
};
@@ -1200,6 +1202,17 @@ class Parser extends Tapable {
12001202
}
12011203

12021204
walkNewExpression(expression) {
1205+
const callee = this.evaluateExpression(expression.callee);
1206+
if(callee.isIdentifier()) {
1207+
const hook = this.hooks.new.for(callee.identifier);
1208+
if(hook !== undefined) {
1209+
const result = hook.call(expression);
1210+
if(result === true) {
1211+
return;
1212+
}
1213+
}
1214+
}
1215+
12031216
this.walkExpression(expression.callee);
12041217
if(expression.arguments)
12051218
this.walkExpressions(expression.arguments);

test/Parser.unittest.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ describe("Parser", () => {
194194
ijksub: ["test"]
195195
}
196196
],
197+
"new Foo(...)": [
198+
function() {
199+
new xyz("membertest");
200+
}, {
201+
xyz: ["membertest"]
202+
}
203+
],
197204
};
198205
/* eslint-enable no-undef */
199206
/* eslint-enable no-unused-vars */
@@ -243,6 +250,11 @@ describe("Parser", () => {
243250
testParser.state.expressions.push(expr.name);
244251
return true;
245252
});
253+
testParser.plugin("new xyz", (expr) => {
254+
if(!testParser.state.xyz) testParser.state.xyz = [];
255+
testParser.state.xyz.push(testParser.parseString(expr.arguments[0]));
256+
return true;
257+
});
246258
const actual = testParser.parse(source);
247259
should.strictEqual(typeof actual, "object");
248260
actual.should.be.eql(state);

0 commit comments

Comments
 (0)