1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-21 22:32:41 +01:00

Further improvements to array comma linter rule

Summary:
Improve the `ArcanistXHPASTLinter::LINT_ARRAY_SEPARATOR` rule to be able to autofix the following code:

```lang=php
array(
  1,
  2,
  3, /* comment */ );
```

Test Plan: Added unit tests.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley

Differential Revision: https://secure.phabricator.com/D12308
This commit is contained in:
Joshua Spence 2015-05-20 07:05:34 +10:00
parent 25855c4427
commit 993166fa49
3 changed files with 43 additions and 23 deletions

View file

@ -13,7 +13,7 @@ max_line_length = 80
indent_style = indent_style =
end_of_line = end_of_line =
max_line_length = max_line_length =
trim_trailing_whitespace = trim_trailing_whitespace = false
[src/parser/__tests__/bundle/*] [src/parser/__tests__/bundle/*]
insert_final_newline = false insert_final_newline = false

View file

@ -3223,29 +3223,39 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter {
$value = last($values); $value = last($values);
$after = last($value->getTokens())->getNextToken(); $after = last($value->getTokens())->getNextToken();
if ($multiline && (!$after || $after->getValue() != ',')) { if ($multiline) {
if ($value->getChildByIndex(1)->getTypeName() == 'n_HEREDOC') { if (!$after || $after->getValue() != ',') {
continue; if ($value->getChildByIndex(1)->getTypeName() == 'n_HEREDOC') {
continue;
}
list($before, $after) = $value->getSurroundingNonsemanticTokens();
$after = implode('', mpull($after, 'getValue'));
$original = $value->getConcreteString();
$replacement = $value->getConcreteString().',';
if (strpos($after, "\n") === false) {
$original .= $after;
$replacement .= $after."\n".$array->getIndentation();
}
$this->raiseLintAtOffset(
$value->getOffset(),
self::LINT_ARRAY_SEPARATOR,
pht('Multi-lined arrays should have trailing commas.'),
$original,
$replacement);
} else if ($value->getLineNumber() == $array->getEndLineNumber()) {
$close = last($array->getTokens());
$this->raiseLintAtToken(
$close,
self::LINT_ARRAY_SEPARATOR,
pht('Closing parenthesis should be on a new line.'),
"\n".$array->getIndentation().$close->getValue());
} }
} else if ($after && $after->getValue() == ',') {
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.'),
$original,
$replacement);
} else if (!$multiline && $after && $after->getValue() == ',') {
$this->raiseLintAtToken( $this->raiseLintAtToken(
$after, $after,
self::LINT_ARRAY_SEPARATOR, self::LINT_ARRAY_SEPARATOR,

View file

@ -28,12 +28,17 @@ array(
1, 1,
2, 2,
3 /* comment */ ); 3 /* comment */ );
array(
1,
2,
3, /* comment */ );
~~~~~~~~~~ ~~~~~~~~~~
advice:3:14 advice:3:14
advice:12:3 advice:12:3
advice:16:3 advice:16:3
advice:26:3 advice:26:3
advice:30:3 advice:30:3
advice:34:20
~~~~~~~~~~ ~~~~~~~~~~
<?php <?php
array(1, 2, 3); array(1, 2, 3);
@ -67,3 +72,8 @@ array(
2, 2,
3, /* comment */ 3, /* comment */
); );
array(
1,
2,
3, /* comment */
);