-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathparse.js
52 lines (39 loc) · 1.35 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
45
46
47
48
49
50
51
52
import fs from 'fs'
export default function (p, context) {
if (context == null) throw new Error("need context to parse properly")
let { parserOptions, parserPath } = context
if (!parserPath) throw new Error("parserPath is required!")
// hack: espree blows up with frozen options
parserOptions = Object.assign({}, parserOptions)
parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures)
// always attach comments
parserOptions.attachComment = true
// require the parser relative to the main module (i.e., ESLint)
const parser = requireParser(parserPath)
return parser.parse(
fs.readFileSync(p, {encoding: 'utf8'}),
parserOptions)
}
import Module from 'module'
import * as path from 'path'
// borrowed from babel-eslint
function createModule(filename) {
var mod = new Module(filename)
mod.filename = filename
mod.paths = Module._nodeModulePaths(path.dirname(filename))
return mod
}
function requireParser(p) {
try {
// attempt to get espree relative to eslint
const eslintPath = require.resolve('eslint')
const eslintModule = createModule(eslintPath)
return require(Module._resolveFilename(p, eslintModule))
} catch(err) { /* ignore */ }
try {
// try relative to entry point
return require.main.require(p)
} catch(err) { /* ignore */ }
// finally, try from here
return require(p)
}