Skip to content

Commit

Permalink
fix(tofu): fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
boneskull authored and naugtur committed Feb 5, 2024
1 parent f02f458 commit fe75965
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 53 deletions.
47 changes: 20 additions & 27 deletions packages/tofu/src/findGlobals.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,36 @@
const { default: traverse } = require('@babel/traverse')
const { isInFunctionDeclaration, isMemberLikeExpression } = require('./util')

const nonReferenceIdentifiers = [
/**
* Types which are not references to globals
*/
const nonReferenceIdentifiers = new Set([
'ArrayPatten',
'BreakStatement',
'CatchClause',
'ClassMethod',
'ContinueStatement',
'ExportDefaultSpecifier',
'ExportSpecifier',
'FunctionDeclaration',
'FunctionExpression',
'ClassMethod',
'ImportDefaultSpecifier',
'ImportNamespaceSpecifier',
'ImportSpecifier',
'LabeledStatement',
'BreakStatement',
'ContinueStatement',
'CatchClause',
'ArrayPatten',
'MetaProperty',
'RestElement',
]

const importExportSpecifierTypes = new Set(
/** @type {const} */ ([
'ImportSpecifier',
'ImportDefaultSpecifier',
'ImportNamespaceSpecifier',
'ExportSpecifier',
'ExportDefaultSpecifier',
'MetaProperty',
])
)
])

module.exports = { findGlobals }

/**
* @typedef {import('@babel/traverse').NodePath<import('@babel/types').Identifier|import('@babel/types').ThisExpression>} IdentifierOrThisExpressionNodePath
* @typedef {import('@babel/traverse').NodePath<
* import('@babel/types').Identifier | import('@babel/types').ThisExpression
* >} IdentifierOrThisExpressionNodePath
*/

