Skip to content

Commit a7e8949

Browse files
rubystargos
authored andcommitted
tools: add [src] links to child-process.html
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>
1 parent 6579d05 commit a7e8949

3 files changed

Lines changed: 60 additions & 25 deletions

File tree

‎test/fixtures/apilinks/exports.js‎

Lines changed: 13 additions & 0 deletions
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+
functionBuffer(){};
6+
7+
exports.Buffer=Buffer;
8+
exports.fn1=functionfn1(){};
9+
10+
varfn2=exports.fn2=function(){};
11+
12+
functionfn3(){};
13+
exports.fn3=fn3;
Lines changed: 6 additions & 0 deletions
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‎

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

6262
// Scan for exports.
6363
constexported={constructors: [],identifiers: []};
64+
constindirect={};
6465
program.forEach((statement)=>{
6566
if(statement.type==='ExpressionStatement'){
6667
constexpr=statement.expression;
@@ -69,35 +70,52 @@ process.argv.slice(2).forEach((file) => {
6970
letlhs=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-
letrhs=expr.right;
76-
while(rhs.type==='AssignmentExpression')rhs=rhs.right;
77-
78-
if(rhs.type==='NewExpression'){
79-
exported.constructors.push(rhs.callee.name);
80-
}elseif(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+
constname=lhs.property.name;
75+
if(expr.right.type==='FunctionExpression'){
76+
definition[`${basename}.${name}`]=
77+
`${link}#L${statement.loc.start.line}`;
78+
}elseif(expr.right.type==='Identifier'){
79+
if(expr.right.name===name){
80+
indirect[name]=`${basename}.${name}`;
8781
}
88-
});
89-
}elseif(rhs.type==='Identifier'){
90-
exported.identifiers.push(rhs.name);
82+
}else{
83+
exported.identifiers.push(name);
84+
}
85+
}elseif(lhs.object.name==='module'){
86+
if(lhs.property.name!=='exports')return;
87+
88+
letrhs=expr.right;
89+
while(rhs.type==='AssignmentExpression')rhs=rhs.right;
90+
91+
if(rhs.type==='NewExpression'){
92+
exported.constructors.push(rhs.callee.name);
93+
}elseif(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+
}elseif(rhs.type==='Identifier'){
103+
exported.identifiers.push(rhs.name);
104+
}
91105
}
92106
}elseif(statement.type==='VariableDeclaration'){
93107
for(constdeclofstatement.declarations){
94108
letinit=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+
}elseif(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-
constindirect={};
113-
114130
program.forEach((statement)=>{
115131
if(statement.type==='ExpressionStatement'){
116132
constexpr=statement.expression;

0 commit comments

Comments
 (0)