blob: 4a0b48639d81e32291fb27bd712fddb3a128c31e [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 = {}) {
25 /** @type {StylelintInternalApi} */
26 // @ts-expect-error -- TS2740: Type '{ _options: LinterOptions; }' is missing the following properties from type 'InternalApi'
Mathias Bynens79e2cf02020-05-29 16:46:17 +020027 const stylelint = { _options: options };
28
Tim van der Lippe16b82282021-11-08 13:50:26 +000029 stylelint._extendExplorer = cosmiconfig('', {
30 transform: augmentConfig.augmentConfigExtended.bind(null, stylelint),
Mathias Bynens79e2cf02020-05-29 16:46:17 +020031 stopDir: STOP_DIR,
32 });
33
34 stylelint._specifiedConfigCache = new Map();
35 stylelint._postcssResultCache = new Map();
Tim van der Lippe16b82282021-11-08 13:50:26 +000036 stylelint._createStylelintResult = createStylelintResult.bind(null, stylelint);
37 stylelint._getPostcssResult = getPostcssResult.bind(null, stylelint);
38 stylelint._lintSource = lintSource.bind(null, stylelint);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020039
Tim van der Lippe16b82282021-11-08 13:50:26 +000040 stylelint.getConfigForFile = getConfigForFile.bind(null, stylelint);
41 stylelint.isPathIgnored = isPathIgnored.bind(null, stylelint);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020042
Tim van der Lippe16b82282021-11-08 13:50:26 +000043 return stylelint;
44}
45
46module.exports = /** @type {typeof import('stylelint').createLinter} */ (createStylelint);