Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to ts-ignore a default import #445

Merged
merged 1 commit into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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