-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
88 lines (78 loc) · 2.13 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
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
const fs = require('fs')
const path = require('path')
const acorn = require('acorn')
const { pass, fail } = require('create-jest-runner')
/**
* This runner is heavily inspired by {@link https://github.com/dollarshaveclub/es-check}
* It's using parts of the code directly from that repository, as there's no easy to way
* to just extract the core. The aim is to be 100% compatible with the original implementation too.
*
* If the core is extracted, this code will be updated to only use that.
*/
const configFilePath = path.resolve(process.cwd(), '.escheckrc')
let configFile = {}
if (fs.existsSync(configFilePath)) {
configFile = JSON.parse(fs.readFileSync(configFilePath))
}
const getEcmaVersion = (version) => {
switch (version) {
case 'es3':
return '3'
case 'es4':
return '4'
case 'es5':
return '5'
case 'es6':
case 'es2015':
return '6'
case 'es7':
case 'es2016':
return '7'
case 'es8':
case 'es2017':
return '8'
case 'es9':
case 'es2018':
return '9'
case 'es10':
case 'es2019':
return '10'
default:
throw new Error(`Invalid ECMA version specified: ${version}`)
}
}
module.exports = ({ testPath, config }) => {
const start = Date.now()
const options = config.testEnvironmentOptions || {}
const contents = fs.readFileSync(testPath, 'utf8')
const ecmaVersion = getEcmaVersion(options.ecmaVersion || configFile.ecmaVersion)
const esmodule = options.module || configFile.module
const allowHashBang = options.allowHashBang || configFile.allowHashBang
const acornOpts = { ecmaVersion, silent: true }
if (esmodule) {
acornOpts.sourceType = 'module'
}
if (allowHashBang) {
acornOpts.allowHashBang = true
}
try {
acorn.parse(contents, acornOpts)
const end = Date.now()
return pass({ start, end, test: { path: testPath } })
} catch (err) {
const end = Date.now()
return fail({
start,
end,
test: {
location: {
line: err.loc.line,
column: err.raisedAt
},
path: testPath,
errorMessage: err,
title: 'ES check'
}
})
}
}