-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(codegen): check javascript property validity for property access (#…
…3649) * fix(codegen): check javascript property validity for property access * fix(codegen): checkstyle fixes * fix(codegen): property access code cleanup
- Loading branch information
Showing
6 changed files
with
107 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ dist-* | |
|
||
*.tsbuildinfo | ||
|
||
codegen/.attach* | ||
codegen/.project | ||
codegen/.classpath | ||
codegen/.settings/ | ||
|
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
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
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
52 changes: 52 additions & 0 deletions
52
...n/java/software/amazon/smithy/aws/typescript/codegen/propertyaccess/PropertyAccessor.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,52 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.aws.typescript.codegen.propertyaccess; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public final class PropertyAccessor { | ||
/** | ||
* Starts with alpha or underscore, and contains only alphanumeric and underscores. | ||
*/ | ||
public static final Pattern VALID_JAVASCRIPT_PROPERTY_NAME = Pattern.compile("^(?![0-9])[a-zA-Z0-9$_]+$"); | ||
|
||
private PropertyAccessor() {} | ||
|
||
/** | ||
* @param propertyName - property being accessed. | ||
* @return brackets wrapping the name if it's not a valid JavaScript property name. | ||
*/ | ||
public static String getPropertyAccessor(String propertyName) { | ||
if (VALID_JAVASCRIPT_PROPERTY_NAME.matcher(propertyName).matches()) { | ||
return "." + propertyName; | ||
} | ||
if (propertyName.contains("\"")) { | ||
// This doesn't handle cases of the special characters being pre-escaped in the propertyName, | ||
// but that case does not currently need to be addressed. | ||
return "[`" + propertyName + "`]"; | ||
} | ||
return "[\"" + propertyName + "\"]"; | ||
} | ||
|
||
/** | ||
* @param variable - object host. | ||
* @param propertyName - property being accessed. | ||
* @return e.g. someObject.prop or someObject['property name'] or reluctantly someObject[`bad"property"name`]. | ||
*/ | ||
public static String getFrom(String variable, String propertyName) { | ||
return variable + getPropertyAccessor(propertyName); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...va/software/amazon/smithy/aws/typescript/codegen/propertyaccess/PropertyAccessorTest.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,24 @@ | ||
package software.amazon.smithy.aws.typescript.codegen.propertyaccess; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
class PropertyAccessorTest { | ||
@Test | ||
void getFrom() { | ||
assertEquals("output.fileSystemId", PropertyAccessor.getFrom("output", "fileSystemId")); | ||
assertEquals("output.__fileSystemId", PropertyAccessor.getFrom("output", "__fileSystemId")); | ||
} | ||
|
||
@Test | ||
void getFromQuoted() { | ||
assertEquals("output[\"0fileSystemId\"]", PropertyAccessor.getFrom("output", "0fileSystemId")); | ||
assertEquals("output[\"file-system-id\"]", PropertyAccessor.getFrom("output", "file-system-id")); | ||
} | ||
|
||
@Test | ||
void getFromExtraQuoted() { | ||
assertEquals("output[`file\"system\"id`]", PropertyAccessor.getFrom("output", "file\"system\"id")); | ||
} | ||
} |