From 649fcbba6f7ea11bc82ad5073aaf2303ede72d60 Mon Sep 17 00:00:00 2001 From: Kyle Farnung Date: Wed, 5 Sep 2018 14:52:26 -0700 Subject: [PATCH] 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 Reviewed-By: James M Snell Reviewed-By: Anna Henningsen --- test/fixtures/apilinks/root.js | 10 ++++++++++ test/fixtures/apilinks/root.json | 2 ++ tools/doc/apilinks.js | 6 ++++-- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/apilinks/root.js create mode 100644 test/fixtures/apilinks/root.json diff --git a/test/fixtures/apilinks/root.js b/test/fixtures/apilinks/root.js new file mode 100644 index 00000000000000..6cf9fee945ac24 --- /dev/null +++ b/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; +} diff --git a/test/fixtures/apilinks/root.json b/test/fixtures/apilinks/root.json new file mode 100644 index 00000000000000..2c63c0851048d8 --- /dev/null +++ b/test/fixtures/apilinks/root.json @@ -0,0 +1,2 @@ +{ +} diff --git a/tools/doc/apilinks.js b/tools/doc/apilinks.js index 0a33125a17b103..d7ec5684b79705 100644 --- a/tools/doc/apilinks.js +++ b/tools/doc/apilinks.js @@ -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') {