-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathparse.js
44 lines (34 loc) · 1.26 KB
/
parse.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
import moduleRequire from './module-require'
import assign from 'object-assign'
import { extname } from 'path'
import debug from 'debug'
const log = debug('eslint-plugin-import:parse')
export default function (path, content, context) {
if (context == null) throw new Error('need context to parse properly')
let { parserOptions } = context
const parserPath = getParserPath(path, context)
if (!parserPath) throw new Error('parserPath is required!')
// hack: espree blows up with frozen options
parserOptions = assign({}, parserOptions)
parserOptions.ecmaFeatures = assign({}, parserOptions.ecmaFeatures)
// always attach comments
parserOptions.attachComment = true
// require the parser relative to the main module (i.e., ESLint)
const parser = moduleRequire(parserPath)
return parser.parse(content, parserOptions)
}
function getParserPath(path, context) {
const parsers = context.settings['import/parsers']
if (parsers != null) {
const extension = extname(path)
for (let parserPath in parsers) {
if (parsers[parserPath].indexOf(extension) > -1) {
// use this alternate parser
log('using alt parser:', parserPath)
return parserPath
}
}
}
// default to use ESLint parser
return context.parserPath
}