-
-
Notifications
You must be signed in to change notification settings - Fork 11
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 getConfigStatus
, update isFileIgnored
#7
Conversation
856419a
to
49f8c9a
Compare
I think we should revisit the expected behavior of
So then we could do: const isFileConfigured = !isFileIgnored && !isExplicitMatch; |
Sounds good 👍🏻I can change the methods and update ESLint to see how that works in practice. This will be a breaking change. |
@mdjermanovic what do you think of #7 (comment)? |
The current humanwhocodes/config-array#138 (comment). In ESLint, we're using Now, before changing the behavior of Edit: I tried making an example where the current behavior would be easily observable with |
Now it's all coming back to me. :) So So then we are really talking about modifying |
On closer look at the implementation, I think that
or some combination of the above. Currently, |
Yeah, while it can be argued that 3. is a subset of 2. because all patterns are considered relative to the base path so no pattern can match a file outside of it, it would still be good to show a distinct message for 3. |
Can you provide an example to clarify why the specific logic of I tried replacing filterCodeBlock(blockFilename) {
- return configs.isExplicitMatch(blockFilename);
+ return configs.getConfig(blockFilename) !== void 0;
} and all tests we have in eslint/eslint are still passing. |
I believe the case was when we had a |
What is the expected behavior in the following case: // eslint.config.js
export default [
{
plugins: {
"my-plugin": {
processors: {
"my-processor": {
// dummy processor that assumes that the whole content of a file is a single ```ts code block
preprocess(text) {
const lines = text.split("\n");
return [{
filename: "foo.ts",
text: lines.slice(1, -1).join("\n")
}]
},
// just increment lines to adjust for the first ```ts line
postprocess(messages) {
const retv = messages[0]; // there's always exactly 1 code block
retv.forEach(message => {
message.line++
message.endLine++;
});
return retv;
}
}
}
}
}
},
{
files: ["**/*.md"],
processor: "my-plugin/my-processor"
},
{
files: ["**/*"],
rules: {
"no-undef": 2
}
}
];
```ts
bar
``` The virtual The current behavior is:
|
Yes, so this is exactly the case I was targeting. IMHO, we should silently skip code blocks that don't have an explicit match. It's quite possible that people won't want to lint every code block, especially if it's not JavaScript. Outputting a warning for every code block that doesn't have a matching config seems likely to only create noise and frustration. Think of a Markdown file that has code examples in multiple languages, most of which ESLint does not lint. |
I agree, but |
Okay, I got confused.
Right, that's what we don't want. So maybe |
It looks like the wrong behavior of |
I started the discussion about |
I think that changing the return type of |
Sounds good to me. @nzakas what do you think? |
I'm not in favor of changing I am in favor of adding another method that can return the reason a file doesn't have a config. |
Thanks! I will add a new method that returns the reason a file doesn't have a config and I will change the behavior of |
49f8c9a
to
840f651
Compare
isFileConfigured
getConfigStatus
, update isFileIgnored
The methods`isFileIgnored`/`isIgnored` will only return `true` if a file is globally ignored.
840f651
to
0c8187b
Compare
@@ -1005,50 +1792,6 @@ describe("ConfigArray", () => { | |||
|
|||
assert.strictEqual(configs.isIgnored(filename), true); | |||
}); | |||
it("should return true when passed matching both files and ignores in a 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.
Many of the tests without global "ignores"
patterns are no longer relevant to isIgnored()
/isFileIgnored()
now, so I removed them. The removed test cases are all covered by the tests for getConfigStatus()
.
This is ready for re-review. I stumbled on three unit tests that don't quite understand, so please have look at the review comments. |
isFileIgnored(filePath) { | ||
return this.getConfig(filePath) === undefined; | ||
return this.getConfigStatus(filePath) === "ignored"; | ||
} |
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.
In ESLint, we'll need to update all uses of isFileIgnored()
to getConfig()
or getConfigStatus()
in order to retain current functionalities, which I think raises the question of whether isFileIgnored
is needed anymore. I think we should just remove 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.
I still like it as a shorthand for getConfigStatus() === "ignored"
.
* @returns {"ignored"|"external"|"unconfigured"|"matched"} One of the constants returned by | ||
* {@linkcode ConfigArray.getConfigStatus}. | ||
*/ | ||
function calculateConfigStatus(configArray, filePath) { |
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.
There's a lot of repeated logic from getConfig
, and a separate cache.
Maybe we could rename the current getConfig
to getConfigWithStatus
, and modify it to cache and return { config, status }
? Then, the new getConfig
would be just return this.getConfigWithStatus(filePath).config;
, and getConfigStatus
would be just return this.getConfigWithStatus(filePath).status;
.
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.
Yeah, I also thought that. Would like @nzakas' perspective on this.
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.
I think that makes sense. 👍
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 for the suggestion, done in 5ce6b18. Please, have a look.
@@ -2334,14 +2922,11 @@ describe("ConfigArray", () => { | |||
); | |||
}); | |||
|
|||
it("should return false when first-level subdirectories are ignored with leading slash and then one is negated", () => { |
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 test was bogus because the first ignore pattern was not ignoring the subdirectory path, so I removed the negated pattern and changed the description to match what is going on.
cache.set(filePath, finalConfig); | ||
return finalConfig; | ||
// cache and return result | ||
configWithStatus = Object.freeze({ status: "unconfigured" }); |
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.
Thoughts about preparing these objects in advance, so that we don't create a new instance for each unconfigured (& ignored & external) file?
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.
Agreed.
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 b194ef8, thanks!
Before we merge, we should also check how the new behavior of |
It breaks it, we'll need to update all calls to |
I tried to run the tests in ESLint with a version of So it will be a breaking upgrade when we switch to |
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! Leaving open for @nzakas to 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.
Because this is a new package name, I'm not too concerned with this being a breaking change as older ESLint's won't automatically get this update.
This PR adds a method
getConfigStatus
toConfigArray
to determine the reason a file has no matching config. This could be used in ESLint to provide a better message for files that don't have a matching configuration. The reason could be:There is also a new method
getConfigWithStatus
that returns a config object and the status for a file. This is now the backing implementation for bothgetConfig
andgetConfigStatus
.This PR also changes the behavior of the methods
isFileIgnored
/isIgnored
. These methods will now only returntrue
for files that are ignored due to ignores patterns. This is a breaking change.Refs eslint/eslint#18263
See also the discussion in humanwhocodes/config-array#138.