-
-
Notifications
You must be signed in to change notification settings - Fork 11
fix: validate files
and ignores
elements
#103
Conversation
Thanks for digging into this. I think this is generally the right direction. My only concern is that now we will be validating What I think might make sense is actually to use a separate schema for the initial validation than the second. So in the initial validation inside of What do you think? |
Thanks @nzakas! You are correct, there is no reason to re-validate |
I've added another commit to make sure that validation fails when |
src/config-array.js
Outdated
@@ -509,6 +514,8 @@ export class ConfigArray extends Array { | |||
|
|||
for (const config of this) { | |||
|
|||
assertValidFilesAndIgnores(config); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this is the only place that's validating files/ignores? I understand from the way the code is currently written that this will effectively be called before getConfig()
, but that's relying on an implementation detail that could change in the future, nevermind being a bit confusing for people trying to learn (or re-learn) the codebase.
What do you think about changing assertValidFilesAndIgnores
to cache the validations and then also call it elsewhere, maybe in getConfig()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 5108e26, thanks! Definitely a cleaner approach to call the validation function in getConfig()
beforehand. I've added a flag to the dataCache
data so we can keep track of config arrays that have already passed the files
-and-ignores
validation, and not validate them again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks good to me. Would like @mdjermanovic to take a look before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works well when getConfig
is called.
However, there are other public methods that might be called first. For example, I copied the build from this branch into node_modules of my local eslint clone, then added { ignores: [1] }
to eslint.config.js
and ran npx eslint lib
:
Oops! Something went wrong! :(
ESLint: 8.47.0
TypeError: matcher.startsWith is not a function
at C:\milos\eslint\node_modules\@humanwhocodes\config-array\api.js:336:17
at Array.reduce (<anonymous>)
at shouldIgnorePath (C:\milos\eslint\node_modules\@humanwhocodes\config-array\api.js:327:17)
at FlatConfigArray.isDirectoryIgnored (C:\milos\eslint\node_modules\@humanwhocodes\config-array\api.js:1044:13)
at deepFilter (C:\milos\eslint\lib\eslint\eslint-helpers.js:279:47)
at Object.isAppliedFilter (C:\milos\eslint\node_modules\@nodelib\fs.walk\out\readers\common.js:12:31)
at AsyncReader._handleEntry (C:\milos\eslint\node_modules\@nodelib\fs.walk\out\readers\async.js:89:50)
at C:\milos\eslint\node_modules\@nodelib\fs.walk\out\readers\async.js:65:22
at callSuccessCallback (C:\milos\eslint\node_modules\@nodelib\fs.scandir\out\providers\async.js:103:5)
at C:\milos\eslint\node_modules\@nodelib\fs.scandir\out\providers\async.js:29:13
In this case, user still gets the same uninformative error as described in the original issue eslint/eslint#17434.
Perhaps we could do this validation in function normalize
and function normalizeSync
?
We can, but that would shift where the validation errors are thrown from. Not a huge deal but something to be aware of. |
Thanks for the review @mdjermanovic. I overlooked that
Alternatively, we could validate Existing unit tests would not need to be changed (they would if validation is shifted into Thoughts? @nzakas |
Normalization already throws errors in some cases - when it finds an array/function item but arrays/functions are not enabled. I personally think it's fine to do some basic validation when normalizing the config array (after preprocessing configs). The basic validation would be that the items are objects and that |
Also, if the config array contains a primitive value, for example: // eslint.config.js
module.exports = ["eslint:reccommended"]; // typo after this change, the error becomes:
Before the change, the error was more informative: |
Thanks! This should be fixed now in 21fb3c4. We still have to decide where we want to validate |
My point wasn't that the method didn't already throw errors in some cases, but rather that the type of error it throws is different than the types thrown by
I'm not a fan of littering validation throughout the class methods. I'd rather keep it centralized, so if that can't be in |
Fine! I will move validation of |
Update as per discussion. Please, have a look: c421b26. |
tests/config-array.test.js
Outdated
|
||
function testValidationError({ only = false, title, configs, expectedError }) { | ||
|
||
const it_ = only ? it.only : it; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we rename it_
to something else? Not a fan of the dangling underscore. Maybe localIt
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 34f47d5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Would like @mdjermanovic to re-verify.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
This PR ensures that
files
andignores
properties of config objects are fully validated before they are accessed in thegetConfig
method.This should fix issue eslint/eslint#17434, except that we may want to add tests to the eslint repo as well.
The list of changes in the current draft implementation is as follows:
files-and-ignores
from the base schema to validate onlyfiles
andignores
.assertValidFilesAndIgnores
that uses the new schema.files
andignores
of each config (not just of the merged configs) in thenormalize
andnormalizeSync
methods.files
array is not empty.ignores
has invalid elements.files
andignores
validation is now a noop.