forked from protofire/solhint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solhint.js
executable file
·206 lines (167 loc) · 5.26 KB
/
solhint.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
const program = require('commander')
const _ = require('lodash')
const fs = require('fs')
const process = require('process')
const linter = require('./lib/index')
const { loadConfig } = require('./lib/config/config-file')
const { validate } = require('./lib/config/config-validator')
const applyFixes = require('./lib/apply-fixes')
const ruleFixer = require('./lib/rule-fixer')
const packageJson = require('./package.json')
function init() {
const version = packageJson.version
program.version(version)
program
.usage('[options] <file> [...other_files]')
.option('-f, --formatter [name]', 'report formatter name (stylish, table, tap, unix)')
.option('-w, --max-warnings [maxWarningsNumber]', 'number of allowed warnings')
.option('-c, --config [file_name]', 'file to use as your .solhint.json')
.option('-q, --quiet', 'report errors only - default: false')
.option('--ignore-path [file_name]', 'file to use as your .solhintignore')
.option('--fix', 'automatically fix problems')
.option('--init', 'create configuration file for solhint')
.description('Linter for Solidity programming language')
.action(execMainAction)
program
.command('stdin')
.description('linting of source code data provided to STDIN')
.option('--filename [file_name]', 'name of file received using STDIN')
.action(processStdin)
program
.command('init-config', null, { noHelp: true })
.description('create configuration file for solhint')
.action(writeSampleConfigFile)
program.parse(process.argv)
if (program.init) {
writeSampleConfigFile()
}
if (program.args.length < 1) {
program.help()
}
}
function execMainAction() {
if (program.init) {
writeSampleConfigFile()
}
let formatterFn
try {
// to check if is a valid formatter before execute linter
formatterFn = getFormatter(program.formatter)
} catch (ex) {
console.error(ex.message)
process.exit(1)
}
const reportLists = program.args.filter(_.isString).map(processPath)
const reports = _.flatten(reportLists)
const warningsCount = reports.reduce((acc, i) => acc + i.warningCount, 0)
const warningsNumberExceeded = program.maxWarnings >= 0 && warningsCount > program.maxWarnings
if (program.fix) {
for (const report of reports) {
const inputSrc = fs.readFileSync(report.filePath).toString()
const fixes = _(report.reports)
.filter(x => x.fix)
.map(x => x.fix(ruleFixer))
.sort((a, b) => a.range[0] - b.range[0])
.value()
const { fixed, output } = applyFixes(fixes, inputSrc)
if (fixed) {
report.reports = report.reports.filter(x => !x.fix)
fs.writeFileSync(report.filePath, output)
}
}
}
if (program.quiet) {
// filter the list of reports, to set errors only.
reports[0].reports = reports[0].reports.filter(i => i.severity === 2)
}
if (printReports(reports, formatterFn)) {
if (program.maxWarnings && !reports[0].errorCount && warningsNumberExceeded) {
console.log(
'Solhint found more warnings than the maximum specified (maximum: %s)',
program.maxWarnings
)
process.exit(1)
}
}
exitWithCode(reports)
}
function processStdin(options) {
const STDIN_FILE = 0
const stdinBuffer = fs.readFileSync(STDIN_FILE)
const report = processStr(stdinBuffer.toString())
report.file = options.filename || 'stdin'
printReports([report])
}
function writeSampleConfigFile() {
const configPath = '.solhint.json'
const sampleConfig = `{
"extends": "solhint:default"
}
`
if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, sampleConfig)
console.log('Configuration file created!')
} else {
console.log('Configuration file already exists')
}
process.exit(0)
}
const readIgnore = _.memoize(() => {
let ignoreFile = '.solhintignore'
try {
if (program.ignorePath) {
ignoreFile = program.ignorePath
}
return fs
.readFileSync(ignoreFile)
.toString()
.split('\n')
.map(i => i.trim())
} catch (e) {
if (program.ignorePath && e.code === 'ENOENT') {
console.error(`\nERROR: ${ignoreFile} is not a valid path.`)
}
return []
}
})
const readConfig = _.memoize(() => {
let config = {}
try {
config = loadConfig(program.config)
} catch (e) {
console.log(e.message)
process.exit(1)
}
const configExcludeFiles = _.flatten(config.excludedFiles)
config.excludedFiles = _.concat(configExcludeFiles, readIgnore())
// validate the configuration before continuing
validate(config)
return config
})
function processStr(input) {
return linter.processStr(input, readConfig())
}
function processPath(path) {
return linter.processPath(path, readConfig())
}
function printReports(reports, formatter) {
console.log(formatter(reports))
return reports
}
function getFormatter(formatter) {
const formatterName = formatter || 'stylish'
try {
return require(`eslint/lib/formatters/${formatterName}`)
} catch (ex) {
ex.message = `\nThere was a problem loading formatter option: ${program.formatter} \nError: ${
ex.message
}`
throw ex
}
}
function exitWithCode(reports) {
const errorsCount = reports.reduce((acc, i) => acc + i.errorCount, 0)
process.exit(errorsCount > 0 ? 1 : 0)
}
init()