1 | 'use strict'
|
2 |
|
3 | const crypto = require('crypto')
|
4 | , moduleRequire = require('eslint-module-utils/module-require').default
|
5 | , hashObject = require('eslint-module-utils/hash').hashObject
|
6 |
|
7 | const cache = new Map()
|
8 |
|
9 |
|
10 | const parserOptions = {
|
11 | loc: true,
|
12 | range: true,
|
13 | raw: true,
|
14 | tokens: true,
|
15 | comment: true,
|
16 | attachComment: true,
|
17 | }
|
18 |
|
19 | exports.parse = function parse(content, options) {
|
20 | options = Object.assign({}, options, parserOptions)
|
21 |
|
22 | if (!options.filePath) {
|
23 | throw new Error('no file path provided!')
|
24 | }
|
25 |
|
26 | const keyHash = crypto.createHash('sha256')
|
27 | keyHash.update(content)
|
28 | hashObject(options, keyHash)
|
29 |
|
30 | const key = keyHash.digest('hex')
|
31 |
|
32 | let ast = cache.get(key)
|
33 | if (ast != null) return ast
|
34 |
|
35 | const realParser = moduleRequire(options.parser)
|
36 |
|
37 | ast = realParser.parse(content, options)
|
38 | cache.set(key, ast)
|
39 |
|
40 | return ast
|
41 | }
|