blob: ffe0a04582d01b35fe16e0a1bb3e95fc93afdea6 [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 16:46:17 +02001'use strict';
2
Mathias Bynens79e2cf02020-05-29 16:46:17 +02003const augmentConfig = require('./augmentConfig');
4const createStylelintResult = require('./createStylelintResult');
5const getConfigForFile = require('./getConfigForFile');
6const getPostcssResult = require('./getPostcssResult');
7const isPathIgnored = require('./isPathIgnored');
8const lintSource = require('./lintSource');
Mathias Bynens79e2cf02020-05-29 16:46:17 +02009const { cosmiconfig } = require('cosmiconfig');
10
11const IS_TEST = process.env.NODE_ENV === 'test';
Tim van der Lippe16b82282021-11-08 13:50:26 +000012const STOP_DIR = IS_TEST ? process.cwd() : undefined;
Mathias Bynens79e2cf02020-05-29 16:46:17 +020013
Tim van der Lippe16b82282021-11-08 13:50:26 +000014/** @typedef {import('stylelint').InternalApi} StylelintInternalApi */
Mathias Bynens79e2cf02020-05-29 16:46:17 +020015
16/**
17 * The stylelint "internal API" is passed among functions
18 * so that methods on a stylelint instance can invoke
Tim van der Lippe16b82282021-11-08 13:50:26 +000019 * each other while sharing options and caches.
20 *
21 * @param {import('stylelint').LinterOptions} options
Mathias Bynens79e2cf02020-05-29 16:46:17 +020022 * @returns {StylelintInternalApi}
23 */
Tim van der Lippe16b82282021-11-08 13:50:26 +000024function createStylelint(options = {}) {
Tim van der Lippe4cb09742022-01-07 14:25:03 +010025 const cwd = options.cwd || process.cwd();
26
Tim van der Lippe16b82282021-11-08 13:50:26 +000027 /** @type {StylelintInternalApi} */
28 // @ts-expect-error -- TS2740: Type '{ _options: LinterOptions; }' is missing the following properties from type 'InternalApi'
Tim van der Lippe4cb09742022-01-07 14:25:03 +010029 const stylelint = { _options: { ...options, cwd } };
Mathias Bynens79e2cf02020-05-29 16:46:17 +020030
Tim van der Lippe16b82282021-11-08 13:50:26 +000031 stylelint._extendExplorer = cosmiconfig('', {
Tim van der Lippe4cb09742022-01-07 14:25:03 +010032 transform: augmentConfig.augmentConfigExtended(cwd),
Mathias Bynens79e2cf02020-05-29 16:46:17 +020033 stopDir: STOP_DIR,
34 });
35
36 stylelint._specifiedConfigCache = new Map();
37 stylelint._postcssResultCache = new Map();
Tim van der Lippe16b82282021-11-08 13:50:26 +000038 stylelint._createStylelintResult = createStylelintResult.bind(null, stylelint);
39 stylelint._getPostcssResult = getPostcssResult.bind(null, stylelint);
40 stylelint._lintSource = lintSource.bind(null, stylelint);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020041
Tim van der Lippe16b82282021-11-08 13:50:26 +000042 stylelint.getConfigForFile = getConfigForFile.bind(null, stylelint);
43 stylelint.isPathIgnored = isPathIgnored.bind(null, stylelint);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020044
Tim van der Lippe16b82282021-11-08 13:50:26 +000045 return stylelint;
46}
47
48module.exports = /** @type {typeof import('stylelint').createLinter} */ (createStylelint);