Skip to content

Commit a590151

Browse files
authored
treat ~ as unary operator, fixes #315 (#317)
1 parent e3e06e1 commit a590151

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/checkstyle/checks/whitespace/OperatorWhitespaceCheck.hx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ class OperatorWhitespaceCheck extends WhitespaceCheckBase {
1111

1212
// =, +=, -=, *=, /=, <<=, >>=, >>>=, &=, |=, ^=
1313
public var assignOpPolicy:WhitespacePolicy;
14-
// ++, --, !
14+
// ++, --, !, ~
1515
public var unaryOpPolicy:WhitespaceUnaryPolicy;
1616
// ?:
1717
public var ternaryOpPolicy:WhitespacePolicy;
1818
// +, -, *, /, %
1919
public var arithmeticOpPolicy:WhitespacePolicy;
2020
// ==, !=, <, <=, >, >=
2121
public var compareOpPolicy:WhitespacePolicy;
22-
// ~, &, |, ^, <<, >>, >>>
22+
// &, |, ^, <<, >>, >>>
2323
public var bitwiseOpPolicy:WhitespacePolicy;
2424
// &&, ||
2525
public var boolOpPolicy:WhitespacePolicy;
@@ -79,6 +79,7 @@ class OperatorWhitespaceCheck extends WhitespaceCheckBase {
7979
function checkUnaryOps(root:TokenTree) {
8080
if ((unaryOpPolicy == null) || (unaryOpPolicy == IGNORE)) return;
8181
var tokens:Array<TokenTree> = root.filter([
82+
Unop(OpNegBits),
8283
Unop(OpNot),
8384
Unop(OpIncrement),
8485
Unop(OpDecrement)

test/checks/whitespace/OperatorWhitespaceCheckTest.hx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ class OperatorWhitespaceCheckTest extends CheckTestCase<OperatorWhitespaceCheckT
88
static inline var MSG_EQUALS_BEFORE:String = 'OperatorWhitespace policy "before" violated by "="';
99
static inline var MSG_EQUALS_AFTER:String = 'OperatorWhitespace policy "after" violated by "="';
1010
static inline var MSG_UNARY_NONE:String = 'OperatorWhitespace policy "none" violated by "++"';
11+
static inline var MSG_UNARY_NONE_BITWISE:String = 'OperatorWhitespace policy "none" violated by "~"';
1112
static inline var MSG_UNARY_INNER:String = 'OperatorWhitespace policy "inner" violated by "++"';
13+
static inline var MSG_UNARY_INNER_BITWISE:String = 'OperatorWhitespace policy "inner" violated by "~"';
1214
static inline var MSG_INTERVAL_NONE:String = 'OperatorWhitespace policy "none" violated by "..."';
1315
static inline var MSG_INTERVAL_AROUND:String = 'OperatorWhitespace policy "around" violated by "..."';
1416
static inline var MSG_FUNC_ARG_AROUND:String = 'OperatorWhitespace policy "around" violated by "->"';
@@ -37,6 +39,7 @@ class OperatorWhitespaceCheckTest extends CheckTestCase<OperatorWhitespaceCheckT
3739
assertNoMsg(check, OPGT);
3840
assertNoMsg(check, MACRO_TYPES);
3941
assertNoMsg(check, MACRO_NOT);
42+
assertNoMsg(check, BITWISE_NEG);
4043
}
4144

4245
public function testIncorrectOperatorWhitespaceToken() {
@@ -90,10 +93,14 @@ class OperatorWhitespaceCheckTest extends CheckTestCase<OperatorWhitespaceCheckT
9093

9194
assertNoMsg(check, UNARY_NO_WHITESPACE);
9295
assertMsg(check, UNARY_INNER_WHITESPACE, MSG_UNARY_NONE);
96+
assertNoMsg(check, BITWISE_NEG);
97+
assertMsg(check, BITWISE_NEG_WRONG, MSG_UNARY_NONE_BITWISE);
9398

9499
check.unaryOpPolicy = INNER;
95100
assertMsg(check, UNARY_NO_WHITESPACE, MSG_UNARY_INNER);
96101
assertNoMsg(check, UNARY_INNER_WHITESPACE);
102+
assertMsg(check, BITWISE_NEG, MSG_UNARY_INNER_BITWISE);
103+
assertNoMsg(check, BITWISE_NEG_WRONG);
97104
}
98105

99106
public function testInterval() {
@@ -471,4 +478,18 @@ abstract OperatorWhitespaceCheckTests(String) to String {
471478
function test() {
472479
}
473480
}";
481+
482+
var BITWISE_NEG = "
483+
class Test {
484+
function test() {
485+
var test = ~test;
486+
}
487+
}";
488+
489+
var BITWISE_NEG_WRONG = "
490+
class Test {
491+
function test() {
492+
var test = ~ test;
493+
}
494+
}";
474495
}

0 commit comments

Comments
 (0)