webhook/node_modules/eslint-plugin-import/memo-parser/index.js

42 lines
946 B
JavaScript
Raw Normal View History

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