|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Scan API sources for definitions. |
| 4 | +// |
| 5 | +// Note the output is produced based on a world class parser, adherence to |
| 6 | +// conventions, and a bit of guess work. Examples: |
| 7 | +// |
| 8 | +// * We scan for top level module.exports statements, and determine what |
| 9 | +// is exported by looking at the source code only (i.e., we don't do |
| 10 | +// an eval). If exports include `Foo`, it probably is a class, whereas |
| 11 | +// if what is exported is `constants` it probably is prefixed by the |
| 12 | +// basename of the source file (e.g., `zlib`), unless that source file is |
| 13 | +// `buffer.js`, in which case the name is just `buf`. unless the constant |
| 14 | +// is `kMaxLength`, in which case it is `buffer`. |
| 15 | +// |
| 16 | +// * We scan for top level definitions for those exports, handling |
| 17 | +// most common cases (e.g., `X.prototype.foo =`, `X.foo =`, |
| 18 | +// `function X(...) {...}`). Over time, we expect to handle more |
| 19 | +// cases (example: ES2015 class definitions). |
| 20 | + |
| 21 | +const acorn = require('../../deps/acorn'); |
| 22 | +const fs = require('fs'); |
| 23 | +const path = require('path'); |
| 24 | +const child_process = require('child_process'); |
| 25 | + |
| 26 | +// Run a command, capturing stdout, ignoring errors. |
| 27 | +function execSync(command) { |
| 28 | + try { |
| 29 | + return child_process.execSync( |
| 30 | + command, |
| 31 | + { stdio: ['ignore', null, 'ignore'] } |
| 32 | + ).toString().trim(); |
| 33 | + } catch { |
| 34 | + return ''; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// Determine orign repo and tag (or hash) of the most recent commit. |
| 39 | +const local_branch = execSync('git name-rev --name-only HEAD'); |
| 40 | +const tracking_remote = execSync(`git config branch.${local_branch}.remote`); |
| 41 | +const remote_url = execSync(`git config remote.${tracking_remote}.url`); |
| 42 | +const repo = (remote_url.match(/(\w+\/\w+)\.git\r?\n?$/) || |
| 43 | + ['', 'nodejs/node'])[1]; |
| 44 | + |
| 45 | +const hash = execSync('git log -1 --pretty=%H') || 'master'; |
| 46 | +const tag = execSync(`git describe --contains ${hash}`).split('\n')[0] || hash; |
| 47 | + |
| 48 | +// Extract definitions from each file specified. |
| 49 | +const definition = {}; |
| 50 | +process.argv.slice(2).forEach((file) => { |
| 51 | + const basename = path.basename(file, '.js'); |
| 52 | + |
| 53 | + // Parse source. |
| 54 | + const source = fs.readFileSync(file, 'utf8'); |
| 55 | + const ast = acorn.parse(source, { ecmaVersion: 10, locations: true }); |
| 56 | + const program = ast.body; |
| 57 | + |
| 58 | + // Scan for exports. |
| 59 | + const exported = { constructors: [], identifiers: [] }; |
| 60 | + program.forEach((statement) => { |
| 61 | + if (statement.type !== 'ExpressionStatement') return; |
| 62 | + const expr = statement.expression; |
| 63 | + if (expr.type !== 'AssignmentExpression') return; |
| 64 | + |
| 65 | + let lhs = expr.left; |
| 66 | + if (expr.left.object.type === 'MemberExpression') lhs = lhs.object; |
| 67 | + if (lhs.type !== 'MemberExpression') return; |
| 68 | + if (lhs.object.name !== 'module') return; |
| 69 | + if (lhs.property.name !== 'exports') return; |
| 70 | + |
| 71 | + let rhs = expr.right; |
| 72 | + while (rhs.type === 'AssignmentExpression') rhs = rhs.right; |
| 73 | + |
| 74 | + if (rhs.type === 'NewExpression') { |
| 75 | + exported.constructors.push(rhs.callee.name); |
| 76 | + } else if (rhs.type === 'ObjectExpression') { |
| 77 | + rhs.properties.forEach((property) => { |
| 78 | + if (property.value.type === 'Identifier') { |
| 79 | + exported.identifiers.push(property.value.name); |
| 80 | + if (/^[A-Z]/.test(property.value.name[0])) { |
| 81 | + exported.constructors.push(property.value.name); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + } else if (rhs.type === 'Identifier') { |
| 86 | + exported.identifiers.push(rhs.name); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + // Scan for definitions matching those exports; currently supports: |
| 91 | + // |
| 92 | + // ClassName.foo = ...; |
| 93 | + // ClassName.prototype.foo = ...; |
| 94 | + // function Identifier(...) {...}; |
| 95 | + // |
| 96 | + const link = `https://github.com/${repo}/blob/${tag}/` + |
| 97 | + path.relative('.', file).replace(/\\/g, '/'); |
| 98 | + |
| 99 | + program.forEach((statement) => { |
| 100 | + if (statement.type === 'ExpressionStatement') { |
| 101 | + const expr = statement.expression; |
| 102 | + if (expr.type !== 'AssignmentExpression') return; |
| 103 | + if (expr.left.type !== 'MemberExpression') return; |
| 104 | + |
| 105 | + let object; |
| 106 | + if (expr.left.object.type === 'MemberExpression') { |
| 107 | + if (expr.left.object.property.name !== 'prototype') return; |
| 108 | + object = expr.left.object.object; |
| 109 | + } else if (expr.left.object.type === 'Identifier') { |
| 110 | + object = expr.left.object; |
| 111 | + } else { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + if (!exported.constructors.includes(object.name)) return; |
| 116 | + |
| 117 | + let objectName = object.name; |
| 118 | + if (expr.left.object.type === 'MemberExpression') { |
| 119 | + objectName = objectName.toLowerCase(); |
| 120 | + if (objectName === 'buffer') objectName = 'buf'; |
| 121 | + } |
| 122 | + |
| 123 | + let name = expr.left.property.name; |
| 124 | + if (expr.left.computed) { |
| 125 | + name = `${objectName}[${name}]`; |
| 126 | + } else { |
| 127 | + name = `${objectName}.${name}`; |
| 128 | + } |
| 129 | + |
| 130 | + definition[name] = `${link}#L${statement.loc.start.line}`; |
| 131 | + } else if (statement.type === 'FunctionDeclaration') { |
| 132 | + const name = statement.id.name; |
| 133 | + if (!exported.identifiers.includes(name)) return; |
| 134 | + if (basename.startsWith('_')) return; |
| 135 | + definition[`${basename}.${name}`] = |
| 136 | + `${link}#L${statement.loc.start.line}`; |
| 137 | + } |
| 138 | + }); |
| 139 | +}); |
| 140 | + |
| 141 | +console.log(JSON.stringify(definition, null, 2)); |
0 commit comments