sarsamurmu / estree-toolkit

Tools for working with ESTree AST
https://estree-toolkit.netlify.app/
MIT License
54 stars 1 forks source link

TypeError: null is not an object (evaluating 'path.node.type') #14

Closed Vexcited closed 1 year ago

Vexcited commented 1 year ago

Hi! I'm currently using a combo of librairies to rewrite an identifier from scripts that can be found while browsing the web.

For reproduction, let's say we'll use the script from https://hoppscotch.io/assets/index-1408198b.js which is a module.

I use meriyah to parse the code using the following:

import { parse } from "meriyah";

const ast = parse(code, {
  module: true
});

Now, I'll use estree-toolkit to traverse the AST, with the following:

import { traverse } from "estree-toolkit";

traverse(ast, {
  $: { scope: true }, // When I disable this (: false), the error goes away... but I need path.scope !
  MemberExpression (path) {
    if (!path.node) return;

    if (path.node.object.type === "Identifier") {
      // do stuff with path.scope ...
      // that part of the code is not important, it already threw anyway.
    }
  }
});

Here is the full trace I get

758 | // From -
759 | //  const { a, b: [c, { d }], e: f = 0, ...g } = x;
760 | // Returns paths to
761 | // - a, c, d, f, g
762 | const findVisiblePathsInPattern = (path, result) => {
763 |     switch (path.node.type) {
               ^
TypeError: null is not an object (evaluating 'path.node.type')
      at findVisiblePathsInPattern (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/scope.mjs:763:12)
      at registerBindingFromPattern (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/scope.mjs:834:4)
      at CatchClause (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/scope.mjs:975:8)
      at crawl (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/scope.mjs:1108:16)
      at init (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/scope.mjs:1052:8)
      at init (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/nodepath.mjs:112:16)
      at crawl (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/scope.mjs:1165:12)
      at init (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/scope.mjs:1052:8)
      at init (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/nodepath.mjs:112:16)
      at crawl (/home/vexcited/Projects/.../node_modules/estree-toolkit/dist-es/scope.mjs:1165:12)

Also, I should mention that I use Bun to run this piece of code. I don't know if that changes anything though.

sarsamurmu commented 1 year ago

Thanks for opening a detailed issue. The issue has been resolved. Use v1.7.3.

Vexcited commented 1 year ago

It's fixed, thanks a lot for the fast reply!