-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.js
53 lines (42 loc) · 1.67 KB
/
run.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
const { codeFrameColumns } = require('@babel/code-frame')
const { pass, fail, skip } = require('create-jest-runner')
const { getStandardAdditionalConfig } = require('./helpers/getStandardAdditionalConfig')
const { fixPossibleErrors } = require('./helpers/fixPossibleErrors')
const { getLocalStandard } = require('./helpers/getLocalStandard')
const getFileContent = require('./helpers/getFileContent')
const { isPathIgnored } = require('./helpers/isPathIgnored')
module.exports = ({testPath, config: {rootDir = process.cwd()}}) => {
const standard = getLocalStandard(rootDir)
let fix = !!process.env.STANDARD_AUTOFIX
const start = +new Date()
const contents = getFileContent(testPath)
const standardAdditionalConfig = getStandardAdditionalConfig(rootDir)
const opts = {
fileName: testPath,
fix,
...standardAdditionalConfig,
cwd: rootDir
}
if (isPathIgnored(opts, standard, testPath)) {
return skip({ start, end: +new Date(), test: { path: testPath } })
}
const result = standard.lintTextSync(contents, opts)
const end = +new Date()
if (fix) {
fixPossibleErrors(result, testPath, standard)
}
if (result.errorCount === 0) {
return pass({ start, end, test: { path: testPath } })
}
let compiledErrorMessage = ''
result.results[0].messages.forEach(({message, ruleId, line, column}) => {
compiledErrorMessage += `${message} (${ruleId} at ${line}:${column})\n\n`
const location = { start: { line, column } }
compiledErrorMessage += `${codeFrameColumns(contents, location, {highlightCode: true})}\n`
})
return fail({
start,
end,
test: { path: testPath, errorMessage: compiledErrorMessage, title: 'Standard error' }
})
}