Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
doc: update apilinks to handle other scenarios
Browse files Browse the repository at this point in the history
* Prevent crash when setting root properties
* Allow return outside of function
  • Loading branch information
kfarnung committed Sep 6, 2018
1 parent e38ec0d commit eb7bc2c
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
6 changes: 4 additions & 2 deletions tools/doc/apilinks.js
Original file line number Diff line number Diff line change
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 @@ -67,8 +69,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 !== 'module') return;
if (lhs.property.name !== 'exports') return;

Expand Down

0 comments on commit eb7bc2c

Please sign in to comment.