/
no-literal-call.js
63 lines (55 loc) · 1.84 KB
/
no-literal-call.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* @fileoverview Rule to disallow a call of a literal.
* @author Toru Nagashima
*/
"use strict"
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const LITERAL_TYPE = /^(?:(?:Array|Object)Expression|(?:Template)?Literal)$/u
const LITERAL_AND_CLASS_TYPE =
/^(?:(?:Array|Class|Object)Expression|(?:Template)?Literal)$/u
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description: "disallow a call of a literal.",
category: "Possible Errors",
recommended: false,
url: "https://github.com/mysticatea/eslint-plugin/blob/v13.0.0/docs/rules/no-literal-call.md",
},
fixable: null,
schema: [],
type: "problem",
},
create(context) {
/**
* Reports a given node if it's a literal.
*
* @param {ASTNode} callee - A callee node to report.
* @param {RegExp} pattern - A pattern of literal types.
* @returns {void}
*/
function check(callee, pattern) {
if (pattern.test(callee.type)) {
context.report({
node: callee,
message: "This is not a function.",
})
}
}
return {
CallExpression(node) {
check(node.callee, LITERAL_AND_CLASS_TYPE)
},
NewExpression(node) {
check(node.callee, LITERAL_TYPE)
},
TaggedTemplateExpression(node) {
check(node.tag, LITERAL_AND_CLASS_TYPE)
},
}
},
}