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

feat: add method isFileConfigured #138

Closed
wants to merge 1 commit into from

Conversation

fasttime
Copy link
Contributor

This PR adds a method isFileConfigured to ConfigArray, to determine if a file has matching configuration or not. This could be used in ESLint to provide a better message for ignored files that don't have a matching configuration.

Refs eslint/eslint#18263

Comment on lines +769 to +774
/**
* Returns the config object for a given file path or a `NoMatchingConfig` indicator.
* @param {string} filePath The complete path of a file to get a config for.
* @returns {Object} The config object or `NoMatchingConfig` indicator for this file.
*/
getConfigOrNoMatch(filePath) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method doesn't need to be public, it could be converted into a local function.


});

describe('isFileIgnored() / isFileConfigured()', () => {
Copy link
Contributor Author

@fasttime fasttime Apr 29, 2024

Choose a reason for hiding this comment

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

I've added assertions for isFileConfigured after those for isFileIgnored, because in ESLint isFileConfigured will be called when isFileIgnored returns true. I think this makes more sense than calling isFileConfigured alone. Alternatively, we could duplicate some test cases to keep the assertions separated.

@nzakas
Copy link
Contributor

nzakas commented Apr 29, 2024

Does isExplicitMatch() already solve this problem?

@fasttime
Copy link
Contributor Author

Does isExplicitMatch() already solve this problem?

I tried that but it breaks the tests in ESLint. It looks like ignoreswith glob patterns are not considered as matches:

configs = new ConfigArray([
    {
        ignores: ['**/bar.*']
    },
    {
        rules: { semi: 'error' }
    }
]);

configs.normalizeSync();

console.log(configs.isFileIgnored('bar.txt')); // true
console.log(configs.isExplicitMatch('bar.txt')); // false
console.log(configs.isFileConfigured('bar.txt')); // true

Comment on lines +954 to +961
/**
* Determines if the given filepath has a matching config.
* @param {string} filePath The complete path of a file to check.
* @returns {boolean} True if the path has a matching config, false if not.
*/
isFileConfigured(filePath) {
return this.getConfigOrNoMatch(filePath) !== NoMatchingConfig;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If the file is ignored by ignore patterns, this returns true regardless of whether or not it has a matching config?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. This is technically okay, because either way the error message "File ignored because of a matching ignore pattern." does apply. But maybe the method could have a more descriptive name?

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we could change the return type of getConfig()? Instead of returning config directly, it could return a { config } object with an additional property that would indicate why is config undefined if that's the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's an interesting idea, it would be a breaking change though.

@mdjermanovic
Copy link
Contributor

Note: there's a functional difference between isExplicitMatch() and looking for explicit matches inside getConfig().

isExplicitMatch() checks if there's at least one config object whose files match. getConfig() does the same, but additionally at least one of the matching patterns must not end with /* or /**.

const configs1 = new ConfigArray([
    {
        files: ["foo/*.txt"]
    }
]);

configs1.normalizeSync();

console.log(configs1.getConfig('foo/bar.txt')); // {}
console.log(configs1.isFileIgnored('foo/bar.txt')); // false
console.log(configs1.isExplicitMatch('foo/bar.txt')); // true

const configs2 = new ConfigArray([
    {
        files: ["foo/*"]
    }
]);

configs2.normalizeSync();

console.log(configs2.getConfig('foo/bar.txt')); // undefined
console.log(configs2.isFileIgnored('foo/bar.txt')); // true
console.log(configs2.isExplicitMatch('foo/bar.txt')); // true!

@nzakas
Copy link
Contributor

nzakas commented May 6, 2024

Let's move this over to https://github.com/eslint/rewrite. I'd like to start using the official ESLint version going forward.

@fasttime
Copy link
Contributor Author

fasttime commented May 7, 2024

I've created a PR in the rewrite repo: eslint/rewrite#7

@fasttime fasttime closed this May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants