Detect and report syntax errors in source code strings.
When you type node src.js
you get a friendly error report about exactly where
the syntax error is. This module lets you check for syntax errors and report
them in a similarly friendly format that wrapping a try/catch around
Function()
or vm.runInNewContext()
doesn't get you.
var fs = require('fs');
var check = require('syntax-error');
var file = __dirname + '/src.js';
var src = fs.readFileSync(file);
var err = check(src, file);
if (err) {
console.error('ERROR DETECTED' + Array(62).join('!'));
console.error(err);
console.error(Array(76).join('-'));
}
$ node check.js
ERROR DETECTED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/home/substack/projects/node-syntax-error/example/src.js:5
if (Array.isArray(x) res.push.apply(res, x);
^
ParseError: Unexpected identifier
---------------------------------------------------------------------------
var check = require('syntax-error')
Check the source code string src
for syntax errors.
Optionally you can specify a filename file
that will show up in the output.
If src
has a syntax error, return an error object err
that can be printed or
stringified.
If there are no syntax errors in src
, return undefined
.
Return the long string description with a source snippet and a ^
under
pointing exactly where the error was detected.
short string description of the error type
line number of the error in the original source (indexing starts at 1)
column number of the error in the original source (indexing starts at 1)
With npm do:
npm install syntax-error
MIT
# syntax-error Detect and report syntax errors in source code strings. [![build status](https://secure.travis-ci.org/substack/node-syntax-error.png)](http://travis-ci.org/substack/node-syntax-error) When you type `node src.js` you get a friendly error report about exactly where the syntax error is. This module lets you check for syntax errors and report them in a similarly friendly format that wrapping a try/catch around `Function()` or `vm.runInNewContext()` doesn't get you. # example ``` js var fs = require('fs'); var check = require('syntax-error'); var file = __dirname + '/src.js'; var src = fs.readFileSync(file); var err = check(src, file); if (err) { console.error('ERROR DETECTED' + Array(62).join('!')); console.error(err); console.error(Array(76).join('-')); } ``` *** ``` $ node check.js ERROR DETECTED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! /home/substack/projects/node-syntax-error/example/src.js:5 if (Array.isArray(x) res.push.apply(res, x); ^ ParseError: Unexpected identifier --------------------------------------------------------------------------- ``` # methods ``` js var check = require('syntax-error') ``` ## var err = check(src, file) Check the source code string `src` for syntax errors. Optionally you can specify a filename `file` that will show up in the output. If `src` has a syntax error, return an error object `err` that can be printed or stringified. If there are no syntax errors in `src`, return `undefined`. ## err.toString() Return the long string description with a source snippet and a `^` under pointing exactly where the error was detected. # attributes ## err.message short string description of the error type ## err.line line number of the error in the original source (indexing starts at 1) ## err.column column number of the error in the original source (indexing starts at 1) # install With [npm](http://npmjs.org) do: ``` npm install syntax-error ``` # license MIT
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 19553 | swellard | Move and rename clients | ||
//guest/perforce_software/helix-web-services/main/source/clients/2016.1.0/javascript/node_modules/syntax-error/readme.markdown | |||||
#1 | 18810 | tjuricek |
First-pass at JavaScript client SDK. JavaScript requires Node with Gulp to "browserfy" the library. It's the easiest way I found to use the swagger-js project; bundle up a wrapping method. There is no JavaScript reference guide. The swagger-js doesn't really document what they do very well, actually. Overall I'm not particularly impressed by swagger-js, it was hard to even figure out what the right method syntax was. We may want to invest time in doing it better. This required setting CORS response headers, which are currently defaulted to a fairly insecure setting. |