Skip to content

Commit

Permalink
tools,doc: apilinks should handle root scenarios
Browse files Browse the repository at this point in the history
* Prevent crash when setting root properties
* Allow return outside of function

PR-URL: #22721
Reviewed-By: Sam Ruby <rubys@intertwingly.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
  • Loading branch information
kfarnung committed Sep 10, 2018
1 parent 66f563d commit 3bdc61d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions test/fixtures/apilinks/root.js
@@ -0,0 +1,10 @@
'use strict';

// Set root member
let foo = true;
foo = false;

// Return outside of function
if (!foo) {
return;
}
2 changes: 2 additions & 0 deletions test/fixtures/apilinks/root.json
@@ -0,0 +1,2 @@
{
}
6 changes: 4 additions & 2 deletions tools/doc/apilinks.js
Expand Up @@ -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
Expand All @@ -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') {
Expand Down

0 comments on commit 3bdc61d

Please sign in to comment.