1 | "use strict"
|
2 | exports.__esModule = true
|
3 |
|
4 | const moduleRequire = require('./module-require').default
|
5 | const extname = require('path').extname
|
6 |
|
7 | const log = require('debug')('eslint-plugin-import:parse')
|
8 |
|
9 | exports.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 |
|
19 | parserOptions = Object.assign({}, parserOptions)
|
20 | parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures)
|
21 |
|
22 |
|
23 | parserOptions.attachComment = true
|
24 |
|
25 |
|
26 | const parser = moduleRequire(parserPath)
|
27 |
|
28 | return parser.parse(content, parserOptions)
|
29 | }
|
30 |
|
31 | function 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 |
|
38 | log('using alt parser:', parserPath)
|
39 | return parserPath
|
40 | }
|
41 | }
|
42 | }
|
43 |
|
44 | return context.parserPath
|
45 | }
|