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

protofire / solhint / #1804

13 Aug 2019 05:32PM UTC coverage: 97.836% (+1.6%) from 96.241%
#1804

push

fvictorio
2.2.0

603 of 678 branches covered (88.94%)

2893 of 2957 relevant lines covered (97.84%)

118.44 hits per line

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

98.31
/lib/comment-directive-parser.js
1
class CommentDirectiveParser {
2
  constructor(tokens) {
3
    this.lastLine = tokens.tokenSource.line
276✔
4
    this.ruleStore = new RuleStore(this.lastLine)
276✔
5

6
    this.parseComments(tokens)
276✔
7
  }
8

9
  parseComments(tokens) {
10
    const items = tokens.filterForChannel(0, tokens.tokens.length - 1, 1)
276✔
11
    if (items) {
276✔
12
      items.forEach(this.onComment.bind(this))
28✔
13
    }
14
  }
15

16
  onComment(lexema) {
17
    const text = lexema.text
35✔
18
    const curLine = lexema.line
35✔
19
    const ruleStore = this.ruleStore
35✔
20

21
    if (text.includes('solhint-disable-line')) {
35✔
22
      const rules = this.parseRuleIds(text, 'solhint-disable-line')
2✔
23
      ruleStore.disableRules(curLine, rules)
2✔
24
      return
2✔
25
    }
26

27
    if (text.includes('solhint-disable-next-line')) {
33✔
28
      const rules = this.parseRuleIds(text, 'solhint-disable-next-line')
7✔
29

30
      if (curLine + 1 <= this.lastLine) {
7✔
31
        ruleStore.disableRules(curLine + 1, rules)
4✔
32
      }
33

34
      return
7✔
35
    }
36

37
    if (text.includes('solhint-disable-previous-line')) {
26✔
38
      const rules = this.parseRuleIds(text, 'solhint-disable-previous-line')
4✔
39

40
      if (curLine > 0) {
4!
41
        ruleStore.disableRules(curLine - 1, rules)
4✔
42
      }
43

44
      return
4✔
45
    }
46

47
    if (text.includes('solhint-disable')) {
22✔
48
      const rules = this.parseRuleIds(text, 'solhint-disable')
10✔
49

50
      ruleStore.disableRulesToEndOfFile(curLine, rules)
10✔
51

52
      return
10✔
53
    }
54

55
    if (text.includes('solhint-enable')) {
12✔
56
      const rules = this.parseRuleIds(text, 'solhint-enable')
6✔
57

58
      ruleStore.enableRulesToEndOfFile(curLine, rules)
6✔
59
    }
60
  }
61

62
  parseRuleIds(text, start) {
63
    const ruleIds = text
29✔
64
      .replace('//', '')
65
      .replace('/*', '')
66
      .replace('*/', '')
67
      .replace(start, '')
68

69
    const rules = ruleIds
29✔
70
      .split(',')
71
      .map(curRule => curRule.trim())
29✔
72
      .filter(i => i.length > 0)
29✔
73

74
    return rules.length > 0 ? rules : 'all'
29✔
75
  }
76

77
  isRuleEnabled(line, ruleId) {
78
    return this.ruleStore.isRuleEnabled(line, ruleId)
219✔
79
  }
80
}
81

82
class RuleStore {
83
  constructor(lastLine) {
84
    this.disableRuleByLine = []
276✔
85
    this.disableAllByLine = []
276✔
86
    this.lastLine = lastLine
276✔
87

88
    this.initRulesTable()
276✔
89
  }
90

91
  initRulesTable() {
92
    for (let i = 1; i <= this.lastLine; i += 1) {
276✔
93
      this.disableRuleByLine[i] = new Set()
2,936✔
94
      this.disableAllByLine[i] = false
2,936✔
95
    }
96
  }
97

98
  disableRules(curLine, newRules) {
99
    if (newRules === 'all') {
56✔
100
      this.disableAllByLine[curLine] = true
37✔
101
    } else {
102
      const lineRules = this.disableRuleByLine[curLine]
19✔
103
      this.disableRuleByLine[curLine] = new Set([...lineRules, ...newRules])
19✔
104
    }
105
  }
106

107
  disableRulesToEndOfFile(startLine, rules) {
108
    this._toEndOfFile(startLine, i => this.disableRules(i, rules))
46✔
109
  }
110

111
  enableRules(curLine, rules) {
112
    if (rules === 'all') {
18✔
113
      this.disableAllByLine[curLine] = false
9✔
114
    } else {
115
      const lineRules = this.disableRuleByLine[curLine]
9✔
116
      rules.forEach(curRule => lineRules.delete(curRule))
9✔
117
    }
118
  }
119

120
  enableRulesToEndOfFile(startLine, rules) {
121
    this._toEndOfFile(startLine, i => this.enableRules(i, rules))
18✔
122
  }
123

124
  isRuleEnabled(line, ruleId) {
125
    return !this.disableAllByLine[line] && !this.disableRuleByLine[line].has(ruleId)
219✔
126
  }
127

128
  _toEndOfFile(from, callback) {
129
    if (!callback) {
16!
130
      return
×
131
    }
132

133
    for (let i = from; i <= this.lastLine; i += 1) {
16✔
134
      callback(i)
64✔
135
    }
136
  }
137
}
138

139
module.exports = CommentDirectiveParser
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