UNPKG

1.61 kBJavaScriptView Raw
1"use strict"
2exports.__esModule = true
3
4const moduleRequire = require('./module-require').default
5const extname = require('path').extname
6
7const log = require('debug')('eslint-plugin-import:parse')
8
9exports.default = function parse(path, content, context) {
10
11 if (context == null) throw new Error('need context to parse properly')
12
13 let parserOptions = context.parserOptions
14 const parserPath = getParserPath(path, context)
15
16 if (!parserPath) throw new Error('parserPath is required!')
17
18 // hack: espree blows up with frozen options
19 parserOptions = Object.assign({}, parserOptions)
20 parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures)
21
22 // always include and attach comments
23 parserOptions.comment = true
24 parserOptions.attachComment = true
25
26 // attach node locations
27 parserOptions.loc = true
28
29 // provide the `filePath` like eslint itself does, in `parserOptions`
30 // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
31 parserOptions.filePath = path
32
33 // require the parser relative to the main module (i.e., ESLint)
34 const parser = moduleRequire(parserPath)
35
36 return parser.parse(content, parserOptions)
37}
38
39function getParserPath(path, context) {
40 const parsers = context.settings['import/parsers']
41 if (parsers != null) {
42 const extension = extname(path)
43 for (let parserPath in parsers) {
44 if (parsers[parserPath].indexOf(extension) > -1) {
45 // use this alternate parser
46 log('using alt parser:', parserPath)
47 return parserPath
48 }
49 }
50 }
51 // default to use ESLint parser
52 return context.parserPath
53}