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