This repository has been archived by the owner on Mar 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.js
112 lines (97 loc) · 3.44 KB
/
index.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
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
108
109
110
111
112
const util = require('@typescript-eslint/eslint-plugin/dist/util')
const isTypeScriptFile = require('../../utils/isTypeScriptFile')
const { AST_NODE_TYPES, ESLintUtils } = require('@typescript-eslint/experimental-utils')
const createRule = ESLintUtils.RuleCreator(
name =>
`https://github.com/stardust-ui/react/tree/master/packages/internal-tooling/eslint/${name}.js`,
)
module.exports = createRule({
name: 'no-visibility-modifiers',
meta: {
type: 'problem',
docs: {
description: 'Require omit modifiers on class properties and methods',
category: 'Best Practices',
recommended: 'error',
},
messages: {
presentModifier: 'Present accessibility modifier on {{type}} {{name}}.',
},
schema: [],
},
defaultOptions: [],
create(context) {
const sourceCode = context.getSourceCode()
/**
* Generates the report for rule violations
*/
function reportIssue(messageId, nodeType, node, nodeName) {
context.report({
node,
messageId,
data: {
type: nodeType,
name: nodeName,
},
})
}
/**
* Checks if a method declaration has an accessibility modifier.
* @param methodDefinition The node representing a MethodDefinition.
*/
function checkMethodAccessibilityModifier(methodDefinition) {
let nodeType = 'method definition'
if (['get', 'set'].includes(methodDefinition.kind)) {
nodeType = `${methodDefinition.kind} property accessor`
}
if (isTypeScriptFile(context.getFilename())) {
const methodName = util.getNameFromClassMember(methodDefinition, sourceCode)
if (!!methodDefinition.accessibility) {
reportIssue('presentModifier', nodeType, methodDefinition, methodName)
}
}
}
/**
* Checks if property has an accessibility modifier.
* @param classProperty The node representing a ClassProperty.
*/
function checkPropertyAccessibilityModifier(classProperty) {
const nodeType = 'class property'
if (isTypeScriptFile(context.getFilename())) {
const propertyName = util.getNameFromPropertyName(classProperty.key)
if (!!classProperty.accessibility) {
reportIssue('presentModifier', nodeType, classProperty, propertyName)
}
}
}
/**
* Checks that the parameter property has the desired accessibility modifiers set.
* @param {TSESTree.TSParameterProperty} node The node representing a Parameter Property
*/
function checkParameterPropertyAccessibilityModifier(node) {
const nodeType = 'parameter property'
if (isTypeScriptFile(context.getFilename())) {
// HAS to be an identifier or assignment or TSC will throw
if (
node.parameter.type !== AST_NODE_TYPES.Identifier &&
node.parameter.type !== AST_NODE_TYPES.AssignmentPattern
) {
return
}
const nodeName =
node.parameter.type === AST_NODE_TYPES.Identifier
? node.parameter.name
: // has to be an Identifier or TSC will throw an error
node.parameter.left.name
if (!!node.accessibility) {
reportIssue('presentModifier', nodeType, node, nodeName)
}
}
}
return {
TSParameterProperty: checkParameterPropertyAccessibilityModifier,
ClassProperty: checkPropertyAccessibilityModifier,
MethodDefinition: checkMethodAccessibilityModifier,
}
},
})