Skip to content

Commit 649fcbb

Browse files
kfarnungtargos
authored andcommitted
tools,doc: apilinks should handle root scenarios
* Prevent crash when setting root properties * Allow return outside of function PR-URL: #22721 Reviewed-By: Sam Ruby <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 9942117 commit 649fcbb

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

test/fixtures/apilinks/root.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
// Set root member
4+
let foo = true;
5+
foo = false;
6+
7+
// Return outside of function
8+
if (!foo) {
9+
return;
10+
}

test/fixtures/apilinks/root.json

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

tools/doc/apilinks.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ process.argv.slice(2).forEach((file) => {
5252

5353
// Parse source.
5454
const source = fs.readFileSync(file, 'utf8');
55-
const ast = acorn.parse(source, { ecmaVersion: 10, locations: true });
55+
const ast = acorn.parse(
56+
source,
57+
{ allowReturnOutsideFunction: true, ecmaVersion: 10, locations: true });
5658
const program = ast.body;
5759

5860
// Build link
@@ -68,8 +70,8 @@ process.argv.slice(2).forEach((file) => {
6870
if (expr.type !== 'AssignmentExpression') return;
6971

7072
let lhs = expr.left;
71-
if (expr.left.object.type === 'MemberExpression') lhs = lhs.object;
7273
if (lhs.type !== 'MemberExpression') return;
74+
if (lhs.object.type === 'MemberExpression') lhs = lhs.object;
7375
if (lhs.object.name === 'exports') {
7476
const name = lhs.property.name;
7577
if (expr.right.type === 'FunctionExpression') {

0 commit comments

Comments
 (0)