2022-11-10 10:43:16 +00:00
|
|
|
'use strict';
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const moduleRequire = require('eslint-module-utils/module-require').default;
|
|
|
|
const hashObject = require('eslint-module-utils/hash').hashObject;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const cache = new Map();
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
// must match ESLint default options or we'll miss the cache every time
|
|
|
|
const parserOptions = {
|
|
|
|
loc: true,
|
|
|
|
range: true,
|
|
|
|
raw: true,
|
|
|
|
tokens: true,
|
|
|
|
comment: true,
|
|
|
|
attachComment: true,
|
2022-11-10 10:43:16 +00:00
|
|
|
};
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
exports.parse = function parse(content, options) {
|
2022-11-10 10:43:16 +00:00
|
|
|
options = Object.assign({}, options, parserOptions);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
|
|
|
if (!options.filePath) {
|
2022-11-10 10:43:16 +00:00
|
|
|
throw new Error('no file path provided!');
|
2020-08-25 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const keyHash = crypto.createHash('sha256');
|
|
|
|
keyHash.update(content);
|
|
|
|
hashObject(options, keyHash);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const key = keyHash.digest('hex');
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
let ast = cache.get(key);
|
|
|
|
if (ast != null) return ast;
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
const realParser = moduleRequire(options.parser);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
ast = realParser.parse(content, options);
|
|
|
|
cache.set(key, ast);
|
2020-08-25 23:57:08 +00:00
|
|
|
|
2022-11-10 10:43:16 +00:00
|
|
|
return ast;
|
|
|
|
};
|