question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule proposal: Require declaring class members upfront

See original GitHub issue

Declaring the members upfront as class fields makes code more readable and can prevent typos. We would also allow initializing in the constructor, but I think we should auto-fix to class field as it’s neater.

Inspired by https://github.com/eslint/eslint/issues/11540.

Any suggestions on the rule name?

Fail

class X {
	func setName(name) {
		this.name = name;
	}

	func say() {
		console.log(this.name);
	}
}

Pass

class X {
	name;

	func setName(name) {
		this.name = name;
	}

	func say() {
		console.log(this.name);
	}
}

How it would prevent typos:

class X {
	name;

	func setName(name) {
		this.name = name;
	}

	func say() {
		// This would have resulted in `undefined` being printed without this rule.
		console.log(this.myName); // ESLint error
	}
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
fiskercommented, Feb 16, 2021

Yes, it’s clear, let’s add this rule.

I’ll add proposal about the member order later, on vacation.

1reaction
sindresorhuscommented, Jan 6, 2021

Your proposal title “Require declaring class members upfront”, didn’t you mean properties should come before the methods? So I proposed a more general rule.

Ah, no. Sorry for being unclear. It’s more like no-undef. By “upfront”, I meant that the members must be declared before being used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Streamlining the Medicaid, Children's Health Insurance ...
This rulemaking proposes changes to simplify the processes for eligible individuals to enroll and retain eligibility in Medicaid, the Children's ...
Read more >
Shortening the Securities Transaction Settlement Cycle
The Commission proposes to require compliance with each of the proposed rules and rule amendments by March 31, 2024. The. Commission solicits ...
Read more >
1.3.1 Class Action Litigation Often Requires Large Up-Front ...
Some class actions involve too much work and too many up-front costs to be handled by a small law office alone. For example,...
Read more >
SO YOU WANT TO START A LOCAL AUTO CLUB OR ...
Below highlights the basic requirements and benefits of starting a club: Basic Requirements ... the ground rules up front for members.
Read more >
Share Class Election Sample Clauses | Law Insider
Should the Selling Group Member wish to customize its Upfront Selling Commission, ... that are required to be signed by the Trustee under...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found