Jan Wittler 4 лет назад
Родитель
Сommit
cc6adb3438
1 измененных файлов с 12 добавлено и 5 удалено
  1. 12 5
      languages/swift/src/main/java/de/jplag/swift/JPlagSwiftListener.java

+ 12 - 5
languages/swift/src/main/java/de/jplag/swift/JPlagSwiftListener.java

@@ -150,7 +150,6 @@ public class JPlagSwiftListener extends Swift5ParserBaseListener {
 
     @Override
     public void enterConstant_declaration(Constant_declarationContext context) {
-        // TODO: differentiate between property and local variable
         transformToken(PROPERTY_DECLARATION, context.getStart(), context.getStop());
         super.enterConstant_declaration(context);
     }
@@ -187,8 +186,7 @@ public class JPlagSwiftListener extends Swift5ParserBaseListener {
 
     @Override
     public void enterGetter_setter_block(Getter_setter_blockContext context) {
-        // if (implicit getter) var example: Int { /* */ }
-        if (context.getChildCount() == 1 && context.getChild(0) instanceof Code_blockContext) {
+        if (isComputedReadOnlyVariableGetterContext(context)) {
             transformToken(PROPERTY_ACCESSOR_BEGIN, context.getStart());
         }
         super.enterGetter_setter_block(context);
@@ -196,13 +194,22 @@ public class JPlagSwiftListener extends Swift5ParserBaseListener {
 
     @Override
     public void exitGetter_setter_block(Getter_setter_blockContext context) {
-        // if (implicit getter) var example: Int { /* */ }
-        if (context.getChildCount() == 1 && context.getChild(0) instanceof Code_blockContext) {
+        if (isComputedReadOnlyVariableGetterContext(context)) {
             transformToken(PROPERTY_ACCESSOR_END, context.getStop());
         }
         super.exitGetter_setter_block(context);
     }
 
+    /**
+     * Indicates whether the given context encodes a computed read-only variable getter. An example of this is
+     * <code>var example: Int { return 1 }</code>.
+     * @param context
+     * @return
+     */
+    private boolean isComputedReadOnlyVariableGetterContext(Getter_setter_blockContext context) {
+        return context.getChildCount() == 1 && context.getChild(0) instanceof Code_blockContext;
+    }
+
     @Override
     public void exitWillSet_clause(WillSet_clauseContext context) {
         transformToken(PROPERTY_ACCESSOR_END, context.getStop());