Page MenuHomePhabricator

D14602.diff
No OneTemporary

D14602.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -373,6 +373,8 @@
'ArcanistUselessOverridingMethodXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUselessOverridingMethodXHPASTLinterRule.php',
'ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase.php',
'ArcanistUserAbortException' => 'exception/usage/ArcanistUserAbortException.php',
+ 'ArcanistVariableReferenceSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistVariableReferenceSpacingXHPASTLinterRule.php',
+ 'ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase.php',
'ArcanistVariableVariableXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistVariableVariableXHPASTLinterRule.php',
'ArcanistVariableVariableXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistVariableVariableXHPASTLinterRuleTestCase.php',
'ArcanistVersionWorkflow' => 'workflow/ArcanistVersionWorkflow.php',
@@ -769,6 +771,8 @@
'ArcanistUselessOverridingMethodXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistUserAbortException' => 'ArcanistUsageException',
+ 'ArcanistVariableReferenceSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistVariableVariableXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistVariableVariableXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistVersionWorkflow' => 'ArcanistWorkflow',
diff --git a/src/lint/linter/xhpast/rules/ArcanistVariableReferenceSpacingXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistVariableReferenceSpacingXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistVariableReferenceSpacingXHPASTLinterRule.php
@@ -0,0 +1,34 @@
+<?php
+
+final class ArcanistVariableReferenceSpacingXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 123;
+
+ public function getLintName() {
+ return pht('Variable Reference Spacing');
+ }
+
+ public function getLintSeverity() {
+ return ArcanistLintSeverity::SEVERITY_WARNING;
+ }
+
+ public function process(XHPASTNode $root) {
+ $references = $root->selectDescendantsOfType('n_VARIABLE_REFERENCE');
+
+ foreach ($references as $reference) {
+ $variable = $reference->getChildByIndex(0);
+
+ list($before, $after) = $variable->getSurroundingNonsemanticTokens();
+
+ if ($before) {
+ $this->raiseLintAtOffset(
+ head($before)->getOffset(),
+ pht('Variable references should not be prefixed with whitespace.'),
+ implode('', mpull($before, 'getValue')),
+ '');
+ }
+ }
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase.php
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistVariableReferenceSpacingXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(
+ dirname(__FILE__).'/variable-reference-spacing/');
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/variable-reference-spacing/function-call.lint-test b/src/lint/linter/xhpast/rules/__tests__/variable-reference-spacing/function-call.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/variable-reference-spacing/function-call.lint-test
@@ -0,0 +1,13 @@
+<?php
+$x = &some_function();
+$x = & some_function();
+$x = &
+ some_function();
+~~~~~~~~~~
+warning:3:7
+warning:4:7
+~~~~~~~~~~
+<?php
+$x = &some_function();
+$x = &some_function();
+$x = &some_function();
diff --git a/src/lint/linter/xhpast/rules/__tests__/variable-reference-spacing/variable.lint-test b/src/lint/linter/xhpast/rules/__tests__/variable-reference-spacing/variable.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/variable-reference-spacing/variable.lint-test
@@ -0,0 +1,13 @@
+<?php
+$x = &$a;
+$x = & $b;
+$x = &
+ $c;
+~~~~~~~~~~
+warning:3:7
+warning:4:7
+~~~~~~~~~~
+<?php
+$x = &$a;
+$x = &$b;
+$x = &$c;

File Metadata

Mime Type
text/plain
Expires
Thu, Mar 20, 6:45 AM (4 d, 12 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7477599
Default Alt Text
D14602.diff (4 KB)

Event Timeline