summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorRich Trott <rtrott@gmail.com>2015-11-08 09:53:50 -0800
committerRich Trott <rtrott@gmail.com>2015-11-10 21:01:53 -0800
commit25cd455407e78a77eaf96f4057e480451f84bd46 (patch)
tree111f4594d8632bf8310669e5593a277a170bbc50 /tools
parent35f2f64edd55157bf713e3ef70600705acd1c581 (diff)
downloadnode-new-25cd455407e78a77eaf96f4057e480451f84bd46.tar.gz
tools: enforce `throw new Error()` with lint rule
Add linting rule requiring `throw new Error()` over `throw Error()`. PR-URL: https://github.com/nodejs/node/pull/3714 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/eslint-rules/new-with-error.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/tools/eslint-rules/new-with-error.js b/tools/eslint-rules/new-with-error.js
new file mode 100644
index 0000000000..b0f550db2f
--- /dev/null
+++ b/tools/eslint-rules/new-with-error.js
@@ -0,0 +1,36 @@
+/**
+ * @fileoverview Require `throw new Error()` rather than `throw Error()`
+ * @author Rich Trott
+ */
+'use strict';
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = function(context) {
+
+ var errorList = context.options.length !== 0 ? context.options : ['Error'];
+
+ return {
+ 'ThrowStatement': function(node) {
+ if (node.argument.type === 'CallExpression' &&
+ errorList.indexOf(node.argument.callee.name) !== -1) {
+ context.report(node, 'Use new keyword when throwing.');
+ }
+ }
+ };
+};
+
+module.exports.schema = {
+ 'type': 'array',
+ 'items': [
+ {
+ 'enum': [0, 1, 2]
+ }
+ ],
+ 'additionalItems': {
+ 'type': 'string'
+ },
+ 'uniqueItems': true
+};