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

Path.getsource can't work well,Cannot read property 'getCode' of undefined #9855

Closed
flytam opened this Issue Apr 14, 2019 · 4 comments

Comments

Projects
None yet
3 participants
@flytam
Copy link

commented Apr 14, 2019

Bug Report

Current Behavior
Can not get the source code

Input Code

// Here are the smallest example
const parser = require("@babel/parser");
const traverse = require("@babel/traverse").default;

const input = `<p>test</p>`;
const ast = parser.parse(input, {
    plugins: ["jsx"]
});

traverse(ast, {
    JSXElement: {
        enter(path) {
            console.log(path.getSource());
        }
    }
});

Expected behavior/code
Ouput the source code

Environment

  • Babel version(s): "@babel/parser": "^7.4.2","@babel/traverse": "^7.4.0",
  • Node/npm version: [e.g. Node 8/npm 5]
  • OS: OSX 10.14,

Additional context/Screenshots

bug

@babel-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 14, 2019

Hey @flytam! 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.

@flytam

This comment has been minimized.

Copy link
Author

commented Apr 18, 2019

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

commented Apr 18, 2019

getSource uses an internal object (hub) which contains some information provided by @babel/core: that object contains the original source code, alongside with other metadata.
If you are using @babel/traverse directly and want a method which uses hub, you need to setup hub manually.
Depending on your usecase, you could use path.toString() instead, which builds the string using @babel/generator and thus doesn't need to retrieve the original source.

@flytam

This comment has been minimized.

Copy link
Author

commented Apr 20, 2019

it works well,thanks

@flytam flytam closed this Apr 20, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.