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

Chore: Simplify space-infix-ops #10935

Merged
merged 3 commits into from Oct 12, 2018

Conversation

madbence
Copy link
Contributor

@madbence madbence 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-deprecated eslint-deprecated bot added the triage An ESLint team member will look at this issue soon label Oct 7, 2018
Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

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

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", "==", "!=", "===", "!==", "&", "^", "|", "&&", "||", "=",
"+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "&=", "^=", "|=",
"?", ":", ",", "**"
Copy link
Member

Choose a reason for hiding this comment

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

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 === ")") {
Copy link
Member

Choose a reason for hiding this comment

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

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
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

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

@madbence madbence Oct 10, 2018

Choose a reason for hiding this comment

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

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

Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

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

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];
Copy link
Member

Choose a reason for hiding this comment

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

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

@not-an-aardvark not-an-aardvark added chore This change is not user-facing and removed triage An ESLint team member will look at this issue soon labels Oct 12, 2018
@not-an-aardvark not-an-aardvark merged commit 4132de7 into eslint:master Oct 12, 2018
btmills added a commit that referenced this pull request Oct 12, 2018
@madbence madbence deleted the space-infix-ops-refactor branch October 19, 2018 23:36
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Apr 11, 2019
@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 Apr 11, 2019
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 chore This change is not user-facing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants