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: lines-around-directive rule (fixes #6069) #6998

Merged
merged 1 commit into from Aug 30, 2016
Merged

New: lines-around-directive rule (fixes #6069) #6998

merged 1 commit into from Aug 30, 2016

Conversation

kaicataldo
Copy link
Member

No description provided.

@mention-bot
Copy link

@kaicataldo, thanks for your PR! By analyzing the annotation information on this pull request, we identified @nzakas, @alberto and @mysticatea to be potential reviewers

@eslintbot
Copy link

LGTM

@mysticatea
Copy link
Member

Thank you for implementation!

My intention was that this rule handles directive prologues (not restrict "use strict").

A Directive Prologue is the longest sequence of ExpressionStatement productions occurring as the initial StatementListItem or ModuleItem productions of a FunctionBody, a ScriptBody, or a ModuleBody and where each ExpressionStatement in the sequence consists entirely of a StringLiteral token followed by a semicolon. The semicolon may appear explicitly or may be inserted by automatic semicolon insertion. A Directive Prologue may be an empty sequence.
http://www.ecma-international.org/ecma-262/7.0/#directive-prologue

A directive prologue might be multiline:

"use asm"
"use strict"

@kaicataldo
Copy link
Member Author

kaicataldo commented Aug 28, 2016

Ah, okay. So to check each directive in the prologue I could maybe do something like this (to borrow this implementation)

function getDirectives(statements) {
    const directives = [];

    for (let i = 0; i < statements.length; i++) {
        const statement = statements[i];

        if (
            statement.type === "ExpressionStatement" &&
            statement.expression.type === "Literal"
        ) {
            directives[i] = statement;
        } else {
            break;
        }
    }

    return directives;
}

Adding do not merge label until I update this.

@kaicataldo kaicataldo added the do not merge This pull request should not be merged yet label Aug 28, 2016
@mysticatea
Copy link
Member

Thank you.
I think good to move the logic to ast-utils.

By the way, directives[i] = statement seems a bug. It should be directives.push(statement) I think.

@kaicataldo
Copy link
Member Author

You're right! Do you mean adding a getDirectives utility to ast-utils? Thanks!

@mysticatea
Copy link
Member

Yes!

@kaicataldo
Copy link
Member Author

kaicataldo commented Aug 28, 2016

Got it working for multiple directives and then realized I wasn't sure if we want to check each directive individually or if we want to check around the entire directive prologue. Any thoughts on this @mysticatea?

Now that I know we're checking all directives, I think it makes sense to have an option to enforce blank newlines around the whole prologue (as a group). This wasn't discussed in the accepted issue though, so maybe we should talk about it there.

@mysticatea
Copy link
Member

Actually, my first post #6069 says "ignores between directives".
My intention was that the rule checks blank lines around a chunk of directives. 😉

@kaicataldo
Copy link
Member Author

Ah, so it does...Missed that somehow. Okay, thanks! Will update accordingly 👍

@kaicataldo
Copy link
Member Author

Still have to update the docs

@kaicataldo kaicataldo force-pushed the fixes6069 branch 4 times, most recently from e45a5fa to 66d9821 Compare August 29, 2016 03:11
@kaicataldo kaicataldo removed the do not merge This pull request should not be merged yet label Aug 29, 2016
@kaicataldo
Copy link
Member Author

kaicataldo commented Aug 29, 2016

Updated with new docs. Please let me know what you think, @mysticatea. Thanks for helping with this!

@kaicataldo kaicataldo force-pushed the fixes6069 branch 2 times, most recently from 4ab5b1a to 6aade1c Compare August 29, 2016 03:23
@eslintbot
Copy link

LGTM

@mysticatea
Copy link
Member

Wow, there are super many tests!
Looks great to me.

directives.push(statement);
} else {
if (!directives.length) {
return null;
Copy link
Member

Choose a reason for hiding this comment

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

Personally, I prefer an empty array instead of null.

(currently, this returns an empty array if the function is empty?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Would your preference be for it to return an empty array for the early return as well? My goal was to make it consistent for when it doesn't find directives.

Copy link
Member

Choose a reason for hiding this comment

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

My preference is that the function always returns an array. We can check the existence of directive prologues by the length of the result (or by iteration of the result).

Also, current implementation seems inconsistent.

// In empty function case, returns `[]`
function foo() {
}

// In non-strict function case, returns `null`
function foo() {
    bar()
}

Copy link
Member Author

Choose a reason for hiding this comment

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

It should return null in both cases (https://github.com/eslint/eslint/pull/6998/files#diff-6198b73cda8ab9ac49ed872ead7fb268R716), but I can definitely make it always return an array 👍

@eslintbot
Copy link

LGTM

@kaicataldo
Copy link
Member Author

Updated once more

@mysticatea
Copy link
Member

Great, thank you very much!
LGTM.

@@ -0,0 +1,320 @@
# Require or disallow newlines around directives (lines-around-directive)
Copy link
Member

Choose a reason for hiding this comment

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

I think we are doing lowercase first word now.

@nzakas
Copy link
Member

nzakas commented Aug 30, 2016

Lgtm, just one tiny thing on the docs. Really nice job on this -- I especially love the intro on the docs. Very well-written. 💯

@kaicataldo kaicataldo force-pushed the fixes6069 branch 4 times, most recently from 54713c7 to 383f2c4 Compare August 30, 2016 04:55
@eslintbot
Copy link

LGTM

@nzakas nzakas merged commit c1f0d76 into master Aug 30, 2016
@kaicataldo kaicataldo deleted the fixes6069 branch August 30, 2016 18:23
@vitorbal
Copy link
Member

Super awesome, @kaicataldo!! Great job! 🎉 🎉

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 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 6, 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.

None yet

7 participants