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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Simplify space-infix-ops #10935

Merged
merged 3 commits into from Oct 12, 2018

Conversation

@madbence
Copy link
Contributor

commented Oct 7, 2018

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[x] Other, please explain:

What changes did you make? (Give an overview)

space-infix-ops is a bit overcomplicated. there can be a lot of tokens between left and right, but only one of them can be an infix operator, the others must be opening and closing parens. if i'm mistaken, i'd happy to close this PR and open another one that adds those test cases 馃樃

Is there anything you'd like reviewers to focus on?

afaik javascript engines are pretty good at caching loop invariants (so there's no need to save tokens.length), so i made the loop a bit more readable. i wasn't able to do performance testing (as space-infix-ops is not part of eslint:recommended, so npm run perf is no good), but i'd happy to tweak the implementation if someone can give me some pointer about how to do it.

@eslint eslint bot added the triage label Oct 7, 2018

@not-an-aardvark
Copy link
Member

left a comment

Thanks for the PR! This definitely seems like a good simplification. I left a suggestion for a slight improvement that I think would make it even better.

"*", "/", "%", "+", "-", "<<", ">>", ">>>", "<", "<=", ">", ">=", "in",
"instanceof", "==", "!=", "===", "!==", "&", "^", "|", "&&", "||", "=",
"+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "&=", "^=", "|=",
"?", ":", ",", "**"

This comment has been minimized.

Copy link
@not-an-aardvark

not-an-aardvark Oct 8, 2018

Member

For what it's worth, I think this list was missing the **= operator, so the rule was failing to report an error for code like this.

const curr = tokens[i];
const next = tokens[i + 1];

if (curr.value === "(" || curr.value === ")") {

This comment has been minimized.

Copy link
@not-an-aardvark

not-an-aardvark Oct 8, 2018

Member

You mentioned that:

there can be a lot of tokens between left and right, but only one of them can be an infix operator, the others must be opening and closing parens.

Off the top of my head, I can't think of any cases where this would be wrong, although it's possible that there are some cases that we haven't thought of.

To make sure it's correct, would it be possible to add the expected operator as a parameter to getFirstNonSpacedToken? For example, checkVar could use something like getFirstNonSpacedToken(leftNode, rightNode, "="). Then the implementation of getFirstNonSpacedToken could look something like:

function getFirstNonSpacedToken(left, right, operatorValue) {
    const operator = sourceCode.getTokenBetween(
        left,
        right,
        token => token.value === operatorValue
    );
    const beforeOperator = sourceCode.getTokenBefore(operator);
    const afterOperator = sourceCode.getTokenAfter(operator);
    // ...check to see if there's a space, as before
}

This comment has been minimized.

Copy link
@madbence

madbence Oct 8, 2018

Author Contributor

nice idea, i think it might fix #10922 as well

madbence added some commits Oct 7, 2018

@madbence madbence force-pushed the madbence:space-infix-ops-refactor branch from 5ed8100 to 7a25462 Oct 10, 2018

return op;
}
function getFirstNonSpacedToken(left, right, op) {
const operator = sourceCode.getTokensBetween(left, right, token => token.value === op)[0];

This comment has been minimized.

Copy link
@madbence

madbence Oct 10, 2018

Author Contributor

i don't think that this'll evaluate to undefined ever, we'll always find the token we're looking for.

@not-an-aardvark
Copy link
Member

left a comment

LGTM, thanks! I have a minor nitpick but it doesn't need to block this from being merged.

return op;
}
function getFirstNonSpacedToken(left, right, op) {
const operator = sourceCode.getTokensBetween(left, right, token => token.value === op)[0];

This comment has been minimized.

Copy link
@not-an-aardvark

not-an-aardvark Oct 10, 2018

Member

Minor nitpick: Could this use getFirstTokenBetween rather than getTokensBetween? (Sorry, my previous example used "getTokenBetween", which
I realized doesn't exist.)

This comment has been minimized.

Copy link
@madbence

madbence Oct 10, 2018

Author Contributor

sure! sorry, i'm not really familiar with the sourceCode API 馃樅

@not-an-aardvark not-an-aardvark added chore and removed triage labels Oct 12, 2018

@not-an-aardvark not-an-aardvark merged commit 4132de7 into eslint:master Oct 12, 2018

5 checks passed

commit-message PR title follows commit message guidelines
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
licence/cla Contributor License Agreement is signed.
Details
release-monitor No patch release is pending
Details

btmills added a commit that referenced this pull request Oct 12, 2018

@madbence madbence deleted the madbence:space-infix-ops-refactor branch Oct 19, 2018

@eslint eslint bot locked and limited conversation to collaborators Apr 11, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
2 participants
You can鈥檛 perform that action at this time.