Skip to content

Membership inference improvements #558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 71 additions & 2 deletions lib/infer/membership.js
Original file line number Diff line number Diff line change
@@ -31,6 +31,45 @@ function findLendsIdentifiers(node) {
}
}

/**
* Extract and return the identifiers for expressions of type this.foo
*
* @param {NodePath} path AssignmentExpression, MemberExpression, or Identifier
* @returns {Array<string>} identifiers
* @private
*/
function extractThis(path) {
var identifiers = [];

path.traverse({
/**
* Add the resolved identifier of this in a path to the identifiers array
* @param {Object} path ast path
* @returns {undefined} has side-effects
* @private
*/
ThisExpression: function (path) {
var scope = path.scope;

while (n.isBlockStatement(scope.block)) {
scope = scope.parent;
}

if (n.isClassMethod(scope.block)) {
identifiers.push(scope.path.parentPath.parentPath.node.id.name, 'prototype');
}

if (n.isFunctionDeclaration(scope.block) ||
n.isFunctionExpression(scope.block)) {
identifiers.push(scope.block.id.name , 'prototype');
}
}
});

return identifiers;
}


/**
* Extract and return the chain of identifiers from the left hand side of expressions
* of the forms `Foo = ...`, `Foo.bar = ...`, `Foo.bar.baz = ...`, etc.
@@ -76,6 +115,33 @@ function countModuleIdentifiers(comment, identifiers) {
return 0;
}

/**
* Returns the comment object after normalizing Foo.prototype and Foo# expressions
* @param {Object} comment parsed comment
* @returns {Object} the normalized comment
*/
function normalizeMemberof(comment) {
var memberof = comment.memberof;

var isPrototype = /.prototype$/;

if (memberof.match(isPrototype) !== null) {
comment.memberof = memberof.replace(isPrototype, '');
comment.scope = 'instance';

return comment;
}

var isInstanceMember = /#$/;

if (memberof.match(isInstanceMember) !== null) {
comment.memberof = memberof.replace(isInstanceMember, '');
comment.scope = 'instance';
}

return comment;
}

/**
* Uses code structure to infer `memberof`, `instance`, and `static`
* tags from the placement of JSDoc
@@ -147,7 +213,7 @@ module.exports = function () {
}

if (comment.memberof) {
return comment;
return normalizeMemberof(comment);
}

if (!comment.context.ast) {
@@ -176,7 +242,10 @@ module.exports = function () {
// Foo.prototype.bar = ...;
// Foo.bar.baz = ...;
if (n.isMemberExpression(path.node)) {
identifiers = extractIdentifiers(path);
identifiers = [].concat(
extractThis(path),
extractIdentifiers(path)
);
if (identifiers.length >= 2) {
inferMembershipFromIdentifiers(comment, identifiers.slice(0, -1));
}
62 changes: 61 additions & 1 deletion test/lib/infer/membership.js
Original file line number Diff line number Diff line change
@@ -38,7 +38,29 @@ test('inferMembership - explicit', function (t) {
})[0], ['memberof', 'scope']), {
memberof: 'Bar',
scope: 'static'
}, 'explicit');
}, 'explicit, static');

t.deepEqual(pick(evaluate(function () {
/**
* Test
* @memberof Bar#
*/
Foo.bar = 0;
})[0], ['memberof', 'scope']), {
memberof: 'Bar',
scope: 'instance'
}, 'explicit, instance');

t.deepEqual(pick(evaluate(function () {
/**
* Test
* @memberof Bar.prototype
*/
Foo.bar = 0;
})[0], ['memberof', 'scope']), {
memberof: 'Bar',
scope: 'instance'
}, 'explicit, prototype');

t.deepEqual(pick(evaluate(function () {
/** Test */
@@ -123,6 +145,44 @@ test('inferMembership - explicit', function (t) {
scope: 'static'
}, 'variable object assignment, function');

t.deepEqual(pick(evaluate(function () {
function Foo() {
{
/** */
this.bar = 0;
}
}
})[0], ['memberof', 'scope']), {
memberof: 'Foo',
scope: 'instance'
}, 'constructor function declaration assignment');

t.deepEqual(pick(evaluate(function () {
var Foo = function Bar() {
{
/** */
this.baz = 0;
}
};
})[0], ['memberof', 'scope']), {
memberof: 'Bar',
scope: 'instance'
}, 'constructor function expression assignment');

t.deepEqual(pick(evaluate(function () {
class Foo {
constructor() {
{
/** */
this.bar = 0;
}
}
}
})[0], ['memberof', 'scope']), {
memberof: 'Foo',
scope: 'instance'
}, 'constructor assignment');

t.deepEqual(pick(evaluate(function () {
/** Test */
module.exports = function () {};