Skip to content

fix(codegen): variable allocation in StringStore #1527

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

Merged
merged 1 commit into from
Feb 17, 2025
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 @@ -5,6 +5,7 @@

package software.amazon.smithy.typescript.codegen.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -77,7 +78,9 @@ private String assignKey(String literal) {
* Prefers the uppercase or word-starting letters.
*/
private String allocateVariable(String literal) {
String[] sections = literal.split("[-_\\s]");
String[] sections = Arrays.stream(literal.split("[-_\\s]"))
.filter(s -> !s.isEmpty())
.toArray(String[]::new);
StringBuilder v = new StringBuilder("_");
Queue<Character> deconfliction = new LinkedList<>();
if (sections.length > 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package software.amazon.smithy.typescript.codegen.util;

import org.junit.jupiter.api.Test;

import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;

class StringStoreTest {

@Test
void var() {
StringStore subject = new StringStore();
String sourceCode = """
const array = [
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s
];
""".formatted(
subject.var("SomeObject"),
subject.var("some_object"),
subject.var("SomeObject"),
subject.var("some_object"),
subject.var("_"),
subject.var("__"),
subject.var("___"),
subject.var("_internal"),
subject.var("__internal"),
subject.var("___internal"),
subject.var("_internal_"),
subject.var("__internal__"),
subject.var("___internal__"),
subject.var("_two--words"),
subject.var("__twoWords__"),
subject.var("___TwoWords__"),
subject.var("$Symbol"),
subject.var("%Symbol"),
subject.var(" !)(@*#&$^% "),
subject.var(" !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^"),
subject.var("**Ack**Ack**"),
subject.var("Spaces Are Cool"),
subject.var("__why Would &&& YouName $something this...")
);

String[] expected = """
const _ = "_";
const _AA = "**Ack**Ack**";
const _S = "$Symbol";
const _SAC = "Spaces Are Cool";
const _SO = "SomeObject";
const _S_ = " !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^";
const _Sy = "%Symbol";
const _TW = "___TwoWords__";
const __ = "__";
const ___ = "___";
const ____ = " !)(@*#&$^% ";
const _i = "_internal";
const _in = "__internal";
const _int = "___internal";
const _inte = "_internal_";
const _inter = "__internal__";
const _intern = "___internal__";
const _so = "some_object";
const _tW = "__twoWords__";
const _tw = "_two--words";
const _wWYt = "__why Would &&& YouName $something this...";

const array = [
_SO,
_so,
_SO,
_so,
_,
__,
___,
_i,
_in,
_int,
_inte,
_inter,
_intern,
_tw,
_tW,
_TW,
_S,
_Sy,
____,
_S_,
_AA,
_SAC,
_wWYt
];
""".split("\n");
String[] actual = (subject.flushVariableDeclarationCode() + "\n"
+ sourceCode).split("\n");

for (int i = 0; i < expected.length; ++i) {
assertEquals(
Objects.toString(i) + ": " + expected[i].trim(),
Objects.toString(i) + ": " + actual[i].trim()
);
}
}
}
Loading