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 in config to allow basename glob patterns #6547

Merged
merged 2 commits into from Jan 22, 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/slow-dots-hug.md
@@ -0,0 +1,5 @@
---
"stylelint": patch
---

Fixed: `overrides.files` in config to allow basename glob patterns
21 changes: 16 additions & 5 deletions lib/__tests__/overrides.test.js
Expand Up @@ -7,8 +7,12 @@ const fixturesPath = path.join(__dirname, 'fixtures', 'config-overrides');

describe('single input file. all overrides are matching', () => {
it('simple override', async () => {
const inputFiles = [
path.join(fixturesPath, 'style.css'),
path.join(fixturesPath, 'testPrintConfig', 'style.css'),
];
const linted = await standalone({
files: [path.join(fixturesPath, 'style.css')],
files: inputFiles,
config: {
rules: {
'block-no-empty': true,
Expand All @@ -25,10 +29,17 @@ describe('single input file. all overrides are matching', () => {
configBasedir: fixturesPath,
});

expect(linted.results).toHaveLength(1);
expect(linted.results[0].warnings).toHaveLength(2);
expect(linted.results[0].warnings[0].rule).toBe('block-no-empty');
expect(linted.results[0].warnings[1].rule).toBe('color-named');
expect(linted.results).toHaveLength(2);
expect(linted.results[0].source).toBe(inputFiles[0]);
expect(linted.results[0].warnings.map((w) => w.rule)).toEqual([
'block-no-empty',
'color-named',
]);
expect(linted.results[1].source).toBe(inputFiles[1]);
expect(linted.results[1].warnings.map((w) => w.rule)).toEqual([
'block-no-empty',
'color-named',
]);
});

it('override with plugins', async () => {
Expand Down
36 changes: 19 additions & 17 deletions lib/augmentConfig.js
Expand Up @@ -19,6 +19,18 @@ const path = require('path');
/** @typedef {import('stylelint').CodeProcessor} StylelintCodeProcessor */
/** @typedef {import('stylelint').ResultProcessor} StylelintResultProcessor */

/**
* @param {string} glob
* @param {string} basedir
* @returns {string}
*/
function absolutizeGlob(glob, basedir) {
const result = path.isAbsolute(glob.replace(/^!/, '')) ? glob : globjoin(basedir, glob);

// Glob patterns for micromatch should be in POSIX-style
return normalizePath(result);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[note] Created the absolutizeGlob() function to reduce duplications.


/**
* - Merges config and stylelint options
* - Makes all paths absolute
Expand Down Expand Up @@ -142,11 +154,7 @@ async function augmentConfigFull(stylelint, filePath, cosmiconfigResult) {
*/
function absolutizePaths(config, configDir, cwd) {
if (config.ignoreFiles) {
config.ignoreFiles = [config.ignoreFiles].flat().map((glob) => {
if (path.isAbsolute(glob.replace(/^!/, ''))) return glob;

return globjoin(configDir, glob);
});
config.ignoreFiles = [config.ignoreFiles].flat().map((glob) => absolutizeGlob(glob, configDir));
}

if (config.plugins) {
Expand Down Expand Up @@ -453,19 +461,13 @@ function applyOverrides(fullConfig, rootConfigDir, filePath) {
);
}

const filesGlobs = [files]
.flat()
.map((glob) => {
if (path.isAbsolute(glob.replace(/^!/, ''))) {
return glob;
}

return globjoin(rootConfigDir, glob);
})
// Glob patterns for micromatch should be in POSIX-style
.map((s) => normalizePath(s));
const absoluteGlobs = [files].flat().map((glob) => absolutizeGlob(glob, rootConfigDir));

if (micromatch.isMatch(filePath, filesGlobs, { dot: true })) {
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 })
) {
config = mergeConfigs(config, configOverrides);
}
}
Expand Down
7 changes: 2 additions & 5 deletions lib/isPathIgnored.js
@@ -1,7 +1,6 @@
'use strict';

const micromatch = require('micromatch');
const normalizePath = require('normalize-path');
const path = require('path');

const filterFilePaths = require('./utils/filterFilePaths');
Expand Down Expand Up @@ -30,12 +29,10 @@ module.exports = async function isPathIgnored(stylelint, filePath) {
return true;
}

// Glob patterns for micromatch should be in POSIX-style
const ignoreFiles = [result.config.ignoreFiles || []].flat().map((s) => normalizePath(s));

const ignoreFiles = result.config.ignoreFiles || [];
const absoluteFilePath = path.isAbsolute(filePath) ? filePath : path.resolve(cwd, filePath);

if (micromatch([absoluteFilePath], ignoreFiles).length) {
if (micromatch([absoluteFilePath], ignoreFiles).length > 0) {
return true;
}

Expand Down