formatter: detect when + and - are used as unary operators instead of binary
diff --git a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
index 73463fc..ae80ce7 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -566,8 +566,6 @@
case ASTokenTypes.TOKEN_OPERATOR_GREATER_THAN:
case ASTokenTypes.TOKEN_OPERATOR_LESS_THAN_EQUALS:
case ASTokenTypes.TOKEN_OPERATOR_GREATER_THAN_EQUALS:
- case ASTokenTypes.TOKEN_OPERATOR_PLUS:
- case ASTokenTypes.TOKEN_OPERATOR_MINUS:
case ASTokenTypes.TOKEN_OPERATOR_STAR:
case ASTokenTypes.TOKEN_OPERATOR_DIVISION:
case ASTokenTypes.TOKEN_OPERATOR_MODULO:
@@ -597,6 +595,14 @@
}
break;
}
+ case ASTokenTypes.TOKEN_OPERATOR_PLUS:
+ case ASTokenTypes.TOKEN_OPERATOR_MINUS: {
+ boolean isUnary = checkTokenBeforeUnaryOperator(prevTokenNotComment);
+ if (!isUnary && insertSpaceBeforeAndAfterBinaryOperators) {
+ requiredSpace = true;
+ }
+ break;
+ }
case ASTokenTypes.TOKEN_OPERATOR_ASSIGNMENT: {
inVarOrConstDeclaration = false;
if (insertSpaceBeforeAndAfterBinaryOperators) {
@@ -986,8 +992,6 @@
case ASTokenTypes.TOKEN_OPERATOR_GREATER_THAN:
case ASTokenTypes.TOKEN_OPERATOR_LESS_THAN_EQUALS:
case ASTokenTypes.TOKEN_OPERATOR_GREATER_THAN_EQUALS:
- case ASTokenTypes.TOKEN_OPERATOR_PLUS:
- case ASTokenTypes.TOKEN_OPERATOR_MINUS:
case ASTokenTypes.TOKEN_OPERATOR_STAR:
case ASTokenTypes.TOKEN_OPERATOR_DIVISION:
case ASTokenTypes.TOKEN_OPERATOR_MODULO:
@@ -1018,6 +1022,14 @@
}
break;
}
+ case ASTokenTypes.TOKEN_OPERATOR_PLUS:
+ case ASTokenTypes.TOKEN_OPERATOR_MINUS: {
+ boolean isUnary = checkTokenBeforeUnaryOperator(prevTokenNotComment);
+ if (!isUnary && insertSpaceBeforeAndAfterBinaryOperators && !skipWhitespaceBeforeSemicolon) {
+ requiredSpace = true;
+ }
+ break;
+ }
case ASTokenTypes.TOKEN_COMMA: {
if (insertSpaceAfterCommaDelimiter && !skipWhitespaceBeforeSemicolon) {
requiredSpace = true;
@@ -1216,6 +1228,13 @@
return builder.toString();
}
+ private boolean checkTokenBeforeUnaryOperator(IASToken token) {
+ return (token instanceof ASToken) ? ((ASToken) token).isOperator()
+ || token.getType() == ASTokenTypes.TOKEN_SQUARE_OPEN || token.getType() == ASTokenTypes.TOKEN_PAREN_OPEN
+ || token.getType() == ASTokenTypes.TOKEN_BLOCK_OPEN || token.getType() == ASTokenTypes.TOKEN_SEMICOLON
+ : (token == null);
+ }
+
private int increaseIndent(int indent) {
return indent + 1;
}
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestIdentifier.java b/formatter/src/test/java/org/apache/royale/formatter/TestIdentifier.java
new file mode 100644
index 0000000..2f00d8a
--- /dev/null
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestIdentifier.java
@@ -0,0 +1,218 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+package org.apache.royale.formatter;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+public class TestIdentifier extends BaseFormatterTests {
+ @Test
+ public void testBasic() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "identifier;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "identifier;",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testUnaryPlus() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "+identifier;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "+identifier;",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testUnaryPlusWithLeadingOperator() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "var a = +identifier;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "var a = +identifier;",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testUnaryMinus() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "-identifier;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "-identifier;",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testUnaryMinusWithLeadingOperator() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "var a = -identifier;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "var a = -identifier;",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testUnaryMinusWithParentheses() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "(-identifier);",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "(-identifier);",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testUnaryMinusWithBrackets() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "a[-identifier];",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "a[-identifier];",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testUnaryMinusWithBlock() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "{\n" +
+ "\t-identifier;\n" +
+ "}",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "{\n" +
+ "\t-identifier;\n" +
+ "}",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testUnaryMinusWithSemicolon() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "a;\n" +
+ "-identifier;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "a;\n" +
+ "-identifier;",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testNot() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "!identifier;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "!identifier;",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testDoubleNot() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "!!identifier;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "!!identifier;",
+ // @formatter:on
+ result);
+ }
+}
\ No newline at end of file
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestNumberLiteral.java b/formatter/src/test/java/org/apache/royale/formatter/TestNumberLiteral.java
new file mode 100644
index 0000000..1148e8f
--- /dev/null
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestNumberLiteral.java
@@ -0,0 +1,77 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+
+package org.apache.royale.formatter;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class TestNumberLiteral extends BaseFormatterTests {
+ @Test
+ public void testFloat() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "123.4;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "123.4;",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testInt() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "123;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "123;",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testNegative() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaceBeforeAndAfterBinaryOperators = true;
+ String result = formatter.formatText(
+ // @formatter:off
+ "-123;",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "-123;",
+ // @formatter:on
+ result);
+ }
+}