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

add CLI option to syntax check script, fixes #9426 #9447

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ and servers.

-p, --print print result of --eval

-c, --check syntax check script without executing

-i, --interactive always enter the REPL even if stdin
does not appear to be a terminal

Expand Down
9 changes: 9 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ using v8::kExternalUint32Array;

static bool print_eval = false;
static bool force_repl = false;
static bool syntax_check_only = false;
static bool trace_deprecation = false;
static bool throw_deprecation = false;
static const char* eval_string = NULL;
Expand Down Expand Up @@ -2685,6 +2686,11 @@ void SetupProcessObject(Environment* env,
READONLY_PROPERTY(process, "_print_eval", True(env->isolate()));
}

// -c, --check
if (syntax_check_only) {
READONLY_PROPERTY(process, "_syntax_check_only", True(env->isolate()));
}

// -i, --interactive
if (force_repl) {
READONLY_PROPERTY(process, "_forceRepl", True(env->isolate()));
Expand Down Expand Up @@ -2911,6 +2917,7 @@ static void PrintHelp() {
" -v, --version print node's version\n"
" -e, --eval script evaluate script\n"
" -p, --print evaluate script and print result\n"
" -c, --check syntax check script without executing\n"
" -i, --interactive always enter the REPL even if stdin\n"
" does not appear to be a terminal\n"
" --no-deprecation silence deprecation warnings\n"
Expand Down Expand Up @@ -3029,6 +3036,8 @@ static void ParseArgs(int* argc,
eval_string += 1;
}
}
} else if (strcmp(arg, "--check") == 0 || strcmp(arg, "-c") == 0) {
syntax_check_only = true;
} else if (strcmp(arg, "--interactive") == 0 || strcmp(arg, "-i") == 0) {
force_repl = true;
} else if (strcmp(arg, "--no-deprecation") == 0) {
Expand Down
9 changes: 9 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@
var path = NativeModule.require('path');
process.argv[1] = path.resolve(process.argv[1]);

// check if user passed `-c` or `--check` arguments to Node.
if (process._syntax_check_only != null) {
var vm = NativeModule.require('vm');
var fs = NativeModule.require('fs');
var source = fs.readFileSync(process.argv[1], 'utf8').replace(/^#.*/, '');
new vm.Script(source, {filename: process.argv[1], displayErrors: true});
process.exit(0);
}

// If this is a worker in cluster mode, start up the communication
// channel.
if (process.env.NODE_UNIQUE_ID) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/syntax/bad_syntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo bar;
2 changes: 2 additions & 0 deletions test/fixtures/syntax/bad_syntax_shebang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
var foo bar;
1 change: 1 addition & 0 deletions test/fixtures/syntax/good_syntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo = 'bar';
2 changes: 2 additions & 0 deletions test/fixtures/syntax/good_syntax_shebang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
var foo = 'bar';
76 changes: 76 additions & 0 deletions test/simple/test-cli-syntax.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var assert = require('assert');
var spawnSync = require('child_process').spawnSync;
var path = require('path');

var common = require('../common');

var node = process.argv[0];

// test both sets of arguments that check syntax
var syntax_args = [
['-c'],
['--check']
];

// test good syntax with and without shebang
[
'syntax/good_syntax.js',
'syntax/good_syntax_shebang.js'
].forEach(function(file) {
file = path.join(common.fixturesDir, file);

// loop each possible option, `-c` or `--check`
syntax_args.forEach(function(args) {
var _args = args.concat([file]);
console.log('calling %s %s', node, _args.join(' '));
var c = spawnSync(node, _args, {encoding: 'utf8'});

// no output should be produced
assert.equal(c.stdout, '', 'stdout produced');
assert.equal(c.stderr, '', 'stderr produced');
assert.equal(c.status, 0, 'code == ' + c.status);
console.log('ok');
});
});

// test bad syntax with and without shebang
[
'syntax/bad_syntax.js',
'syntax/bad_syntax_shebang.js'
].forEach(function(file) {
file = path.join(common.fixturesDir, file);

// loop each possible option, `-c` or `--check`
syntax_args.forEach(function(args) {
var _args = args.concat([file]);
console.log('calling %s %s', node, _args.join(' '));
var c = spawnSync(node, _args, {encoding: 'utf8'});

// no output should be produced
assert.equal(c.stdout, '', 'stdout produced');
assert(c.stderr, 'stderr not produced');
assert.equal(c.status, 1, 'code == ' + c.status);
console.log('ok');
});
});