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

protofire / solhint / #1714

18 Jul 2019 04:17PM UTC coverage: 97.828% (-0.04%) from 97.864%
#1714

push

fvictorio
2.1.2

589 of 666 branches covered (88.44%)

2792 of 2854 relevant lines covered (97.83%)

119.1 hits per line

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

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

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

7
  docs: {
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)
267✔
29

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

101
    return curCtx && curCtx.complexityScope
25✔
102
  }
103

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

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

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

117
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