Skip to content

Commit

Permalink
Add ability to ts-ignore a default import
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
adamthom-amzn committed Oct 6, 2021
1 parent c6f19cd commit dd1ef82
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Set;
import java.util.TreeMap;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.utils.Pair;
import software.amazon.smithy.utils.SmithyInternalApi;

/**
Expand All @@ -30,7 +31,7 @@
final class ImportDeclarations {

private final Path relativize;
private final Map<String, String> defaultImports = new TreeMap<>();
private final Map<String, Pair<String, Ignore>> defaultImports = new TreeMap<>();
private final Map<String, Map<String, String>> namedImports = new TreeMap<>();

ImportDeclarations(String relativize) {
Expand All @@ -43,10 +44,18 @@ final class ImportDeclarations {
}

ImportDeclarations addDefaultImport(String name, String module) {
return addDefaultImport(name, module, Ignore.notIgnored());
}

ImportDeclarations addIgnoredDefaultImport(String name, String module, String reason) {
return addDefaultImport(name, module, Ignore.ignored(reason));
}

private ImportDeclarations addDefaultImport(String name, String module, Ignore ignore) {
module = getRelativizedModule(relativize, module);

if (!module.isEmpty() && (relativize == null || !module.equals(relativize.toString()))) {
defaultImports.put(module, name);
defaultImports.put(module, new Pair<>(name, ignore));
}

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

if (!defaultImports.isEmpty()) {
for (Map.Entry<String, String> importEntry : defaultImports.entrySet()) {
for (Map.Entry<String, Pair<String, Ignore>> importEntry : defaultImports.entrySet()) {
boolean ignore = importEntry.getValue().getRight().ignore;
if (ignore) {
result.append("// @ts-ignore: ").append(importEntry.getValue().getRight().reason).append("\n");
}
result.append("import ")
.append(importEntry.getValue())
.append(importEntry.getValue().getLeft())
.append(" from \"")
.append(importEntry.getKey())
.append("\";\n");
.append("\";");
if (ignore) {
result.append(" // eslint-disable-line");
}
result.append('\n');
}
result.append("\n");
result.append('\n');
}

if (!namedImports.isEmpty()) {
Expand Down Expand Up @@ -133,4 +150,23 @@ private static String getRelativizedModule(Path relativize, String module) {
}
return module;
}

private static final class Ignore {
final boolean ignore;
final String reason;

private Ignore(boolean ignore, String reason) {
this.ignore = ignore;
this.reason = reason;
}

static Ignore notIgnored() {
return new Ignore(false, null);
}

static Ignore ignored(String reason) {
return new Ignore(true, reason);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,19 @@ public TypeScriptWriter addDefaultImport(String name, String from) {
return this;
}

/**
* default import from a module, annotated with @ts-ignore.
*
* @param name Name of default import.
* @param from Module to default import from.
* @param reason The reason for ignoring the import
* @return Returns the writer.
*/
public TypeScriptWriter addIgnoredDefaultImport(String name, String from, String reason) {
imports.addIgnoredDefaultImport(name, from, reason);
return this;
}

/**
* Imports a type using an alias from a module only if necessary.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ public void canImportDefaultImport() {
assertThat(result, containsString("import foo from \"@types/foo\";"));
}

@Test
public void canImportDefaultImportWithIgnore() {
ImportDeclarations declarations = new ImportDeclarations("/foo/bar");
declarations.addIgnoredDefaultImport("foo", "@types/foo", "I want to");
String result = declarations.toString();

assertThat(result, containsString("// @ts-ignore: I want to\nimport foo from \"@types/foo\"; // eslint-disable-line"));
}


@Test
public void canImportDefaultImportWithNamedImport() {
ImportDeclarations declarations = new ImportDeclarations("/foo/bar");
Expand Down

0 comments on commit dd1ef82

Please sign in to comment.