Skip to content

Commit a7e8949

Browse files
rubystargos
authored andcommittedSep 9, 2018
tools: add [src] links to child-process.html
handle exports. as an alternative to module.exports PR-URL: #22706 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 6579d05 commit a7e8949

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed
 

‎test/fixtures/apilinks/exports.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
// Support `exports` as an alternative to `module.exports`.
4+
5+
function Buffer() {};
6+
7+
exports.Buffer = Buffer;
8+
exports.fn1 = function fn1() {};
9+
10+
var fn2 = exports.fn2 = function() {};
11+
12+
function fn3() {};
13+
exports.fn3 = fn3;

‎test/fixtures/apilinks/exports.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"exports.Buffer": "exports.js#L5",
3+
"exports.fn1": "exports.js#L8",
4+
"exports.fn2": "exports.js#L10",
5+
"exports.fn3": "exports.js#L12"
6+
}

‎tools/doc/apilinks.js

+41-25
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ process.argv.slice(2).forEach((file) => {
6161

6262
// Scan for exports.
6363
const exported = { constructors: [], identifiers: [] };
64+
const indirect = {};
6465
program.forEach((statement) => {
6566
if (statement.type === 'ExpressionStatement') {
6667
const expr = statement.expression;
@@ -69,35 +70,52 @@ process.argv.slice(2).forEach((file) => {
6970
let lhs = expr.left;
7071
if (expr.left.object.type === 'MemberExpression') lhs = lhs.object;
7172
if (lhs.type !== 'MemberExpression') return;
72-
if (lhs.object.name !== 'module') return;
73-
if (lhs.property.name !== 'exports') return;
74-
75-
let rhs = expr.right;
76-
while (rhs.type === 'AssignmentExpression') rhs = rhs.right;
77-
78-
if (rhs.type === 'NewExpression') {
79-
exported.constructors.push(rhs.callee.name);
80-
} else if (rhs.type === 'ObjectExpression') {
81-
rhs.properties.forEach((property) => {
82-
if (property.value.type === 'Identifier') {
83-
exported.identifiers.push(property.value.name);
84-
if (/^[A-Z]/.test(property.value.name[0])) {
85-
exported.constructors.push(property.value.name);
86-
}
73+
if (lhs.object.name === 'exports') {
74+
const name = lhs.property.name;
75+
if (expr.right.type === 'FunctionExpression') {
76+
definition[`${basename}.${name}`] =
77+
`${link}#L${statement.loc.start.line}`;
78+
} else if (expr.right.type === 'Identifier') {
79+
if (expr.right.name === name) {
80+
indirect[name] = `${basename}.${name}`;
8781
}
88-
});
89-
} else if (rhs.type === 'Identifier') {
90-
exported.identifiers.push(rhs.name);
82+
} else {
83+
exported.identifiers.push(name);
84+
}
85+
} else if (lhs.object.name === 'module') {
86+
if (lhs.property.name !== 'exports') return;
87+
88+
let rhs = expr.right;
89+
while (rhs.type === 'AssignmentExpression') rhs = rhs.right;
90+
91+
if (rhs.type === 'NewExpression') {
92+
exported.constructors.push(rhs.callee.name);
93+
} else if (rhs.type === 'ObjectExpression') {
94+
rhs.properties.forEach((property) => {
95+
if (property.value.type === 'Identifier') {
96+
exported.identifiers.push(property.value.name);
97+
if (/^[A-Z]/.test(property.value.name[0])) {
98+
exported.constructors.push(property.value.name);
99+
}
100+
}
101+
});
102+
} else if (rhs.type === 'Identifier') {
103+
exported.identifiers.push(rhs.name);
104+
}
91105
}
92106
} else if (statement.type === 'VariableDeclaration') {
93107
for (const decl of statement.declarations) {
94108
let init = decl.init;
95109
while (init && init.type === 'AssignmentExpression') init = init.left;
96110
if (!init || init.type !== 'MemberExpression') continue;
97-
if (init.object.name !== 'module') continue;
98-
if (init.property.name !== 'exports') continue;
99-
exported.constructors.push(decl.id.name);
100-
definition[decl.id.name] = `${link}#L${statement.loc.start.line}`;
111+
if (init.object.name === 'exports') {
112+
definition[`${basename}.${init.property.name}`] =
113+
`${link}#L${statement.loc.start.line}`;
114+
} else if (init.object.name === 'module') {
115+
if (init.property.name !== 'exports') continue;
116+
exported.constructors.push(decl.id.name);
117+
definition[decl.id.name] = `${link}#L${statement.loc.start.line}`;
118+
}
101119
}
102120
}
103121
});
@@ -107,10 +125,8 @@ process.argv.slice(2).forEach((file) => {
107125
// ClassName.foo = ...;
108126
// ClassName.prototype.foo = ...;
109127
// function Identifier(...) {...};
110-
// class Foo {...}
128+
// class Foo {...};
111129
//
112-
const indirect = {};
113-
114130
program.forEach((statement) => {
115131
if (statement.type === 'ExpressionStatement') {
116132
const expr = statement.expression;

0 commit comments

Comments
 (0)
Please sign in to comment.