Skip to content

Strange cache in Babel 8 Beta 3 #17563

@coderaiser

Description

@coderaiser

💻

  • Would you like to work on a fix?

How are you using Babel?

When I traverse AST with noScope it somehow cached inside AST, so next traverse also works without scope.

If I use JSON.parse(JSON.stringify(ast)) everything works good. I'm not sure is it a bug or feature, but all previous versions of Babel 8 and Babel 7 (including v7.28.5) has no such cache.

Input code

import * as parser from "@babel/parser";
import traverse from "@babel/traverse";
import * as t from "@babel/types";

const code = `
    export const a = 1;
    const b = a;
`;

const ast = parser.parse(code, {
    sourceType: "module",
});

traverse(ast, {
    noScope: true,
    Identifier(path) {
        console.log("Found identifier:", path.node.name);
    }
});

traverse(ast, {
  Identifier(path) {
    console.log("Found identifier:", path.node.name);
    path.scope.getOwnBinding('a');
  }
});

Configuration file name

No response

Configuration

No response

Current and expected behavior

current:

Found identifier: a
Found identifier: b
Found identifier: a
Found identifier: a
file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:1521
    return this.bindings[name];
                        ^

TypeError: Cannot read properties of undefined (reading 'a')
    at Scope.getOwnBinding (file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:1521:25)
    at Identifier (file:///Users/coderaiser/babel/1.mjs:30:32)
    at NodePath._call (file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:4896:20)
    at _visit (file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:4822:15)
    at _visitPaths (file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:4789:9)
    at _traverse (file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:4860:11)
    at _visit (file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:4824:21)
    at _visitPaths (file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:4789:9)
    at _traverse (file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:4858:11)
    at _visit (file:///Users/coderaiser/babel/node_modules/@babel/traverse/lib/index.js:4824:21)

Node.js v24.8.0

Expected:

Found identifier: a
Found identifier: b
Found identifier: a
Found identifier: a
Found identifier: b
Found identifier: a

Environment

Latest version of babel (beta 3)

Possible solution

No response

Additional context

No response

Metadata

Metadata

Assignees

Labels

babel 8outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions