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

protofire / solhint / #4251

28 Jan 2020 06:12PM UTC coverage: 96.241% (+63.2%) from 33.076%
#4251

push

fvictorio
Merge branch '3.0' of github.com:protofire/solhint into 3.0

362 of 437 branches covered (82.84%)

1946 of 2022 relevant lines covered (96.24%)

43.04 hits per line

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

93.33
/lib/comment-directive-parser.js
1
class CommentDirectiveParser {
2
  constructor(tokens) {
3
    const lastToken = tokens[tokens.length - 1]
152✔
4
    this.lastLine = lastToken ? lastToken.loc.end.line : 0
152✔
5
    this.ruleStore = new RuleStore(this.lastLine)
152✔
6

7
    this.parseComments(tokens)
152✔
8
  }
9

10
  parseComments(tokens) {
11
    const items = tokens.filter(
152✔
12
      token => token.type === 'Keyword' && /^(\/\/|\/\*)/.test(token.value)
3,464✔
13
    )
14
    items.forEach(item => this.onComment(item))
152✔
15
  }
16

17
  onComment(lexema) {
18
    const text = lexema.value
9✔
19
    const curLine = lexema.loc.start.line
9✔
20
    const ruleStore = this.ruleStore
9✔
21

22
    if (text.includes('solhint-disable-line')) {
9!
23
      const rules = this.parseRuleIds(text, 'solhint-disable-line')
×
24
      ruleStore.disableRules(curLine, rules)
×
25
      return
×
26
    }
27

28
    if (text.includes('solhint-disable-next-line')) {
9✔
29
      const rules = this.parseRuleIds(text, 'solhint-disable-next-line')
2✔
30

31
      if (curLine + 1 <= this.lastLine) {
2!
32
        ruleStore.disableRules(curLine + 1, rules)
2✔
33
      }
34

35
      return
2✔
36
    }
37

38
    if (text.includes('solhint-disable-previous-line')) {
7✔
39
      const rules = this.parseRuleIds(text, 'solhint-disable-previous-line')
2✔
40

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

45
      return
2✔
46
    }
47

48
    if (text.includes('solhint-disable')) {
5✔
49
      const rules = this.parseRuleIds(text, 'solhint-disable')
3✔
50

51
      ruleStore.disableRulesToEndOfFile(curLine, rules)
3✔
52

53
      return
3✔
54
    }
55

56
    if (text.includes('solhint-enable')) {
2!
57
      const rules = this.parseRuleIds(text, 'solhint-enable')
2✔
58

59
      ruleStore.enableRulesToEndOfFile(curLine, rules)
2✔
60
    }
61
  }
62

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

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

75
    return rules.length > 0 ? rules : 'all'
9✔
76
  }
77

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

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

89
    this.initRulesTable()
152✔
90
  }
91

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

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

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

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

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

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

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

134
    for (let i = from; i <= this.lastLine; i += 1) {
5✔
135
      callback(i)
15✔
136
    }
137
  }
138
}
139

140
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

© 2025 Coveralls, Inc