Skip to content

⬅️ Back to Table of Contents

📄 discriminateAnyType.test.ts

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 4
📊 Variables & Constants 5
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/type-utils/tests/discriminateAnyType.test.ts

📦 Imports

Name Source
TSESTree @typescript-eslint/typescript-estree
AnyType ../src/index.js
discriminateAnyType ../src/index.js
parseCodeForEslint ./test-utils/custom-matchers/custom-matchers.js

Variables & Constants

Name Type Kind Value Exported
declaration TSESTree.VariableDeclaration const ast.body.at(-1) as TSESTree.VariableDeclaration
code "\nclass Foo {\n foo() {\n return this;\n }\n protected then(resolve: () => void): void {\n resolve();\n }\n};\n " const ``
class Foo {
foo() {
return this;
}
protected then(resolve: () => void): void {
resolve();
}
};
``
classDeclration TSESTree.ClassDeclaration const ast.body[0] as TSESTree.ClassDeclaration
method TSESTree.MethodDefinition const `classDeclration.body
.body[0] as TSESTree.MethodDefinition`
returnStatement TSESTree.ReturnStatement const `method.value.body?.body.at(
-1,
) as TSESTree.ReturnStatement`

Functions

getDeclarationId(ast: TSESTree.Program): TSESTree.Node

Code
function getDeclarationId(ast: TSESTree.Program): TSESTree.Node {
    const declaration = ast.body.at(-1) as TSESTree.VariableDeclaration;
    const { id } = declaration.declarations[0];
    return id;
  }
  • Parameters:
  • ast: TSESTree.Program
  • Return Type: TSESTree.Node
  • Calls:
  • ast.body.at

getTypes(code: string, getNode: GetNode): { checker: any; program: any; tsNode: any; type: any; }

Code
function getTypes(code: string, getNode: GetNode) {
    const { ast, services } = parseCodeForEslint(code);
    const node = getNode(ast);
    const type = services.getTypeAtLocation(getNode(ast));
    return {
      checker: services.program.getTypeChecker(),
      program: services.program,
      tsNode: services.esTreeNodeToTSNodeMap.get(node),
      type,
    };
  }
  • Parameters:
  • code: string
  • getNode: GetNode
  • Return Type: { checker: any; program: any; tsNode: any; type: any; }
  • Calls:
  • parseCodeForEslint (from ./test-utils/custom-matchers/custom-matchers.js)
  • getNode
  • services.getTypeAtLocation
  • services.program.getTypeChecker
  • services.esTreeNodeToTSNodeMap.get

Type Aliases

GetNode

type GetNode = (ast: TSESTree.Program) => TSESTree.Node;