All files ast.ts

99.03% Statements 103/104
98.95% Branches 95/96
100% Functions 11/11
99.02% Lines 102/103

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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386                                                                                                                                                                                                                              908x     1665x                 908x   349661x 22419x     327242x   327242x 130x     327112x 1519x     327112x     3847x     645x 16x 8x     637x     383x     2231x       1322x 3x     1322x         9547x     319x     247x     4x     8487x     108x     74x     185x     15x     363x     127x     28x           23x     8670x     89x   89x 16x 36x         28x   28x 8x         16x 36x       89x         12913x   12913x 792x               12913x 843x               12913x 37x   12876x     12913x 25x 12888x 12829x     12913x     237x   237x 1677x 1659x       237x     2416x   2416x 6x   2416x 2x     2416x 8x   2408x     2416x 4x   2412x     2416x     5905x   4448x   5905x 18258x 6912x   6912x 6905x 6905x     11353x     4448x 4448x     240x         8x         12x         349661x 1661x           340663x 598400x 279x       340384x       908x       9587x 3845x     5742x 3845x           6994x         333053x    
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,
  SwitchCase,
} from "estree";
import { asyncWalk } from "estree-walker";
import { type IgnoreHint } from "./ignore-hints";
 
declare module "estree" {
  interface BaseNode {
    start: number;
    end: number;
  }
 
  interface ParenthesizedExpression extends BaseExpression {
    type: "ParenthesizedExpression";
    expression: Expression;
  }
 
  interface ExpressionMap {
    ParenthesizedExpression: ParenthesizedExpression;
  }
}
 
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, cases: SwitchCase[]) => void;
  onConditionalExpression: (
    node: ConditionalExpression,
    branches: (Node | null | undefined)[],
  ) => 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?.type === "ParenthesizedExpression") {
              node.body = node.body.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" && hint !== "else") {
              branches.push(node.alternate);
            }
 
            return visitors.onIfStatement(node, branches);
          }
          case "SwitchStatement": {
            const cases = [];
 
            for (const _case of node.cases) {
              if (getIgnoreHint(_case) !== "next") {
                cases.push(_case);
              }
            }
 
            return visitors.onSwitchStatement(node, cases);
          }
          case "ConditionalExpression": {
            const branches = [];
 
            if (node.consequent.type === "ParenthesizedExpression") {
              node.consequent = node.consequent.expression;
            }
            if (node.alternate.type === "ParenthesizedExpression") {
              node.alternate = node.alternate.expression;
            }
 
            if (getIgnoreHint(node.consequent) === "next") {
              setSkipped(node.consequent);
            } else {
              branches.push(node.consequent);
            }
 
            if (getIgnoreHint(node.alternate) === "next") {
              setSkipped(node.alternate);
            } else {
              branches.push(node.alternate);
            }
 
            return visitors.onConditionalExpression(node, branches);
          }
          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;
}