Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expression conversion #7165

Open
wants to merge 9 commits into
base: dev/patch
Choose a base branch
from
43 changes: 17 additions & 26 deletions src/main/java/ch/njol/skript/lang/SkriptParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ private static <T> Variable<T> parseVariable(String expr, Class<? extends T>[] r
}
}


@Nullable
@SuppressWarnings({"unchecked", "rawtypes"})
private <T> Expression<? extends T> parseSingleExpr(boolean allowUnparsedLiteral, @Nullable LogEntry error, Class<? extends T>... types) {
Expand Down Expand Up @@ -370,22 +369,12 @@ private <T> Expression<? extends T> parseSingleExpr(boolean allowUnparsedLiteral
if (parsedExpression != null) { // Expression/VariableString parsing success
for (Class<? extends T> type : types) {
// Check return type against everything that expression accepts
if (parsedExpression.canReturn(type)) {
log.printLog();
return (Expression<? extends T>) parsedExpression;
}
if (parsedExpression.canReturn(type))
return convertExpression(parsedExpression, log, type);
}

// No directly same type found
Class<T>[] objTypes = (Class<T>[]) types; // Java generics... ?
Expression<? extends T> convertedExpression = parsedExpression.getConvertedExpression(objTypes);
if (convertedExpression != null) {
log.printLog();
return convertedExpression;
}
// Print errors, if we couldn't get the correct type
log.printError(parsedExpression.toString(null, false) + " " + Language.get("is") + " " + notOfType(types), ErrorQuality.NOT_AN_EXPRESSION);
return null;
return convertExpression(parsedExpression, log, (Class<T>[]) types);
}
log.clear();
}
Expand Down Expand Up @@ -556,8 +545,7 @@ private Expression<?> parseSingleExpr(boolean allowUnparsedLiteral, @Nullable Lo
return null;
}

log.printLog();
return parsedExpression;
return convertExpression(parsedExpression, log, type);
}
}

Expand All @@ -566,16 +554,8 @@ private Expression<?> parseSingleExpr(boolean allowUnparsedLiteral, @Nullable Lo
return null;
}

// No directly same type found
Expression<?> convertedExpression = parsedExpression.getConvertedExpression((Class<Object>[]) types);
if (convertedExpression != null) {
log.printLog();
return convertedExpression;
}

// Print errors, if we couldn't get the correct type
log.printError(parsedExpression.toString(null, false) + " " + Language.get("is") + " " + notOfType(types), ErrorQuality.NOT_AN_EXPRESSION);
return null;
//noinspection unchecked
return convertExpression(parsedExpression, log, (Class<Object>[]) types);
}
log.clear();
}
Expand Down Expand Up @@ -619,6 +599,17 @@ private Expression<?> parseSingleExpr(boolean allowUnparsedLiteral, @Nullable Lo
}
}

@SafeVarargs
private static <T> Expression<? extends T> convertExpression(Expression<?> expression, ParseLogHandler log, Class<T>... types) {
Expression<? extends T> convertedExpression = expression.getConvertedExpression(types);
if (convertedExpression != null) {
log.printLog();
return convertedExpression;
}
log.printError(expression.toString(null, false) + " " + Language.get("is") + " " + notOfType(types), ErrorQuality.NOT_AN_EXPRESSION);
return null;
}

/**
* Matches ',', 'and', 'or', etc. as well as surrounding whitespace.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
test "expressions sometimes dont convert":
assert formatted ({_foo} + {_bar}) is not set with "formatting nothing shouldn't throw an error"

set {_foo} to "test"
assert formatted ({_foo} + {_bar}) is not set with "formatting string + none shouldn't throw an error"

set {_foo} to 1
set {_bar} to 2
assert formatted ({_foo} + {_bar}) is not set with "formatting number + number shouldn't throw an error"

set {_foo} to "foo"
set {_bar} to "bar"
assert formatted ({_foo} + {_bar}) is "foobar" with "formatting strings doesn't work"