-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
add CLI option to syntax check script #2411
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
module.exports.stripBOM = stripBOM; | ||
|
||
/** | ||
* Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) | ||
* because the buffer-to-string conversion in `fs.readFileSync()` | ||
* translates it to FEFF, the UTF-16 BOM. | ||
*/ | ||
function stripBOM(content) { | ||
if (content.charCodeAt(0) === 0xFEFF) { | ||
content = content.slice(1); | ||
} | ||
return content; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,22 @@ | |
process.argv[1] = path.resolve(process.argv[1]); | ||
|
||
var Module = NativeModule.require('module'); | ||
|
||
// 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could go either way cause it's not at all consistent in the file, we might be shifting that way but there's only one use of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rvagg Oh, okay. I thought if we allowed the new code being landed to follow this rule strictly, then we just have to cleanup only the old code. |
||
var internalModule = NativeModule.require('internal/module'); | ||
// read the source | ||
var filename = Module._resolveFilename(process.argv[1]); | ||
var source = fs.readFileSync(filename, 'utf-8'); | ||
// remove shebang and BOM | ||
source = internalModule.stripBOM(source.replace(/^\#\!.*/, '')); | ||
// compile the script, this will throw if it fails | ||
new vm.Script(source, {filename: filename, displayErrors: true}); | ||
process.exit(0); | ||
} | ||
|
||
startup.preloadModules(); | ||
if (global.v8debug && | ||
process.execArgv.some(function(arg) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var foo bar; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
var foo bar; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
var foo = 'bar'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env node | ||
var foo = 'bar'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const spawnSync = require('child_process').spawnSync; | ||
const path = require('path'); | ||
|
||
const common = require('../common'); | ||
|
||
var node = process.execPath; | ||
|
||
// test both sets of arguments that check syntax | ||
var syntaxArgs = [ | ||
['-c'], | ||
['--check'] | ||
]; | ||
|
||
// test good syntax with and without shebang | ||
[ | ||
'syntax/good_syntax.js', | ||
'syntax/good_syntax', | ||
'syntax/good_syntax_shebang.js', | ||
'syntax/good_syntax_shebang', | ||
].forEach(function(file) { | ||
file = path.join(common.fixturesDir, file); | ||
|
||
// loop each possible option, `-c` or `--check` | ||
syntaxArgs.forEach(function(args) { | ||
var _args = args.concat(file); | ||
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); | ||
}); | ||
}); | ||
|
||
// test bad syntax with and without shebang | ||
[ | ||
'syntax/bad_syntax.js', | ||
'syntax/bad_syntax', | ||
'syntax/bad_syntax_shebang.js', | ||
'syntax/bad_syntax_shebang' | ||
].forEach(function(file) { | ||
file = path.join(common.fixturesDir, file); | ||
|
||
// loop each possible option, `-c` or `--check` | ||
syntaxArgs.forEach(function(args) { | ||
var _args = args.concat(file); | ||
var c = spawnSync(node, _args, {encoding: 'utf8'}); | ||
|
||
// no stdout should be produced | ||
assert.equal(c.stdout, '', 'stdout produced'); | ||
|
||
// stderr should have a syntax error message | ||
var match = c.stderr.match(/^SyntaxError: Unexpected identifier$/m); | ||
assert(match, 'stderr incorrect'); | ||
|
||
assert.equal(c.status, 1, 'code == ' + c.status); | ||
}); | ||
}); | ||
|
||
// test file not found | ||
[ | ||
'syntax/file_not_found.js', | ||
'syntax/file_not_found' | ||
].forEach(function(file) { | ||
file = path.join(common.fixturesDir, file); | ||
|
||
// loop each possible option, `-c` or `--check` | ||
syntaxArgs.forEach(function(args) { | ||
var _args = args.concat(file); | ||
var c = spawnSync(node, _args, {encoding: 'utf8'}); | ||
|
||
// no stdout should be produced | ||
assert.equal(c.stdout, '', 'stdout produced'); | ||
|
||
// stderr should have a module not found error message | ||
var match = c.stderr.match(/^Error: Cannot find module/m); | ||
assert(match, 'stderr incorrect'); | ||
|
||
assert.equal(c.status, 1, 'code == ' + c.status); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer using consistent camel casing. I am not sure if we can fix the
_print_eval
above. cc @bnoordhuisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be happy to change this if everyone agrees this should be done - I just tried to match the style I saw in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's consistent with
_print_eval
. I'm fine with it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh okay then. I was just worried that we would introduce non camel case identifiers in to the js land.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed? the syntax check won't actually launch the script so there will never be access to
process._syntax_check_only
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait. nm. was thinking from the users' perspective. forgot about internal tooling.