isPathIgnored.js 1.3 KB

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