Skip to content

Commit 4c714a9

Browse files
setting for sorting type declarations (properties retains original order)
1 parent 0446cfb commit 4c714a9

File tree

6 files changed

+21
-17
lines changed

6 files changed

+21
-17
lines changed

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
@@ -19,5 +19,6 @@ public class Settings {
1919
public DateMapping mapDate = DateMapping.asDate;
2020
public TypeProcessor customTypeProcessor = null;
2121
public boolean sortDeclarations = false;
22+
public boolean sortTypeDeclarations = false;
2223
public boolean noFileComment = false;
2324
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ private void emitFileComment() {
4040
}
4141

4242
private void emitModule(TsModel model) {
43-
if (settings.sortDeclarations) {
44-
model.sort();
45-
}
4643
if (settings.module != null) {
4744
writeNewLine();
4845
writeIndentedLine("declare module '" + settings.module + "' {");
@@ -78,12 +75,20 @@ private void emitObjects(TsModel model) {
7875

7976
private void emitInterfaces(TsModel model) {
8077
String exportPrefix = forceExportKeyword ? "export " : "";
81-
for (TsBeanModel bean : model.getBeans()) {
78+
final List<TsBeanModel> beans = model.getBeans();
79+
if (settings.sortDeclarations || settings.sortTypeDeclarations) {
80+
Collections.sort(beans);
81+
}
82+
for (TsBeanModel bean : beans) {
8283
writeNewLine();
8384
final String parent = bean.getParent() != null ? " extends " + bean.getParent() : "";
8485
writeIndentedLine(exportPrefix + "interface " + bean.getName() + parent + " {");
8586
indent++;
86-
for (TsPropertyModel property : bean.getProperties()) {
87+
final List<TsPropertyModel> properties = bean.getProperties();
88+
if (settings.sortDeclarations) {
89+
Collections.sort(properties);
90+
}
91+
for (TsPropertyModel property : properties) {
8792
emitProperty(property);
8893
}
8994
indent--;

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
package cz.habarta.typescript.generator.emitter;
33

44
import cz.habarta.typescript.generator.*;
5-
import java.util.ArrayList;
6-
import java.util.Collections;
7-
import java.util.List;
5+
import java.util.*;
86

97

108
public class TsBeanModel implements Comparable<TsBeanModel> {
@@ -40,7 +38,4 @@ public int compareTo(TsBeanModel o) {
4038
return name.toString().compareTo(o.name.toString());
4139
}
4240

43-
public void sort() {
44-
Collections.sort(properties);
45-
}
4641
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,4 @@ public LinkedHashSet<TsType.AliasType> getTypeAliases() {
1818
return typeAliases;
1919
}
2020

21-
public void sort() {
22-
for (TsBeanModel bean : beans) {
23-
bean.sort();
24-
}
25-
Collections.sort(beans);
26-
}
2721
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class GenerateTask extends DefaultTask {
2727
public DateMapping mapDate;
2828
public String customTypeProcessor;
2929
public boolean sortDeclarations;
30+
public boolean sortTypeDeclarations;
3031
public boolean noFileComment;
3132

3233

@@ -65,6 +66,7 @@ public void generate() throws Exception {
6566
settings.customTypeProcessor = (TypeProcessor) classLoader.loadClass(customTypeProcessor).newInstance();
6667
}
6768
settings.sortDeclarations = sortDeclarations;
69+
settings.sortTypeDeclarations = sortTypeDeclarations;
6870
settings.noFileComment = noFileComment;
6971

7072
// TypeScriptGenerator

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ public class GenerateMojo extends AbstractMojo {
128128
@Parameter
129129
private boolean sortDeclarations;
130130

131+
/**
132+
* If true TypeScript type declarations (interfaces) will be sorted alphabetically.
133+
*/
134+
@Parameter
135+
private boolean sortTypeDeclarations;
136+
131137
/**
132138
* If true generated file will not contain comment at the top.
133139
* By default there is a comment with timestamp and typescript-generator version.
@@ -167,6 +173,7 @@ public void execute() {
167173
settings.customTypeProcessor = (TypeProcessor) classLoader.loadClass(customTypeProcessor).newInstance();
168174
}
169175
settings.sortDeclarations = sortDeclarations;
176+
settings.sortTypeDeclarations = sortTypeDeclarations;
170177
settings.noFileComment = noFileComment;
171178

172179
// TypeScriptGenerator

0 commit comments

Comments
 (0)