diff --git a/.changeset/sour-tables-lick.md b/.changeset/sour-tables-lick.md new file mode 100644 index 0000000000..3c2c5cf6cf --- /dev/null +++ b/.changeset/sour-tables-lick.md @@ -0,0 +1,5 @@ +--- +"stylelint": patch +--- + +Fixed: `allowEmptyInput` in configuration files diff --git a/lib/__tests__/fixtures/config-allow-empty-input.json b/lib/__tests__/fixtures/config-allow-empty-input.json new file mode 100644 index 0000000000..69bd0d0436 --- /dev/null +++ b/lib/__tests__/fixtures/config-allow-empty-input.json @@ -0,0 +1,4 @@ +{ + "allowEmptyInput": true, + "rules": {} +} diff --git a/lib/__tests__/standalone.test.js b/lib/__tests__/standalone.test.js index 219d3876ee..bde31b0c3e 100644 --- a/lib/__tests__/standalone.test.js +++ b/lib/__tests__/standalone.test.js @@ -178,6 +178,17 @@ it('standalone with nonexistent-file and allowEmptyInput enabled (in config) qui expect(output).toBe('[]'); }); +it('standalone with nonexistent-file and allowEmptyInput enabled (in config file) quietly exits', async () => { + const { results, errored, output } = await standalone({ + files: `${fixturesPath}/nonexistent-file.css`, + configFile: `${fixturesPath}/config-allow-empty-input.json`, + }); + + expect(results).toHaveLength(0); + expect(errored).toBe(false); + expect(output).toBe('[]'); +}); + describe('standalone passing code with syntax error', () => { let results; diff --git a/lib/standalone.js b/lib/standalone.js index ceed4f4b24..a7ed9058a5 100644 --- a/lib/standalone.js +++ b/lib/standalone.js @@ -11,6 +11,7 @@ const createStylelint = require('./createStylelint'); const createPartialStylelintResult = require('./createPartialStylelintResult'); const filterFilePaths = require('./utils/filterFilePaths'); const formatters = require('./formatters'); +const getConfigForFile = require('./getConfigForFile'); const getFileIgnorer = require('./utils/getFileIgnorer'); const getFormatterOptionsText = require('./utils/getFormatterOptionsText'); const lintSource = require('./lintSource'); @@ -251,7 +252,7 @@ async function standalone({ }); stylelintResults = await Promise.all(getStylelintResults); - } else if (allowEmptyInput ?? config?.allowEmptyInput) { + } else if (allowEmptyInput ?? config?.allowEmptyInput ?? (await canAllowEmptyInput(stylelint))) { stylelintResults = await Promise.all([]); } else if (filePathsLengthBeforeIgnore) { // All input files ignored @@ -311,4 +312,14 @@ function handleError(error) { throw error; } +/** + * @param {import('stylelint').InternalApi} stylelint + * @returns {Promise} + */ +async function canAllowEmptyInput(stylelint) { + const config = await getConfigForFile(stylelint); + + return Boolean(config?.config?.allowEmptyInput); +} + module.exports = standalone;