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

Add except: ["inside-block"] to rule-empty-line-before #2887

Closed
octoxan opened this issue Sep 13, 2017 · 9 comments
Closed

Add except: ["inside-block"] to rule-empty-line-before #2887

octoxan opened this issue Sep 13, 2017 · 9 comments
Labels
good first issue is good for newcomers status: wip is being worked on by someone type: new option a new option for an existing rule

Comments

@octoxan
Copy link

octoxan commented Sep 13, 2017

This is a bug where the rule "rule-empty-line-before" doesn't work properly, telling me something needs a new line when it should not.

Which rule, if any, is this issue related to?

"rule-empty-line-before"

What CSS is needed to reproduce this issue?

e.g.

.foo-foo {
	font-size: .75em;
}

.foo {
	text-align: center;
	.bar {
		font-style: italic;
	}
}

What stylelint configuration is needed to reproduce this issue?

e.g.

{
  "rules": {
    "rule-empty-line-before": ["always", {
        except: ["inside-block-and-after-rule", "first-nested"]
      }]
  }
}

Which version of stylelint are you using?

8.1.1

How are you running stylelint: CLI, PostCSS plugin, Node API?

With npm and inside of phpstorm.

Does your issue relate to non-standard syntax (e.g. SCSS, nesting, etc.)?

Nesting, scss.

What did you expect to happen?

No warnings to be flagged. I want a warning on if there's no empty lines before non-nested rules, and if there's any empty lines on nested rules. .bar is nested, and should not throw an error.

What actually happened (e.g. what warnings or errors you are getting)?

.bar throws an error "Expected empty line before rule", but should fall under either first-nested or inside-block-and-after-rule

@jeddy3
Copy link
Member

jeddy3 commented Sep 13, 2017

@octoxan Thanks for the report and for using the template.

I believe the rule is behaving correctly here as .bar is inside a block, but it is after a declaration and not a rule.

The following is an unusual style that I've not seen before:

.foo {
	text-align: center;
	.bar {
		font-style: italic;
	}
}

Most requests are for:

.foo {
	text-align: center;

	.bar {
		font-style: italic;
	}
}

However, I see no harm in adding an except: ["inside-block"] secondary option to the rule to allow this code style. @octoxan Please consider contributing this option. There's a section in the Developer Guide on how to get started. There is also an existing ignore: ["inside-block"] option (code here) to work from, so adding this new option should be trival.

@jeddy3 jeddy3 changed the title Problem with "rule-empty-line-before" not working properly Add except: ["inside-block"] to rule-empty-line-before Sep 13, 2017
@jeddy3 jeddy3 added status: ready to implement is ready to be worked on by someone type: new option a new option for an existing rule good first issue is good for newcomers labels Sep 13, 2017
@octoxan
Copy link
Author

octoxan commented Sep 13, 2017

@jeddy3
Oh yeah I'll totally do that tonight!

@gucong3000
Copy link
Member

gucong3000 commented Sep 14, 2017

Meybe it can help you:

--- a/lib/rules/rule-empty-line-before/index.js
+++ b/lib/rules/rule-empty-line-before/index.js
@@ -34,6 +34,7 @@ const rule = function(expectation, options, context) {
           ignore: ["after-comment", "inside-block"],
           except: [
             "after-rule",
+            "inside-block",
             "after-single-line-comment",
             "first-nested",
             "inside-block-and-after-rule"
@@ -69,11 +70,10 @@ const rule = function(expectation, options, context) {
         return;
       }

+      const isNested = rule.parent.type !== "root";
+
       // Optionally ignore the expectation if inside a block
-      if (
-        optionsMatches(options, "ignore", "inside-block") &&
-        rule.parent !== root
-      ) {
+      if (optionsMatches(options, "ignore", "inside-block") && isNested) {
         return;
       }

@@ -114,10 +114,11 @@ const rule = function(expectation, options, context) {

       // Optionally reverse the expectation for single line comments
       if (
+        (optionsMatches(options, "except", "inside-block") && isNested) ||
         optionsMatches(options, "except", "after-single-line-comment") &&
-        rule.prev() &&
-        rule.prev().type === "comment" &&
-        isSingleLineString(rule.prev().toString())
+          rule.prev() &&
+          rule.prev().type === "comment" &&
+          isSingleLineString(rule.prev().toString())
       ) {
         expectEmptyLineBefore = !expectEmptyLineBefore;
       }

also resolves compatibility in HTML files rule.parent.type !== "root"

@jeddy3
Copy link
Member

jeddy3 commented Sep 14, 2017

also resolves compatibility in HTML files rule.parent.type !== "root"

Interesting. SGTM.

@jeddy3
Copy link
Member

jeddy3 commented Sep 14, 2017

also resolves compatibility in HTML files rule.parent.type !== "root"

@gucong3000 Let's pull that out into a separate issue and PR, and keep this one focused on just adding the new option. Feel free to create an issue about the HTML file incompatibility in the rule.

@gucong3000
Copy link
Member

Let's pull that out into a separate issue and PR

#2891

@gucong3000 gucong3000 added the status: blocked is blocked by another issue or pr label Sep 14, 2017
@gucong3000 gucong3000 removed the status: blocked is blocked by another issue or pr label Sep 24, 2017
@gucong3000
Copy link
Member

@octoxan Are you still working on this?

@jeddy3 jeddy3 added status: wip is being worked on by someone and removed status: ready to implement is ready to be worked on by someone labels Oct 22, 2017
@hudochenkov hudochenkov removed good first issue is good for newcomers Hacktoberfest labels Oct 24, 2017
@jeddy3 jeddy3 added the good first issue is good for newcomers label Dec 7, 2017
@b13nxx
Copy link
Member

b13nxx commented Aug 16, 2018

@jeddy3 I believe this issue is no longer matter.

@jeddy3
Copy link
Member

jeddy3 commented Aug 16, 2018

@GorwinJororis Thanks for spotting this!

@jeddy3 jeddy3 closed this as completed Aug 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue is good for newcomers status: wip is being worked on by someone type: new option a new option for an existing rule
Development

No branches or pull requests

6 participants