• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

protofire / solhint / #1432

11 Aug 2023 05:22PM UTC coverage: 29.665% (-62.6%) from 92.281%
#1432

push

web-flow
Merge pull request #473 from protofire/develop

MERGE Develop into Master

0 of 750 branches covered (0.0%)

26 of 86 new or added lines in 13 files covered. (30.23%)

740 existing lines in 58 files now uncovered.

434 of 1463 relevant lines covered (29.67%)

0.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.0
/lib/rules/best-practises/code-complexity.js
1
const BaseChecker = require('../base-checker')
1✔
2
const { severityDescription } = require('../../doc/utils')
1✔
3

4
const ruleId = 'code-complexity'
1✔
5
const DEFAULT_SEVERITY = 'warn'
1✔
6
const DEFAULT_COMPLEXITY = 7
1✔
7
const meta = {
1✔
8
  type: 'best-practises',
9

10
  docs: {
11
    description: 'Function has cyclomatic complexity "current" but allowed no more than maxcompl.',
12
    category: 'Best Practise Rules',
13
    options: [
14
      {
15
        description: severityDescription,
16
        default: DEFAULT_SEVERITY,
17
      },
18
      {
19
        description: 'Maximum allowed cyclomatic complexity',
20
        default: DEFAULT_COMPLEXITY,
21
      },
22
    ],
23
    examples: {
24
      good: [
25
        {
26
          description: 'Low code complexity',
27
          code: require('../../../test/fixtures/best-practises/code-complexity-low'),
28
        },
29
      ],
30
      bad: [
31
        {
32
          description: 'High code complexity',
33
          code: require('../../../test/fixtures/best-practises/code-complexity-high'),
34
        },
35
      ],
36
    },
37
  },
38

39
  isDefault: false,
40
  recommended: false,
41
  defaultSetup: [DEFAULT_SEVERITY, DEFAULT_COMPLEXITY],
42

43
  schema: { type: 'integer' },
44
}
45

46
class CodeComplexityChecker extends BaseChecker {
47
  constructor(reporter, config) {
UNCOV
48
    super(reporter, ruleId, meta)
×
49

UNCOV
50
    this.maxComplexity = (config && config.getNumber('code-complexity', 7)) || 7
×
51
  }
52

53
  FunctionDefinition(node) {
UNCOV
54
    this._attachComplexityScope(node)
×
55
  }
56

57
  ModifierDefinition(node) {
UNCOV
58
    this._attachComplexityScope(node)
×
59
  }
60

61
  IfStatement(node) {
UNCOV
62
    this._complexityPlusOne(node)
×
63
  }
64

65
  WhileStatement(node) {
UNCOV
66
    this._complexityPlusOne(node)
×
67
  }
68

69
  DoWhileStatement(node) {
UNCOV
70
    this._complexityPlusOne(node)
×
71
  }
72

73
  ForStatement(node) {
UNCOV
74
    this._complexityPlusOne(node)
×
75
  }
76

77
  'FunctionDefinition:exit'(node) {
UNCOV
78
    this._verifyComplexityScope(node)
×
79
  }
80

81
  'ModifierDefinition:exit'(node) {
UNCOV
82
    this._verifyComplexityScope(node)
×
83
  }
84

85
  _attachComplexityScope(node) {
UNCOV
86
    ComplexityScope.activate(node)
×
87
  }
88

89
  _complexityPlusOne(node) {
UNCOV
90
    const scope = ComplexityScope.of(node)
×
UNCOV
91
    if (scope) {
×
UNCOV
92
      scope.complexityPlusOne()
×
93
    }
94
  }
95

96
  _verifyComplexityScope(node) {
UNCOV
97
    const scope = ComplexityScope.of(node)
×
98

UNCOV
99
    if (scope && scope.complexity > this.maxComplexity) {
×
UNCOV
100
      this._error(node, scope)
×
101
    }
102
  }
103

104
  _error(node, scope) {
UNCOV
105
    const curComplexity = scope.complexity
×
UNCOV
106
    const maxComplexity = this.maxComplexity
×
107

UNCOV
108
    const message = `Function has cyclomatic complexity ${curComplexity} but allowed no more than ${maxComplexity}`
×
UNCOV
109
    this.error(node, message)
×
110
  }
111
}
112

113
class ComplexityScope {
114
  static of(node) {
UNCOV
115
    let curNode = node
×
116

UNCOV
117
    while (curNode && !curNode.complexityScope) {
×
UNCOV
118
      curNode = curNode.parent
×
119
    }
120

UNCOV
121
    return curNode && curNode.complexityScope
×
122
  }
123

124
  static activate(node) {
UNCOV
125
    node.complexityScope = new ComplexityScope()
×
126
  }
127

128
  constructor() {
UNCOV
129
    this.complexity = 1
×
130
  }
131

132
  complexityPlusOne() {
UNCOV
133
    this.complexity += 1
×
134
  }
135
}
136

137
module.exports = CodeComplexityChecker
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc