createStylelint.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const augmentConfig = require('./augmentConfig');
  3. const createStylelintResult = require('./createStylelintResult');
  4. const getConfigForFile = require('./getConfigForFile');
  5. const getPostcssResult = require('./getPostcssResult');
  6. const isPathIgnored = require('./isPathIgnored');
  7. const lintSource = require('./lintSource');
  8. const { cosmiconfig } = require('cosmiconfig');
  9. const IS_TEST = process.env.NODE_ENV === 'test';
  10. const STOP_DIR = IS_TEST ? process.cwd() : undefined;
  11. /** @typedef {import('stylelint').InternalApi} StylelintInternalApi */
  12. /**
  13. * The stylelint "internal API" is passed among functions
  14. * so that methods on a stylelint instance can invoke
  15. * each other while sharing options and caches.
  16. *
  17. * @param {import('stylelint').LinterOptions} options
  18. * @returns {StylelintInternalApi}
  19. */
  20. function createStylelint(options = {}) {
  21. const cwd = options.cwd || process.cwd();
  22. /** @type {StylelintInternalApi} */
  23. // @ts-expect-error -- TS2740: Type '{ _options: LinterOptions; }' is missing the following properties from type 'InternalApi'
  24. const stylelint = { _options: { ...options, cwd } };
  25. stylelint._extendExplorer = cosmiconfig('', {
  26. transform: augmentConfig.augmentConfigExtended(cwd),
  27. stopDir: STOP_DIR,
  28. });
  29. stylelint._specifiedConfigCache = new Map();
  30. stylelint._postcssResultCache = new Map();
  31. stylelint._createStylelintResult = createStylelintResult.bind(null, stylelint);
  32. stylelint._getPostcssResult = getPostcssResult.bind(null, stylelint);
  33. stylelint._lintSource = lintSource.bind(null, stylelint);
  34. stylelint.getConfigForFile = getConfigForFile.bind(null, stylelint);
  35. stylelint.isPathIgnored = isPathIgnored.bind(null, stylelint);
  36. return stylelint;
  37. }
  38. module.exports = /** @type {typeof import('stylelint').createLinter} */ (createStylelint);