[Pkg-javascript-commits] [node-syntax-error] 01/47: initial thing that works

Bastien Roucariès rouca at moszumanska.debian.org
Fri Aug 25 19:29:57 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-syntax-error.

commit 90fc19934c62db5f2565d8d03b466b9c7d18eba2
Author: James Halliday <mail at substack.net>
Date:   Wed Aug 1 23:50:48 2012 -0700

    initial thing that works
---
 index.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/index.js b/index.js
new file mode 100644
index 0000000..190ff37
--- /dev/null
+++ b/index.js
@@ -0,0 +1,59 @@
+var esprima = require('esprima');
+
+module.exports = function (src, file) {
+    if (typeof src !== 'string') src = String(src);
+    
+    try {
+        Function(src);
+        return;
+    }
+    catch (err) {
+        if (err.constructor.name !== 'SyntaxError') throw err;
+        return errorInfo(src, file);
+    }
+};
+
+function errorInfo (src, file) {
+    try {
+        esprima.parse(src);
+        return;
+    }
+    catch (err) {
+        return new ParseError(err, src, file);
+    }
+}
+
+function ParseError (err, src, file) {
+    SyntaxError.call(this);
+    
+    this.message = err.message.replace(/^Line \d+: /, '');
+    
+    this.line = err.lineNumber;
+    this.column = err.column;
+    
+    this.annotated = '\n'
+        + (file || '(anonymous file)')
+        + ':' + this.line
+        + '\n'
+        + src.split('\n')[this.line - 1]
+        + '\n'
+        + Array(this.column).join(' ') + '^'
+        + '\n'
+        + 'ParseError: ' + this.message
+    ;
+}
+
+ParseError.prototype = new SyntaxError;
+
+ParseError.prototype.toString = function () {
+    return this.annotated;
+};
+
+ParseError.prototype.inspect = function () {
+    return '[ParseError: '
+        + this.message
+        + ', '
+        + '(line ' + this.line + ', column ' + this.column + ')'
+        + ']'
+    ;
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-syntax-error.git



More information about the Pkg-javascript-commits mailing list