16
16
package software .amazon .smithy .typescript .codegen ;
17
17
18
18
import java .nio .file .Path ;
19
+ import java .util .List ;
20
+ import java .util .Set ;
19
21
import java .util .function .BiFunction ;
20
22
import java .util .function .UnaryOperator ;
21
23
import software .amazon .smithy .codegen .core .CodegenException ;
22
24
import software .amazon .smithy .codegen .core .Symbol ;
25
+ import software .amazon .smithy .codegen .core .SymbolDependency ;
23
26
import software .amazon .smithy .codegen .core .SymbolReference ;
24
27
import software .amazon .smithy .codegen .core .SymbolWriter ;
25
28
import software .amazon .smithy .model .Model ;
29
32
import software .amazon .smithy .model .traits .DeprecatedTrait ;
30
33
import software .amazon .smithy .model .traits .DocumentationTrait ;
31
34
import software .amazon .smithy .model .traits .InternalTrait ;
35
+ import software .amazon .smithy .utils .SetUtils ;
32
36
import software .amazon .smithy .utils .SmithyUnstableApi ;
33
37
import software .amazon .smithy .utils .StringUtils ;
34
38
49
53
@ SmithyUnstableApi
50
54
public final class TypeScriptWriter extends SymbolWriter <TypeScriptWriter , ImportDeclarations > {
51
55
public static final String CODEGEN_INDICATOR = "// smithy-typescript generated code\n " ;
56
+ public static final Set <String > EXEMPT_DEPENDENCIES = SetUtils .of (
57
+ "buffer" ,
58
+ "child_process" ,
59
+ "crypto" ,
60
+ "dns" ,
61
+ "dns/promises" ,
62
+ "events" ,
63
+ "fs" ,
64
+ "fs/promises" ,
65
+ "http" ,
66
+ "http2" ,
67
+ "https" ,
68
+ "os" ,
69
+ "path" ,
70
+ "path/posix" ,
71
+ "path/win32" ,
72
+ "process" ,
73
+ "stream" ,
74
+ "stream/consumers" ,
75
+ "stream/promises" ,
76
+ "stream/web" ,
77
+ "tls" ,
78
+ "url" ,
79
+ "util" ,
80
+ "zlib"
81
+ );
52
82
53
83
private final String moduleName ;
54
84
private final boolean withAttribution ;
@@ -114,6 +144,38 @@ public TypeScriptWriter addIgnoredDefaultImport(String name, String from, String
114
144
*/
115
145
@ Deprecated
116
146
public TypeScriptWriter addImport (String name , String as , String from ) {
147
+ final boolean isPackageImport =
148
+ from .startsWith ("@" )
149
+ || (!from .startsWith ("/" ) && !from .startsWith ("." ));
150
+
151
+ if (isPackageImport ) {
152
+ String [] packageNameSegments = from .split ("/" );
153
+ String packageName =
154
+ from .startsWith ("@" )
155
+ ? packageNameSegments [0 ] + "/" + packageNameSegments [1 ]
156
+ : packageNameSegments [0 ];
157
+ List <SymbolDependency > dependencies = getDependencies ();
158
+ if (!EXEMPT_DEPENDENCIES .contains (packageName )
159
+ && dependencies .stream ().noneMatch (dep -> dep .getPackageName ().equals (packageName ))) {
160
+ throw new CodegenException ("""
161
+ The import %s does not correspond to a registered dependency.
162
+ TypeScriptWriter::addDependency() is required before ::addImport(), or call ::addImportUnchecked().
163
+ """ .formatted (from ));
164
+ }
165
+ }
166
+
167
+ getImportContainer ().addImport (name , as , from );
168
+ return this ;
169
+ }
170
+
171
+ /**
172
+ * Imports a type using an alias from a module only if necessary.
173
+ * @return Returns the writer.
174
+ *
175
+ * @deprecated Use {@link TypeScriptWriter#addImport(String, String, TypeScriptDependency)} addImport}
176
+ */
177
+ @ Deprecated
178
+ public TypeScriptWriter addImportUnchecked (String name , String as , String from ) {
117
179
getImportContainer ().addImport (name , as , from );
118
180
return this ;
119
181
}
@@ -126,6 +188,15 @@ public TypeScriptWriter addImport(String name, String as, String from) {
126
188
* @param from PackageContainer to import the type from.
127
189
* @return Returns the writer.
128
190
*/
191
+ public TypeScriptWriter addImport (String name , String as , Dependency from ) {
192
+ addDependency (from );
193
+ return this .addImport (name , as , from .getPackageName ());
194
+ }
195
+
196
+ /**
197
+ * @deprecated use {@link #addImport(String name, String as, Dependency from)}.
198
+ */
199
+ @ Deprecated
129
200
public TypeScriptWriter addImport (String name , String as , PackageContainer from ) {
130
201
return this .addImport (name , as , from .getPackageName ());
131
202
}
0 commit comments