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

protofire / solhint / #1482

13 Aug 2019 05:32PM UTC coverage: 97.856% (+1.8%) from 96.073%
#1482

push

fvictorio
2.2.0

603 of 678 branches covered (88.94%)

2921 of 2985 relevant lines covered (97.86%)

117.34 hits per line

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

94.29
/lib/rules/best-practises/code-complexity.js
1
const BaseChecker = require('./../base-checker')
1✔
2

1✔
3
const ruleId = 'code-complexity'
4
const meta = {
1✔
5
  type: 'best-practises',
1✔
6

1✔
7
  docs: {
1✔
8
    description: 'Function has cyclomatic complexity "current" but allowed no more than maxcompl.',
9
    category: 'Best Practise Rules'
10
  },
11

12
  isDefault: false,
13
  recommended: false,
14
  defaultSetup: ['warn', 7],
15

16
  schema: [
17
    {
18
      type: 'array',
19
      items: [{ type: 'integer' }],
20
      uniqueItems: true,
21
      minItems: 2
22
    }
23
  ]
24
}
25

26
class CodeComplexityChecker extends BaseChecker {
27
  constructor(reporter, config) {
28
    super(reporter, ruleId, meta)
29

30
    this.maxComplexity = (config && config.getNumber('code-complexity', 7)) || 7
31
  }
32

33
  enterFunctionDefinition(ctx) {
34
    this._attachComplexityScope(ctx)
35
  }
36

37
  enterModifierDefinition(ctx) {
38
    this._attachComplexityScope(ctx)
39
  }
40

41
  enterIfStatement(ctx) {
42
    this._complexityPlusOne(ctx)
43
  }
44

45
  enterWhileStatement(ctx) {
46
    this._complexityPlusOne(ctx)
47
  }
48

49
  enterDoWhileStatement(ctx) {
50
    this._complexityPlusOne(ctx)
51
  }
52

53
  enterForStatement(ctx) {
54
    this._complexityPlusOne(ctx)
55
  }
277✔
56

57
  exitFunctionDefinition(ctx) {
277✔
58
    this._verifyComplexityScope(ctx)
59
  }
60

61
  exitModifierDefinition(ctx) {
3✔
62
    this._verifyComplexityScope(ctx)
63
  }
64

65
  _attachComplexityScope(ctx) {
×
66
    ComplexityScope.activate(ctx)
67
  }
68

69
  _complexityPlusOne(ctx) {
10✔
70
    const scope = ComplexityScope.of(ctx)
71
    if (scope) {
72
      scope.complexityPlusOne()
73
    }
6✔
74
  }
75

76
  _verifyComplexityScope(ctx) {
77
    const scope = ComplexityScope.of(ctx)
3✔
78

79
    if (scope && scope.complexity > this.maxComplexity) {
80
      this._error(ctx, scope)
81
    }
3✔
82
  }
83

84
  _error(ctx, scope) {
85
    const curComplexity = scope.complexity
3✔
86
    const maxComplexity = this.maxComplexity
87

88
    const message = `Function has cyclomatic complexity ${curComplexity} but allowed no more than ${maxComplexity}`
89
    this.error(ctx, message)
×
90
  }
91
}
92

93
class ComplexityScope {
3✔
94
  static of(ctx) {
95
    let curCtx = ctx
96

97
    while (curCtx && !curCtx.complexityScope) {
22✔
98
      curCtx = curCtx.parentCtx
22!
99
    }
22✔
100

101
    return curCtx && curCtx.complexityScope
102
  }
103

104
  static activate(ctx) {
3✔
105
    ctx.complexityScope = new ComplexityScope()
106
  }
3✔
107

1✔
108
  constructor() {
109
    this.complexity = 1
110
  }
111

112
  complexityPlusOne() {
1✔
113
    this.complexity += 1
1✔
114
  }
115
}
1✔
116

1✔
117
module.exports = CodeComplexityChecker
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