Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | const _ = require('lodash'); |
| 4 | const augmentConfig = require('./augmentConfig'); |
| 5 | const createStylelintResult = require('./createStylelintResult'); |
| 6 | const getConfigForFile = require('./getConfigForFile'); |
| 7 | const getPostcssResult = require('./getPostcssResult'); |
| 8 | const isPathIgnored = require('./isPathIgnored'); |
| 9 | const lintSource = require('./lintSource'); |
| 10 | const path = require('path'); |
| 11 | const { cosmiconfig } = require('cosmiconfig'); |
| 12 | |
| 13 | const IS_TEST = process.env.NODE_ENV === 'test'; |
| 14 | const STOP_DIR = IS_TEST ? path.resolve(__dirname, '..') : undefined; |
| 15 | |
| 16 | /** @typedef {import('stylelint').StylelintInternalApi} StylelintInternalApi */ |
| 17 | |
| 18 | /** |
| 19 | * The stylelint "internal API" is passed among functions |
| 20 | * so that methods on a stylelint instance can invoke |
| 21 | * each other while sharing options and caches |
| 22 | * @param {import('stylelint').StylelintStandaloneOptions} options |
| 23 | * @returns {StylelintInternalApi} |
| 24 | */ |
| 25 | module.exports = function (options = {}) { |
| 26 | /** @type {Partial<StylelintInternalApi>} */ |
| 27 | const stylelint = { _options: options }; |
| 28 | |
| 29 | // Two separate explorers so they can each have their own transform |
| 30 | // function whose results are cached by cosmiconfig |
| 31 | stylelint._fullExplorer = cosmiconfig('stylelint', { |
| 32 | // @ts-ignore TODO TYPES found out which cosmiconfig types are valid |
| 33 | transform: _.partial( |
| 34 | augmentConfig.augmentConfigFull, |
| 35 | /** @type{StylelintInternalApi} */ (stylelint), |
| 36 | ), |
| 37 | stopDir: STOP_DIR, |
| 38 | }); |
| 39 | // @ts-ignore TODO TYPES found out which cosmiconfig types are valid |
| 40 | stylelint._extendExplorer = cosmiconfig(null, { |
| 41 | transform: _.partial( |
| 42 | augmentConfig.augmentConfigExtended, |
| 43 | /** @type{StylelintInternalApi} */ (stylelint), |
| 44 | ), |
| 45 | stopDir: STOP_DIR, |
| 46 | }); |
| 47 | |
| 48 | stylelint._specifiedConfigCache = new Map(); |
| 49 | stylelint._postcssResultCache = new Map(); |
| 50 | stylelint._createStylelintResult = _.partial( |
| 51 | createStylelintResult, |
| 52 | /** @type{StylelintInternalApi} */ (stylelint), |
| 53 | ); |
| 54 | stylelint._getPostcssResult = _.partial( |
| 55 | getPostcssResult, |
| 56 | /** @type{StylelintInternalApi} */ (stylelint), |
| 57 | ); |
| 58 | stylelint._lintSource = _.partial(lintSource, /** @type{StylelintInternalApi} */ (stylelint)); |
| 59 | |
| 60 | stylelint.getConfigForFile = _.partial( |
| 61 | getConfigForFile, |
| 62 | /** @type{StylelintInternalApi} */ (stylelint), |
| 63 | ); |
| 64 | stylelint.isPathIgnored = _.partial( |
| 65 | isPathIgnored, |
| 66 | /** @type{StylelintInternalApi} */ (stylelint), |
| 67 | ); |
| 68 | |
| 69 | return /** @type{StylelintInternalApi} */ (stylelint); |
| 70 | }; |