Skip to content

Commit

Permalink
Fix allowEmptyInput in configuration files (#6929)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Jun 17, 2023
1 parent 0e4d302 commit faa1d16
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/sour-tables-lick.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `allowEmptyInput` in configuration files
4 changes: 4 additions & 0 deletions lib/__tests__/fixtures/config-allow-empty-input.json
@@ -0,0 +1,4 @@
{
"allowEmptyInput": true,
"rules": {}
}
11 changes: 11 additions & 0 deletions lib/__tests__/standalone.test.js
Expand Up @@ -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;

Expand Down
13 changes: 12 additions & 1 deletion lib/standalone.js
Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -311,4 +312,14 @@ function handleError(error) {
throw error;
}

/**
* @param {import('stylelint').InternalApi} stylelint
* @returns {Promise<boolean>}
*/
async function canAllowEmptyInput(stylelint) {
const config = await getConfigForFile(stylelint);

return Boolean(config?.config?.allowEmptyInput);
}

module.exports = standalone;

0 comments on commit faa1d16

Please sign in to comment.