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 overrides.files negated pattern regression #7468

Merged
merged 3 commits into from
Jan 19, 2024
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/ten-rivers-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `overrides.files` negated pattern regression introduced in 15.0.0
21 changes: 21 additions & 0 deletions lib/__tests__/overrides.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,24 @@ it('two files', async () => {
expect(linted.results[1].warnings).toHaveLength(1);
expect(linted.results[1].warnings[0].rule).toBe('property-disallowed-list');
});

it('negation glob pattern', async () => {
const { results } = await standalone({
files: [path.join(fixturesPath, 'style.css'), path.join(fixturesPath, 'style.module.css')],
config: {
rules: { 'block-no-empty': null },
overrides: [
{
files: ['!(**/style.css)'],
rules: { 'block-no-empty': true },
},
],
},
});

expect(results).toHaveLength(2);
expect(results).toHaveProperty('[0].source', expect.stringContaining('style.css'));
expect(results).toHaveProperty('[0].warnings', []);
expect(results).toHaveProperty('[1].source', expect.stringContaining('style.module.css'));
expect(results).toHaveProperty('[1].warnings[0].rule', 'block-no-empty');
});
8 changes: 6 additions & 2 deletions lib/augmentConfig.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ function applyOverrides(fullConfig, rootConfigDir, filePath) {
);
}

/** @type {(glob: string) => boolean} */
const nonegateGlob = (glob) => !glob.startsWith('!');

for (const override of overrides) {
const { files, ...configOverrides } = override;

Expand All @@ -389,12 +392,13 @@ function applyOverrides(fullConfig, rootConfigDir, filePath) {
);
}

const absoluteGlobs = [files].flat().map((glob) => absolutizeGlob(glob, rootConfigDir));
const fileList = [files].flat();
const absoluteGlobs = fileList.map((glob) => absolutizeGlob(glob, rootConfigDir));

if (
micromatch.isMatch(filePath, absoluteGlobs, { dot: true }) ||
// E.g. `*.css` matches any CSS files in any directories.
micromatch.isMatch(filePath, files, { dot: true, basename: true })
micromatch.isMatch(filePath, fileList.filter(nonegateGlob), { dot: true, basename: true })
) {
config = mergeConfigs(config, configOverrides);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/augmentConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ export function applyOverrides(fullConfig, rootConfigDir, filePath) {
);
}

/** @type {(glob: string) => boolean} */
const nonegateGlob = (glob) => !glob.startsWith('!');

for (const override of overrides) {
const { files, ...configOverrides } = override;

Expand All @@ -387,12 +390,13 @@ export function applyOverrides(fullConfig, rootConfigDir, filePath) {
);
}

const absoluteGlobs = [files].flat().map((glob) => absolutizeGlob(glob, rootConfigDir));
const fileList = [files].flat();
const absoluteGlobs = fileList.map((glob) => absolutizeGlob(glob, rootConfigDir));

if (
micromatch.isMatch(filePath, absoluteGlobs, { dot: true }) ||
// E.g. `*.css` matches any CSS files in any directories.
micromatch.isMatch(filePath, files, { dot: true, basename: true })
micromatch.isMatch(filePath, fileList.filter(nonegateGlob), { dot: true, basename: true })
) {
config = mergeConfigs(config, configOverrides);
}
Expand Down