From a5304e472d1882283dfc1860a1b9db165b3e16a4 Mon Sep 17 00:00:00 2001 From: Joshua Spence Date: Mon, 31 Aug 2015 06:49:29 +1000 Subject: [PATCH] Add a linter rule for newlines after PHP open tags Summary: ` invalid-default-parameter.lint-test} | 7 +-- .../xhpast/invalid-modifiers.lint-test | 11 +++-- .../xhpast/lamba-func-function.lint-test | 3 +- .../language-construct-parentheses.lint-test | 22 +++++---- .../xhpast/list-assignment.lint-test | 8 ++-- .../xhpast/lowercase-functions.lint-test | 4 +- .../xhpast/modifier-ordering.lint-test | 10 ++-- .../xhpast/naming-conventions.lint-test | 46 +++++++++---------- .../xhpast/newline-after-open-tag.lint-test | 24 ++++++++++ .../xhpast/no-segfault-on-abstract.lint-test | 1 + .../xhpast/no-segfault-on-exit.lint-test | 1 + .../linter/__tests__/xhpast/nowdoc.lint-test | 3 +- .../xhpast/object-operating-spacing.lint-test | 6 ++- .../xhpast/parens-hug-contents.lint-test | 34 +++++++------- .../__tests__/xhpast/php54-incompat.lint-test | 9 ++-- .../__tests__/xhpast/preg-quote.lint-test | 1 - .../reused-iterator-reference.lint-test | 1 - .../__tests__/xhpast/reused-local.lint-test | 1 - .../xhpast/semicolon-spacing.lint-test | 4 +- .../single-pass-adjacent-patches.lint-test | 6 ++- .../space-after-control-keywords.lint-test | 18 ++++---- .../space-around-more-operators.lint-test | 18 ++++---- .../xhpast/space-around-operators.lint-test | 24 +++++----- .../xhpast/surprising-constructors.lint-test | 5 +- .../xhpast/tautological-expressions.lint-test | 1 - .../xhpast/tostring-exception.lint-test | 3 +- ...unary-postfix-expression-spacing.lint-test | 4 +- .../unary-prefix-expression-spacing.lint-test | 6 ++- .../unnecessary-final-modifier.lint-test | 5 +- .../unreasonably-deep-nesting.lint-test | 2 + .../use-of-this-in-static-method.lint-test | 1 - .../xhpast/variable-variables.lint-test | 3 +- .../xhpast/wrong-concat-operator.lint-test | 3 +- ...istNewlineAfterOpenTagXHPASTLinterRule.php | 46 +++++++++++++++++++ 58 files changed, 327 insertions(+), 190 deletions(-) rename src/lint/linter/__tests__/xhpast/{invalid-default-paramter.lint-test => invalid-default-parameter.lint-test} (91%) create mode 100644 src/lint/linter/__tests__/xhpast/newline-after-open-tag.lint-test create mode 100644 src/lint/linter/xhpast/rules/ArcanistNewlineAfterOpenTagXHPASTLinterRule.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index 722bea0a..f8b6a685 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -171,6 +171,7 @@ phutil_register_library_map(array( 'ArcanistMissingLinterException' => 'lint/linter/exception/ArcanistMissingLinterException.php', 'ArcanistModifierOrderingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistModifierOrderingXHPASTLinterRule.php', 'ArcanistNamingConventionsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNamingConventionsXHPASTLinterRule.php', + 'ArcanistNewlineAfterOpenTagXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNewlineAfterOpenTagXHPASTLinterRule.php', 'ArcanistNoEffectException' => 'exception/usage/ArcanistNoEffectException.php', 'ArcanistNoEngineException' => 'exception/usage/ArcanistNoEngineException.php', 'ArcanistNoLintLinter' => 'lint/linter/ArcanistNoLintLinter.php', @@ -455,6 +456,7 @@ phutil_register_library_map(array( 'ArcanistMissingLinterException' => 'Exception', 'ArcanistModifierOrderingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistNamingConventionsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', + 'ArcanistNewlineAfterOpenTagXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistNoEffectException' => 'ArcanistUsageException', 'ArcanistNoEngineException' => 'ArcanistUsageException', 'ArcanistNoLintLinter' => 'ArcanistLinter', diff --git a/src/lint/linter/__tests__/xhpast/array-comma.lint-test b/src/lint/linter/__tests__/xhpast/array-comma.lint-test index 6945002c..51128f2e 100644 --- a/src/lint/linter/__tests__/xhpast/array-comma.lint-test +++ b/src/lint/linter/__tests__/xhpast/array-comma.lint-test @@ -1,4 +1,5 @@ 'val1', $a => 'val2', @@ -34,9 +35,9 @@ $f = array( $a => 'var2', ); ~~~~~~~~~~ -error:5:3 -error:8:3 -error:15:3 -error:20:3 -error:25:3 -error:34:3 +error:6:3 +error:9:3 +error:16:3 +error:21:3 +error:26:3 +error:35:3 diff --git a/src/lint/linter/__tests__/xhpast/duplicate-switch-case.lint-test b/src/lint/linter/__tests__/xhpast/duplicate-switch-case.lint-test index 309e04d6..2c8e5c5c 100644 --- a/src/lint/linter/__tests__/xhpast/duplicate-switch-case.lint-test +++ b/src/lint/linter/__tests__/xhpast/duplicate-switch-case.lint-test @@ -1,4 +1,5 @@ + + + + + + + + + + doSomething(); id(new Something()) ->doSomething(); ~~~~~~~~~~ -warning:2:3 -warning:2:6 +warning:3:3 +warning:3:6 ~~~~~~~~~~ doSomething(); id(new Something()) ->doSomething(); diff --git a/src/lint/linter/__tests__/xhpast/parens-hug-contents.lint-test b/src/lint/linter/__tests__/xhpast/parens-hug-contents.lint-test index c0058bc0..d6a38e88 100644 --- a/src/lint/linter/__tests__/xhpast/parens-hug-contents.lint-test +++ b/src/lint/linter/__tests__/xhpast/parens-hug-contents.lint-test @@ -1,4 +1,5 @@ $y, ); ~~~~~~~~~~ -warning:3:3 -warning:3:5 -warning:4:4 -warning:5:3 -warning:12:9 -warning:13:10 -warning:14:9 -warning:24:5 +warning:4:3 +warning:4:5 +warning:5:4 +warning:6:3 +warning:13:9 +warning:14:10 +warning:15:9 +warning:25:5 ~~~~~~~~~~ 500) of string concatenations. We emit n_CONCATENATION_LIST instead of // n_BINARY_EXPRESSION to avoid various call-depth traps in PHP, HPHP, and the @@ -43,6 +44,7 @@ ~~~~~~~~~~ ~~~~~~~~~~ 500) of string concatenations. We emit n_CONCATENATION_LIST instead of // n_BINARY_EXPRESSION to avoid various call-depth traps in PHP, HPHP, and the diff --git a/src/lint/linter/__tests__/xhpast/use-of-this-in-static-method.lint-test b/src/lint/linter/__tests__/xhpast/use-of-this-in-static-method.lint-test index 3ae1214b..395939bb 100644 --- a/src/lint/linter/__tests__/xhpast/use-of-this-in-static-method.lint-test +++ b/src/lint/linter/__tests__/xhpast/use-of-this-in-static-method.lint-test @@ -8,7 +8,6 @@ final class A { $this->f(); } } - ~~~~~~~~~~ error:3:13 XHP19 Class-Filename Mismatch error:8:5 Use of $this in a static method. diff --git a/src/lint/linter/__tests__/xhpast/variable-variables.lint-test b/src/lint/linter/__tests__/xhpast/variable-variables.lint-test index d3e9a2f1..60c06632 100644 --- a/src/lint/linter/__tests__/xhpast/variable-variables.lint-test +++ b/src/lint/linter/__tests__/xhpast/variable-variables.lint-test @@ -1,5 +1,6 @@ $bar; // okay ~~~~~~~~~~ -error:2:1 +error:3:1 diff --git a/src/lint/linter/__tests__/xhpast/wrong-concat-operator.lint-test b/src/lint/linter/__tests__/xhpast/wrong-concat-operator.lint-test index 70f2450d..3910c597 100644 --- a/src/lint/linter/__tests__/xhpast/wrong-concat-operator.lint-test +++ b/src/lint/linter/__tests__/xhpast/wrong-concat-operator.lint-test @@ -1,9 +1,10 @@ selectTokensOfType('T_OPEN_TAG'); + + foreach ($tokens as $token) { + for ($next = $token->getNextToken(); + $next; + $next = $next->getNextToken()) { + + if ($next->getTypeName() == 'T_WHITESPACE' && + preg_match('/\n\s*\n/', $next->getValue())) { + continue 2; + } + + if ($token->getLineNumber() != $next->getLineNumber()) { + break; + } + + if ($next->getTypeName() == 'T_CLOSE_TAG') { + continue 2; + } + } + + $next = $token->getNextToken(); + $this->raiseLintAtToken( + $next, + pht('`%s` should be separated from code by an empty line.', 'getValue()); + } + } + +}