Skip to content

Commit

Permalink
Preserve Groovy-style type annotations in formatting, remove warning
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Sherman <[email protected]>
  • Loading branch information
bentsherman committed Dec 12, 2024
1 parent 9a09d93 commit ff2b901
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
21 changes: 21 additions & 0 deletions modules/compiler/src/main/java/script/formatter/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,18 @@ public void visitArguments(List<Expression> args, boolean wrap) {
}
}

private boolean inVariableDeclaration;

private boolean inWrappedPipeChain;

@Override
public void visitBinaryExpression(BinaryExpression node) {
if( node instanceof DeclarationExpression ) {
if( node.getNodeMetaData(IMPLICIT_DECLARATION) != Boolean.TRUE )
append("def ");
inVariableDeclaration = true;
visit(node.getLeftExpression());
inVariableDeclaration = false;
if( !(node.getRightExpression() instanceof EmptyExpression) ) {
append(" = ");
visit(node.getRightExpression());
Expand Down Expand Up @@ -475,6 +479,10 @@ else if( code.getStatements().size() == 1 && code.getStatements().get(0) instanc
public void visitParameters(Parameter[] parameters) {
for( int i = 0; i < parameters.length; i++ ) {
var param = parameters[i];
if( isLegacyType(param.getType()) ) {
visitTypeName(param.getType());
append(' ');
}
append(param.getName());
if( param.hasInitialExpression() ) {
append('=');
Expand Down Expand Up @@ -618,6 +626,10 @@ public void visitClassExpression(ClassExpression node) {
}

protected void visitTypeName(ClassNode type) {
if( isLegacyType(type) ) {
append(type.getNodeMetaData(LEGACY_TYPE));
return;
}
if( type.isArray() ) {
visitTypeName(type.getComponentType());
return;
Expand All @@ -644,6 +656,10 @@ protected void visitTypeName(ClassNode type) {

@Override
public void visitVariableExpression(VariableExpression node) {
if( inVariableDeclaration && isLegacyType(node.getType()) ) {
visitTypeName(node.getType());
append(' ');
}
append(node.getText());
}

Expand Down Expand Up @@ -697,6 +713,10 @@ public void visit(Expression node) {

// helpers

public static boolean isLegacyType(ClassNode cn) {
return cn.getNodeMetaData(LEGACY_TYPE) != null;
}

private boolean shouldWrapExpression(Expression node) {
return node.getLineNumber() < node.getLastLineNumber();
}
Expand Down Expand Up @@ -739,6 +759,7 @@ private boolean shouldWrapPipeExpression(BinaryExpression node) {
private static final String IMPLICIT_DECLARATION = "_IMPLICIT_DECLARATION";
private static final String INSIDE_PARENTHESES_LEVEL = "_INSIDE_PARENTHESES_LEVEL";
private static final String LEADING_COMMENTS = "_LEADING_COMMENTS";
private static final String LEGACY_TYPE = "_LEGACY_TYPE";
private static final String QUOTE_CHAR = "_QUOTE_CHAR";
private static final String TRAILING_COMMENT = "_TRAILING_COMMENT";
private static final String VERBATIM_TEXT = "_VERBATIM_TEXT";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ public void visitProcess(ProcessNode node) {
public void visitFunction(FunctionNode node) {
fmt.appendLeadingComments(node);
fmt.append("def ");
if( Formatter.isLegacyType(node.getReturnType()) ) {
fmt.visitTypeName(node.getReturnType());
fmt.append(' ');
}
fmt.append(node.getName());
fmt.append('(');
fmt.visitParameters(node.getParameters());
Expand Down
27 changes: 12 additions & 15 deletions modules/compiler/src/main/java/script/parser/ScriptAstBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -629,13 +629,12 @@ private Statement outputTargetBody(OutputTargetBodyContext ctx) {
}

private FunctionNode functionDef(FunctionDefContext ctx) {
checkLegacyType(ctx.legacyType());

var name = identifier(ctx.identifier());
var returnType = legacyType(ctx.legacyType());
var params = Optional.ofNullable(formalParameterList(ctx.formalParameterList())).orElse(Parameter.EMPTY_ARRAY);
var code = blockStatements(ctx.blockStatements());

var result = ast( new FunctionNode(name, null, params, code), ctx );
var result = ast( new FunctionNode(name, returnType, params, code), ctx );
checkInvalidVarName(name, result);
groovydocManager.handle(result, ctx);
return result;
Expand Down Expand Up @@ -781,9 +780,8 @@ private Statement variableDeclaration(VariableDeclarationContext ctx) {
}
else {
// single assignment
checkLegacyType(ctx.legacyType());

var target = variableName(ctx.identifier());
target.setType(legacyType(ctx.legacyType()));
var initializer = ctx.initializer != null
? expression(ctx.initializer)
: EmptyExpression.INSTANCE;
Expand Down Expand Up @@ -1510,9 +1508,7 @@ private Parameter[] formalParameterList(FormalParameterListContext ctx) {
}

private Parameter formalParameter(FormalParameterContext ctx) {
checkLegacyType(ctx.legacyType());

var type = ClassHelper.dynamicType();
var type = legacyType(ctx.legacyType());
var name = identifier(ctx.identifier());
var defaultValue = ctx.expression() != null
? expression(ctx.expression())
Expand Down Expand Up @@ -1607,6 +1603,13 @@ private GenericsType genericsType(TypeContext ctx) {
return ast( new GenericsType(type(ctx)), ctx );
}

private ClassNode legacyType(ParserRuleContext ctx) {
var result = ClassHelper.dynamicType();
if( ctx != null )
result.putNodeMetaData(LEGACY_TYPE, ctx.getText());
return result;
}

/// COMMENTS

private void saveLeadingComments(ASTNode node, ParserRuleContext ctx) {
Expand Down Expand Up @@ -1708,13 +1711,6 @@ private boolean isInsideParentheses(NodeMetaDataHandler nodeMetaDataHandler) {
return insideParenLevel != null && insideParenLevel.intValue() > 0;
}

private void checkLegacyType(ParserRuleContext ctx) {
if( ctx != null ) {
var node = ast( new EmptyStatement(), ctx );
collectWarning("Groovy-style type annotations are ignored", node);
}
}

private CompilationFailedException createParsingFailedException(String msg, ParserRuleContext ctx) {
return createParsingFailedException(
new SyntaxException(msg,
Expand Down Expand Up @@ -1826,6 +1822,7 @@ public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex,
private static final String FULLY_QUALIFIED = "_FULLY_QUALIFIED";
private static final String INSIDE_PARENTHESES_LEVEL = "_INSIDE_PARENTHESES_LEVEL";
private static final String LEADING_COMMENTS = "_LEADING_COMMENTS";
private static final String LEGACY_TYPE = "_LEGACY_TYPE";
private static final String QUOTE_CHAR = "_QUOTE_CHAR";
private static final String TRAILING_COMMENT = "_TRAILING_COMMENT";
private static final String VERBATIM_TEXT = "_VERBATIM_TEXT";
Expand Down

0 comments on commit ff2b901

Please sign in to comment.