Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

command line switch to syntax check javascript #9426

Closed
bahamas10 opened this issue Mar 17, 2015 · 11 comments
Closed

command line switch to syntax check javascript #9426

bahamas10 opened this issue Mar 17, 2015 · 11 comments

Comments

@bahamas10
Copy link

It would be nice to be able to pass a flag to node that would tell it to just syntax check, and not execute, the given script.

With ruby you can run ruby -c file.rb and bash bash -n file.sh which both result in just a syntax check without any code execution. Both work in a similar fashion where proper syntax will result in no output with an exit code of 0, whereas improper syntax will generate debug information on stderr and an exit code of non-0.

We (job) currently use the following as a pre-commit git hook to syntax check json, ruby, bash, and javascript. Even though vm is a stability level of 3, it still could change in the future which could break this functionality. Having a single command line switch to do effectively what is done in check_js would be extremely convenient and useful.

check_json() {
    debug 'checking JSON syntax'
    json < "$1" > /dev/null
}

check_ruby() {
    debug 'checking ruby syntax'
    ruby -c "$1" > /dev/null
}

check_bash() {
    debug 'checking bash syntax'
    bash -n "$1" > /dev/null
}

check_js() {
    debug 'checking javascript syntax'
    node -e '
    var vm = require("vm");
    var fs = require("fs");
    var source = fs.readFileSync(process.argv[1], "utf8").replace(/^#.*/, "");
    new vm.Script(source, {displayErrors: true});
    ' "$1"
}
@jasnell
Copy link
Member

jasnell commented Mar 18, 2015

This is a job best left to linters, isn't it? (http://jshint.com/)

@bahamas10
Copy link
Author

@jasnell linters are really meant to do static analysis of code, ie. is this variable used? did you do assignment when you meant conditional checking? etc. I simply just want to know if node will read a javascript file successfully, or if it will break when it is attempted.

Because of this, it's possible that jshint will report errors for things that, though maybe unintended, are completely valid, such as unused variables, functions inside loops, etc.

By using the node executable to check the syntax of a program, I'm guaranteed that all features used in that program will work when run by node. Whereas, with something like jshint, it's possible jshint will say something is valid that will actually break on the current version of node used. For example, jshint might be setup to allow ES6 features like class, function * etc., but node itself will break when that is used.

@CGavrila
Copy link

I'm just going to throw this out there, not necessarily because I think it's a particularly good idea, just maybe a shorter option.

node -e "eval('`cat test.js`')"

It is shorter, but you will still have issues escaping the characters and maybe others that I haven't thought about or that might depend on what you use it for.

@bahamas10
Copy link
Author

@CGavrila ^ isn't that effectively the same as node test.js (except for issues with unescaped characters)? I'm not trying to evaluate the code... I just want to look for any syntax errors.

@CGavrila
Copy link

Ah, indeed, it will execute the code as well. My bad, just ignore. :)

@OrangeDog
Copy link

If you inserted return; at the top of the evald script it should work.
So hacks, many cry.

@bahamas10
Copy link
Author

@OrangeDog there is like 99% of me that hates that... but the 1% of me absolutely loves its ingenuity. Would you grant that a command line switch to do this is slightly nicer than an eval + return? 😛

@OrangeDog
Copy link

"Slightly" is an understatement.

@bahamas10
Copy link
Author

i've created #9447 with an implementation

@ianmaddox
Copy link

For future readers, this is now implemented with the -c flag.

@ORESoftware
Copy link

I am just going to repeat what @ianmaddox just because it's true:

For future readers, this is all now implemented with the -c flag, as in:

node -c index.js

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants