-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Make sure type-aware lint rules' test cases are type-error-free #8298
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
Comments
I like this. But also per #8231 we'd need a way to mark a test as intentionally having an "error" type. |
I sent a draft just to check how many tests contain semantic TS errors: #8351 There is really lot of them...
Some tests, such as So if we decide we want to fix all of them, that's a huge git diff (maybe we can split it into several prs) Also, I'm not sure how best to handle cases with fixers. I suppose we'd like to be able to write both types of test cases:
Any thoughts on this? I just want to make sure we want to fix all the failed tests before I start fixing the 1643 cases 😄 |
The issue is that there are a lot of tests where we don't actually care if there is a type error because the code is testing a pure syntactic code path. For example - So if we were to build this we would need a way to easily opt out, IMO. It's not worth us writing type-correct test cases for every single case, IMO, as that would involve multi-line tests where a single-line would have been sufficient. |
😭. ...although, hey, 1643 / 29272 is ~5.6%. I suppose that's an 'A' grade...? 😂
I'm now wondering whether it should be opt in or opt out... We the typescript-eslint team are very good at doing this right. But I don't know that we'd want to have that assumption hold by default for not-us folks. And even we've had slips in. I like how #8351 has an Having it key on specific error codes is a little trickier given that they aren't stable version-to-version. That'll likely cause pain down the line the next time TypeScript shuffles around error codes in a version. Which happens once in a while. And is a pity because in theory I do really like the idea of needing to specify which errors are allowed. 😞
Agreed - for any big change like this I'm a favor of splitting out |
They're a lot more stable than the TS team says they are. |
You know, Jake Bailey recently asked me out of curiosity what my next big ask for the TS team is... maybe finally getting around to asking for stable error categories is up there. |
I think any rule whose tested code path interacts with the TS compiler should be error-free. For |
The difficult thing is that the only way to "register" types is via code. So that means injecting code into the test cases. The complicated thing is that will show up in the test runner output then which can be confusing. That also adds the complexity of needing to ensure the test case doesn't declare the same variables that are "pre-declared" |
For end-users it would probably be more convenient if this feature was opt-in. Since otherwise we may break a lot of existing tests. I see that this feature can be enabled separately in each test case: ruleTester.run('my-rule', rule, {
valid: [
{
code: 'const x = 1;',
checkWithTSC: true,
},
],
}) Or on each RuleTester instance: const ruleTester = new RuleTester({
// ...
checkTestsWithTSC: true
}) Moreover, we can add the ability to configure default behavior globally for all RuleTester instances in some test "setup" file, like for RuleTester.checkTestsWithTSC = true I agree that some rules don't require all tests to be error free, some of them may be sensitive to TS errors only in some specific conditions to check some rule implementation details. Yes, ensuring that existing test cases are free of TS errors will require a lot of work, but later on when someone adds new tests, it won't be so hard anymore. WDYT? |
I think having it on by default is fine if the case uses type information. In that case I think being breaking is fine here --- we're highlighting a case where their test case could be plain wrong. Type errors fall to But for non-type-aware cases I agree that it's not correct to be on by default. I'd even say it's plain wrong to be on at all for them. A non-type-aware rule only checks syntax so types are irrelevant and ensuring types are correct can get in the way of devx. |
I agree. This proposal is solely for type-aware rules. |
So, to summarize the discussion above: If the check is not explicitly requested for a particular test case, we perform a TSC check when one of So this would be a kind of opt-out feature, right? About ignoring specific TS diagnostics: The ability to ignore specific codes on a per-test basis sounds good, but as @JoshuaKGoldberg mentioned, these codes are not yet considered stable. If we look at I also don't really like the idea of using only raw diagnostic codes because it's very unintuitive. Personally, I wouldn't want to open a test file and see only Using messages to identify a specific diagnostic (e.g. I really don't know if there is a good way to ignore specific errors :( |
That ... could work somewhat. But it's a little complex. We'd have to wrap versions of the services used in tests. Plus, rules don't guarantee always calling to those APIs. There will likely be individual cases where they're not called, but type errors should still be checked. Maybe this can just always be on if
I'd think so, yeah. |
My thought too. We would check for type errors if the rule requires type checking. And each test case can have an option to disable type error checking. |
A few thoughts after iterating on #8351:
|
👍 agreed
Good point. That property has been kind of an unofficial standard for a while, including default support by https://github.com/bmish/eslint-doc-generator... but this would be the first instance of it being recognized by any other tooling. My hunch is we'd want to have this be an opt-in option to start, to minimize how much of a breaking change it'd be. So folks could do
I like it. If it makes it a lot more complex I wouldn't be upset about it being a followup though. |
Suggestion
We should add an extra type-checking step to rule-tester when testing type-aware lint rules. Right now, we have some test cases that don't pass, most notably because they have undeclared variables (which become
any
). However we don't guarantee linting behavior when there are type errors (because the result is likelyany
, which either created new lint errors or masks actual lint errors). Such type errors may be hard to catch in more complex situations.Brad mentioned there may be a perf penalty, but it would be useful to experiment nonetheless.
The text was updated successfully, but these errors were encountered: