VariableDeclaration()

in plugins/eslint-plugin-aws-toolkits/lib/rules/no-incorrect-once-usage.ts [109:170]


            VariableDeclaration(node) {
                // Top-level module assignment is ok
                if (
                    node.parent?.type === AST_NODE_TYPES.Program ||
                    node.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration
                ) {
                    return
                }

                for (const declaration of node.declarations) {
                    if (
                        declaration.init?.type === AST_NODE_TYPES.CallExpression &&
                        declaration.id.type === AST_NODE_TYPES.Identifier &&
                        declaration.init.callee?.type === AST_NODE_TYPES.Identifier &&
                        declaration.init.callee.name === 'once'
                    ) {
                        const scope = context.sourceCode.getScope(declaration)
                        const variable = scope.variables.find(
                            (v) => v.name === (declaration.id as TSESTree.Identifier).name
                        ) // we already confirmed the type in the if statement... why is TS mad?
                        let isUsedInLoopScope = false

                        if (variable) {
                            const refs = variable.references.filter((ref) => ref.identifier !== declaration.id)

                            // Check if it is being referenced multiple times
                            // TODO: expand to check if it is being referenced inside nested scopes only? (currently checks current scope as well)
                            if (refs.length > 1) {
                                continue
                            }

                            // Check if it is being referenced once, but inside a loop.
                            for (const ref of refs) {
                                let currNode: TSESTree.Node | undefined = ref.identifier

                                while (currNode && currNode !== scope.block) {
                                    if (
                                        currNode.type === AST_NODE_TYPES.ForStatement ||
                                        currNode.type === AST_NODE_TYPES.ForInStatement ||
                                        currNode.type === AST_NODE_TYPES.ForOfStatement ||
                                        currNode.type === AST_NODE_TYPES.WhileStatement ||
                                        currNode.type === AST_NODE_TYPES.DoWhileStatement
                                    ) {
                                        isUsedInLoopScope = true
                                        break
                                    }
                                    currNode = currNode.parent
                                }
                            }
                        }

                        // If the variable is somehow not assigned? or only used once and not in a loop.
                        if (variable === undefined || !isUsedInLoopScope) {
                            context.report({
                                node: declaration.init.callee,
                                messageId: 'notReusableErr',
                            })
                            continue
                        }
                    }
                }
            },