17
17
18
18
import java .nio .file .Path ;
19
19
import java .nio .file .Paths ;
20
- import java .util .Map ;
21
- import java .util .TreeMap ;
22
20
import java .util .function .BiFunction ;
23
21
import software .amazon .smithy .codegen .core .CodegenException ;
24
22
import software .amazon .smithy .codegen .core .Symbol ;
39
37
* module paths against the moduleName of the writer. Module names that
40
38
* start with anything other than "." (e.g., "@", "/", etc.) are never
41
39
* relativized.
40
+ *
41
+ * TODO: Make this public when it's stable.
42
42
*/
43
43
final class TypeScriptWriter extends CodeWriter {
44
44
45
45
private final Path moduleName ;
46
- private final Map < String , Map < String , String >> imports = new TreeMap <>() ;
46
+ private final ImportDeclarations imports ;
47
47
48
48
TypeScriptWriter (String moduleName ) {
49
49
this .moduleName = Paths .get (moduleName );
50
+ imports = new ImportDeclarations (moduleName );
50
51
51
52
setIndentText (" " );
52
53
trimTrailingSpaces (true );
@@ -62,21 +63,30 @@ final class TypeScriptWriter extends CodeWriter {
62
63
*/
63
64
TypeScriptWriter addImport (Symbol symbol ) {
64
65
if (!symbol .getNamespace ().isEmpty ()) {
65
- addImport (symbol .getName (), symbol .getNamespace ());
66
+ addImport (symbol .getName (), symbol .getName (), symbol .getNamespace ());
67
+ for (SymbolReference reference : symbol .getReferences ()) {
68
+ addImport (reference );
69
+ }
66
70
}
67
71
68
72
return this ;
69
73
}
70
74
71
75
/**
72
- * Imports a type from a module only if necessary .
76
+ * Imports a symbol reference .
73
77
*
74
- * @param name Type to import.
75
- * @param from Module to import the type from.
78
+ * @param reference Symbol reference to import.
76
79
* @return Returns the writer.
77
80
*/
78
- TypeScriptWriter addImport (String name , String from ) {
79
- return addImport (name , name , from );
81
+ TypeScriptWriter addImport (SymbolReference reference ) {
82
+ // If there's no alias, then just import the symbol normally.
83
+ if (reference .getAlias ().equals (reference .getSymbol ().getName ())) {
84
+ return addImport (reference .getSymbol ());
85
+ }
86
+
87
+ // Symbols with references are always imported since they don't
88
+ // conflict and must be imported in order for the code to work.
89
+ return addImport (reference .getSymbol ().getName (), reference .getAlias (), reference .getSymbol ().getNamespace ());
80
90
}
81
91
82
92
/**
@@ -88,15 +98,7 @@ TypeScriptWriter addImport(String name, String from) {
88
98
* @return Returns the writer.
89
99
*/
90
100
TypeScriptWriter addImport (String name , String as , String from ) {
91
- if (!from .startsWith ("@" ) && !from .startsWith ("/" )) {
92
- // A relative import is resolved against the current file.
93
- from = moduleName .relativize (Paths .get (from )).toString ();
94
- }
95
-
96
- if (!from .equals (moduleName .toString ()) && !from .isEmpty ()) {
97
- imports .computeIfAbsent (from , m -> new TreeMap <>()).put (as , name );
98
- }
99
-
101
+ imports .addImport (name , as , from );
100
102
return this ;
101
103
}
102
104
@@ -161,41 +163,7 @@ boolean writeMemberDocs(Model model, MemberShape member) {
161
163
162
164
@ Override
163
165
public String toString () {
164
- StringBuilder result = new StringBuilder ();
165
-
166
- if (!imports .isEmpty ()) {
167
- for (Map .Entry <String , Map <String , String >> entry : imports .entrySet ()) {
168
- String module = entry .getKey ();
169
- Map <String , String > imports = entry .getValue ();
170
-
171
- if (imports .size () == 1 ) {
172
- Map .Entry <String , String > singleEntry = imports .entrySet ().iterator ().next ();
173
- result .append ("import { " )
174
- .append (createImportStatement (singleEntry ))
175
- .append (" } from \" " )
176
- .append (module )
177
- .append ("\" ;\n " );
178
- } else {
179
- result .append ("import {\n " );
180
- for (Map .Entry <String , String > importEntry : imports .entrySet ()) {
181
- result .append (" " );
182
- result .append (createImportStatement (importEntry ));
183
- result .append (",\n " );
184
- }
185
- result .append ("} from \" " ).append (module ).append ("\" ;\n " );
186
- }
187
- }
188
- result .append ("\n " );
189
- }
190
-
191
- result .append (super .toString ());
192
- return result .toString ();
193
- }
194
-
195
- private String createImportStatement (Map .Entry <String , String > entry ) {
196
- return entry .getKey ().equals (entry .getValue ())
197
- ? entry .getKey ()
198
- : entry .getValue () + " as " + entry .getKey ();
166
+ return imports .toString () + super .toString ();
199
167
}
200
168
201
169
/**
@@ -213,7 +181,7 @@ public String apply(Object type, String indent) {
213
181
214
182
for (SymbolReference reference : typeSymbol .getReferences ()) {
215
183
if (reference .hasOption (SymbolReference .ContextOption .USE )) {
216
- addImport (reference . getSymbol () );
184
+ addImport (reference );
217
185
}
218
186
}
219
187
0 commit comments