isPathIgnored.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const filterFilePaths = require('./utils/filterFilePaths');
  3. const getFileIgnorer = require('./utils/getFileIgnorer');
  4. const micromatch = require('micromatch');
  5. const normalizePath = require('normalize-path');
  6. const path = require('path');
  7. /**
  8. * To find out if a path is ignored, we need to load the config,
  9. * which may have an ignoreFiles property. We then check the path
  10. * against these.
  11. * @param {import('stylelint').InternalApi} stylelint
  12. * @param {string} [filePath]
  13. * @return {Promise<boolean>}
  14. */
  15. module.exports = async function isPathIgnored(stylelint, filePath) {
  16. if (!filePath) {
  17. return false;
  18. }
  19. const cwd = stylelint._options.cwd;
  20. const ignorer = getFileIgnorer(stylelint._options);
  21. const result = await stylelint.getConfigForFile(filePath, filePath);
  22. if (!result) {
  23. return true;
  24. }
  25. // Glob patterns for micromatch should be in POSIX-style
  26. const ignoreFiles = /** @type {Array<string>} */ (result.config.ignoreFiles || []).map((s) =>
  27. normalizePath(s),
  28. );
  29. const absoluteFilePath = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);
  30. if (micromatch([absoluteFilePath], ignoreFiles).length) {
  31. return true;
  32. }
  33. // Check filePath with .stylelintignore file
  34. if (filterFilePaths(ignorer, [path.relative(cwd, absoluteFilePath)]).length === 0) {
  35. return true;
  36. }
  37. return false;
  38. };