UNPKG

2.55 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 comments and tokens (for doc parsing)
23 parserOptions.comment = true
24 parserOptions.attachComment = true // keeping this for backward-compat with older parsers
25 parserOptions.tokens = true
26
27 // attach node locations
28 parserOptions.loc = true
29 parserOptions.range = true
30
31 // provide the `filePath` like eslint itself does, in `parserOptions`
32 // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
33 parserOptions.filePath = path
34
35 // @typescript-eslint/parser will parse the entire project with typechecking if you provide
36 // "project" or "projects" in parserOptions. Removing these options means the parser will
37 // only parse one file in isolate mode, which is much, much faster.
38 // https://github.com/benmosher/eslint-plugin-import/issues/1408#issuecomment-509298962
39 delete parserOptions.project
40 delete parserOptions.projects
41
42 // require the parser relative to the main module (i.e., ESLint)
43 const parser = moduleRequire(parserPath)
44
45 if (typeof parser.parseForESLint === 'function') {
46 let ast
47 try {
48 ast = parser.parseForESLint(content, parserOptions).ast
49 } catch (e) {
50 //
51 }
52 if (!ast || typeof ast !== 'object') {
53 console.warn(
54 '`parseForESLint` from parser `' +
55 parserPath +
56 '` is invalid and will just be ignored'
57 )
58 } else {
59 return ast
60 }
61 }
62
63 return parser.parse(content, parserOptions)
64}
65
66function getParserPath(path, context) {
67 const parsers = context.settings['import/parsers']
68 if (parsers != null) {
69 const extension = extname(path)
70 for (let parserPath in parsers) {
71 if (parsers[parserPath].indexOf(extension) > -1) {
72 // use this alternate parser
73 log('using alt parser:', parserPath)
74 return parserPath
75 }
76 }
77 }
78 // default to use ESLint parser
79 return context.parserPath
80}