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

The parser is blowing up on a shebang #1151

Closed
dijs opened this issue Mar 24, 2015 · 6 comments
Closed

The parser is blowing up on a shebang #1151

dijs opened this issue Mar 24, 2015 · 6 comments

Comments

@dijs
Copy link

dijs commented Mar 24, 2015

Can this "#!/usr/bin/env node" not throw a parse error?

@michaelficarra
Copy link
Contributor

No, that is not valid JavaScript. You can strip shebangs yourself before giving input to esprima.

@dijs
Copy link
Author

dijs commented Mar 24, 2015

Sounds good.

@dijs dijs closed this as completed Mar 24, 2015
@ariya
Copy link
Contributor

ariya commented Mar 24, 2015

For an illustrative example on how to do this (taken from jsparser):

function trimHashbang(code) {
    if (code.substring(0, 2) === '#!') {
        var end = code.indexOf('\n');
        var filler = '';
        for (var i = 0; i < end; ++i) {
           filler += ' ';
        }
        code = filler + code.substring(end, code.length);
    }
    return code;
}

Of course, you also need to account for a possible return not inside a function. Check the above referred jsparser for details.

@michaelficarra
Copy link
Contributor

eslint uses essentially

function trimShebang(text) {
  return text.replace(/^#!([^\r\n]+)/, function(match, captured) { return "//" + captured; });
}

@ariya
Copy link
Contributor

ariya commented Mar 24, 2015

"I know, I'll use regular expressions." 😉

@dmethvin
Copy link
Member

problems++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants