You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2021/09/09 22:12:54 UTC

[royale-compiler] branch develop updated (6ef095b -> d2358fd)

This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a change to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git.


    from 6ef095b  JSRoyaleEmitter: don't emit the namespace for namespace access expressions because the identifier emitter will handle that
     new 583f4e2  formatter: fix indentation of methods in interfaces
     new 89893ab  formatter: better formatting for object literal inside a ternary statement
     new d2358fd  formatter: detect when + and - are used as unary operators instead of binary

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/royale/formatter/FORMATTER.java     |  50 +++++++---
 ...rithmeticOperators.java => TestIdentifier.java} | 102 +++++++++++++--------
 .../royale/formatter/TestInterfaceDeclaration.java |  52 +++++++++++
 .../{TestXML.java => TestNumberLiteral.java}       |  22 ++---
 .../royale/formatter/TestTernaryStatement.java     |  18 ++++
 5 files changed, 181 insertions(+), 63 deletions(-)
 copy formatter/src/test/java/org/apache/royale/formatter/{TestArithmeticOperators.java => TestIdentifier.java} (74%)
 copy formatter/src/test/java/org/apache/royale/formatter/{TestXML.java => TestNumberLiteral.java} (85%)

[royale-compiler] 03/03: formatter: detect when + and - are used as unary operators instead of binary

Posted by jo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git

commit d2358fd70a762a123385a9b32bae681eb19a390d
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Thu Sep 9 14:47:05 2021 -0700

    formatter: detect when + and - are used as unary operators instead of binary
---
 .../org/apache/royale/formatter/FORMATTER.java     |  27 ++-
 .../apache/royale/formatter/TestIdentifier.java    | 218 +++++++++++++++++++++
 .../apache/royale/formatter/TestNumberLiteral.java |  77 ++++++++
 3 files changed, 318 insertions(+), 4 deletions(-)

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 @@ class FORMATTER {
 					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 @@ class FORMATTER {
 						}
 						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 @@ class FORMATTER {
 					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 @@ class FORMATTER {
 						}
 						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 @@ class FORMATTER {
 		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);
+	}
+}

[royale-compiler] 02/03: formatter: better formatting for object literal inside a ternary statement

Posted by jo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git

commit 89893ab38c74e9e489bbbdf1796d23c7acd076fe
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Thu Sep 9 14:46:28 2021 -0700

    formatter: better formatting for object literal inside a ternary statement
---
 .../org/apache/royale/formatter/FORMATTER.java     | 22 +++++++++++++---------
 .../royale/formatter/TestTernaryStatement.java     | 18 ++++++++++++++++++
 2 files changed, 31 insertions(+), 9 deletions(-)

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 7959d31..73463fc 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -484,8 +484,13 @@ class FORMATTER {
 							blockOpenPending = prevTokenNotComment == null
 									|| prevTokenNotComment.getType() == ASTokenTypes.TOKEN_SEMICOLON
 									|| prevTokenNotComment.getType() == ASTokenTypes.TOKEN_BLOCK_OPEN
-									|| prevTokenNotComment.getType() == ASTokenTypes.TOKEN_BLOCK_CLOSE
-									|| prevTokenNotComment.getType() == ASTokenTypes.TOKEN_COLON;
+									|| prevTokenNotComment.getType() == ASTokenTypes.TOKEN_BLOCK_CLOSE;
+							if (!blockOpenPending && prevTokenNotComment.getType() == ASTokenTypes.TOKEN_COLON
+									&& !blockStack.isEmpty()) {
+								IASToken blockToken = blockStack.get(blockStack.size() - 1).token;
+								blockOpenPending = blockToken.getType() == ASTokenTypes.TOKEN_KEYWORD_DEFAULT
+										|| blockToken.getType() == ASTokenTypes.TOKEN_KEYWORD_CASE;
+							}
 							if (blockOpenPending) {
 								blockStack.add(new BlockStackItem(token));
 							}
@@ -719,9 +724,8 @@ class FORMATTER {
 										&& prevStackItem.blockDepth <= 0) {
 									blockStack.remove(blockStack.size() - 1);
 									if (prevStackItem.token.getType() != ASTokenTypes.TOKEN_KEYWORD_CLASS
-										&& prevStackItem.token.getType() != ASTokenTypes.TOKEN_KEYWORD_INTERFACE
-										&& prevStackItem.token.getType() != ASTokenTypes.TOKEN_KEYWORD_FUNCTION)
-									{
+											&& prevStackItem.token.getType() != ASTokenTypes.TOKEN_KEYWORD_INTERFACE
+											&& prevStackItem.token.getType() != ASTokenTypes.TOKEN_KEYWORD_FUNCTION) {
 										indent = decreaseIndent(indent);
 									}
 								}
@@ -763,11 +767,11 @@ class FORMATTER {
 							if (item.blockDepth <= 0) {
 								blockStack.remove(blockStack.size() - 1);
 							}
+							if (!(item instanceof ObjectLiteralBlockStackItem)
+									&& (nextToken == null || nextToken.getType() != ASTokenTypes.TOKEN_SEMICOLON)) {
+								numRequiredNewLines = Math.max(numRequiredNewLines, 1);
+							}
 						}
-						if (nextToken == null || nextToken.getType() != ASTokenTypes.TOKEN_SEMICOLON) {
-							numRequiredNewLines = Math.max(numRequiredNewLines, 1);
-						}
-
 						break;
 					}
 					case ASTokenTypes.TOKEN_OPERATOR_INCREMENT:
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestTernaryStatement.java b/formatter/src/test/java/org/apache/royale/formatter/TestTernaryStatement.java
index cd6ef98..3432198 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestTernaryStatement.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestTernaryStatement.java
@@ -61,4 +61,22 @@ public class TestTernaryStatement extends BaseFormatterTests {
 				// @formatter:on
 				result);
 	}
+	@Test
+	public void testObjectLiterals() {
+		FORMATTER formatter = new FORMATTER();
+		formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
+		formatter.placeOpenBraceOnNewLine = true;
+		formatter.insertSpaces = false;
+		String result = formatter.formatText(
+		// @formatter:off
+			"condition ? {} : {};",
+			// @formatter:on
+			problems
+		);
+		assertEquals(
+		// @formatter:off
+			"condition ? {} : {};",
+				// @formatter:on
+				result);
+	}
 }

[royale-compiler] 01/03: formatter: fix indentation of methods in interfaces

Posted by jo...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git

commit 583f4e2ade3e8d6510395e2254d66236ddf4e9f0
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Thu Sep 9 14:12:12 2021 -0700

    formatter: fix indentation of methods in interfaces
---
 .../org/apache/royale/formatter/FORMATTER.java     |  7 ++-
 .../royale/formatter/TestInterfaceDeclaration.java | 52 ++++++++++++++++++++++
 2 files changed, 58 insertions(+), 1 deletion(-)

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 19970db..7959d31 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -718,7 +718,12 @@ class FORMATTER {
 										&& prevStackItem.token.getType() != ASTokenTypes.TOKEN_KEYWORD_DEFAULT
 										&& prevStackItem.blockDepth <= 0) {
 									blockStack.remove(blockStack.size() - 1);
-									indent = decreaseIndent(indent);
+									if (prevStackItem.token.getType() != ASTokenTypes.TOKEN_KEYWORD_CLASS
+										&& prevStackItem.token.getType() != ASTokenTypes.TOKEN_KEYWORD_INTERFACE
+										&& prevStackItem.token.getType() != ASTokenTypes.TOKEN_KEYWORD_FUNCTION)
+									{
+										indent = decreaseIndent(indent);
+									}
 								}
 							}
 						}
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestInterfaceDeclaration.java b/formatter/src/test/java/org/apache/royale/formatter/TestInterfaceDeclaration.java
index e259d13..149ab72 100644
--- a/formatter/src/test/java/org/apache/royale/formatter/TestInterfaceDeclaration.java
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestInterfaceDeclaration.java
@@ -115,4 +115,56 @@ public class TestInterfaceDeclaration extends BaseFormatterTests {
 				// @formatter:on
 				result);
 	}
+
+	@Test
+	public void testOneMethod() {
+		FORMATTER formatter = new FORMATTER();
+		formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
+		formatter.placeOpenBraceOnNewLine = true;
+		formatter.insertSpaces = false;
+		String result = formatter.formatText(
+		// @formatter:off
+			"interface MyInterface\n" +
+			"{\n" +
+			"\tfunction myMethod():void;\n" +
+			"}",
+			// @formatter:on
+			problems
+		);
+		assertEquals(
+		// @formatter:off
+				"interface MyInterface\n" +
+				"{\n" +
+				"\tfunction myMethod():void;\n" +
+				"}",
+				// @formatter:on
+				result);
+	}
+
+	@Test
+	public void testMultipleMethods() {
+		FORMATTER formatter = new FORMATTER();
+		formatter.insertSpaceAfterKeywordsInControlFlowStatements = true;
+		formatter.placeOpenBraceOnNewLine = true;
+		formatter.insertSpaces = false;
+		String result = formatter.formatText(
+		// @formatter:off
+			"interface MyInterface\n" +
+			"{\n" +
+			"\tfunction myMethod1():void;\n" +
+			"\tfunction myMethod2():void;\n" +
+			"}",
+			// @formatter:on
+			problems
+		);
+		assertEquals(
+		// @formatter:off
+				"interface MyInterface\n" +
+				"{\n" +
+				"\tfunction myMethod1():void;\n" +
+				"\tfunction myMethod2():void;\n" +
+				"}",
+				// @formatter:on
+				result);
+	}
 }