-
Notifications
You must be signed in to change notification settings - Fork 145
/
index.js
120 lines (98 loc) · 2.95 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
function hasLeadingUnderscore(text) {
return text && text[0] === '_'
}
class Base {
constructor(reporter, config, source, fileName) {
this.ignoreDeprecated = true;
this.deprecatedPrefix = '__DEPRECATED_';
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)
)
}
},
];