Skip to content

⬅️ Back to Table of Contents

📄 TSESTreeNode.ts

📊 Analysis Summary

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

📚 Table of Contents

🛠️ File Location:

📂 packages/scope-manager/tests/test-utils/serializers/TSESTreeNode.ts

📦 Imports

Name Source
TSESTree @typescript-eslint/types
NewPlugin @vitest/pretty-format
AST_NODE_TYPES @typescript-eslint/types
createIdGenerator ../../../src/ID

Variables & Constants

Name Type Kind Value Exported
EXCLUDED_KEYS Set<string> const `new Set([
// prevent cycles
'parent',
// type is printed in front of the object
'type',
// locations are just noise
'loc',
'range',
])`
SEEN_NODES Map<Node, number> const new Map<Node, number>()
serializer NewPlugin const `{
serialize(node: Node): string {
if (node.type === AST_NODE_TYPES.Identifier) {
return Identifier<"${(node as Identifier).name}">;
}
const keys = Object.keys(node).filter(k => !EXCLUDED_KEYS.has(k));
if (keys.length === 0) {
  return node.type;
}

if (SEEN_NODES.has(node)) {
  return `${node.type}$${SEEN_NODES.get(node)}`;
}

const id = generator();
SEEN_NODES.set(node, id);
return `${node.type}$${id}`;

}, test(val): boolean { return !!( val && typeof val === 'object' && // make sure it's not one of the classes from the package Object.getPrototypeOf(val) === Object.prototype && 'type' in val && (val as TSESTree.Node).type in AST_NODE_TYPES ); }, }` | ✓ |


Functions

serialize(node: Node): string

Code
serialize(node: Node): string {
    if (node.type === AST_NODE_TYPES.Identifier) {
      return `Identifier<"${(node as Identifier).name}">`;
    }

    const keys = Object.keys(node).filter(k => !EXCLUDED_KEYS.has(k));
    if (keys.length === 0) {
      return node.type;
    }

    if (SEEN_NODES.has(node)) {
      return `${node.type}$${SEEN_NODES.get(node)}`;
    }

    const id = generator();
    SEEN_NODES.set(node, id);
    return `${node.type}$${id}`;
  }
  • Parameters:
  • node: Node
  • Return Type: string
  • Calls:
  • Object.keys(node).filter
  • EXCLUDED_KEYS.has
  • SEEN_NODES.has
  • SEEN_NODES.get
  • generator
  • SEEN_NODES.set

test(val: any): boolean

Code
test(val): boolean {
    return !!(
      val &&
      typeof val === 'object' &&
      // make sure it's not one of the classes from the package
      Object.getPrototypeOf(val) === Object.prototype &&
      'type' in val &&
      (val as TSESTree.Node).type in AST_NODE_TYPES
    );
  }
  • Parameters:
  • val: any
  • Return Type: boolean
  • Calls:
  • Object.getPrototypeOf
  • Internal Comments:
    // make sure it's not one of the classes from the package (x4)
    

Type Aliases

Node

type Node = { type: AST_NODE_TYPES } & Record<string, unknown>;

Identifier

type Identifier = { name: string; type: AST_NODE_TYPES.Identifier } & Node;