Page MenuHomePhabricator

D13937.id33642.diff
No OneTemporary

D13937.id33642.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
@@ -234,6 +234,7 @@
'ArcanistSyntaxErrorXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistSyntaxErrorXHPASTLinterRule.php',
'ArcanistTasksWorkflow' => 'workflow/ArcanistTasksWorkflow.php',
'ArcanistTautologicalExpressionXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistTautologicalExpressionXHPASTLinterRule.php',
+ 'ArcanistTernaryExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistTernaryExpressionSpacingXHPASTLinterRule.php',
'ArcanistTestResultParser' => 'unit/parser/ArcanistTestResultParser.php',
'ArcanistTestXHPASTLintSwitchHook' => 'lint/linter/__tests__/ArcanistTestXHPASTLintSwitchHook.php',
'ArcanistTextLinter' => 'lint/linter/ArcanistTextLinter.php',
@@ -517,6 +518,7 @@
'ArcanistSyntaxErrorXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistTasksWorkflow' => 'ArcanistWorkflow',
'ArcanistTautologicalExpressionXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistTernaryExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistTestResultParser' => 'Phobject',
'ArcanistTestXHPASTLintSwitchHook' => 'ArcanistXHPASTLintSwitchHook',
'ArcanistTextLinter' => 'ArcanistLinter',
diff --git a/src/lint/linter/__tests__/xhpast/ternary-expression-spacing.lint-test b/src/lint/linter/__tests__/xhpast/ternary-expression-spacing.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/ternary-expression-spacing.lint-test
@@ -0,0 +1,20 @@
+<?php
+
+$x?$y:$z;
+$x?:$y;
+~~~~~~~~~~
+warning:3:3
+warning:3:6
+warning:4:3
+warning:4:4
+~~~~~~~~~~
+<?php
+
+$x ? $y : $z;
+$x ? : $y;
+~~~~~~~~~~
+{
+ "config": {
+ "xhpast.php-version": "5.4.0"
+ }
+}
diff --git a/src/lint/linter/xhpast/rules/ArcanistTernaryExpressionSpacingXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistTernaryExpressionSpacingXHPASTLinterRule.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/ArcanistTernaryExpressionSpacingXHPASTLinterRule.php
@@ -0,0 +1,49 @@
+<?php
+
+final class ArcanistTernaryExpressionSpacingXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 81;
+
+ public function getLintName() {
+ return pht('Space Around Ternary Operator');
+ }
+
+ public function getLintSeverity() {
+ return ArcanistLintSeverity::SEVERITY_WARNING;
+ }
+
+ public function process(XHPASTNode $root) {
+ $expressions = $root->selectDescendantsOfType('n_TERNARY_EXPRESSION');
+
+ foreach ($expressions as $expression) {
+ $operators = $expression->selectTokensOfTypes(array('?', ':'));
+
+ foreach ($operators as $operator) {
+ $before = $operator->getNonsemanticTokensBefore();
+ $after = $operator->getNonsemanticTokensBefore();
+
+ $operator_value = $operator->getValue();
+ $replace = null;
+
+ if (empty($before) && empty($after)) {
+ $replace = " {$operator_value} ";
+ } else if (empty($before)) {
+ $replace = " {$operator_value}";
+ } else if (empty($after)) {
+ $replace = "{$operator_value} ";
+ }
+
+ if ($replace !== null) {
+ $this->raiseLintAtToken(
+ $operator,
+ pht(
+ 'Convention: ternary operators should be '.
+ 'surrounded by whitespace.'),
+ $replace);
+ }
+ }
+ }
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Fri, Nov 1, 11:52 PM (1 w, 6 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6756501
Default Alt Text
D13937.id33642.diff (3 KB)

Event Timeline