Page MenuHomePhabricator

D14602.id35322.diff
No OneTemporary

D14602.id35322.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
@@ -292,6 +292,8 @@
'ArcanistPyLintLinterTestCase' => 'lint/linter/__tests__/ArcanistPyLintLinterTestCase.php',
'ArcanistRaggedClassTreeEdgeXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistRaggedClassTreeEdgeXHPASTLinterRule.php',
'ArcanistRaggedClassTreeEdgeXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistRaggedClassTreeEdgeXHPASTLinterRuleTestCase.php',
+ 'ArcanistReferenceSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistReferenceSpacingXHPASTLinterRule.php',
+ 'ArcanistReferenceSpacingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistReferenceSpacingXHPASTLinterRuleTestCase.php',
'ArcanistRepositoryAPI' => 'repository/api/ArcanistRepositoryAPI.php',
'ArcanistRepositoryAPIMiscTestCase' => 'repository/api/__tests__/ArcanistRepositoryAPIMiscTestCase.php',
'ArcanistRepositoryAPIStateTestCase' => 'repository/api/__tests__/ArcanistRepositoryAPIStateTestCase.php',
@@ -688,6 +690,8 @@
'ArcanistPyLintLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistRaggedClassTreeEdgeXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistRaggedClassTreeEdgeXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
+ 'ArcanistReferenceSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistReferenceSpacingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistRepositoryAPI' => 'Phobject',
'ArcanistRepositoryAPIMiscTestCase' => 'PhutilTestCase',
'ArcanistRepositoryAPIStateTestCase' => 'PhutilTestCase',
diff --git a/src/lint/linter/xhpast/rules/ArcanistReferenceSpacingXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistReferenceSpacingXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistReferenceSpacingXHPASTLinterRule.php
@@ -0,0 +1,34 @@
+<?php
+
+final class ArcanistReferenceSpacingXHPASTLinterRule
+ 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__/ArcanistReferenceSpacingXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistReferenceSpacingXHPASTLinterRuleTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistReferenceSpacingXHPASTLinterRuleTestCase.php
@@ -0,0 +1,10 @@
+<?php
+
+final class ArcanistReferenceSpacingXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(dirname(__FILE__).'/reference-spacing/');
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/reference-spacing/function-call.lint-test b/src/lint/linter/xhpast/rules/__tests__/reference-spacing/function-call.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/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__/reference-spacing/variable.lint-test b/src/lint/linter/xhpast/rules/__tests__/reference-spacing/variable.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/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;
diff --git a/test.php b/test.php
new file mode 100644
--- /dev/null
+++ b/test.php
@@ -0,0 +1,3 @@
+<?php
+
+$x = &foobar();

File Metadata

Mime Type
text/plain
Expires
Sun, Mar 30, 6:34 AM (1 w, 1 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7726756
Default Alt Text
D14602.id35322.diff (4 KB)

Event Timeline