Skip to content

Commit

Permalink
fix(inference): Robust parsing for shorthand object methods
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tmcw committed Feb 24, 2017
1 parent 7530a2b commit 802dc4c
Show file tree
Hide file tree
Showing 10 changed files with 792 additions and 326 deletions.
8 changes: 6 additions & 2 deletions lib/infer/finders.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,19 @@ function findTarget(path/*: Object */) {
path = path.get('declaration');
}

// var x = init;
if (t.isVariableDeclaration(path)) {
// var x = init;
path = path.get('declarations')[0];

// foo.x = TARGET
} else if (t.isExpressionStatement(path)) {
// foo.x = TARGET
path = path.get('expression').get('right');
} else if (t.isObjectProperty(path)) {
// var foo = { x: TARGET };
path = path.get('value');
}


return path.node && path;
}

Expand Down
23 changes: 16 additions & 7 deletions lib/infer/membership.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,23 @@ module.exports = function () {
}
}

// Foo = { bar() {} };
// Foo.prototype = { bar() {} };
// Foo.bar = { baz() {} };
// Shorthand methods on ordinary objects
if (n.isObjectMethod(path.node) &&
n.isObjectExpression(path.parentPath) &&
n.isAssignmentExpression(path.parentPath.parentPath)) {
identifiers = extractIdentifiers(path.parentPath.parentPath.get('left'));
if (identifiers.length >= 1) {
n.isObjectExpression(path.parentPath)) {

// Foo = { bar() {} };
// Foo.prototype = { bar() {} };
// Foo.bar = { baz() {} };
if (n.isAssignmentExpression(path.parentPath.parentPath)) {
identifiers = extractIdentifiers(path.parentPath.parentPath.get('left'));
if (identifiers.length >= 1) {
inferMembershipFromIdentifiers(comment, identifiers);
}
}

// var Foo = { bar() {} };
if (n.isVariableDeclarator(path.parentPath.parentPath)) {
identifiers = [path.parentPath.parentPath.get('id').node.name];
inferMembershipFromIdentifiers(comment, identifiers);
}
}
Expand Down
Loading

0 comments on commit 802dc4c

Please sign in to comment.