-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
underscore-internal.ts
107 lines (102 loc) · 3.44 KB
/
underscore-internal.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/eslint-plugin-etc
*/
import { TSESTree as es } from "@typescript-eslint/experimental-utils";
import { findParent, getParserServices, isIdentifier } from "eslint-etc";
import * as ts from "typescript";
import { getTagsFromDeclaration } from "../tslint-tag";
import { ruleCreator } from "../utils";
const rule = ruleCreator({
defaultOptions: [],
meta: {
docs: {
description:
"Forbids internal APIs that are not prefixed with underscores.",
recommended: false,
},
fixable: undefined,
hasSuggestions: false,
messages: {
forbidden: "Internal APIs not prefixed with underscores are forbidden.",
},
schema: [],
type: "problem",
},
name: "underscore-internal",
create: (context) => {
const { esTreeNodeToTSNodeMap } = getParserServices(context);
function checkDeclaration(identifier: es.BindingName, tsNode: ts.Node) {
const tags = getTagsFromDeclaration("internal", tsNode);
if (tags.length > 0) {
context.report({
messageId: "forbidden",
node: identifier,
});
}
}
return {
"ClassDeclaration[id.name=/^[^_]/]": (node: es.ClassDeclaration) => {
if (node.id) {
checkDeclaration(node.id, esTreeNodeToTSNodeMap.get(node));
}
},
"PropertyDefinition[key.name=/^[^_]/]": (node: es.PropertyDefinition) => {
if (isIdentifier(node.key)) {
checkDeclaration(node.key, esTreeNodeToTSNodeMap.get(node));
}
},
"FunctionDeclaration[id.name=/^[^_]/]": (
node: es.FunctionDeclaration
) => {
if (node.id) {
checkDeclaration(node.id, esTreeNodeToTSNodeMap.get(node));
}
},
"MethodDefinition[key.name=/^[^_]/]": (node: es.MethodDefinition) => {
if (isIdentifier(node.key)) {
checkDeclaration(node.key, esTreeNodeToTSNodeMap.get(node));
}
},
"TSEnumDeclaration[id.name=/^[^_]/]": (node: es.TSEnumDeclaration) => {
checkDeclaration(node.id, esTreeNodeToTSNodeMap.get(node));
},
"TSEnumMember[id.name=/^[^_]/]": (node: es.TSEnumMember) => {
if (isIdentifier(node.id)) {
checkDeclaration(node.id, esTreeNodeToTSNodeMap.get(node));
}
},
"TSInterfaceDeclaration[id.name=/^[^_]/]": (
node: es.TSInterfaceDeclaration
) => {
checkDeclaration(node.id, esTreeNodeToTSNodeMap.get(node));
},
"TSMethodSignature[key.name=/^[^_]/]": (node: es.TSMethodSignature) => {
if (isIdentifier(node.key)) {
checkDeclaration(node.key, esTreeNodeToTSNodeMap.get(node));
}
},
"TSPropertySignature[key.name=/^[^_]/]": (
node: es.TSPropertySignature
) => {
if (isIdentifier(node.key)) {
checkDeclaration(node.key, esTreeNodeToTSNodeMap.get(node));
}
},
"TSTypeAliasDeclaration[id.name=/^[^_]/]": (
node: es.TSTypeAliasDeclaration
) => {
checkDeclaration(node.id, esTreeNodeToTSNodeMap.get(node));
},
"VariableDeclarator[id.name=/^[^_]/]": (node: es.VariableDeclarator) => {
checkDeclaration(
node.id,
esTreeNodeToTSNodeMap.get(
findParent(node, "VariableDeclaration") as es.VariableDeclaration
)
);
},
};
},
});
export = rule;