-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95e8cb3
commit 4505848
Showing
7 changed files
with
211 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/main/java/com/sleekbyte/tailor/listeners/RedundantSelfListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.sleekbyte.tailor.listeners; | ||
|
||
import com.sleekbyte.tailor.antlr.SwiftBaseListener; | ||
import com.sleekbyte.tailor.antlr.SwiftParser; | ||
import com.sleekbyte.tailor.antlr.SwiftParser.ClosureExpressionContext; | ||
import com.sleekbyte.tailor.antlr.SwiftParser.ExternalParameterNameContext; | ||
import com.sleekbyte.tailor.antlr.SwiftParser.FunctionDeclarationContext; | ||
import com.sleekbyte.tailor.antlr.SwiftParser.InitializerDeclarationContext; | ||
import com.sleekbyte.tailor.antlr.SwiftParser.LocalParameterNameContext; | ||
import com.sleekbyte.tailor.antlr.SwiftParser.ParameterClauseContext; | ||
import com.sleekbyte.tailor.antlr.SwiftParser.ParameterContext; | ||
import com.sleekbyte.tailor.antlr.SwiftParser.ParameterListContext; | ||
import com.sleekbyte.tailor.common.Location; | ||
import com.sleekbyte.tailor.common.Messages; | ||
import com.sleekbyte.tailor.common.Rules; | ||
import com.sleekbyte.tailor.output.Printer; | ||
import com.sleekbyte.tailor.utils.ListenerUtil; | ||
import com.sleekbyte.tailor.utils.ParseTreeUtil; | ||
import org.antlr.v4.runtime.ParserRuleContext; | ||
import org.antlr.v4.runtime.tree.ParseTree; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Parse tree listener for redundant self keyword usage. | ||
*/ | ||
public final class RedundantSelfListener extends SwiftBaseListener { | ||
|
||
private Printer printer; | ||
|
||
public RedundantSelfListener(Printer printer) { | ||
this.printer = printer; | ||
} | ||
|
||
@Override | ||
public void enterSelfExpression(SwiftParser.SelfExpressionContext ctx) { | ||
Location location = ListenerUtil.getContextStartLocation(ctx); | ||
// Do not flag | ||
// 1. standalone self usages | ||
// 2. self usage in initializer(s) and closures | ||
ParseTree dot = ParseTreeUtil.getRightNode(ctx); | ||
if (dot != null && !isInInitializerOrClosure(ctx)) { | ||
// Extract function parameter names | ||
List<String> parameterNames = getFunctionParameters(getParentFunction(ctx)); | ||
ParseTree property = ParseTreeUtil.getRightSibling(dot); | ||
if (property != null && parameterNames.contains(property.getText())) { | ||
return; | ||
} | ||
// Flag usage of self | ||
// 1. outside closures and initializer(s) | ||
// 2. inside methods that do not have same named parameters | ||
printer.warn(Rules.REDUNDANT_SELF, Messages.EXPLICIT_CALL_TO_SELF, location); | ||
|
||
} | ||
} | ||
|
||
private static boolean isInInitializerOrClosure(ParserRuleContext ctx) { | ||
if (ctx == null) { | ||
return false; | ||
} | ||
while (ctx != null) { | ||
ctx = ctx.getParent(); | ||
if (ctx instanceof ClosureExpressionContext | ||
|| ctx instanceof InitializerDeclarationContext) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private static FunctionDeclarationContext getParentFunction(ParserRuleContext ctx) { | ||
if (ctx == null) { | ||
return null; | ||
} | ||
while (ctx != null) { | ||
ctx = ctx.getParent(); | ||
if (ctx instanceof FunctionDeclarationContext) { | ||
return (FunctionDeclarationContext) ctx; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private List<String> getFunctionParameters(FunctionDeclarationContext ctx) { | ||
ArrayList<String> parameterNames = new ArrayList<>(); | ||
|
||
if (ctx == null) { | ||
return parameterNames; | ||
} | ||
|
||
List<ParameterClauseContext> parameterClauses = ctx.functionSignature().parameterClauses().parameterClause(); | ||
for (ParameterClauseContext parameterClause : parameterClauses) { | ||
ParameterListContext parameterList = parameterClause.parameterList(); | ||
if (parameterList != null) { | ||
for (ParseTree item : parameterList.children) { | ||
ParameterContext parameter; | ||
if (item.getText().equals(",")) { | ||
continue; | ||
} else if (item instanceof ParameterListContext) { | ||
parameter = ((ParameterListContext) item).parameter(); | ||
} else { | ||
parameter = (ParameterContext) item; | ||
} | ||
LocalParameterNameContext localParameterName = parameter.localParameterName(); | ||
if (localParameterName != null) { | ||
parameterNames.add(parameter.localParameterName().getText()); | ||
} else { | ||
ExternalParameterNameContext externalParameterName = parameter.externalParameterName(); | ||
parameterNames.add(externalParameterName.getText()); | ||
} | ||
} | ||
} | ||
} | ||
return parameterNames; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/java/com/sleekbyte/tailor/functional/RedundantSelfTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.sleekbyte.tailor.functional; | ||
|
||
|
||
import com.sleekbyte.tailor.common.Messages; | ||
import com.sleekbyte.tailor.common.Rules; | ||
import com.sleekbyte.tailor.common.Severity; | ||
import com.sleekbyte.tailor.output.Printer; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
|
||
/** | ||
* Functional tests for {@link com.sleekbyte.tailor.listeners.RedundantSelfListener}. | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public final class RedundantSelfTest extends RuleTest { | ||
|
||
@Override | ||
protected String[] getCommandArgs() { | ||
return new String[] { | ||
"--only", "redundant-self" | ||
}; | ||
} | ||
|
||
@Override | ||
protected void addAllExpectedMsgs() { | ||
addExpectedMsg(13, 12); | ||
addExpectedMsg(14, 19); | ||
addExpectedMsg(22, 9); | ||
addExpectedMsg(22, 19); | ||
addExpectedMsg(35, 9); | ||
} | ||
|
||
private void addExpectedMsg(int line, int column) { | ||
expectedMessages.add(Printer.genOutputStringForTest(Rules.REDUNDANT_SELF, inputFile.getName(), line, column, | ||
Severity.WARNING, Messages.EXPLICIT_CALL_TO_SELF)); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/test/swift/com/sleekbyte/tailor/functional/RedundantSelfTest.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import Foundation | ||
|
||
class SomeClass { | ||
let x = 2 | ||
let y = 10 | ||
|
||
init() { | ||
print(self.x) | ||
self.demo(2, y: "2") | ||
} | ||
|
||
func demo(d: Int, y: String) { | ||
if self.x == 2 { | ||
print(self.x) | ||
} else { | ||
print(self.y) | ||
} | ||
} | ||
|
||
func callDemoTwice() { | ||
demo(x, y: "2") | ||
self.demo(self.x, y: "2") | ||
} | ||
|
||
} | ||
|
||
private class History { | ||
var events: [String] | ||
|
||
init(events: [String]) { | ||
self.events = events | ||
} | ||
|
||
func rewrite() { | ||
self.events = [] | ||
} | ||
|
||
} | ||
|
||
extension History { | ||
|
||
var whenVictorious: () -> () { | ||
return { | ||
self.rewrite() | ||
} | ||
} | ||
} |