Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions test/fixtures/apilinks/reverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

// Parallel assignment to the exported variable and module.exports.

function ok() {
}

const asserts = module.exports = ok;

asserts.ok = ok;
3 changes: 3 additions & 0 deletions test/fixtures/apilinks/reverse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"asserts.ok": "reverse.js#L10"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance we could figure out that this should link to L5? (It seems too tricky to me but I figured I’d ask 🙂)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look. I'm inclined to only do that for statements of the form:

x.y = y;

In other words, not for the following (which would be an alias):

x.y = z;

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that sounds reasonable, yeah :)

}
60 changes: 35 additions & 25 deletions tools/doc/apilinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,32 +58,42 @@ process.argv.slice(2).forEach((file) => {
// Scan for exports.
const exported = { constructors: [], identifiers: [] };
program.forEach((statement) => {
if (statement.type !== 'ExpressionStatement') return;
const expr = statement.expression;
if (expr.type !== 'AssignmentExpression') return;

let lhs = expr.left;
if (expr.left.object.type === 'MemberExpression') lhs = lhs.object;
if (lhs.type !== 'MemberExpression') return;
if (lhs.object.name !== 'module') return;
if (lhs.property.name !== 'exports') return;

let rhs = expr.right;
while (rhs.type === 'AssignmentExpression') rhs = rhs.right;

if (rhs.type === 'NewExpression') {
exported.constructors.push(rhs.callee.name);
} else if (rhs.type === 'ObjectExpression') {
rhs.properties.forEach((property) => {
if (property.value.type === 'Identifier') {
exported.identifiers.push(property.value.name);
if (/^[A-Z]/.test(property.value.name[0])) {
exported.constructors.push(property.value.name);
if (statement.type === 'ExpressionStatement') {
const expr = statement.expression;
if (expr.type !== 'AssignmentExpression') return;

let lhs = expr.left;
if (expr.left.object.type === 'MemberExpression') lhs = lhs.object;
if (lhs.type !== 'MemberExpression') return;
if (lhs.object.name !== 'module') return;
if (lhs.property.name !== 'exports') return;

let rhs = expr.right;
while (rhs.type === 'AssignmentExpression') rhs = rhs.right;

if (rhs.type === 'NewExpression') {
exported.constructors.push(rhs.callee.name);
} else if (rhs.type === 'ObjectExpression') {
rhs.properties.forEach((property) => {
if (property.value.type === 'Identifier') {
exported.identifiers.push(property.value.name);
if (/^[A-Z]/.test(property.value.name[0])) {
exported.constructors.push(property.value.name);
}
}
}
});
} else if (rhs.type === 'Identifier') {
exported.identifiers.push(rhs.name);
});
} else if (rhs.type === 'Identifier') {
exported.identifiers.push(rhs.name);
}
} else if (statement.type === 'VariableDeclaration') {
for (const decl of statement.declarations) {
let init = decl.init;
while (init && init.type === 'AssignmentExpression') init = init.left;
if (!init || init.type !== 'MemberExpression') continue;
if (init.object.name !== 'module') continue;
if (init.property.name !== 'exports') continue;
exported.constructors.push(decl.id.name);
}
}
});

Expand Down