forked from protofire/solhint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (58 loc) · 1.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const fs = require('fs')
const parser = require('@solidity-parser/parser')
const glob = require('glob')
const ignore = require('ignore')
const astParents = require('ast-parents')
const { applyExtends } = require('./config/config-file')
const Reporter = require('./reporter')
const TreeListener = require('./tree-listener')
const checkers = require('./rules/index')
function parseInput(inputStr) {
try {
// first we try to parse the string as we normally do
return parser.parse(inputStr, { loc: true, range: true })
} catch (e) {
// using 'loc' may throw when inputStr is empty or only has comments
return parser.parse(inputStr, {})
}
}
function processStr(inputStr, config = {}, fileName = '') {
config = applyExtends(config)
let ast
try {
ast = parseInput(inputStr)
} catch (e) {
if (e instanceof parser.ParserError) {
const reporter = new Reporter([], config)
for (const error of e.errors) {
reporter.addReport(
error.line,
error.column,
Reporter.SEVERITY.ERROR,
`Parse error: ${error.message}`
)
}
return reporter
} else {
throw e
}
}
const tokens = parser.tokenize(inputStr, { loc: true })
const reporter = new Reporter(tokens, config)
const listener = new TreeListener(checkers(reporter, config, inputStr, tokens, fileName))
astParents(ast)
parser.visit(ast, listener)
return reporter
}
function processFile(file, config) {
const report = processStr(fs.readFileSync(file).toString(), config, file)
report.file = file
return report
}
function processPath(path, config) {
const ignoreFilter = ignore().add(config.excludedFiles)
const allFiles = glob.sync(path, {})
const files = ignoreFilter.filter(allFiles)
return files.map(curFile => processFile(curFile, config))
}
module.exports = { processPath, processFile, processStr }