-
Notifications
You must be signed in to change notification settings - Fork 90
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
fix: check dependencies when adding imports #1239
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ccfc23c
fix: check dependencies when adding imports
kuhe 10a39fe
avoid method overload with super type
kuhe a57d225
handle node: prefix package imports
kuhe 7c6b7de
exempt node: prefix packages from enforced registration
kuhe 0a22c9a
remove addImportUnchecked
kuhe 3f84a1b
remove stray deprecation tag
kuhe b92fd56
convert import logic to class
kuhe f5b3dff
comment grammar
kuhe de18465
add internalapi annotation to ImportFrom
kuhe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { | ||
kuhe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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" | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the long run, we should move it to taking
Dependency
as the third parameter.