Skip to content

Commit 802dc4c

Browse files
authored
fix(inference): Robust parsing for shorthand object methods
This extends findTarget and the membership interference steps, treating ObjectProperty within var-assigned objects the same as we did for ObjectMethods. It also adds tests that specifically test findTarget directly. Fixes #649 and fixes #678
1 parent 7530a2b commit 802dc4c

10 files changed

+792
-326
lines changed

lib/infer/finders.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ function findTarget(path/*: Object */) {
2222
path = path.get('declaration');
2323
}
2424

25-
// var x = init;
2625
if (t.isVariableDeclaration(path)) {
26+
// var x = init;
2727
path = path.get('declarations')[0];
2828

29-
// foo.x = TARGET
3029
} else if (t.isExpressionStatement(path)) {
30+
// foo.x = TARGET
3131
path = path.get('expression').get('right');
32+
} else if (t.isObjectProperty(path)) {
33+
// var foo = { x: TARGET };
34+
path = path.get('value');
3235
}
3336

37+
3438
return path.node && path;
3539
}
3640

lib/infer/membership.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,23 @@ module.exports = function () {
296296
}
297297
}
298298

299-
// Foo = { bar() {} };
300-
// Foo.prototype = { bar() {} };
301-
// Foo.bar = { baz() {} };
299+
// Shorthand methods on ordinary objects
302300
if (n.isObjectMethod(path.node) &&
303-
n.isObjectExpression(path.parentPath) &&
304-
n.isAssignmentExpression(path.parentPath.parentPath)) {
305-
identifiers = extractIdentifiers(path.parentPath.parentPath.get('left'));
306-
if (identifiers.length >= 1) {
301+
n.isObjectExpression(path.parentPath)) {
302+
303+
// Foo = { bar() {} };
304+
// Foo.prototype = { bar() {} };
305+
// Foo.bar = { baz() {} };
306+
if (n.isAssignmentExpression(path.parentPath.parentPath)) {
307+
identifiers = extractIdentifiers(path.parentPath.parentPath.get('left'));
308+
if (identifiers.length >= 1) {
309+
inferMembershipFromIdentifiers(comment, identifiers);
310+
}
311+
}
312+
313+
// var Foo = { bar() {} };
314+
if (n.isVariableDeclarator(path.parentPath.parentPath)) {
315+
identifiers = [path.parentPath.parentPath.get('id').node.name];
307316
inferMembershipFromIdentifiers(comment, identifiers);
308317
}
309318
}

0 commit comments

Comments
 (0)