tools,doc: apilinks should handle root scenarios

* Prevent crash when setting root properties
* Allow return outside of function

PR-URL: https://github.com/nodejs/node/pull/22721
Reviewed-By: Sam Ruby <rubys@intertwingly.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
pull/22721/head
Kyle Farnung 2018-09-05 14:52:26 -07:00
parent 66f563dec7
commit 3bdc61d4d1
3 changed files with 16 additions and 2 deletions

10
test/fixtures/apilinks/root.js vendored 100644
View File

@ -0,0 +1,10 @@
'use strict';
// Set root member
let foo = true;
foo = false;
// Return outside of function
if (!foo) {
return;
}

View File

@ -0,0 +1,2 @@
{
}

View File

@ -52,7 +52,9 @@ process.argv.slice(2).forEach((file) => {
// Parse source.
const source = fs.readFileSync(file, 'utf8');
const ast = acorn.parse(source, { ecmaVersion: 10, locations: true });
const ast = acorn.parse(
source,
{ allowReturnOutsideFunction: true, ecmaVersion: 10, locations: true });
const program = ast.body;
// Build link
@ -68,8 +70,8 @@ process.argv.slice(2).forEach((file) => {
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.type === 'MemberExpression') lhs = lhs.object;
if (lhs.object.name === 'exports') {
const name = lhs.property.name;
if (expr.right.type === 'FunctionExpression') {