From 2e66d03c68e470748aea87960402b94285cf19d0 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Mon, 18 May 2015 08:06:05 +1000 Subject: [PATCH] Add a linter rule for spacing after a cast Summary: Ref T7409. Add a linter rule to ensure that a cast is not followed by a space. This is largely based on [[https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/Generic/Sniffs/Formatting/NoSpaceAfterCastSniff.php | Generic_Sniffs_Formatting_NoSpaceAfterCastSniff]]. Test Plan: Added unit tests. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin, epriestley Maniphest Tasks: T7409 Differential Revision: https://secure.phabricator.com/D12321 --- src/lint/linter/ArcanistXHPASTLinter.php | 26 ++++++++++++++++++- .../__tests__/xhpast/cast-spacing.lint-test | 12 +++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/lint/linter/__tests__/xhpast/cast-spacing.lint-test diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 76ea6043..6ee850a1 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -65,6 +65,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { const LINT_USELESS_OVERRIDING_METHOD = 63; const LINT_NO_PARENT_SCOPE = 64; const LINT_ALIAS_FUNCTION = 65; + const LINT_CAST_SPACING = 66; private $blacklistedFunctions = array(); private $naminghook; @@ -203,6 +204,8 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { => pht('No Parent Scope'), self::LINT_ALIAS_FUNCTION => pht('Alias Functions'), + self::LINT_CAST_SPACING + => pht('Cast Spacing'), ); } @@ -255,6 +258,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { self::LINT_CLASS_NAME_LITERAL => $advice, self::LINT_USELESS_OVERRIDING_METHOD => $advice, self::LINT_ALIAS_FUNCTION => $advice, + self::LINT_CAST_SPACING => $advice, ); } @@ -322,7 +326,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { public function getVersion() { // The version number should be incremented whenever a new rule is added. - return '27'; + return '28'; } protected function resolveFuture($path, Future $future) { @@ -414,6 +418,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { 'lintUselessOverridingMethods' => self::LINT_USELESS_OVERRIDING_METHOD, 'lintNoParentScope' => self::LINT_NO_PARENT_SCOPE, 'lintAliasFunctions' => self::LINT_ALIAS_FUNCTION, + 'lintCastSpacing' => self::LINT_CAST_SPACING, ); foreach ($method_codes as $method => $codes) { @@ -4100,6 +4105,25 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { } } + private function lintCastSpacing(XHPASTNode $root) { + $cast_expressions = $root->selectDescendantsOfType('n_CAST_EXPRESSION'); + + foreach ($cast_expressions as $cast_expression) { + $cast = $cast_expression->getChildOfType(0, 'n_CAST'); + + list($before, $after) = $cast->getSurroundingNonsemanticTokens(); + $after = head($after); + + if ($after) { + $this->raiseLintAtToken( + $after, + self::LINT_CAST_SPACING, + pht('A cast statement must not be followed by a space.'), + ''); + } + } + } + /** * Retrieve all calls to some specified function(s). diff --git a/src/lint/linter/__tests__/xhpast/cast-spacing.lint-test b/src/lint/linter/__tests__/xhpast/cast-spacing.lint-test new file mode 100644 index 00000000..a5611554 --- /dev/null +++ b/src/lint/linter/__tests__/xhpast/cast-spacing.lint-test @@ -0,0 +1,12 @@ +