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,40 @@ 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
+ """
162
+ The import %s does not correspond to a registered dependency.
163
+ TypeScriptWriter::addDependency() is required before ::addImport(), or call ::addImportUnchecked().
164
+ """ .formatted (from )
165
+ );
166
+ }
167
+ }
168
+
169
+ getImportContainer ().addImport (name , as , from );
170
+ return this ;
171
+ }
172
+
173
+ /**
174
+ * Imports a type using an alias from a module only if necessary.
175
+ * @return Returns the writer.
176
+ *
177
+ * @deprecated Use {@link TypeScriptWriter#addImport(String, String, TypeScriptDependency)} addImport}
178
+ */
179
+ @ Deprecated
180
+ public TypeScriptWriter addImportUnchecked (String name , String as , String from ) {
117
181
getImportContainer ().addImport (name , as , from );
118
182
return this ;
119
183
}
@@ -126,6 +190,15 @@ public TypeScriptWriter addImport(String name, String as, String from) {
126
190
* @param from PackageContainer to import the type from.
127
191
* @return Returns the writer.
128
192
*/
193
+ public TypeScriptWriter addImport (String name , String as , Dependency from ) {
194
+ addDependency (from );
195
+ return this .addImport (name , as , from .getPackageName ());
196
+ }
197
+
198
+ /**
199
+ * @deprecated use {@link #addImport(String name, String as, Dependency from)}.
200
+ */
201
+ @ Deprecated
129
202
public TypeScriptWriter addImport (String name , String as , PackageContainer from ) {
130
203
return this .addImport (name , as , from .getPackageName ());
131
204
}
0 commit comments