Skip to content

Commit

Permalink
tools: add [src] links to child-process.html
Browse files Browse the repository at this point in the history
handle exports. as an alternative to module.exports

PR-URL: #22706
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
rubys authored and targos committed Sep 9, 2018
1 parent 6579d05 commit a7e8949
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 25 deletions.
13 changes: 13 additions & 0 deletions test/fixtures/apilinks/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

// Support `exports` as an alternative to `module.exports`.

function Buffer() {};

exports.Buffer = Buffer;
exports.fn1 = function fn1() {};

var fn2 = exports.fn2 = function() {};

function fn3() {};
exports.fn3 = fn3;
6 changes: 6 additions & 0 deletions test/fixtures/apilinks/exports.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"exports.Buffer": "exports.js#L5",
"exports.fn1": "exports.js#L8",
"exports.fn2": "exports.js#L10",
"exports.fn3": "exports.js#L12"
}
66 changes: 41 additions & 25 deletions tools/doc/apilinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ process.argv.slice(2).forEach((file) => {

// Scan for exports.
const exported = { constructors: [], identifiers: [] };
const indirect = {};
program.forEach((statement) => {
if (statement.type === 'ExpressionStatement') {
const expr = statement.expression;
Expand All @@ -69,35 +70,52 @@ process.argv.slice(2).forEach((file) => {
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 (lhs.object.name === 'exports') {
const name = lhs.property.name;
if (expr.right.type === 'FunctionExpression') {
definition[`${basename}.${name}`] =
`${link}#L${statement.loc.start.line}`;
} else if (expr.right.type === 'Identifier') {
if (expr.right.name === name) {
indirect[name] = `${basename}.${name}`;
}
});
} else if (rhs.type === 'Identifier') {
exported.identifiers.push(rhs.name);
} else {
exported.identifiers.push(name);
}
} else if (lhs.object.name === 'module') {
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 (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);
definition[decl.id.name] = `${link}#L${statement.loc.start.line}`;
if (init.object.name === 'exports') {
definition[`${basename}.${init.property.name}`] =
`${link}#L${statement.loc.start.line}`;
} else if (init.object.name === 'module') {
if (init.property.name !== 'exports') continue;
exported.constructors.push(decl.id.name);
definition[decl.id.name] = `${link}#L${statement.loc.start.line}`;
}
}
}
});
Expand All @@ -107,10 +125,8 @@ process.argv.slice(2).forEach((file) => {
// ClassName.foo = ...;
// ClassName.prototype.foo = ...;
// function Identifier(...) {...};
// class Foo {...}
// class Foo {...};
//
const indirect = {};

program.forEach((statement) => {
if (statement.type === 'ExpressionStatement') {
const expr = statement.expression;
Expand Down

0 comments on commit a7e8949

Please sign in to comment.