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

protofire / solhint / #77846

27 Jan 2023 01:47PM UTC coverage: 90.015% (-6.3%) from 96.281%
#77846

push

web-flow
Merge pull request #389 from juanpcapurro/fix-doc-generation

Fix doc generation

519 of 629 branches covered (82.51%)

1226 of 1362 relevant lines covered (90.01%)

149.53 hits per line

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

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

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

10
  parseComments(tokens) {
11
    const items = tokens.filter(
272✔
12
      (token) => token.type === 'Keyword' && /^(\/\/|\/\*)/.test(token.value)
6,102✔
13
    )
14
    items.forEach((item) => this.onComment(item))
272✔
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.replace('//', '').replace('/*', '').replace('*/', '').replace(start, '')
9✔
65

66
    const rules = ruleIds
9✔
67
      .split(',')
68
      .map((curRule) => curRule.trim())
9✔
69
      .filter((i) => i.length > 0)
9✔
70

71
    return rules.length > 0 ? rules : 'all'
9✔
72
  }
73

74
  isRuleEnabled(line, ruleId) {
75
    return this.ruleStore.isRuleEnabled(line, ruleId)
140✔
76
  }
77
}
78

79
class RuleStore {
80
  constructor(lastLine) {
81
    this.disableRuleByLine = []
272✔
82
    this.disableAllByLine = []
272✔
83
    this.lastLine = lastLine
272✔
84

85
    this.initRulesTable()
272✔
86
  }
87

88
  initRulesTable() {
89
    for (let i = 1; i <= this.lastLine; i += 1) {
272✔
90
      this.disableRuleByLine[i] = new Set()
2,606✔
91
      this.disableAllByLine[i] = false
2,606✔
92
    }
93
  }
94

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

104
  disableRulesToEndOfFile(startLine, rules) {
105
    this._toEndOfFile(startLine, (i) => this.disableRules(i, rules))
11✔
106
  }
107

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

117
  enableRulesToEndOfFile(startLine, rules) {
118
    this._toEndOfFile(startLine, (i) => this.enableRules(i, rules))
4✔
119
  }
120

121
  isRuleEnabled(line, ruleId) {
122
    const allRulesDisabled = this.disableAllByLine[line]
140✔
123
    const ruleDisabled = this.disableRuleByLine[line] && this.disableRuleByLine[line].has(ruleId)
140✔
124
    return !allRulesDisabled && !ruleDisabled
140✔
125
  }
126

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

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

138
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