Skip to content

Commit

Permalink
fix: better handle of assignment operator alignment
Browse files Browse the repository at this point in the history
The parser have an issue when using nullsafeoperator, so this commit
fixes this issue.

glayzzle/php-parser#1128
  • Loading branch information
abdul-alhasany committed Jul 25, 2023
1 parent 00f67df commit 7a3e1ea
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "taqwim",
"displayName": "PHPTaqwim",
"description": "PHP linter and formatter",
"version": "0.0.34",
"version": "0.0.35",
"homepage": "https://taqwim.kalimah-apps.com/",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion taqwim/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@kalimahapps/taqwim",
"description": "PHP linter and formatter",
"author": "khr2003",
"version": "0.0.34",
"version": "0.0.35",
"homepage": "https://taqwim.kalimah-apps.com/docs/",
"repository": {
"type": "git",
Expand Down
28 changes: 25 additions & 3 deletions taqwim/src/rules/spacing/assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* This rule also provides the ability to align assignment with adjacent statements.
* Alignment only works with single line statements.
*/

/* eslint complexity: ["warn", 7] */
import type Fixer from '@taqwim/fixer';
import type {
RuleDataOptional,
Expand Down Expand Up @@ -136,19 +136,40 @@ class AssignmentAlign {
* @return {boolean} false if there was no report
*/
reportAndFixTraillingSpace(node: AstNode): boolean {
const { report } = this.context;
const { report, sourceLines } = this.context;

const assingNode = node.expression as AstAssign ?? node;
const { right, loc } = assingNode as AstAssign;
const { loc: rightLoc } = right;
const operatorPosition = this.getOperatorPosition(loc);

if (operatorPosition === false) {
return false;
}

/**
* we can not use right.loc because it provides the wrong start
* position (column and offset) when using nullsafe operator
* e.g. $foo = $bar?->baz;
*
* @see https://github.com/glayzzle/php-parser/issues/1128
*/
const rightPosition = findAheadRegex(
sourceLines,
{
start: operatorPosition.end,
end: right.loc.end,
},
/(?<rightLoc>\S+)/u
);

if (rightPosition === false || rightPosition?.groups?.rightLoc === undefined) {
return false;
}

const { rightLoc } = rightPosition.groups;
const foundSpace = rightLoc.start.column - operatorPosition.end.column;
const isCorrectSpacing = foundSpace === 1;

if (rightLoc.start.line !== operatorPosition.end.line || isCorrectSpacing === true) {
return false;
}
Expand Down Expand Up @@ -344,6 +365,7 @@ class AssignmentAlign {
const { left, loc: assignmentLoc, operator } = expression as AstAssign;

const operatorPosition = this.getOperatorPosition(assignmentLoc);

if (operatorPosition === false) {
return;
}
Expand Down
7 changes: 6 additions & 1 deletion taqwim/test/rules/spacing.assignment/align-correct-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,9 @@ public function log2($msg)
};
$bar = $msg;
}
});
});

$with_parenthesis = ($object?->property);
$test = $object?->property;
$without_nullsafe = $object->property;
$with_multiple_nullsafe = $object?->call()?->property2;
7 changes: 6 additions & 1 deletion taqwim/test/rules/spacing.assignment/align-incorrect-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,9 @@ public function log2($msg)
};
$bar= $msg;
}
});
});

$with_parenthesis = ($object?->property);
$test = $object?->property;
$without_nullsafe = $object->property;
$with_multiple_nullsafe = $object?->call()?->property2;
2 changes: 1 addition & 1 deletion taqwim/test/rules/spacing.assignment/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"align-incorrect-1": {
"description": "Aligned assignment statements",
"expected": 108
"expected": 111
}
}
}
2 changes: 1 addition & 1 deletion taqwim/test/rules/spacing.assignment/fixer/1-to.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$var12121 = $a / $b * $c + $d;
$var = $a + $b / $c * $d;

$var =($a + $b) % $c * $d - $e;
$var = ($a + $b) % $c * $d - $e;

$var = $a +
$b % $c * $d - $e;
Expand Down
2 changes: 1 addition & 1 deletion taqwim/test/rules/spacing.assignment/fixer/3-to.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$var12121 = $a / $b * $c + $d;
$var = $a + $b / $c * $d;

$var =($a + $b) % $c * $d - $e;
$var = ($a + $b) % $c * $d - $e;

$var = $a +
$b % $c * $d - $e;
Expand Down
7 changes: 6 additions & 1 deletion taqwim/test/rules/spacing.assignment/fixer/4-from.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ public function log2($msg)
};
$bar= $msg;
}
});
});

$with_parenthesis = ($object?->property);
$test = $object?->property;
$without_nullsafe = $object->property;
$with_multiple_nullsafe = $object?->call()?->property2;
7 changes: 6 additions & 1 deletion taqwim/test/rules/spacing.assignment/fixer/4-to.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ public function log2($msg)
};
$bar = $msg;
}
});
});

$with_parenthesis = ($object?->property);
$test = $object?->property;
$without_nullsafe = $object->property;
$with_multiple_nullsafe = $object?->call()?->property2;

0 comments on commit 7a3e1ea

Please sign in to comment.