Skip to content

Commit 9b26765

Browse files
authored
fix(codegen): variable allocation in StringStore (#1527)
1 parent 2c12355 commit 9b26765

File tree

2 files changed

+129
-1
lines changed

2 files changed

+129
-1
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/StringStore.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

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

8+
import java.util.Arrays;
89
import java.util.HashMap;
910
import java.util.HashSet;
1011
import java.util.LinkedList;
@@ -77,7 +78,9 @@ private String assignKey(String literal) {
7778
* Prefers the uppercase or word-starting letters.
7879
*/
7980
private String allocateVariable(String literal) {
80-
String[] sections = literal.split("[-_\\s]");
81+
String[] sections = Arrays.stream(literal.split("[-_\\s]"))
82+
.filter(s -> !s.isEmpty())
83+
.toArray(String[]::new);
8184
StringBuilder v = new StringBuilder("_");
8285
Queue<Character> deconfliction = new LinkedList<>();
8386
if (sections.length > 1) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package software.amazon.smithy.typescript.codegen.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.Objects;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class StringStoreTest {
10+
11+
@Test
12+
void var() {
13+
StringStore subject = new StringStore();
14+
String sourceCode = """
15+
const array = [
16+
%s,
17+
%s,
18+
%s,
19+
%s,
20+
%s,
21+
%s,
22+
%s,
23+
%s,
24+
%s,
25+
%s,
26+
%s,
27+
%s,
28+
%s,
29+
%s,
30+
%s,
31+
%s,
32+
%s,
33+
%s,
34+
%s,
35+
%s,
36+
%s,
37+
%s,
38+
%s
39+
];
40+
""".formatted(
41+
subject.var("SomeObject"),
42+
subject.var("some_object"),
43+
subject.var("SomeObject"),
44+
subject.var("some_object"),
45+
subject.var("_"),
46+
subject.var("__"),
47+
subject.var("___"),
48+
subject.var("_internal"),
49+
subject.var("__internal"),
50+
subject.var("___internal"),
51+
subject.var("_internal_"),
52+
subject.var("__internal__"),
53+
subject.var("___internal__"),
54+
subject.var("_two--words"),
55+
subject.var("__twoWords__"),
56+
subject.var("___TwoWords__"),
57+
subject.var("$Symbol"),
58+
subject.var("%Symbol"),
59+
subject.var(" !)(@*#&$^% "),
60+
subject.var(" !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^"),
61+
subject.var("**Ack**Ack**"),
62+
subject.var("Spaces Are Cool"),
63+
subject.var("__why Would &&& YouName $something this...")
64+
);
65+
66+
String[] expected = """
67+
const _ = "_";
68+
const _AA = "**Ack**Ack**";
69+
const _S = "$Symbol";
70+
const _SAC = "Spaces Are Cool";
71+
const _SO = "SomeObject";
72+
const _S_ = " !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^";
73+
const _Sy = "%Symbol";
74+
const _TW = "___TwoWords__";
75+
const __ = "__";
76+
const ___ = "___";
77+
const ____ = " !)(@*#&$^% ";
78+
const _i = "_internal";
79+
const _in = "__internal";
80+
const _int = "___internal";
81+
const _inte = "_internal_";
82+
const _inter = "__internal__";
83+
const _intern = "___internal__";
84+
const _so = "some_object";
85+
const _tW = "__twoWords__";
86+
const _tw = "_two--words";
87+
const _wWYt = "__why Would &&& YouName $something this...";
88+
89+
const array = [
90+
_SO,
91+
_so,
92+
_SO,
93+
_so,
94+
_,
95+
__,
96+
___,
97+
_i,
98+
_in,
99+
_int,
100+
_inte,
101+
_inter,
102+
_intern,
103+
_tw,
104+
_tW,
105+
_TW,
106+
_S,
107+
_Sy,
108+
____,
109+
_S_,
110+
_AA,
111+
_SAC,
112+
_wWYt
113+
];
114+
""".split("\n");
115+
String[] actual = (subject.flushVariableDeclarationCode() + "\n"
116+
+ sourceCode).split("\n");
117+
118+
for (int i = 0; i < expected.length; ++i) {
119+
assertEquals(
120+
Objects.toString(i) + ": " + expected[i].trim(),
121+
Objects.toString(i) + ": " + actual[i].trim()
122+
);
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)