From 16770ed2104ec58704c72b2577cc2c6667289faa Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Wed, 10 Sep 2014 22:30:01 +1000 Subject: [PATCH] Fix linting for spacing around default parameter assignment in function/method declaration Summary: Fix linting for spacing around default parameter assignment in function/method declaration so that `function foo($x = null) {}` is preferred in favor of `function foo($x=null) {}`. Test Plan: Modified existing unit tests. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: epriestley, Korvin Differential Revision: https://secure.phabricator.com/D10429 --- src/lint/linter/ArcanistXHPASTLinter.php | 30 +++++++++++++++++-- .../xhpast/space-around-operators.lint-test | 3 +- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 06363b1c..5ddba15f 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -1989,8 +1989,34 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { } } - // TODO: Spacing around default parameter assignment in function/method - // declarations (which is not n_BINARY_EXPRESSION). + $parameters = $root->selectDescendantsOfType('n_DECLARATION_PARAMETER'); + foreach ($parameters as $parameter) { + if ($parameter->getChildByIndex(2)->getTypeName() == 'n_EMPTY') { + continue; + } + + $operator = head($parameter->selectTokensOfType('=')); + $before = $operator->getNonsemanticTokensBefore(); + $after = $operator->getNonsemanticTokensAfter(); + + $replace = null; + if (empty($before) && empty($after)) { + $replace = ' = '; + } else if (empty($before)) { + $replace = ' ='; + } else if (empty($after)) { + $replace = '= '; + } + + if ($replace !== null) { + $this->raiseLintAtToken( + $operator, + self::LINT_BINARY_EXPRESSION_SPACING, + 'Convention: logical and arithmetic operators should be '. + 'surrounded by whitespace.', + $replace); + } + } } private function lintSpaceAroundConcatenationOperators(XHPASTNode $root) { diff --git a/src/lint/linter/__tests__/xhpast/space-around-operators.lint-test b/src/lint/linter/__tests__/xhpast/space-around-operators.lint-test index 3b8b5542..2f94003e 100644 --- a/src/lint/linter/__tests__/xhpast/space-around-operators.lint-test +++ b/src/lint/linter/__tests__/xhpast/space-around-operators.lint-test @@ -31,6 +31,7 @@ warning:8:3 warning:9:4 warning:10:3 warning:11:3 +warning:13:14 warning:20:52 warning:21:54 warning:22:21 @@ -48,7 +49,7 @@ $a -= $b; $a -= $b; $a === $b; $a.$b; -function x($n=null) {} +function x($n = null) {} function x($n = null) {} $y /* ! */ += // ?