forked from smithy-lang/smithy-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: check dependencies when adding imports (smithy-lang#1239)
* fix: check dependencies when adding imports * avoid method overload with super type * handle node: prefix package imports * exempt node: prefix packages from enforced registration * remove addImportUnchecked * remove stray deprecation tag * convert import logic to class * comment grammar * add internalapi annotation to ImportFrom
- Loading branch information
Showing
4 changed files
with
226 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...odegen/src/main/java/software/amazon/smithy/typescript/codegen/validation/ImportFrom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.typescript.codegen.validation; | ||
|
||
import java.util.Set; | ||
import software.amazon.smithy.utils.SetUtils; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Interprets the string portion of an import statement. | ||
*/ | ||
@SmithyInternalApi | ||
public class ImportFrom { | ||
public static final Set<String> NODE_NATIVE_DEPENDENCIES = SetUtils.of( | ||
"buffer", | ||
"child_process", | ||
"crypto", | ||
"dns", | ||
"events", | ||
"fs", | ||
"http", | ||
"http2", | ||
"https", | ||
"os", | ||
"path", | ||
"process", | ||
"stream", | ||
"tls", | ||
"url", | ||
"util", | ||
"zlib" | ||
); | ||
|
||
private final String from; | ||
|
||
public ImportFrom(String importTargetExpression) { | ||
this.from = importTargetExpression; | ||
} | ||
|
||
/** | ||
* @return whether we recognize it as a Node.js native module. These | ||
* do not need to be declared in package.json. This check | ||
* is not exhaustive since the list of native modules varies | ||
* by version. | ||
*/ | ||
public boolean isNodejsNative() { | ||
String[] packageNameSegments = from.split("/"); | ||
return from.startsWith("node:") | ||
|| NODE_NATIVE_DEPENDENCIES.contains(packageNameSegments[0]); | ||
} | ||
|
||
/** | ||
* @return whether the import has an org or namespace prefix like \@smithy/pkg. | ||
*/ | ||
public boolean isNamespaced() { | ||
return from.startsWith("@") && from.contains("/"); | ||
} | ||
|
||
/** | ||
* @return whether the import starts with / or . indicating a relative import. | ||
* These would not be added to package.json dependencies. | ||
*/ | ||
public boolean isRelative() { | ||
return from.startsWith("/") || from.startsWith("."); | ||
} | ||
|
||
/** | ||
* @return whether the import should correspond to an entry in | ||
* package.json. | ||
*/ | ||
public boolean isDeclarablePackageImport() { | ||
return !isNodejsNative() && !isRelative(); | ||
} | ||
|
||
/** | ||
* @return the package name. This excludes sub-paths of packages. | ||
* | ||
* For example in \@smithy/pkg/module the package name is \@smithy/pkg. | ||
*/ | ||
public String getPackageName() { | ||
String[] packageNameSegments = from.split("/"); | ||
String packageName; | ||
if (isNodejsNative()) { | ||
packageName = packageNameSegments[0].substring("node:".length()); | ||
} else if (isNamespaced()) { | ||
packageName = packageNameSegments[0] + "/" + packageNameSegments[1]; | ||
} else { | ||
packageName = packageNameSegments[0]; | ||
} | ||
return packageName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...en/src/test/java/software/amazon/smithy/typescript/codegen/validation/ImportFromTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package software.amazon.smithy.typescript.codegen.validation; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class ImportFromTest { | ||
|
||
@Test | ||
void isNodejsNative() { | ||
assertTrue( | ||
new ImportFrom("node:buffer").isNodejsNative() | ||
); | ||
assertTrue( | ||
new ImportFrom("stream").isNodejsNative() | ||
); | ||
assertFalse( | ||
new ImportFrom("@smithy/util").isNodejsNative() | ||
); | ||
assertFalse( | ||
new ImportFrom("../file").isNodejsNative() | ||
); | ||
} | ||
|
||
@Test | ||
void isNamespaced() { | ||
assertTrue( | ||
new ImportFrom("@smithy/util/submodule").isNamespaced() | ||
); | ||
assertTrue( | ||
new ImportFrom("@smithy/util").isNamespaced() | ||
); | ||
assertFalse( | ||
new ImportFrom("node:stream").isNamespaced() | ||
); | ||
assertFalse( | ||
new ImportFrom("fs/promises").isNamespaced() | ||
); | ||
} | ||
|
||
@Test | ||
void isRelative() { | ||
assertTrue( | ||
new ImportFrom("/file/path").isRelative() | ||
); | ||
assertTrue( | ||
new ImportFrom("./file/path").isRelative() | ||
); | ||
assertTrue( | ||
new ImportFrom("../../../../file/path").isRelative() | ||
); | ||
assertFalse( | ||
new ImportFrom("@smithy/util").isRelative() | ||
); | ||
assertFalse( | ||
new ImportFrom("fs/promises").isRelative() | ||
); | ||
} | ||
|
||
@Test | ||
void isDeclarablePackageImport() { | ||
assertTrue( | ||
new ImportFrom("@smithy/util/submodule").isDeclarablePackageImport() | ||
); | ||
assertTrue( | ||
new ImportFrom("@smithy/util").isDeclarablePackageImport() | ||
); | ||
assertTrue( | ||
new ImportFrom("smithy_pkg").isDeclarablePackageImport() | ||
); | ||
assertTrue( | ||
new ImportFrom("smithy_pkg/array").isDeclarablePackageImport() | ||
); | ||
assertFalse( | ||
new ImportFrom("node:buffer").isDeclarablePackageImport() | ||
); | ||
assertFalse( | ||
new ImportFrom("../pkg/pkg").isDeclarablePackageImport() | ||
); | ||
} | ||
|
||
@Test | ||
void getPackageName() { | ||
assertEquals( | ||
new ImportFrom("smithy_pkg/array").getPackageName(), | ||
"smithy_pkg" | ||
); | ||
assertEquals( | ||
new ImportFrom("@smithy/util/submodule").getPackageName(), | ||
"@smithy/util" | ||
); | ||
assertEquals( | ||
new ImportFrom("node:fs/promises").getPackageName(), | ||
"fs" | ||
); | ||
assertEquals( | ||
new ImportFrom("smithy_pkg").getPackageName(), | ||
"smithy_pkg" | ||
); | ||
} | ||
} |