2022-11-10 11:43:16 +01:00
|
|
|
'use strict';
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
const crypto = require('crypto');
|
|
|
|
const moduleRequire = require('eslint-module-utils/module-require').default;
|
|
|
|
const hashObject = require('eslint-module-utils/hash').hashObject;
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
const cache = new Map();
|
2020-08-26 01:57:08 +02: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 11:43:16 +01:00
|
|
|
};
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
exports.parse = function parse(content, options) {
|
2022-11-10 11:43:16 +01:00
|
|
|
options = Object.assign({}, options, parserOptions);
|
2020-08-26 01:57:08 +02:00
|
|
|
|
|
|
|
if (!options.filePath) {
|
2022-11-10 11:43:16 +01:00
|
|
|
throw new Error('no file path provided!');
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
const keyHash = crypto.createHash('sha256');
|
|
|
|
keyHash.update(content);
|
|
|
|
hashObject(options, keyHash);
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
const key = keyHash.digest('hex');
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
let ast = cache.get(key);
|
|
|
|
if (ast != null) return ast;
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
const realParser = moduleRequire(options.parser);
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
ast = realParser.parse(content, options);
|
|
|
|
cache.set(key, ast);
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
return ast;
|
|
|
|
};
|