Skip to content

Remove TS no-use-before-define rule - #55806

Closed
noahtallen wants to merge 1 commit into
trunkfrom
try/disable-no-use-before-define
Closed

noahtallen wants to merge 1 commit into
trunkfrom
try/disable-no-use-before-define

Conversation

@noahtallen

@noahtallen noahtallen commented Aug 27, 2021 •

Copy link
Copy Markdown
Contributor

Changes proposed in this Pull Request

While working on some TypeScript files in #32930, I noticed that I was unable to commit due to the "no-use-before-define" typescript-eslint rule. I propose we remove this rule, as I don't see a benefit to it.

It prevents this sort of pattern, where a helper component is defined after the main component:

export default function ErrorMessage( { children }: { children?: React.ReactNode } ) {
	return <Error>{ children }</Error>;
}

const Error = styled.div< React.HTMLAttributes< HTMLDivElement > >``

In my opinion, that is a bit too picky. :) Plus, in TypeScript, if a definition for a variable isn't found, compilation should fail. I think that is a better way to handle problems with variables not existing.

I also found this, but I think it is for a different rule (no use before declare)

Since most modern TypeScript doesn’t use var, [no use before declare] is generally discouraged and is kept around for legacy purposes. It is slow to compute, is not enabled in the built-in configuration presets, and should not be used to inform TSLint design decisions.

https://palantir.github.io/tslint/rules/no-use-before-declare/

Testing instructions

CI should pass.

@noahtallen
noahtallen requested a review from a team as a code owner August 27, 2021 21:21
@noahtallen noahtallen self-assigned this Aug 27, 2021
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Aug 27, 2021
@github-actions

github-actions Bot commented Aug 27, 2021 •

Copy link
Copy Markdown

@noahtallen
noahtallen requested review from a team August 27, 2021 21:22
@matticbot

Copy link
Copy Markdown
Contributor

This PR does not affect the size of JS and CSS bundles shipped to the user's browser.

Generated by performance advisor bot at iscalypsofastyet.com.

@scinos

scinos commented Aug 28, 2021

Copy link
Copy Markdown
Contributor

Makes a lot of sense to me. Thanks for removing it!

@jsnajdr

jsnajdr commented Aug 30, 2021

Copy link
Copy Markdown
Member

The rule actually prevents a runtime error from happening, like:

const a = () => b(2);
console.log( a() );
const b = ( x: number ) => x * 3;

TypeScript compiles this without any warning, but at runtime it fails:

const a = () => b(2);
                ^
ReferenceError: Cannot access 'b' before initialization

If you used function b( x: number ) to declare b, the rules are different: function definitions are automatically hoisted to the top of the module, no matter where they physically are.

Because the warning seems trivial to fix by just reordering the definitions, and because the JS version of no-use-before-define is still active after this PR, do we really need to remove this rule?

@noahtallen

Copy link
Copy Markdown
Contributor Author

Ah, that's really unfortunate. I agree, if this is the only way to catch that error, we need to keep the rule. I do wonder if there is a smarter way to do the rule, because I'd guess most of our react code is written to avoid execution before functions are defined. (E.g. most everything happens after calling a function when everything has been defined, not as part of the script's initial execution)

I noticed it when working in files already breaking the rule. So I guess those will need to be refactored.

I'll close this since we can't just remove it altogether

@noahtallen noahtallen closed this Aug 30, 2021
@github-actions github-actions Bot removed the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Aug 30, 2021
@jsnajdr

jsnajdr commented Sep 2, 2021

Copy link
Copy Markdown
Member

I do wonder if there is a smarter way to do the rule, because I'd guess most of our react code is written to avoid execution before functions are defined.

If you really want to use a convention where the "main" function is written at the beginning of the module, and the helper functions it calls are defined after it, you can always declare them with the function keyword. Such functions are automatically hoisted to the top of the module and their order doesn't matter. It's only variable definitions using const and let where order matters.

@sirreal
sirreal deleted the try/disable-no-use-before-define branch January 14, 2025 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants