Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax check for JavaScript using command

Are there equivalent to perl -c syntax check for JavaScript from command? Given that I have NodeJS installed?

JSLint is not considered as it is not a real parser. I think YUI compressor is possible but I don't want to install Java on production machines, so I am checking if Node.JS already provided this syntax check mechanism.

like image 617
Ryan Avatar asked Jun 14 '12 15:06

Ryan


People also ask

How to check if a JavaScript file is part of JavaScript?

Tip: To check if a JavaScript file is part of JavaScript project, just open the file in VS Code and run the JavaScript: Go to Project Configuration command. This command opens the jsconfig.json that the JavaScript file belongs to.

What is the syntax of JavaScript?

JavaScript Syntax. JavaScript. Syntax. ❮ Previous Next ❯. JavaScript syntax is the set of rules, how JavaScript programs are constructed: var x, y, z; // Declare Variables. x = 5; y = 6; // Assign Values. z = x + y; // Compute Values.

How do I check syntax in Node JS?

If you want to perform a syntax check like that way we do in perl ( another scripting language) you can simply use. node -c check syntax - node -c test.js will show no syntax error!! Note - we can even use it to check syntax for all files in a dir. - node -c *.js Hope that helps.

How to enable type checking in a JavaScript file?

The easiest way to enable type checking in a JavaScript file is by adding // @ts-check to the top of a file. // @ts-check let itsAsEasyAs = 'abc'; itsAsEasyAs = 123; // Error: Type '123' is not assignable to type 'string'


1 Answers

@Ryan

If you want to perform a syntax check like that way we do in perl ( another scripting language) you can simply use. node -c

e.g. a js file as test.js has -

let x = 30
if ( x == 30 ) {
  console.log("hello");
else {
  console.log( "world");
}

now type in node -c test.js it will show you

test.js:5
 else {
 ^^^^
SyntaxError: Unexpected token else
    at startup (bootstrap_node.js:144:11)
    at bootstrap_node.js:509:3

Now after fixing the syntax issue as

let x = 30
if ( x == 30 ) {
  console.log("hello");
} else {
  console.log( "world");
}

check syntax - node -c test.js will show no syntax error!!

Note - we can even use it to check syntax for all files in a dir. - node -c *.js

Hope that helps.

like image 131
Arnab Avatar answered Oct 13 '22 09:10

Arnab