Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14022614
D13937.id.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D13937.id.diff
View Options
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
@@ -315,6 +315,8 @@
'ArcanistTasksWorkflow' => 'workflow/ArcanistTasksWorkflow.php',
'ArcanistTautologicalExpressionXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistTautologicalExpressionXHPASTLinterRule.php',
'ArcanistTautologicalExpressionXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistTautologicalExpressionXHPASTLinterRuleTestCase.php',
+ 'ArcanistTernaryExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistTernaryExpressionSpacingXHPASTLinterRule.php',
+ 'ArcanistTernaryExpressionSpacingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistTernaryExpressionSpacingXHPASTLinterRuleTestCase.php',
'ArcanistTestResultParser' => 'unit/parser/ArcanistTestResultParser.php',
'ArcanistTestXHPASTLintSwitchHook' => 'lint/linter/__tests__/ArcanistTestXHPASTLintSwitchHook.php',
'ArcanistTextLinter' => 'lint/linter/ArcanistTextLinter.php',
@@ -693,6 +695,8 @@
'ArcanistTasksWorkflow' => 'ArcanistWorkflow',
'ArcanistTautologicalExpressionXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistTautologicalExpressionXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
+ 'ArcanistTernaryExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistTernaryExpressionSpacingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistTestResultParser' => 'Phobject',
'ArcanistTestXHPASTLintSwitchHook' => 'ArcanistXHPASTLintSwitchHook',
'ArcanistTextLinter' => 'ArcanistLinter',
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,60 @@
+<?php
+
+final class ArcanistTernaryExpressionSpacingXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 93;
+
+ 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('?', ':'));
+ $shorthand = $expression->getChildByIndex(2)->getTypeName() == 'n_EMPTY';
+
+ foreach ($operators as $operator) {
+ $before = $operator->getNonsemanticTokensBefore();
+ $after = $operator->getNonsemanticTokensBefore();
+
+ $operator_value = $operator->getValue();
+ $replace = null;
+
+ switch ($operator_value) {
+ case '?':
+ if ($shorthand) {
+ $replace = " {$operator_value}";
+ } else {
+ $replace = " {$operator_value} ";
+ }
+ break;
+
+ case ':':
+ if ($shorthand) {
+ $replace = "{$operator_value} ";
+ } else {
+ $replace = " {$operator_value} ";
+ }
+ break;
+ }
+
+ if ($replace !== null) {
+ $this->raiseLintAtToken(
+ $operator,
+ pht(
+ 'Convention: ternary operators should be '.
+ 'surrounded by whitespace.'),
+ $replace);
+ }
+ }
+ }
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistTernaryExpressionSpacingXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistTernaryExpressionSpacingXHPASTLinterRuleTestCase.php
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistTernaryExpressionSpacingXHPASTLinterRuleTestCase.php
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistTernaryExpressionSpacingXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(
+ dirname(__FILE__).'/ternary-expression-spacing/');
+ }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ternary-expression-spacing/ternary-expression-spacing.lint-test b/src/lint/linter/xhpast/rules/__tests__/ternary-expression-spacing/ternary-expression-spacing.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/xhpast/rules/__tests__/ternary-expression-spacing/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"
+ }
+}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Nov 7, 6:25 PM (1 w, 6 h ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6714023
Default Alt Text
D13937.id.diff (4 KB)
Attached To
Mode
D13937: Add a ternary expression spacing linter rule
Attached
Detach File
Event Timeline
Log In to Comment