UNPKG

1.33 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 attach comments
23 parserOptions.attachComment = true
24
25 // require the parser relative to the main module (i.e., ESLint)
26 const parser = moduleRequire(parserPath)
27
28 return parser.parse(content, parserOptions)
29}
30
31function getParserPath(path, context) {
32 const parsers = context.settings['import/parsers']
33 if (parsers != null) {
34 const extension = extname(path)
35 for (let parserPath in parsers) {
36 if (parsers[parserPath].indexOf(extension) > -1) {
37 // use this alternate parser
38 log('using alt parser:', parserPath)
39 return parserPath
40 }
41 }
42 }
43 // default to use ESLint parser
44 return context.parserPath
45}