From 3a5ffdc2fdf2f14da7eb6fb545b60c971ea437c6 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Tue, 7 Apr 2015 07:51:52 +1000 Subject: [PATCH] Improve array comma rule Summary: Improve `ArcanistXHPASTLinter::LINT_ARRAY_SEPARATOR` in handling multi-line arrays, see D12280 and D12281 for example. Depends on D12295. Test Plan: Updated unit tests. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: Korvin, epriestley Differential Revision: https://secure.phabricator.com/D12296 --- src/lint/linter/ArcanistXHPASTLinter.php | 18 +++++++++++--- .../__tests__/xhpast/array-comma.lint-test | 24 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 16f6022f..7e7ac94a 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -2951,11 +2951,23 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { continue; } - $this->raiseLintAtNode( - $value, + list($before, $after) = $value->getSurroundingNonsemanticTokens(); + $after = implode('', mpull($after, 'getValue')); + + $original = $value->getConcreteString(); + $replacement = $value->getConcreteString().','; + + if (strpos($after, "\n") === false) { + $original .= $after; + $replacement .= rtrim($after)."\n".$array->getIndentation(); + } + + $this->raiseLintAtOffset( + $value->getOffset(), self::LINT_ARRAY_SEPARATOR, pht('Multi-lined arrays should have trailing commas.'), - $value->getConcreteString().','); + $original, + $replacement); } else if (!$multiline && $after && $after->getValue() == ',') { $this->raiseLintAtToken( $after, diff --git a/src/lint/linter/__tests__/xhpast/array-comma.lint-test b/src/lint/linter/__tests__/xhpast/array-comma.lint-test index 031f3f3c..595ff28f 100644 --- a/src/lint/linter/__tests__/xhpast/array-comma.lint-test +++ b/src/lint/linter/__tests__/xhpast/array-comma.lint-test @@ -9,7 +9,7 @@ array( array( 1, 2, - 3 + 3 // comment ); array( 'foo', @@ -20,10 +20,20 @@ array( This is some heredoc text. EOTEXT ); +array( + 1, + 2, + 3); +array( + 1, + 2, + 3 /* comment */ ); ~~~~~~~~~~ advice:3:14 advice:12:3 advice:16:3 +advice:26:3 +advice:30:3 ~~~~~~~~~~