-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathno-empty-blocks.js
111 lines (92 loc) · 2.76 KB
/
no-empty-blocks.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
const BaseChecker = require('../base-checker')
const { isFallbackFunction, isReceiveFunction } = require('../../common/ast-types')
const ruleId = 'no-empty-blocks'
const meta = {
type: 'best-practices',
docs: {
description: 'Code block has zero statements inside. Exceptions apply.',
category: 'Best Practice Rules',
examples: {
bad: [
{ description: 'Empty block on if statement', code: 'if (condition) { }' },
{ description: 'Empty contract', code: 'contract Foo { }' },
{
description: 'Empty block in constructor without parent initialization',
code: 'constructor () { }',
},
],
good: [
{ description: 'Empty fallback function', code: 'fallback () external { }' },
{
description: 'Empty constructor with member initialization list',
code: 'constructor(uint param) Foo(param) Bar(param*2) { }',
},
],
},
notes: [
{
note: 'The rule ignores an empty constructor by default as long as base contracts are being inherited. See "Empty Constructor" example.',
},
],
},
isDefault: false,
recommended: true,
defaultSetup: 'warn',
schema: null,
}
class NoEmptyBlocksChecker extends BaseChecker {
constructor(reporter) {
super(reporter, ruleId, meta)
}
ContractDefinition(node) {
this.isAssemblyFor = false
this._validateContractPartsCount(node)
}
Block(node) {
if (node.parent.isConstructor && node.parent.modifiers.length > 0) {
return
}
const isFallbackFunctionBlock = isFallbackFunction(node.parent)
const isReceiveFunctionBlock = isReceiveFunction(node.parent)
if (isFallbackFunctionBlock || isReceiveFunctionBlock) {
// ignore empty blocks in fallback or receive functions
return
}
this._validateChildrenCount(node, 'statements')
}
StructDefinition(node) {
this._validateChildrenCount(node, 'members')
}
EnumDefinition(node) {
this._validateChildrenCount(node, 'members')
}
AssemblyBlock(node) {
if (!this.isAssemblyFor) {
this._validateChildrenCount(node, 'operations')
}
}
AssemblyFor(node) {
this.isAssemblyFor = true
const operationsCount = node.body.operations.length
if (operationsCount === 0) this._error(node)
}
'AssemblyFor:exit'() {
this.isAssemblyFor = false
}
_validateChildrenCount(node, children) {
const blockChildrenCount = node[children].length
if (blockChildrenCount === 0) {
this._error(node)
}
}
_validateContractPartsCount(node) {
const contractPartCount = node.subNodes.length
if (contractPartCount === 0) {
this._error(node)
}
}
_error(node) {
this.warn(node, 'Code contains empty blocks')
}
}
module.exports = NoEmptyBlocksChecker