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
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 16 additions & 25 deletions lib/rules/space-infix-ops.js
Expand Up @@ -34,37 +34,25 @@ module.exports = {

create(context) {
const int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;

const OPERATORS = [
"*", "/", "%", "+", "-", "<<", ">>", ">>>", "<", "<=", ">", ">=", "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 sourceCode = context.getSourceCode();

/**
* Returns the first token which violates the rule
* @param {ASTNode} left - The left node of the main node
* @param {ASTNode} right - The right node of the main node
* @param {string} op - The operator of the main node
* @returns {Object} The violator token or null
* @private
*/
function getFirstNonSpacedToken(left, right) {
const tokens = sourceCode.getTokensBetween(left, right, 1);

for (let i = 1, l = tokens.length - 1; i < l; ++i) {
const op = tokens[i];

if (
(op.type === "Punctuator" || op.type === "Keyword") &&
OPERATORS.indexOf(op.value) >= 0 &&
(tokens[i - 1].range[1] >= op.range[0] || op.range[1] >= tokens[i + 1].range[0])
) {
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

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 馃樅

const prev = sourceCode.getTokenBefore(operator);
const next = sourceCode.getTokenAfter(operator);

if (!sourceCode.isSpaceBetweenTokens(prev, operator) || !sourceCode.isSpaceBetweenTokens(operator, next)) {
return operator;
}

return null;
}

Expand Down Expand Up @@ -110,7 +98,10 @@ module.exports = {
const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left;
const rightNode = node.right;

const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode);
// search for = in AssignmentPattern nodes
const operator = node.operator || "=";

const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, operator);

if (nonSpacedNode) {
if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) {
Expand All @@ -126,8 +117,8 @@ module.exports = {
* @private
*/
function checkConditional(node) {
const nonSpacedConsequesntNode = getFirstNonSpacedToken(node.test, node.consequent);
const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate);
const nonSpacedConsequesntNode = getFirstNonSpacedToken(node.test, node.consequent, "?");
const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate, ":");

if (nonSpacedConsequesntNode) {
report(node, nonSpacedConsequesntNode);
Expand All @@ -147,7 +138,7 @@ module.exports = {
const rightNode = node.init;

if (rightNode) {
const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode);
const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, "=");

if (nonSpacedNode) {
report(node, nonSpacedNode);
Expand Down