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

New Rule: no-lone-blocks (fixes #512) #572

Merged
merged 1 commit into from
Feb 1, 2014

Conversation

btmills
Copy link
Member

@btmills btmills commented Feb 1, 2014

Implements @michaelficarra's proposal to disallow BlockStatement > BlockStatement or Program > BlockStatement forms as described in #512.

function checkForLoners(node) {
// Check for any occurrence of BlockStatement > BlockStatement or
// Program > BlockStatement
node.body.forEach(function (stmt) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of triggering on the parent and traversing the children (we try to avoid any additional traversals, even ones this small), trigger on the child (which will always be BlockStatement) and check the parent.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add to @michaelficarra, ESLint already does a traversal down, so doing another traversal down inside of a rule is wasteful. You can listen for "BlockStatement", then use context.getAncenstors() to check the parent.

@nzakas
Copy link
Member

nzakas commented Feb 1, 2014

Aside from the note above, the rest looks really awesome. If you can clean that piece up then I'll be happy to merge it in.

@btmills
Copy link
Member Author

btmills commented Feb 1, 2014

That makes perfect sense. Updated and squashed.

@@ -74,6 +74,7 @@
"no-extend-native": 2,
"no-yoda": 2,
"no-path-concat": 0,
"no-lone-blocks": 1,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, missed this before. Should be "2" (for an error).

Removed extra down-traversal in favor of looking up one level at
ancestors and changed config from warning to error to incorporate
PR feedback.
@btmills
Copy link
Member Author

btmills commented Feb 1, 2014

Fixed, now it's an error.

nzakas added a commit that referenced this pull request Feb 1, 2014
New Rule: no-lone-blocks (fixes #512)
@nzakas nzakas merged commit 492e518 into eslint:master Feb 1, 2014
@btmills btmills deleted the no-lone-blocks branch February 1, 2014 22:42
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants