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

Use @this to satisfy ESLint's no-invalid-this #14406

Merged
9 commits merged into from
Mar 23, 2021

Conversation

deyaaeldeen
Copy link
Member

@deyaaeldeen deyaaeldeen commented Mar 22, 2021

Problem

no-invalid-this makes sure uses of this are valid (see docs and implementation). However, uses of this are rampant in our test suites because this is how mocha unit tests are structured, the Mocha context can be accessed only through this.

Fix

So instead of disabling no-invalid-this in our test suites, this PR tags functions that reference this with @this and that satisfies the rule requirements (see docs).

Discussion

It could be argued that this work just replaces one comment annotation with another so we did not really solve the underlying problem. However, the inherent problem lies in how mocha itself works and there is nothing we can do other than probably migrating to another framework that is more sane/type-safe. One minor improvement we get is we now have slightly less syntactic overhead because we need to tag just the function instead of individual lines in its body that violate the rule.

Trade-offs

Pros:

  • function tags are less than line tags

Cons:

  • whitelisting one more tag with the tsdoc linter that our devs need to learn about
  • still having rampant tags everywhere

Fixes #11404

@ramya-rao-a
Copy link
Contributor

/azp run js - eventgrid - ci

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@ghost
Copy link

ghost commented Mar 23, 2021

Hello @ramya-rao-a!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 18b3f30 into Azure:master Mar 23, 2021
@deyaaeldeen deyaaeldeen deleted the linting/removing-no-invalid-this branch March 23, 2021 12:44
@maorleger
Copy link
Member

maorleger commented Mar 23, 2021

I'm a little late to the party here but I am not in favor of this approach personally for the following reasons:

  1. Since it's mostly test code I'd rather disable no-invalid-this in tests if the rule is not typescript-aware (i.e. this is valid, properly annotated, and properly captured by Mocha but still flagged incorrectly). For test code this really feels like noise to me.
  2. In product code we will be better off addressing this case-by-case. Tagging @this feels like something I would add to make the linter happy without too much thought whereas disabling the rule as a one-off is something I would give more thought to. It also doesn't solve the underlying problem, just makes the tooling happy. this could still be invalid couldn't it?
  3. wide-reaching PRs like this make rebasing a nightmare for anything that is currently in-flight.

I love the work you put into this, and I love that we're making our tooling better, but since we didn't solve the underlying problem what do I do when I have a linter error in tests, just tag @this blindly? That feels like the rule isn't adding much value when working with Mocha. Just my 2 cents though!

@deyaaeldeen
Copy link
Member Author

@maorleger I feel your pain and you can see a discussion around this in the PR description. I am not really fond of either this approach or turning off the rule all together in tests. The underlying issue lies in how mocha works which is disturbing to me. I mean, look at some of the side-effects of their design: #13005.

I can be ok with turning the rule off for tests but that solution still does not solve the underlying problem. Anyhow, I do not have a strong preference either way and we should not expect to use a lot of this going forward anyway or at least we should discourage its use.

@maorleger
Copy link
Member

maorleger commented Mar 24, 2021

@deyaaeldeen Yeah, I think it just comes with the territory - this is weird in JS (weird as in unexpected and different than other languages) and it's made worse because Mocha uses it everywhere.

One compromise / improvement we can make here is using typescript-eslint's no-invalid-this instead of eslint's version which does recognize when you pass the this "parameter" to a typescript function.

So then instead of

it("can abort creating a key", /** @this Mocha.Context */ async function() {

we would use

it("can abort creating a key", async function(this: Mocha.Context) 

And then at least it's just TypeScript without the inline JSDoc comments - what do you think? We already use typescript-eslint in a few places.

Edit, we also get the benefit of the compiler letting us know that we cannot use the this parameter in an arrow function, likely improving the experience when using it in Mocha.

@ramya-rao-a
Copy link
Contributor

Sounds great to me!

Sorry for being trigger happy and merging this PR :(
Got excited to be able to enable the linter in CI now that the errors got fixed

ghost pushed a commit that referenced this pull request Mar 25, 2021
Implements the approach described here: #14406 (comment) to linting `this` references. Reminder to myself: look for typescript-eslint alternatives when the behavior of eslint is not satisfactory.
jay-most pushed a commit to jay-most/azure-sdk-for-js that referenced this pull request Apr 26, 2021
# Problem

`no-invalid-this` makes sure uses of `this` are valid (see [docs](https://eslint.org/docs/rules/no-invalid-this) and [implementation](https://github.com/eslint/eslint/blob/8984c91372e64d1e8dd2ce21b87b80977d57bff9/lib/rules/utils/ast-utils.js#L900)). However, uses of `this` are rampant in our test suites because this is how mocha unit tests are structured, the Mocha context can be accessed only through `this`. 

# Fix

So instead of disabling `no-invalid-this` in our test suites, this PR tags functions that reference `this` with `@this` and that satisfies the rule requirements (see [docs](https://eslint.org/docs/rules/no-invalid-this)).

# Discussion

It could be argued that this work just replaces one comment annotation with another so we did not really solve the underlying problem. However, the inherent problem lies in how mocha itself works and there is nothing we can do other than probably migrating to another framework that is more sane/type-safe. One minor improvement we get is we now have slightly less syntactic overhead because we need to tag just the function instead of individual lines in its body that violate the rule.

# Trade-offs

Pros:
- function tags are less than line tags

Cons:
- whitelisting one more tag with the tsdoc linter that our devs need to learn about
- still having rampant tags everywhere

Fixes Azure#11404
jay-most pushed a commit to jay-most/azure-sdk-for-js that referenced this pull request Apr 26, 2021
Implements the approach described here: Azure#14406 (comment) to linting `this` references. Reminder to myself: look for typescript-eslint alternatives when the behavior of eslint is not satisfactory.
openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-js that referenced this pull request May 20, 2021
Fix pattern of cluster name and data center name (Azure#14406)

* Fix pattern of cluster name and data center name

* Fix pattern of cluster name and data center name

Co-authored-by: wentingwu <[email protected]>
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ESLint] Investigate how to satisfy the no-invalid-this rule
3 participants