All files ast.ts

97.75% Statements 87/89
97.61% Branches 82/84
100% Functions 11/11
97.72% Lines 86/88

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354                                                                                                                                                                                                              888x     175x                 888x   29101x 1849x     27252x   27252x 80x     27172x 79x     27172x     220x     128x 16x 8x     120x     24x     249x       132x       3x         132x         1872x     92x     24x           308x     20x     24x     60x     8x     12x     36x     20x           20x     756x     48x   48x 16x 36x         28x   28x 8x         16x 36x       48x         604x   604x 292x               604x 276x               604x 36x   568x     604x 24x 580x 544x     604x     80x     84x     140x   121x   140x 401x 147x   147x 140x 140x     261x     121x 121x     88x         8x         12x         29101x 171x           27399x 2786x 191x       27208x       888x       628x 212x     416x 212x           215x         27348x    
import {
  type ObjectMethod,
  type ClassMethod,
  type ClassBody as BabelClassBody,
  type Node as BabelNode,
} from "@babel/types";
import type {
  ArrowFunctionExpression,
  BreakStatement,
  ContinueStatement,
  DebuggerStatement,
  DoWhileStatement,
  Expression,
  ExpressionStatement,
  ForInStatement,
  ForOfStatement,
  ForStatement,
  FunctionDeclaration,
  IfStatement,
  LabeledStatement,
  MethodDefinition,
  Node,
  Property,
  ReturnStatement,
  SwitchStatement,
  ThrowStatement,
  TryStatement,
  VariableDeclarator,
  WhileStatement,
  WithStatement,
  ConditionalExpression,
  LogicalExpression,
  AssignmentPattern,
  FunctionExpression,
  ClassBody,
  BlockStatement,
} from "estree";
import { asyncWalk } from "estree-walker";
import { type IgnoreHint } from "./ignore-hints";
 
declare module "estree" {
  interface BaseNode {
    start: number;
    end: number;
  }
}
 
type ParenthesizedExpression = {
  type: "ParenthesizedExpression";
  expression: Expression;
};
 
interface Visitors {
  // Functions
  onFunctionDeclaration: (node: FunctionDeclaration) => void;
  onFunctionExpression: (node: FunctionExpression) => void;
  onArrowFunctionExpression: (node: ArrowFunctionExpression) => void;
  onMethodDefinition: (node: MethodDefinition) => void;
  onProperty: (node: Property) => void;
  onClassMethod: (node: ClassMethod) => void;
  onObjectMethod: (node: ObjectMethod) => void;
 
  // Statements
  onExpressionStatement: (node: ExpressionStatement) => void;
  onBreakStatement: (node: BreakStatement) => void;
  onContinueStatement: (node: ContinueStatement) => void;
  onDebuggerStatement: (node: DebuggerStatement) => void;
  onReturnStatement: (node: ReturnStatement) => void;
  onThrowStatement: (node: ThrowStatement) => void;
  onTryStatement: (node: TryStatement) => void;
  onForStatement: (node: ForStatement) => void;
  onForInStatement: (node: ForInStatement) => void;
  onForOfStatement: (node: ForOfStatement) => void;
  onWhileStatement: (node: WhileStatement) => void;
  onDoWhileStatement: (node: DoWhileStatement) => void;
  onWithStatement: (node: WithStatement) => void;
  onLabeledStatement: (node: LabeledStatement) => void;
  onVariableDeclarator: (node: VariableDeclarator) => void;
  onClassBody: (node: ClassBody | BabelClassBody) => void;
 
  // Branches
  onIfStatement: (
    node: IfStatement,
    branches: (Node | null | undefined)[],
  ) => void;
  onSwitchStatement: (node: SwitchStatement) => void;
  onConditionalExpression: (node: ConditionalExpression) => void;
  onLogicalExpression: (
    node: LogicalExpression,
    branches: (Node | null | undefined)[],
  ) => void;
  onAssignmentPattern: (node: AssignmentPattern) => void;
}
export type FunctionNodes = Parameters<
  Visitors[
    | "onArrowFunctionExpression"
    | "onFunctionDeclaration"
    | "onFunctionExpression"
    | "onMethodDefinition"
    | "onProperty"]
>[0];
 
export function getWalker() {
  let nextIgnore: Node | false = false;
 
  function onIgnore(node: Node) {
    nextIgnore = node;
  }
 
  async function walk(
    ast: unknown,
    ignoreHints: IgnoreHint[],
    ignoreClassMethods: string[] | undefined,
    visitors: Visitors,
  ) {
    return await asyncWalk(ast as Node, {
      async enter(node) {
        if (nextIgnore !== false) {
          return;
        }
 
        const hint = getIgnoreHint(node);
 
        if (hint === "next") {
          return onIgnore(node);
        }
 
        if (isSkipped(node)) {
          onIgnore(node);
        }
 
        switch (node.type) {
          // Functions
          case "FunctionDeclaration": {
            return visitors.onFunctionDeclaration(node);
          }
          case "FunctionExpression": {
            if (ignoreClassMethods && node.id?.name) {
              if (ignoreClassMethods.includes(node.id.name)) {
                return onIgnore(node);
              }
            }
            return visitors.onFunctionExpression(node);
          }
          case "MethodDefinition": {
            return visitors.onMethodDefinition(node);
          }
          case "Property": {
            return visitors.onProperty(node);
          }
          case "ArrowFunctionExpression": {
            // Get inner body in cases where parenthesis are preserverd, e.g. acorn, oxc: https://oxc.rs/docs/learn/ecmascript/grammar.html#parenthesized-expression
            if (
              (node.body as unknown as ParenthesizedExpression)?.type ===
              "ParenthesizedExpression"
            ) {
              node.body = (
                node.body as unknown as ParenthesizedExpression
              ).expression;
            }
 
            return visitors.onArrowFunctionExpression(node);
          }
 
          // Statements
          case "ExpressionStatement": {
            return visitors.onExpressionStatement(node);
          }
          case "BreakStatement": {
            return visitors.onBreakStatement(node);
          }
          case "ContinueStatement": {
            return visitors.onContinueStatement(node);
          }
          case "DebuggerStatement": {
            return visitors.onDebuggerStatement(node);
          }
          case "ReturnStatement": {
            return visitors.onReturnStatement(node);
          }
          case "ThrowStatement": {
            return visitors.onThrowStatement(node);
          }
          case "TryStatement": {
            return visitors.onTryStatement(node);
          }
          case "ForStatement": {
            return visitors.onForStatement(node);
          }
          case "ForInStatement": {
            return visitors.onForInStatement(node);
          }
          case "ForOfStatement": {
            return visitors.onForOfStatement(node);
          }
          case "WhileStatement": {
            return visitors.onWhileStatement(node);
          }
          case "DoWhileStatement": {
            return visitors.onDoWhileStatement(node);
          }
          case "WithStatement": {
            return visitors.onWithStatement(node);
          }
          case "LabeledStatement": {
            return visitors.onLabeledStatement(node);
          }
          case "VariableDeclarator": {
            return visitors.onVariableDeclarator(node);
          }
          case "ClassBody": {
            const classBody = node as ClassBody | BabelClassBody;
 
            if (ignoreClassMethods) {
              for (const child of classBody.body) {
                if (
                  child.type === "MethodDefinition" ||
                  child.type === "ClassMethod"
                ) {
                  const name =
                    child.key.type === "Identifier" && child.key.name;
 
                  if (name && ignoreClassMethods.includes(name)) {
                    setSkipped(child);
                  }
                }
              }
 
              classBody.body = classBody.body.filter(
                (child) => !isSkipped(child),
              ) as typeof classBody.body;
            }
 
            return visitors.onClassBody(classBody);
          }
 
          // Branches
          case "IfStatement": {
            const branches = [];
 
            if (node.consequent.type !== "BlockStatement") {
              node.consequent = {
                type: "BlockStatement",
                body: [node.consequent],
                start: node.consequent.start,
                end: node.consequent.end,
              } satisfies BlockStatement;
            }
 
            if (node.alternate && node.alternate.type !== "BlockStatement") {
              node.alternate = {
                type: "BlockStatement",
                body: [node.alternate],
                start: node.alternate.start,
                end: node.alternate.end,
              } satisfies BlockStatement;
            }
 
            if (hint === "if") {
              setSkipped(node.consequent);
            } else {
              branches.push(node.consequent);
            }
 
            if (hint === "else" && node.alternate) {
              setSkipped(node.alternate);
            } else if (hint !== "if") {
              branches.push(node.alternate);
            }
 
            return visitors.onIfStatement(node, branches);
          }
          case "SwitchStatement": {
            return visitors.onSwitchStatement(node);
          }
          case "ConditionalExpression": {
            return visitors.onConditionalExpression(node);
          }
          case "LogicalExpression": {
            if (isSkipped(node)) return;
 
            const branches: Node[] = [];
 
            function visit(child: Node) {
              if (child.type === "LogicalExpression") {
                setSkipped(child);
 
                if (getIgnoreHint(child) !== "next") {
                  visit(child.left);
                  return visit(child.right);
                }
              }
              branches.push(child);
            }
 
            visit(node);
            return visitors.onLogicalExpression(node, branches);
          }
          case "AssignmentPattern": {
            return visitors.onAssignmentPattern(node);
          }
 
          // @ts-expect-error -- Babel AST
          case "ClassMethod": {
            return visitors.onClassMethod(node);
          }
 
          // @ts-expect-error -- Babel AST
          case "ObjectMethod": {
            return visitors.onObjectMethod(node);
          }
        }
      },
      async leave(node) {
        if (node === nextIgnore) {
          nextIgnore = false;
        }
      },
    });
 
    function getIgnoreHint(node: Node) {
      for (const hint of ignoreHints) {
        if (hint.loc.end === node.start) {
          return hint.type;
        }
      }
 
      return null;
    }
  }
 
  return { walk, onIgnore };
}
 
export function getFunctionName(node: Node | BabelNode) {
  if (node.type === "Identifier") {
    return node.name;
  }
 
  if ("id" in node && node.id) {
    return getFunctionName(node.id);
  }
}
 
function setSkipped(node: Node | BabelNode) {
  // @ts-expect-error -- internal
  node.__skipped = true;
}
 
function isSkipped(node: Node | BabelNode) {
  // @ts-expect-error -- internal
  return node.__skipped === true;
}