-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
186 lines (163 loc) · 4.61 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
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
'use strict'
const fs = require('fs')
const path = require('path')
const acorn = require('acorn')
const glob = require('glob')
const sourceMap = require('source-map')
/**
* options { Object }
* --context The base directory, an absolute path, default process.cwd()
* --files Array of glob pattern
* --ecmaVersion Indicates the ECMAScript version to parse.Default is es5.
* --module use ES modules, default false
* --allowHashBang supports files that start with hash bang, default false
* --not=folderName1,folderName2 An array of file/folder names that you would like to ignore. Defaults to `[]`.
*/
function check (options = {}) {
const configs = {
context: process.cwd(),
files: [],
ecmaVersion: 'es5',
module: false,
allowHashBang: false,
not: []
}
const configFilePath = path.resolve(options.context || configs.context, '.escheckrc')
let config = {}
return new Promise(async (resolve, reject) => {
/**
* Check for a configuration file. If one exists, default to those options
* if no command line arguments are passed in
*/
if (fs.existsSync(configFilePath)) {
config = JSON.parse(fs.readFileSync(configFilePath))
}
Object.assign(configs, config, options)
const { context, ecmaVersion, module: esmodule, allowHashBang, not: pathsToIgnore } = configs
const files = [].concat(configs.files)
if (!files || !files.length) {
reject(new Error('No files were passed in please pass in a list of files to es-check!'))
}
/**
* define ecmaScript version
* - Default ecmaScript version is '5'
*/
let e
switch (ecmaVersion) {
case 'es3':
e = '3'
break
case 'es4':
e = '4'
break
case 'es5':
e = '5'
break
case 'es6':
e = '6'
break
case 'es7':
e = '7'
break
case 'es8':
e = '8'
break
case 'es9':
e = '9'
break
case 'es10':
e = '10'
break
case 'es2015':
e = '6'
break
case 'es2016':
e = '7'
break
case 'es2017':
e = '8'
break
case 'es2018':
e = '9'
break
case 'es2019':
e = '10'
break
default:
reject(new Error('Invalid ecmaScript version, please pass a valid version'))
}
const errArray = []
const globOpts = { cwd: context, root: context, nodir: true }
const acornOpts = { ecmaVersion: e, silent: true }
const filterForIgnore = (globbedFiles) => {
if (pathsToIgnore && pathsToIgnore.length > 0) {
const filtered = globbedFiles.filter((filePath) => !pathsToIgnore
.some(ignoreValue => filePath.includes(ignoreValue)))
return filtered
}
return globbedFiles
}
if (esmodule) {
acornOpts.sourceType = 'module'
}
if (allowHashBang) {
acornOpts.allowHashBang = true
}
files.forEach((pattern) => {
/**
* pattern => glob or array
*/
const globbedFiles = glob.sync(pattern, globOpts)
if (globbedFiles.length === 0) {
reject(new Error(`ES-Check: Did not find any files to check for ${pattern}.`))
}
const filteredFiles = filterForIgnore(globbedFiles);
filteredFiles.forEach(async (file) => {
const code = fs.readFileSync(path.resolve(globOpts.root, file), 'utf8')
try {
acorn.parse(code, acornOpts)
} catch (err) {
let errorObj = {
err,
stack: err.stack,
...err.loc,
file
}
errArray.push(errorObj)
}
})
})
if (errArray.length) {
const errors = await getSource(globOpts.root, errArray)
resolve(errors)
} else {
resolve([])
}
})
}
async function getSource (root, errors) {
for (let err of errors) {
try {
// 如果有sourcemap文件,通过sourcemap定位到源文件
const source = fs.readFileSync(path.resolve(root, err.file + '.map')).toString()
const consumer = await new sourceMap.SourceMapConsumer(source)
const sm = consumer.originalPositionFor({
line: err.line,
column: err.column
})
const sources = consumer.sources
const smIndex = sources.indexOf(sm.source)
const smContent = consumer.sourcesContent[smIndex]
const rawLines = smContent.split(/\r?\n/g)
err.source = sm.source
err.line = sm.line
err.column = sm.column
err.code = rawLines[sm.line - 1]
} catch (rErr) {
err.source = '-'
err.code = '-'
}
}
return errors
}
module.exports = check