UNPKG

1.16 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to restrict what can be thrown as an exception.
3 * @author Dieter Oberkofler
4 */
5
6"use strict";
7
8const astUtils = require("../ast-utils");
9
10//------------------------------------------------------------------------------
11// Rule Definition
12//------------------------------------------------------------------------------
13
14module.exports = {
15 meta: {
16 docs: {
17 description: "disallow throwing literals as exceptions",
18 category: "Best Practices",
19 recommended: false,
20 url: "https://eslint.org/docs/rules/no-throw-literal"
21 },
22
23 schema: []
24 },
25
26 create(context) {
27
28 return {
29
30 ThrowStatement(node) {
31 if (!astUtils.couldBeError(node.argument)) {
32 context.report({ node, message: "Expected an object to be thrown." });
33 } else if (node.argument.type === "Identifier") {
34 if (node.argument.name === "undefined") {
35 context.report({ node, message: "Do not throw undefined." });
36 }
37 }
38
39 }
40
41 };
42
43 }
44};