index.js 946 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. const crypto = require('crypto');
  3. const moduleRequire = require('eslint-module-utils/module-require').default;
  4. const hashObject = require('eslint-module-utils/hash').hashObject;
  5. const cache = new Map();
  6. // must match ESLint default options or we'll miss the cache every time
  7. const parserOptions = {
  8. loc: true,
  9. range: true,
  10. raw: true,
  11. tokens: true,
  12. comment: true,
  13. attachComment: true,
  14. };
  15. exports.parse = function parse(content, options) {
  16. options = Object.assign({}, options, parserOptions);
  17. if (!options.filePath) {
  18. throw new Error('no file path provided!');
  19. }
  20. const keyHash = crypto.createHash('sha256');
  21. keyHash.update(content);
  22. hashObject(options, keyHash);
  23. const key = keyHash.digest('hex');
  24. let ast = cache.get(key);
  25. if (ast != null) return ast;
  26. const realParser = moduleRequire(options.parser);
  27. ast = realParser.parse(content, options);
  28. cache.set(key, ast);
  29. return ast;
  30. };