-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: flakey5 <[email protected]>
- Loading branch information
Showing
5 changed files
with
152 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/generators/api-links/utils/handleExportedObjectExpression.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// @ts-check | ||
'use strict'; | ||
|
||
import { CONSTRUCTOR_EXPRESSION } from '../constants.mjs'; | ||
|
||
/** | ||
* @param {import('../types').ProgramExports} exports | ||
* @param {import('acorn').NewExpression} rhs | ||
*/ | ||
function handleNewExpression(exports, rhs) { | ||
// module.exports = new Asd() | ||
exports.ctors.push(rhs.callee.name); | ||
} | ||
|
||
/** | ||
* @param {import('../types').ProgramExports} exports | ||
* @param {import('acorn').ObjectExpression} rhs | ||
*/ | ||
function handleObjectExpression(exports, rhs) { | ||
// module.exports = {} | ||
// We need to go through all of the properties and register them | ||
rhs.properties.forEach(({ value }) => { | ||
switch (value.type) { | ||
case 'Identifier': { | ||
exports.identifiers.push(value.name); | ||
|
||
if (CONSTRUCTOR_EXPRESSION.test(value.name[0])) { | ||
exports.ctors.push(value.name); | ||
} | ||
|
||
break; | ||
} | ||
case 'CallExpression': { | ||
if (value.callee.name !== 'deprecate') { | ||
break; | ||
} | ||
|
||
// Handle exports wrapped in the `deprecate` function | ||
// Ex/ https://github.com/nodejs/node/blob/e96072ad57348ce423a8dd7639dcc3d1c34e847d/lib/buffer.js#L1334 | ||
|
||
exports.identifiers.push(value.arguments[0].name); | ||
|
||
break; | ||
} | ||
default: { | ||
// Not relevant | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param {import('../types').ProgramExports} exports | ||
* @param {import('acorn').Identifier} rhs | ||
*/ | ||
function handleIdentifier(exports, rhs) { | ||
// Something else, let's save it for when we're searching for | ||
// declarations | ||
if (rhs.name !== undefined) { | ||
exports.identifiers.push(rhs.name); | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('../types').ProgramExports} exports | ||
* @param {import('acorn').AssignmentExpression} param0 | ||
*/ | ||
export function handleExportedObjectExpression(exports, { right: rhs }) { | ||
// We need to move right until we find the value of the assignment. | ||
// (if `a=b`, we want `b`) | ||
while (rhs.type === 'AssignmentExpression') { | ||
rhs = rhs.right; | ||
} | ||
|
||
switch (rhs.type) { | ||
/** @see https://github.com/estree/estree/blob/master/es5.md#newexpression */ | ||
case 'NewExpression': { | ||
handleNewExpression(exports, rhs); | ||
break; | ||
} | ||
/** @see https://github.com/estree/estree/blob/master/es5.md#objectexpression */ | ||
case 'ObjectExpression': { | ||
handleObjectExpression(exports, rhs); | ||
break; | ||
} | ||
/** @see https://github.com/estree/estree/blob/master/es5.md#identifier */ | ||
case 'Identifier': { | ||
handleIdentifier(exports, rhs); | ||
break; | ||
} | ||
default: { | ||
// Not relevant | ||
break; | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/generators/api-links/utils/handleExportedPropertyExpression.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// @ts-check | ||
'use strict'; | ||
|
||
/** | ||
* @param {import('../types.d.ts').ProgramExports} exports | ||
* @param {import('acorn').AssignmentExpression} param1 | ||
* @param {string} basename | ||
* @param {Record<string, number>} nameToLineNumberMap | ||
*/ | ||
export function handleExportedPropertyExpression( | ||
exports, | ||
{ left: lhs, right: rhs, loc }, | ||
basename, | ||
nameToLineNumberMap | ||
) { | ||
switch (rhs.type) { | ||
/** @see https://github.com/estree/estree/blob/master/es5.md#functionexpression */ | ||
case 'FunctionExpression': { | ||
// module.exports.something = () => {} | ||
nameToLineNumberMap[`${basename}.${lhs.property.name}`] = loc.start.line; | ||
|
||
break; | ||
} | ||
/** @see https://github.com/estree/estree/blob/master/es5.md#identifier */ | ||
case 'Identifier': { | ||
// Save this for later in case it's referenced | ||
// module.exports.asd = something | ||
if (rhs.name === lhs.property.name) { | ||
exports.indirects[lhs.property.name] = | ||
`${basename}.${lhs.property.name}`; | ||
} | ||
|
||
break; | ||
} | ||
default: { | ||
if (lhs.property.name !== undefined) { | ||
// Something else, let's save it for when we're searching for | ||
// declarations | ||
exports.identifiers.push(lhs.property.name); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
} |