Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 3 | const isPathNotFoundError = require('./utils/isPathNotFoundError'); |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 4 | const lintPostcssResult = require('./lintPostcssResult'); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 5 | const path = require('path'); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 6 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 7 | /** @typedef {import('stylelint').InternalApi} StylelintInternalApi */ |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 8 | /** @typedef {import('stylelint').GetLintSourceOptions} Options */ |
| 9 | /** @typedef {import('postcss').Result} Result */ |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 10 | /** @typedef {import('stylelint').PostcssResult} PostcssResult */ |
| 11 | /** @typedef {import('stylelint').StylelintPostcssResult} StylelintPostcssResult */ |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * Run stylelint on a PostCSS Result, either one that is provided |
| 15 | * or one that we create |
| 16 | * @param {StylelintInternalApi} stylelint |
| 17 | * @param {Options} options |
| 18 | * @returns {Promise<PostcssResult>} |
| 19 | */ |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 20 | module.exports = async function lintSource(stylelint, options = {}) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 21 | if (!options.filePath && options.code === undefined && !options.existingPostcssResult) { |
| 22 | return Promise.reject(new Error('You must provide filePath, code, or existingPostcssResult')); |
| 23 | } |
| 24 | |
| 25 | const isCodeNotFile = options.code !== undefined; |
| 26 | |
| 27 | const inputFilePath = isCodeNotFile ? options.codeFilename : options.filePath; |
| 28 | |
| 29 | if (inputFilePath !== undefined && !path.isAbsolute(inputFilePath)) { |
| 30 | if (isCodeNotFile) { |
| 31 | return Promise.reject(new Error('codeFilename must be an absolute path')); |
| 32 | } |
| 33 | |
| 34 | return Promise.reject(new Error('filePath must be an absolute path')); |
| 35 | } |
| 36 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 37 | const isIgnored = await stylelint.isPathIgnored(inputFilePath).catch((err) => { |
| 38 | if (isCodeNotFile && isPathNotFoundError(err)) return false; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 39 | |
| 40 | throw err; |
| 41 | }); |
| 42 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 43 | if (isIgnored) { |
| 44 | return options.existingPostcssResult |
| 45 | ? Object.assign(options.existingPostcssResult, { |
| 46 | stylelint: createEmptyStylelintPostcssResult(), |
| 47 | }) |
| 48 | : createEmptyPostcssResult(inputFilePath); |
| 49 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 50 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 51 | const configSearchPath = stylelint._options.configFile || inputFilePath; |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 52 | const cwd = stylelint._options.cwd; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 53 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 54 | const configForFile = await stylelint |
| 55 | .getConfigForFile(configSearchPath, inputFilePath) |
| 56 | .catch((err) => { |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 57 | if (isCodeNotFile && isPathNotFoundError(err)) return stylelint.getConfigForFile(cwd); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 58 | |
| 59 | throw err; |
| 60 | }); |
| 61 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 62 | if (!configForFile) { |
| 63 | return Promise.reject(new Error('Config file not found')); |
| 64 | } |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 65 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 66 | const config = configForFile.config; |
| 67 | const existingPostcssResult = options.existingPostcssResult; |
| 68 | const stylelintResult = { |
| 69 | ruleSeverities: {}, |
| 70 | customMessages: {}, |
| 71 | disabledRanges: {}, |
| 72 | }; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 73 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 74 | const postcssResult = |
| 75 | existingPostcssResult || |
| 76 | (await stylelint._getPostcssResult({ |
| 77 | code: options.code, |
| 78 | codeFilename: options.codeFilename, |
| 79 | filePath: inputFilePath, |
| 80 | codeProcessors: config.codeProcessors, |
| 81 | customSyntax: config.customSyntax, |
| 82 | })); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 83 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 84 | const stylelintPostcssResult = Object.assign(postcssResult, { |
| 85 | stylelint: stylelintResult, |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 86 | }); |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 87 | |
| 88 | await lintPostcssResult(stylelint._options, stylelintPostcssResult, config); |
| 89 | |
| 90 | return stylelintPostcssResult; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | /** |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 94 | * @returns {StylelintPostcssResult} |
| 95 | */ |
| 96 | function createEmptyStylelintPostcssResult() { |
| 97 | return { |
| 98 | ruleSeverities: {}, |
| 99 | customMessages: {}, |
| 100 | disabledRanges: {}, |
| 101 | ignored: true, |
| 102 | stylelintError: false, |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @param {string} [filePath] |
| 108 | * @returns {PostcssResult} |
| 109 | */ |
| 110 | function createEmptyPostcssResult(filePath) { |
| 111 | return { |
| 112 | root: { |
| 113 | source: { |
| 114 | input: { file: filePath }, |
| 115 | }, |
| 116 | }, |
| 117 | messages: [], |
| 118 | opts: undefined, |
| 119 | stylelint: createEmptyStylelintPostcssResult(), |
| 120 | warn: () => {}, |
| 121 | }; |
| 122 | } |