/**
*
* @param {import('@babel/types').Node} ast
* @returns {Map<string, IdentifierOrThisExpressionNodePath[]>}
*/
Expand All @@ -45,12 +44,7 @@ function findGlobals(ast) {
Identifier: (path) => {
// skip if not being used as reference
const parentType = path.parent.type
if (nonReferenceIdentifiers.includes(parentType)) {
return
}

// toss out esm imports/exports
if (importExportSpecifierTypes.has(parentType)) {
if (nonReferenceIdentifiers.has(parentType)) {
return
}

Expand Down Expand Up @@ -104,7 +98,6 @@ function findGlobals(ast) {
return globals

/**
*
* @param {IdentifierOrThisExpressionNodePath} path
* @param {string} [name]
*/
Expand Down
1 change: 1 addition & 0 deletions packages/tofu/src/inspectSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ function inspectDynamicRequires(ast) {
* @returns {{ cjsImports: string[] }}
*/
function inspectRequires(ast, packagesToInspect, deep = true) {
/** @type {string[][]} */
const cjsImports = []
const requireCalls = findAllCallsToRequire(ast)
requireCalls.forEach((path) => {
Expand Down
53 changes: 27 additions & 26 deletions packages/tofu/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,23 @@ module.exports = {

/**
* @typedef MemberExpressionNesting
* @property {import("./inspectPrimordialAssignments").NonComputedMemberLikeExpression[]} memberExpressions
* @property {import("./inspectPrimordialAssignments").MemberLikeExpression} parentOfMembershipChain
* @property {import("./inspectPrimordialAssignments").MemberLikeExpression|import("@babel/types").LVal} topmostMember
* @property {import('./inspectPrimordialAssignments').NonComputedMemberLikeExpression[]} memberExpressions
* @property {import('./inspectPrimordialAssignments').MemberLikeExpression} parentOfMembershipChain
* @property {import('./inspectPrimordialAssignments').MemberLikeExpression
* | import('@babel/types').LVal} topmostMember
*/

/**
* @param {import("@babel/types").Node} identifierNode
* @param {import("@babel/types").Node[]} parents
* @param {import('@babel/types').Node} identifierNode
* @param {import('@babel/types').Node[]} parents
* @returns
*/
function getMemberExpressionNesting(identifierNode, parents) {
// remove the identifier node itself
const parentsOnly = parents.slice(0, -1)
// find unbroken membership chain closest to identifier
const memberExpressions = getTailmostMatchingChain(
/** @type {import("./inspectPrimordialAssignments").NonComputedMemberLikeExpression[]} */ (
/** @type {import('./inspectPrimordialAssignments').NonComputedMemberLikeExpression[]} */ (
parentsOnly
),
isNonComputedMemberLikeExpression
Expand All @@ -52,8 +53,7 @@ function getMemberExpressionNesting(identifierNode, parents) {
}

/**
*
* @param {import("./inspectPrimordialAssignments").MemberLikeExpression[]} memberExpressions
* @param {import('./inspectPrimordialAssignments').MemberLikeExpression[]} memberExpressions
* @returns {string[]}
*/
function getPathFromMemberExpressionChain(memberExpressions) {
Expand All @@ -64,8 +64,7 @@ function getPathFromMemberExpressionChain(memberExpressions) {
}

/**
*
* @param {import("@babel/types").Node} node
* @param {import('@babel/types').Node} node
* @returns {string}
*/
function getNameFromNode(node) {
Expand All @@ -83,8 +82,7 @@ function getNameFromNode(node) {
}

/**
*
* @param {import("@babel/types").Node} node
* @param {import('@babel/types').Node} node
* @returns {node is import("./inspectPrimordialAssignments").MemberLikeExpression}
*/
function isMemberLikeExpression(node) {
Expand All @@ -94,23 +92,21 @@ function isMemberLikeExpression(node) {
}

/**
*
* @param {import("./inspectPrimordialAssignments").MemberLikeExpression} node
* @param {import('./inspectPrimordialAssignments').MemberLikeExpression} node
* @returns {node is import("./inspectPrimordialAssignments").NonComputedMemberLikeExpression}
*/
function isNonComputedMemberLikeExpression(node) {
return !node.computed && isMemberLikeExpression(node)
}

/**
*
* @param {import("@babel/types").LVal} identifierNode
* @param {import("@babel/types").Node[]} parents
* @param {import('@babel/types').LVal} identifierNode
* @param {import('@babel/types').Node[]} parents
* @returns {boolean}
*/
function isUndefinedCheck(identifierNode, parents) {
const parentExpression =
/** @type {import("@babel/types").UnaryExpression} */ (
/** @type {import('@babel/types').UnaryExpression} */ (
parents[parents.length - 2]
)
const isTypeof =
Expand All @@ -134,7 +130,8 @@ function getTailmostMatchingChain(items, matcher) {
}

/**
* if array contains 'x' and 'x.y' just keep 'x'
* If array contains 'x' and 'x.y' just keep 'x'
*
* @param {Map<string, import('lavamoat-core').GlobalPolicyValue>} globalsConfig
* @returns
*/
Expand Down Expand Up @@ -163,7 +160,8 @@ function reduceToTopmostApiCalls(globalsConfig) {
}

/**
* if array contains 'x' and 'x.y' just keep 'x'
* If array contains 'x' and 'x.y' just keep 'x'
*
* @param {string[]} keyPathStrings
* @returns {string[]}
*/
Expand Down Expand Up @@ -194,7 +192,8 @@ function reduceToTopmostApiCallsFromStrings(keyPathStrings) {
}

/**
* add variable to results, if not already set
* Add variable to results, if not already set
*
* @param {Map<string, import('lavamoat-core').GlobalPolicyValue>} globalsConfig
* @param {string} identifierPath
* @param {import('lavamoat-core').GlobalPolicyValue} identifierUse
Expand All @@ -208,6 +207,7 @@ function addGlobalUsage(globalsConfig, identifierPath, identifierUse) {

/**
* Merge two global policy configs (as `Map`s) together
*
* @param {Map<string, import('lavamoat-core').GlobalPolicyValue>} configA
* @param {Map<string, import('lavamoat-core').GlobalPolicyValue>} configB
* @returns
Expand All @@ -233,7 +233,7 @@ function objToMap(obj) {
/**
* @template V
* @param {Map<PropertyKey, V>} map
* @returns {{[k: string]: V}}if array contains 'x' and 'x.y' just keep 'x'
* @returns {{ [k: string]: V }} If array contains 'x' and 'x.y' just keep 'x'
*/
function mapToObj(map) {
return Object.fromEntries(map)
Expand All @@ -242,8 +242,8 @@ function mapToObj(map) {
/**
* Returns an array of a `NodePath`'s parent nodes (to the root)
*
* @param {import('@babel/traverse').NodePath<any>|null} nodePath
* @returns {import("@babel/types").Node[]}
* @param {import('@babel/traverse').NodePath<any> | null} nodePath
* @returns {import('@babel/types').Node[]}
*/
function getParents(nodePath) {
/** @type {import('@babel/types').Node[]} */
Expand All @@ -260,9 +260,10 @@ function getParents(nodePath) {
}

/**
* Determines if this `Node` is a descendant of a `FunctionDeclaration` or `FunctionExpression`.
* Determines if this `Node` is a descendant of a `FunctionDeclaration` or
* `FunctionExpression`.
*
* @param {import('@babel/traverse').NodePath<any>} path
* @param {import('@babel/traverse').NodePath<any>} nodePath
* @returns {boolean}
*/
function isInFunctionDeclaration(nodePath) {
Expand Down

0 comments on commit fe75965

Please sign in to comment.