Skip to content

Commit 0252e92

Browse files
experimental enum inlining (closes #52), improved TsType formating
1 parent 345e61b commit 0252e92

File tree

9 files changed

+96
-23
lines changed

9 files changed

+96
-23
lines changed

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/GenericsTypeProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public GenericReferenceType(Symbol symbol, List<TsType> typeArguments) {
6060
}
6161

6262
@Override
63-
public String toString() {
64-
return symbol + "<" + Utils.join(typeArguments, ", ") + ">";
63+
public String format(Settings settings) {
64+
return symbol + "<" + Utils.join(format(typeArguments, settings), ", ") + ">";
6565
}
6666
}
6767

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/Settings.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Settings {
3535
public List<File> javadocXmlFiles = null;
3636
public List<EmitterExtension> extensions = new ArrayList<>();
3737
public List<Class<? extends Annotation>> optionalAnnotations = new ArrayList<>();
38+
public boolean experimentalInlineEnums = false;
3839

3940

4041
public void loadCustomTypeProcessor(ClassLoader classLoader, String customTypeProcessor) {

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/TsType.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,20 @@ public TsType.OptionalType optional() {
3232
return new TsType.OptionalType(this);
3333
}
3434

35+
public abstract String format(Settings settings);
36+
37+
protected static List<String> format(List<TsType> types, Settings settings) {
38+
final List<String> formatted = new ArrayList<>();
39+
for (TsType type : types) {
40+
formatted.add(type.format(settings));
41+
}
42+
return formatted;
43+
}
44+
3545
@Override
36-
public abstract String toString();
46+
public String toString() {
47+
return format(new Settings());
48+
}
3749

3850
public static class BasicType extends TsType {
3951

@@ -44,7 +56,7 @@ public BasicType(String name) {
4456
}
4557

4658
@Override
47-
public String toString() {
59+
public String format(Settings settings) {
4860
return name;
4961
}
5062
}
@@ -61,7 +73,7 @@ public ReferenceType(Symbol symbol) {
6173
}
6274

6375
@Override
64-
public String toString() {
76+
public String format(Settings settings) {
6577
return symbol.toString();
6678
}
6779

@@ -82,8 +94,8 @@ public BasicArrayType(TsType elementType) {
8294
}
8395

8496
@Override
85-
public String toString() {
86-
return elementType + "[]";
97+
public String format(Settings settings) {
98+
return elementType.format(settings) + "[]";
8799
}
88100

89101
}
@@ -99,8 +111,8 @@ public IndexedArrayType(TsType indexType, TsType elementType) {
99111
}
100112

101113
@Override
102-
public String toString() {
103-
return "{ [index: " + indexType + "]: " + elementType + " }";
114+
public String format(Settings settings) {
115+
return "{ [index: " + indexType.format(settings) + "]: " + elementType.format(settings) + " }";
104116
}
105117

106118
}
@@ -114,8 +126,8 @@ public UnionType(List<? extends TsType> types) {
114126
}
115127

116128
@Override
117-
public String toString() {
118-
return Utils.join(types, " | ");
129+
public String format(Settings settings) {
130+
return Utils.join(format(types, settings), " | ");
119131
}
120132

121133
}
@@ -124,13 +136,13 @@ public static class StringLiteralType extends TsType {
124136

125137
public final String literal;
126138

127-
public StringLiteralType(java.lang.String literal) {
139+
public StringLiteralType(String literal) {
128140
this.literal = literal;
129141
}
130142

131143
@Override
132-
public String toString() {
133-
return "\"" + literal + "\"";
144+
public String format(Settings settings) {
145+
return settings.quotes + literal + settings.quotes;
134146
}
135147

136148
}
@@ -144,8 +156,8 @@ public OptionalType(TsType type) {
144156
}
145157

146158
@Override
147-
public String toString() {
148-
return type.toString();
159+
public String format(Settings settings) {
160+
return type.format(settings);
149161
}
150162

151163
}

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/ModelCompiler.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public TsModel javaToTypeScript(Model model) {
4343
TsModel tsModel = processModel(symbolTable, model);
4444
tsModel = transformDates(symbolTable, tsModel);
4545
tsModel = transformEnums(tsModel);
46+
if (settings.experimentalInlineEnums) {
47+
tsModel = inlineEnums(tsModel, symbolTable);
48+
}
4649
symbolTable.resolveSymbolNames(settings);
4750
return tsModel;
4851
}
@@ -150,6 +153,31 @@ private TsModel transformEnums(TsModel tsModel) {
150153
return tsModel.setTypeAliases(new ArrayList<>(typeAliases));
151154
}
152155

156+
private TsModel inlineEnums(final TsModel tsModel, final SymbolTable symbolTable) {
157+
final Set<TsAliasModel> inlinedAliases = new LinkedHashSet<>();
158+
final TsModel newTsModel = transformBeanPropertyTypes(tsModel, new TsType.Transformer() {
159+
@Override
160+
public TsType transform(TsType tsType) {
161+
if (tsType instanceof TsType.EnumReferenceType) {
162+
final TsType.ReferenceType reference = (TsType.ReferenceType) tsType;
163+
final Class<?> cls = symbolTable.getSymbolClass(reference.symbol);
164+
if (cls != null) {
165+
for (TsAliasModel alias : tsModel.getTypeAliases()) {
166+
if (alias.getOrigin() == cls) {
167+
inlinedAliases.add(alias);
168+
return alias.getDefinition();
169+
}
170+
}
171+
}
172+
}
173+
return tsType;
174+
}
175+
});
176+
final ArrayList<TsAliasModel> aliases = new ArrayList<>(tsModel.getTypeAliases());
177+
aliases.removeAll(inlinedAliases);
178+
return newTsModel.setTypeAliases(aliases);
179+
}
180+
153181
private static TsModel transformBeanPropertyTypes(TsModel tsModel, TsType.Transformer transformer) {
154182
final List<TsBeanModel> newBeans = new ArrayList<>();
155183
for (TsBeanModel bean : tsModel.getBeans()) {

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/compiler/SymbolTable.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ public Symbol getSymbol(Class<?> cls) {
2222
return symbols.get(cls);
2323
}
2424

25+
public Class<?> getSymbolClass(Symbol symbol) {
26+
for (Map.Entry<Class<?>, Symbol> entry : symbols.entrySet()) {
27+
if (entry.getValue() == symbol) {
28+
return entry.getKey();
29+
}
30+
}
31+
return null;
32+
}
33+
2534
public Symbol getSyntheticSymbol(String name) {
2635
if (!syntheticSymbols.containsKey(name)) {
2736
syntheticSymbols.put(name, new Symbol(name));

typescript-generator-core/src/main/java/cz/habarta/typescript/generator/emitter/Emitter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private void emitProperty(TsPropertyModel property) {
117117
emitComments(property.getComments());
118118
final TsType tsType = property.getTsType();
119119
final String questionMark = settings.declarePropertiesAsOptional || (tsType instanceof TsType.OptionalType) ? "?" : "";
120-
writeIndentedLine(toPropertyName(property.getName()) + questionMark + ": " + tsType + ";");
120+
writeIndentedLine(toPropertyName(property.getName()) + questionMark + ": " + tsType.format(settings) + ";");
121121
}
122122

123123
private String toPropertyName(String name) {
@@ -150,7 +150,7 @@ private void emitTypeAliases(TsModel model, boolean exportKeyword) {
150150
for (TsAliasModel alias : aliases) {
151151
writeNewLine();
152152
emitComments(alias.getComments());
153-
writeIndentedLine(exportKeyword, "type " + alias.getName() + " = " + alias.getDefinition() + ";");
153+
writeIndentedLine(exportKeyword, "type " + alias.getName() + " = " + alias.getDefinition().format(settings) + ";");
154154
}
155155
}
156156

typescript-generator-core/src/test/java/cz/habarta/typescript/generator/EnumTest.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,42 @@ public class EnumTest {
1111
public void test() {
1212
final Settings settings = TestUtils.settings();
1313
final String actual = new TypeScriptGenerator(settings).generateTypeScript(Input.from(AClass.class));
14-
final String expected =
14+
final String expected = (
1515
"\n" +
1616
"interface AClass {\n" +
1717
" direction: Direction;\n" +
1818
"}\n" +
1919
"\n" +
2020
"type Direction = 'North' | 'East' | 'South' | 'West';\n"
21-
.replace("'", "\"");
21+
).replace("'", "\"");
2222
assertEquals(expected, actual);
2323
}
2424

2525
@Test
2626
public void testSingleEnum() {
2727
final Settings settings = TestUtils.settings();
2828
final String actual = new TypeScriptGenerator(settings).generateTypeScript(Input.from(Direction.class));
29-
final String expected =
29+
final String expected = (
3030
"\n" +
3131
"type Direction = 'North' | 'East' | 'South' | 'West';\n"
32-
.replace("'", "\"");
32+
).replace("'", "\"");
3333
assertEquals(expected, actual);
3434
}
3535

36+
@Test
37+
public void inlineEnumTest() {
38+
final Settings settings = TestUtils.settings();
39+
settings.quotes = "'";
40+
settings.experimentalInlineEnums = true;
41+
final String output = new TypeScriptGenerator(settings).generateTypeScript(Input.from(AClass.class));
42+
final String expected =
43+
"\n" +
44+
"interface AClass {\n" +
45+
" direction: 'North' | 'East' | 'South' | 'West';\n" +
46+
"}\n";
47+
assertEquals(expected, output);
48+
}
49+
3650
private static class AClass {
3751
public Direction direction;
3852
}

typescript-generator-gradle-plugin/src/main/java/cz/habarta/typescript/generator/gradle/GenerateTask.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class GenerateTask extends DefaultTask {
3535
public List<File> javadocXmlFiles;
3636
public List<String> extensions;
3737
public List<String> optionalAnnotations;
38-
38+
public boolean experimentalInlineEnums;
3939

4040
@TaskAction
4141
public void generate() throws Exception {
@@ -83,6 +83,7 @@ public void generate() throws Exception {
8383
settings.javadocXmlFiles = javadocXmlFiles;
8484
settings.loadExtensions(classLoader, extensions);
8585
settings.loadOptionalAnnotations(classLoader, optionalAnnotations);
86+
settings.experimentalInlineEnums = experimentalInlineEnums;
8687
settings.validateFileName(new File(outputFile));
8788

8889
// TypeScriptGenerator

typescript-generator-maven-plugin/src/main/java/cz/habarta/typescript/generator/maven/GenerateMojo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,13 @@ public class GenerateMojo extends AbstractMojo {
192192
@Parameter
193193
private List<String> optionalAnnotations;
194194

195+
/**
196+
* Defines enum type on places where the enum is used (inline).
197+
* (Without this flag enum type is created once as type alias and is referenced from places where the enum is used.)
198+
*/
199+
@Parameter
200+
private boolean experimentalInlineEnums;
201+
195202
@Parameter(defaultValue = "${project}", readonly = true, required = true)
196203
private MavenProject project;
197204

@@ -230,6 +237,7 @@ public void execute() {
230237
settings.javadocXmlFiles = javadocXmlFiles;
231238
settings.loadExtensions(classLoader, extensions);
232239
settings.loadOptionalAnnotations(classLoader, optionalAnnotations);
240+
settings.experimentalInlineEnums = experimentalInlineEnums;
233241
settings.validateFileName(outputFile);
234242

235243
// TypeScriptGenerator

0 commit comments

Comments
 (0)