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

Add support for controlled trailing semi-colons and injecting custom properties #1059

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public class Settings {
public boolean jackson2ModuleDiscovery = false;
public List<Class<? extends Module>> jackson2Modules = new ArrayList<>();
public ClassLoader classLoader = null;
public boolean disableTrailingSemiColon = false;
public Map<String, List<String>> injectCustomProperties = Map.of();

private boolean defaultStringEnumsOverriddenByExtension = false;

Expand Down Expand Up @@ -903,5 +905,4 @@ private static int modifierToBitMask(javax.lang.model.element.Modifier modifier)
default: return 0;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public TsType getTsType() {

public String format(Settings settings) {
final String questionMark = (tsType instanceof TsType.OptionalType) ? "?" : "";
return Emitter.quoteIfNeeded(name, settings) + questionMark + ": " + tsType.format(settings) + ";";
final String trailingChar = settings.disableTrailingSemiColon ? "" : ";";
return Emitter.quoteIfNeeded(name, settings) + questionMark + ": " + tsType.format(settings) + trailingChar;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public TypeProcessor getCommonTypeProcessor() {
final ModelParser.Factory modelParserFactory = getModelParserFactory();
final List<TypeProcessor> specificTypeProcessors = Stream
.concat(
restFactories.stream().map(factory -> factory.getSpecificTypeProcessor()),
restFactories.stream().map(RestApplicationParser.Factory::getSpecificTypeProcessor),
Stream.of(modelParserFactory.getSpecificTypeProcessor())
)
.filter(Objects::nonNull)
Expand All @@ -167,8 +167,7 @@ private TypeProcessor createTypeProcessor(List<TypeProcessor> specificTypeProces
processors.add(new CustomMappingTypeProcessor(settings.getValidatedCustomTypeMappings()));
processors.addAll(specificTypeProcessors);
processors.add(new DefaultTypeProcessor(settings.getLoadedDataLibraries()));
final TypeProcessor typeProcessor = new TypeProcessor.Chain(processors);
return typeProcessor;
return new TypeProcessor.Chain(processors);
}

public ModelParser getModelParser() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ private void emitImports() {
writeNewLine();
for (ModuleDependency dependency : settings.moduleDependencies) {
if (!dependency.global) {
writeIndentedLine("import * as " + dependency.importAs + " from " + quote(dependency.importFrom, settings) + ";");
writeIndentedLine("import * as " + dependency.importAs + " from " + quote(dependency.importFrom, settings) + getTrailingChar());
}
}
}
if (settings.importDeclarations != null && !settings.importDeclarations.isEmpty()) {
writeNewLine();
for (String importDeclaration : settings.importDeclarations) {
writeIndentedLine(importDeclaration + ";");
writeIndentedLine(importDeclaration + getTrailingChar());
}
}
}
Expand Down Expand Up @@ -194,6 +194,7 @@ private void emitBean(TsBeanModel bean, boolean exportKeyword) {
for (TsPropertyModel property : bean.getProperties()) {
emitProperty(property);
}
emitInjectedProperties(bean);
if (bean.getConstructor() != null) {
emitCallable(bean.getConstructor());
}
Expand All @@ -212,7 +213,23 @@ private void emitProperty(TsPropertyModel property) {
final String readonlyString = property.modifiers.isReadonly ? "readonly " : "";
final String questionMark = tsType instanceof TsType.OptionalType ? "?" : "";
final String defaultString = property.getDefaultValue() != null ? " = " + property.getDefaultValue().format(settings) : "";
writeIndentedLine(staticString + readonlyString + quoteIfNeeded(property.getName(), settings) + questionMark + ": " + tsType.format(settings) + defaultString + ";");
writeIndentedLine(staticString + readonlyString + quoteIfNeeded(property.getName(), settings) + questionMark + ": " + tsType.format(settings) + defaultString + getTrailingChar());
}

private void emitInjectedProperties(TsBeanModel bean) {

settings.injectCustomProperties
.keySet()
.stream()
.filter(className -> className.equals(bean.getName().getSimpleName()))
.findFirst().ifPresent((ignored) -> writeIndentedLine("// custom properties"));

settings.injectCustomProperties
.entrySet()
.stream()
.filter(entry -> entry.getKey().equals(bean.getName().getSimpleName()))
.flatMap(matched -> matched.getValue().stream())
.forEach(this::writeIndentedLine);
}

private void emitDecorators(List<TsDecorator> decorators) {
Expand Down Expand Up @@ -271,7 +288,7 @@ private void emitCallable(TsCallableModel method) {
indent--;
writeIndentedLine("}");
} else {
writeIndentedLine(signature + ";");
writeIndentedLine(signature + getTrailingChar());
}
}

Expand Down Expand Up @@ -326,7 +343,7 @@ private void emitStatements(List<TsStatement> statements) {

private void emitReturnStatement(TsReturnStatement returnStatement) {
if (returnStatement.getExpression() != null) {
writeIndentedLine("return " + returnStatement.getExpression().format(settings) + ";");
writeIndentedLine("return " + returnStatement.getExpression().format(settings) + getTrailingChar());
} else {
writeIndentedLine("return;");
}
Expand All @@ -347,7 +364,7 @@ private void emitIfStatement(TsIfStatement ifStatement) {
}

private void emitExpressionStatement(TsExpressionStatement expressionStatement) {
writeIndentedLine(expressionStatement.getExpression().format(settings) + ";");
writeIndentedLine(expressionStatement.getExpression().format(settings) + getTrailingChar());
}

private void emitVariableDeclarationStatement(TsVariableDeclarationStatement variableDeclarationStatement) {
Expand All @@ -356,7 +373,7 @@ private void emitVariableDeclarationStatement(TsVariableDeclarationStatement var
+ variableDeclarationStatement.getName()
+ (variableDeclarationStatement.getType() != null ? ": " + variableDeclarationStatement.getType().format(settings) : "")
+ (variableDeclarationStatement.getInitializer() != null ? " = " + variableDeclarationStatement.getInitializer().format(settings) : "")
+ ";"
+ getTrailingChar()
);
}

Expand Down Expand Up @@ -385,7 +402,7 @@ private void emitTypeAlias(TsAliasModel alias, boolean exportKeyword) {
final String genericParameters = alias.getTypeParameters().isEmpty()
? ""
: "<" + formatList(settings, alias.getTypeParameters()) + ">";
writeIndentedLine(exportKeyword, "type " + alias.getName().getSimpleName() + genericParameters + " = " + alias.getDefinition().format(settings) + ";");
writeIndentedLine(exportKeyword, "type " + alias.getName().getSimpleName() + genericParameters + " = " + alias.getDefinition().format(settings) + getTrailingChar());
}

private void emitLiteralEnum(TsEnumModel enumModel, boolean exportKeyword, boolean declareKeyword) {
Expand Down Expand Up @@ -438,7 +455,7 @@ public void writeIndentedLine(String line) {
private void emitUmdNamespace() {
if (settings.umdNamespace != null) {
writeNewLine();
writeIndentedLine("export as namespace " + settings.umdNamespace + ";");
writeIndentedLine("export as namespace " + settings.umdNamespace + getTrailingChar());
}
}

Expand Down Expand Up @@ -502,4 +519,7 @@ private void close() {
}
}

private String getTrailingChar() {
return settings.disableTrailingSemiColon ? "" : ";";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gradle.api.DefaultTask;
import org.gradle.api.Task;
Expand Down Expand Up @@ -129,6 +130,8 @@ public class GenerateTask extends DefaultTask {
public boolean jackson2ModuleDiscovery;
public List<String> jackson2Modules;
public Logger.Level loggingLevel;
public boolean disableTrailingSemiColon;
public Map<String, List<String>> injectCustomProperties;

private Settings createSettings(URLClassLoader classLoader) {
final Settings settings = new Settings();
Expand Down Expand Up @@ -212,6 +215,8 @@ private Settings createSettings(URLClassLoader classLoader) {
settings.jackson2ModuleDiscovery = jackson2ModuleDiscovery;
settings.loadJackson2Modules(classLoader, jackson2Modules);
settings.classLoader = classLoader;
settings.disableTrailingSemiColon = disableTrailingSemiColon;
settings.injectCustomProperties = injectCustomProperties;
return settings;
}

Expand Down