Skip to content

Commit 5e31190

Browse files
committed
fix(codegen): variable allocation in StringStore
1 parent 2c12355 commit 5e31190

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
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,118 @@
1+
package software.amazon.smithy.typescript.codegen.util;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class StringStoreTest {
8+
9+
@Test
10+
void var() {
11+
StringStore subject = new StringStore();
12+
String sourceCode = """
13+
const array = [
14+
%s,
15+
%s,
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+
];
38+
""".formatted(
39+
subject.var("SomeObject"),
40+
subject.var("some_object"),
41+
subject.var("SomeObject"),
42+
subject.var("some_object"),
43+
subject.var("_"),
44+
subject.var("__"),
45+
subject.var("___"),
46+
subject.var("_internal"),
47+
subject.var("__internal"),
48+
subject.var("___internal"),
49+
subject.var("_internal_"),
50+
subject.var("__internal__"),
51+
subject.var("___internal__"),
52+
subject.var("_two--words"),
53+
subject.var("__twoWords__"),
54+
subject.var("___TwoWords__"),
55+
subject.var("$Symbol"),
56+
subject.var("%Symbol"),
57+
subject.var(" !)(@*#&$^% "),
58+
subject.var(" !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^"),
59+
subject.var("**Ack**Ack**"),
60+
subject.var("Spaces Are Cool"),
61+
subject.var("__why Would &&& YouName $something this...")
62+
);
63+
64+
assertEquals(
65+
"""
66+
const _ = "_";
67+
const _AA = "**Ack**Ack**";
68+
const _S = "$Symbol";
69+
const _SAC = "Spaces Are Cool";
70+
const _SO = "SomeObject";
71+
const _S_ = " !)( @ )(@*#&$^* SmithyTypeScript# &)(@*#&$^ $^% )(@*#&$^";
72+
const _Sy = "%Symbol";
73+
const _TW = "___TwoWords__";
74+
const __ = "__";
75+
const ___ = "___";
76+
const ____ = " !)(@*#&$^% ";
77+
const _i = "_internal";
78+
const _in = "__internal";
79+
const _int = "___internal";
80+
const _inte = "_internal_";
81+
const _inter = "__internal__";
82+
const _intern = "___internal__";
83+
const _so = "some_object";
84+
const _tW = "__twoWords__";
85+
const _tw = "_two--words";
86+
const _wWYt = "__why Would &&& YouName $something this...";
87+
88+
const array = [
89+
_SO,
90+
_so,
91+
_SO,
92+
_so,
93+
_,
94+
__,
95+
___,
96+
_i,
97+
_in,
98+
_int,
99+
_inte,
100+
_inter,
101+
_intern,
102+
_tw,
103+
_tW,
104+
_TW,
105+
_S,
106+
_Sy,
107+
____,
108+
_S_,
109+
_AA,
110+
_SAC,
111+
_wWYt
112+
];
113+
""",
114+
subject.flushVariableDeclarationCode() + "\n"
115+
+ sourceCode
116+
);
117+
}
118+
}

0 commit comments

Comments
 (0)