ignore.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. 'use strict';
  2. exports.__esModule = true;
  3. const extname = require('path').extname;
  4. const log = require('debug')('eslint-plugin-import:utils:ignore');
  5. // one-shot memoized
  6. let cachedSet; let lastSettings;
  7. function validExtensions(context) {
  8. if (cachedSet && context.settings === lastSettings) {
  9. return cachedSet;
  10. }
  11. lastSettings = context.settings;
  12. cachedSet = makeValidExtensionSet(context.settings);
  13. return cachedSet;
  14. }
  15. function makeValidExtensionSet(settings) {
  16. // start with explicit JS-parsed extensions
  17. const exts = new Set(settings['import/extensions'] || [ '.js' ]);
  18. // all alternate parser extensions are also valid
  19. if ('import/parsers' in settings) {
  20. for (const parser in settings['import/parsers']) {
  21. const parserSettings = settings['import/parsers'][parser];
  22. if (!Array.isArray(parserSettings)) {
  23. throw new TypeError('"settings" for ' + parser + ' must be an array');
  24. }
  25. parserSettings.forEach(ext => exts.add(ext));
  26. }
  27. }
  28. return exts;
  29. }
  30. exports.getFileExtensions = makeValidExtensionSet;
  31. exports.default = function ignore(path, context) {
  32. // check extension whitelist first (cheap)
  33. if (!hasValidExtension(path, context)) return true;
  34. if (!('import/ignore' in context.settings)) return false;
  35. const ignoreStrings = context.settings['import/ignore'];
  36. for (let i = 0; i < ignoreStrings.length; i++) {
  37. const regex = new RegExp(ignoreStrings[i]);
  38. if (regex.test(path)) {
  39. log(`ignoring ${path}, matched pattern /${ignoreStrings[i]}/`);
  40. return true;
  41. }
  42. }
  43. return false;
  44. };
  45. function hasValidExtension(path, context) {
  46. return validExtensions(context).has(extname(path));
  47. }
  48. exports.hasValidExtension = hasValidExtension;