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