Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to skip child nodes #11147

Open
liuxingbaoyu opened this issue Feb 16, 2020 · 5 comments
Open

How to skip child nodes #11147

liuxingbaoyu opened this issue Feb 16, 2020 · 5 comments

Comments

@liuxingbaoyu
Copy link
Contributor

@liuxingbaoyu liuxingbaoyu commented Feb 16, 2020

Code:

var traverse = require("@babel/traverse").default;
var parser = require("@babel/parser");
var generate = require("@babel/generator").default;
var types = require("@babel/types");

var ast = parser.parse('{"abc";"123";}');
traverse(ast, {
  BlockStatement(path) {
    var p = path.get("body")[0];
    console.log("before", p.shouldSkip);
    p.skip();
    console.log("after", p.shouldSkip, path.get("body")[0].shouldSkip);
  },
  ExpressionStatement(path) {
    console.log(generate(path.node).code);
  }
});

return;

Output:

before false
after true false
"abc";
"123";
@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Feb 16, 2020

Hey @liuxingbaoyu! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Feb 16, 2020

Hi @liuxingbaoyu! This issue is missing some important information we'll need to be able to reproduce this issue.

Please understand that we receive a high volume of issues, and there are only a limited number of volunteers that help maintain this project. The easier it is for us to decipher an issue with the info provided, the more likely it is that we'll be able to help.

Please make sure you have the following information documented in this ticket:

  1. Your Babel configuration (typically from .babelrc or babel.config.js)
  2. The current (incorrect) behavior you're seeing
  3. The behavior you expect
  4. A short, self-contained example

Please provide either a link to the problem via the repl, or if the repl is insufficient, a new and minimal repository with instructions on how to build/replicate the issue.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Feb 16, 2020

This is definitely a bug. If you try to run this code in the monorepo:

const load = n => require(`./packages/babel-${n}`);
const babel = load("core");
const parser = load("parser");
const traverse = load("traverse").default;
const generate = load("generator").default;

const ast = parser.parse('{"abc";"123";}');
traverse(ast, {
  BlockStatement(path) {
    const p = path.get("body")[0];
    console.log("before", p.shouldSkip, path.get("body")[0] === p);
    p.skip();

    console.log("after 1", p.shouldSkip);
    path.get("body");
    console.log("after 2", p.shouldSkip);
  },
  ExpressionStatement(path) {
    console.log(generate(path.node).code);
  },
});

It will log after 1 true, after 1 false, but shouldSkip shouldn't change from after 1 to after2.

@liuxingbaoyu

This comment has been minimized.

Copy link
Contributor Author

@liuxingbaoyu liuxingbaoyu commented Feb 17, 2020

I don't know if this is a bug or not allowed, so I asked a question😛

@liuxingbaoyu

This comment has been minimized.

Copy link
Contributor Author

@liuxingbaoyu liuxingbaoyu commented Feb 17, 2020

Related to this, but I don't know how to fix it😭

this._traverseFlags = 0;

    at NodePath.setContext (e:\mypath\babel\node_modules\@babel\traverse\lib\path\context.js:141:3)
    at e:\mypath\babel\node_modules\@babel\traverse\lib\path\family.js:188:10
    at Array.map (<anonymous>)
    at NodePath._getKey (e:\mypath\babel\node_modules\@babel\traverse\lib\path\family.js:181:22)
    at NodePath.get (e:\mypath\babel\node_modules\@babel\traverse\lib\path\family.js:170:17)
    at BlockStatement (e:\mypath\babel\test.js:51:10)
    at NodePath._call (e:\mypath\babel\node_modules\@babel\traverse\lib\path\context.js:55:20)
    at NodePath.call (e:\mypath\babel\node_modules\@babel\traverse\lib\path\context.js:42:17)
    at NodePath.visit (e:\mypath\babel\node_modules\@babel\traverse\lib\path\context.js:90:31)

edited: It may be possible to backup _traverseFlags and restore after this.setScope ();

edited2: But this will affect normal traversal behavior.

var traverse = require("@babel/traverse").default;
var parser = require("@babel/parser");
var generate = require("@babel/generator").default;
var types = require("@babel/types");

var a = false;
var ast = parser.parse('{"abc";"123";}');
traverse(ast, {
  ExpressionStatement(path) {
    !a && path.traverse({
      StringLiteral(p) {
        p.skip();
        console.log("skip");
      }
    });
    a = true;
  },
  StringLiteral(path) {
    console.log(generate(path.node).code);
  }
});

Normal output:

skip
"abc"
"123"

Modified output:

skip
"123"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.