Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix allowEmptyInput in configuration files #6929

Merged
merged 1 commit into from Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;