-
Notifications
You must be signed in to change notification settings - Fork 155
/
Copy pathindex.js
172 lines (143 loc) · 4.37 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
function hasLeadingUnderscore(text) {
return text && text[0] === '_'
}
function match(text, regex) {
return text.replace(regex, '').length === 0
}
function isMixedCase(text) {
return match(text, /[_]*[a-z$]+[a-zA-Z0-9$]*[_]?/)
}
function isUpperSnakeCase(text) {
return match(text, /_{0,2}[A-Z0-9$]+[_A-Z0-9$]*/)
}
class Base {
constructor(reporter, config, source, fileName) {
this.ignoreDeprecated = true;
this.deprecatedPrefix = '__DEPRECATED_';
this.underscorePrefix = '__';
this.reporter = reporter;
this.ignored = this.constructor.global;
this.ruleId = this.constructor.ruleId;
if (this.ruleId === undefined) {
throw Error('missing ruleId static property');
}
}
error(node, message, fix) {
if (!this.ignored) {
this.reporter.error(node, this.ruleId, message, fix);
}
}
}
module.exports = [
class extends Base {
static ruleId = 'leading-underscore';
ContractDefinition(node) {
if (node.kind === 'library') {
this.inLibrary = true
}
}
'ContractDefinition:exit'() {
this.inLibrary = false
}
StateVariableDeclaration() {
this.inStateVariableDeclaration = true
}
'StateVariableDeclaration:exit'() {
this.inStateVariableDeclaration = false
}
VariableDeclaration(node) {
if (!this.inLibrary) {
if (!this.inStateVariableDeclaration) {
this.validateName(node, false, 'variable')
return
}
this.validateName(node, 'variable')
}
}
FunctionDefinition(node) {
if (!this.inLibrary) {
if (!node.name) {
return
}
for (const parameter of node.parameters) {
parameter.visibility = node.visibility
}
this.validateName(node, 'function')
}
}
validateName(node, type) {
if (this.ignoreDeprecated && node.name.startsWith(this.deprecatedPrefix)) {
return
}
const isPrivate = node.visibility === 'private'
const isInternal = node.visibility === 'internal' || node.visibility === 'default'
const isConstant = node.isDeclaredConst
const isImmutable = node.isImmutable
const shouldHaveLeadingUnderscore = (isPrivate || isInternal) && !(isConstant || isImmutable)
if (node.name === null) {
return
}
if (hasLeadingUnderscore(node.name) !== shouldHaveLeadingUnderscore) {
this._error(node, node.name, shouldHaveLeadingUnderscore, type)
}
}
fixStatement(node, shouldHaveLeadingUnderscore, type) {
let range
if (type === 'function') {
range = node.range
range[0] += 8
} else if (type === 'parameter') {
range = node.identifier.range
} else {
range = node.identifier.range
range[0] -= 1
}
return (fixer) =>
shouldHaveLeadingUnderscore
? fixer.insertTextBeforeRange(range, ' _')
: fixer.removeRange([range[0] + 1, range[0] + 1])
}
_error(node, name, shouldHaveLeadingUnderscore, type) {
this.error(
node,
`'${name}' ${shouldHaveLeadingUnderscore ? 'should' : 'should not'} start with _`,
// this.fixStatement(node, shouldHaveLeadingUnderscore, type)
)
}
},
class extends Base {
static ruleId = 'func-name-mixedcase';
FunctionDefinition(node) {
// Allow __DEPRECATED_ prefixed functions and __ prefixed functions
if (node.name.startsWith(this.deprecatedPrefix) || node.name.startsWith(this.underscorePrefix)) {
return
}
if (!isMixedCase(node.name) && !node.isConstructor) {
// Allow external functions to be in UPPER_SNAKE_CASE - for immutable state getters
if (node.visibility === 'external' && isUpperSnakeCase(node.name)) {
return
}
this.error(node, 'Function name must be in mixedCase',)
}
}
},
class extends Base {
static ruleId = 'var-name-mixedcase';
VariableDeclaration(node) {
if (node.name.startsWith(this.deprecatedPrefix)) {
return
}
if (!node.isDeclaredConst && !node.isImmutable) {
this.validateVariablesName(node)
}
}
validateVariablesName(node) {
if (node.name.startsWith(this.deprecatedPrefix)) {
return
}
if (!isMixedCase(node.name)) {
this.error(node, 'Variable name must be in mixedCase')
}
}
}
];