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

Enabling @typescript-eslint/no-use-before-define in the eslint-plugin #318

Open
1 of 2 tasks
emileber opened this issue Jan 23, 2022 · 1 comment
Open
1 of 2 tasks

Comments

@emileber
Copy link

emileber commented Jan 23, 2022

Overview

I'd like to enforce @typescript-eslint/no-use-before-define in our projects and before extending the config on our side, I was wondering why it was turned off by default and if it could be turned on in this package instead?

Type

  • New feature
  • Changes to existing features

Motivation

While TypeScript prevents most usage before the identifier is actually defined, there are cases that aren't caught and these lead to confusing code using identifiers and types before they're defined, like in the following:

// Uses `MyEnum` type and `myFunction` before they're defined.
const test: MyEnum = myFunction();

// Function implementation hidden away, using hoisting.
function myFunction() {
  // Uses an enum value before it is defined.
  return MyEnum.Test;
}

// While TypeScript catches `const` and `let` usage, it doesn't catch type defs and enums by default.
enum MyEnum {
  Test,
}

I don't believe relying on hoisting is a good practice. I personally see hoisting as a side-effect of JS parsing engines, not an actual feature we should use in code meant for humans. There are edge-cases where hoisting is necessary, these exceptions should probably be highlighted by a eslint-disable-next-line comment.

With the following settings

"@typescript-eslint/no-use-before-define": [
  "error", 
  { 
    "enums": true,
    "typedefs": true, 
    "ignoreTypeReferences": false 
  }
]

...the reversed usage order is made clear.

image

⚠️ That said, enabling this rule would probably break a lot of projects relying on hoisting.

@emileber
Copy link
Author

emileber commented Jan 23, 2022

I also see that the rule is enabled for JS (though with "nofunc" which doesn't limit using hoisting), it's then disabled when overriding rules for TS, but the actual TS rule is also explicitly off.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant