Skip to content

Commit 859c97f

Browse files
committed
Add ability to ts-ignore a default import
When importing package.json outside srcDir, TypeScript will fail checks even though compiled JS will work fine. Annotating the import with @ts-ignore, and using eslint-disable-line to prevent import reordering from separating the ts-ignore comment from the line being ignored, allows us to suppress these errors without reorganizing the generated module and without postprocessing the generator output.
1 parent c6f19cd commit 859c97f

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ImportDeclarations.java

+42-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Set;
2222
import java.util.TreeMap;
2323
import software.amazon.smithy.codegen.core.CodegenException;
24+
import software.amazon.smithy.utils.Pair;
2425
import software.amazon.smithy.utils.SmithyInternalApi;
2526

2627
/**
@@ -30,7 +31,7 @@
3031
final class ImportDeclarations {
3132

3233
private final Path relativize;
33-
private final Map<String, String> defaultImports = new TreeMap<>();
34+
private final Map<String, Pair<String, Ignore>> defaultImports = new TreeMap<>();
3435
private final Map<String, Map<String, String>> namedImports = new TreeMap<>();
3536

3637
ImportDeclarations(String relativize) {
@@ -43,10 +44,18 @@ final class ImportDeclarations {
4344
}
4445

4546
ImportDeclarations addDefaultImport(String name, String module) {
47+
return addDefaultImport(name, module, Ignore.notIgnored());
48+
}
49+
50+
ImportDeclarations addIgnoredDefaultImport(String name, String module, String reason) {
51+
return addDefaultImport(name, module, Ignore.ignored(reason));
52+
}
53+
54+
private ImportDeclarations addDefaultImport(String name, String module, Ignore ignore) {
4655
module = getRelativizedModule(relativize, module);
4756

4857
if (!module.isEmpty() && (relativize == null || !module.equals(relativize.toString()))) {
49-
defaultImports.put(module, name);
58+
defaultImports.put(module, new Pair<>(name, ignore));
5059
}
5160

5261
return this;
@@ -71,14 +80,22 @@ public String toString() {
7180
StringBuilder result = new StringBuilder();
7281

7382
if (!defaultImports.isEmpty()) {
74-
for (Map.Entry<String, String> importEntry : defaultImports.entrySet()) {
83+
for (Map.Entry<String, Pair<String, Ignore>> importEntry : defaultImports.entrySet()) {
84+
boolean ignore = importEntry.getValue().getRight().ignore;
85+
if (ignore) {
86+
result.append("// @ts-ignore: ").append(importEntry.getValue().getRight().reason).append("\n");
87+
}
7588
result.append("import ")
76-
.append(importEntry.getValue())
89+
.append(importEntry.getValue().getLeft())
7790
.append(" from \"")
7891
.append(importEntry.getKey())
79-
.append("\";\n");
92+
.append("\";");
93+
if (ignore) {
94+
result.append(" // eslint-disable-line");
95+
}
96+
result.append('\n');
8097
}
81-
result.append("\n");
98+
result.append('\n');
8299
}
83100

84101
if (!namedImports.isEmpty()) {
@@ -133,4 +150,23 @@ private static String getRelativizedModule(Path relativize, String module) {
133150
}
134151
return module;
135152
}
153+
154+
private static final class Ignore {
155+
final boolean ignore;
156+
final String reason;
157+
158+
private Ignore(boolean ignore, String reason) {
159+
this.ignore = ignore;
160+
this.reason = reason;
161+
}
162+
163+
static Ignore notIgnored() {
164+
return new Ignore(false, null);
165+
}
166+
167+
static Ignore ignored(String reason) {
168+
return new Ignore(true, reason);
169+
}
170+
171+
}
136172
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/TypeScriptWriter.java

+13
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ public TypeScriptWriter addDefaultImport(String name, String from) {
165165
return this;
166166
}
167167

168+
/**
169+
* default import from a module, annotated with @ts-ignore.
170+
*
171+
* @param name Name of default import.
172+
* @param from Module to default import from.
173+
* @param reason The reason for ignoring the import
174+
* @return Returns the writer.
175+
*/
176+
public TypeScriptWriter addIgnoredDefaultImport(String name, String from, String reason) {
177+
imports.addIgnoredDefaultImport(name, from, reason);
178+
return this;
179+
}
180+
168181
/**
169182
* Imports a type using an alias from a module only if necessary.
170183
*

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/ImportDeclarationsTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ public void canImportDefaultImport() {
108108
assertThat(result, containsString("import foo from \"@types/foo\";"));
109109
}
110110

111+
@Test
112+
public void canImportDefaultImportWithIgnore() {
113+
ImportDeclarations declarations = new ImportDeclarations("/foo/bar");
114+
declarations.addIgnoredDefaultImport("foo", "@types/foo", "I want to");
115+
String result = declarations.toString();
116+
117+
assertThat(result, containsString("// @ts-ignore: I want to\nimport foo from \"@types/foo\";"));
118+
}
119+
120+
111121
@Test
112122
public void canImportDefaultImportWithNamedImport() {
113123
ImportDeclarations declarations = new ImportDeclarations("/foo/bar");

0 commit comments

Comments
 (0)