diff --git a/package.json b/package.json index 5368332..b11111f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/taqwim/package.json b/taqwim/package.json index c93b6c7..bd2168d 100644 --- a/taqwim/package.json +++ b/taqwim/package.json @@ -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", diff --git a/taqwim/src/rules/spacing/assignment.ts b/taqwim/src/rules/spacing/assignment.ts index 763dd68..82efe3f 100644 --- a/taqwim/src/rules/spacing/assignment.ts +++ b/taqwim/src/rules/spacing/assignment.ts @@ -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, @@ -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, + }, + /(?\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; } @@ -344,6 +365,7 @@ class AssignmentAlign { const { left, loc: assignmentLoc, operator } = expression as AstAssign; const operatorPosition = this.getOperatorPosition(assignmentLoc); + if (operatorPosition === false) { return; } diff --git a/taqwim/test/rules/spacing.assignment/align-correct-1.php b/taqwim/test/rules/spacing.assignment/align-correct-1.php index 54e8606..352c75f 100644 --- a/taqwim/test/rules/spacing.assignment/align-correct-1.php +++ b/taqwim/test/rules/spacing.assignment/align-correct-1.php @@ -127,4 +127,9 @@ public function log2($msg) }; $bar = $msg; } -}); \ No newline at end of file +}); + +$with_parenthesis = ($object?->property); +$test = $object?->property; +$without_nullsafe = $object->property; +$with_multiple_nullsafe = $object?->call()?->property2; \ No newline at end of file diff --git a/taqwim/test/rules/spacing.assignment/align-incorrect-1.php b/taqwim/test/rules/spacing.assignment/align-incorrect-1.php index 0423803..7a73b2d 100644 --- a/taqwim/test/rules/spacing.assignment/align-incorrect-1.php +++ b/taqwim/test/rules/spacing.assignment/align-incorrect-1.php @@ -124,4 +124,9 @@ public function log2($msg) }; $bar= $msg; } -}); \ No newline at end of file +}); + +$with_parenthesis = ($object?->property); +$test = $object?->property; +$without_nullsafe = $object->property; +$with_multiple_nullsafe = $object?->call()?->property2; \ No newline at end of file diff --git a/taqwim/test/rules/spacing.assignment/data.json b/taqwim/test/rules/spacing.assignment/data.json index 7986712..9cb6d50 100644 --- a/taqwim/test/rules/spacing.assignment/data.json +++ b/taqwim/test/rules/spacing.assignment/data.json @@ -16,7 +16,7 @@ }, "align-incorrect-1": { "description": "Aligned assignment statements", - "expected": 108 + "expected": 111 } } } \ No newline at end of file diff --git a/taqwim/test/rules/spacing.assignment/fixer/1-to.php b/taqwim/test/rules/spacing.assignment/fixer/1-to.php index b31fc8d..1af3cf0 100644 --- a/taqwim/test/rules/spacing.assignment/fixer/1-to.php +++ b/taqwim/test/rules/spacing.assignment/fixer/1-to.php @@ -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; diff --git a/taqwim/test/rules/spacing.assignment/fixer/3-to.php b/taqwim/test/rules/spacing.assignment/fixer/3-to.php index a150a95..26b5bb4 100644 --- a/taqwim/test/rules/spacing.assignment/fixer/3-to.php +++ b/taqwim/test/rules/spacing.assignment/fixer/3-to.php @@ -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; diff --git a/taqwim/test/rules/spacing.assignment/fixer/4-from.php b/taqwim/test/rules/spacing.assignment/fixer/4-from.php index 520663a..d3c171e 100644 --- a/taqwim/test/rules/spacing.assignment/fixer/4-from.php +++ b/taqwim/test/rules/spacing.assignment/fixer/4-from.php @@ -84,4 +84,9 @@ public function log2($msg) }; $bar= $msg; } -}); \ No newline at end of file +}); + +$with_parenthesis = ($object?->property); +$test = $object?->property; +$without_nullsafe = $object->property; +$with_multiple_nullsafe = $object?->call()?->property2; \ No newline at end of file diff --git a/taqwim/test/rules/spacing.assignment/fixer/4-to.php b/taqwim/test/rules/spacing.assignment/fixer/4-to.php index e9cb7a4..805f9ec 100644 --- a/taqwim/test/rules/spacing.assignment/fixer/4-to.php +++ b/taqwim/test/rules/spacing.assignment/fixer/4-to.php @@ -84,4 +84,9 @@ public function log2($msg) }; $bar = $msg; } -}); \ No newline at end of file +}); + +$with_parenthesis = ($object?->property); +$test = $object?->property; +$without_nullsafe = $object->property; +$with_multiple_nullsafe = $object?->call()?->property2; \ No newline at end of file