From bc817ec7c383a015daf015626ef71f6d071b519c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 27 Sep 2026 11:06:03 +0100 Subject: Add string literals to TypeScript AST --- .../lang/typescript/TypescriptWriterTests.java | 26 ++++++++++++++ .../ast/TypescriptStringLiteralMatcher.java | 41 ++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteralMatcher.java (limited to 'src/test/java') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java index 4963e09..86a94ea 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/TypescriptWriterTests.java @@ -567,6 +567,26 @@ public class TypescriptWriterTests { assertThat(string, equalTo("x")); } + // === String literals === + + @Test + public void stringLiteral() throws IOException { + var typescript = string("hello"); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("\"hello\"")); + } + + @Test + public void stringLiteralWithSpecialCharacters() throws IOException { + var typescript = string("\"\\\n\r\u000b\t\b\f"); + + var string = write(writer -> writer.writeExpression(typescript)); + + assertThat(string, equalTo("\"\\\"\\\\\\n\\r\\v\\t\\b\\f\"")); + } + // === typeof === @Test @@ -615,6 +635,12 @@ public class TypescriptWriterTests { .build(); } + private static TypescriptStringLiteral string(String value) { + return TypescriptStringLiteral.arbitrary() + .withValue(value) + .build(); + } + private String write(TypescriptWriter.Write write) throws IOException { return write(write, Optional.empty()); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteralMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteralMatcher.java new file mode 100644 index 0000000..8dd0f6a --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/typescript/ast/TypescriptStringLiteralMatcher.java @@ -0,0 +1,41 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.typescript.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher imports + +public final class TypescriptStringLiteralMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher isTypescriptStringLiteral() { + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private TypescriptStringLiteralMatcher( + java.util.List> submatchers + ) { + this.submatchers = submatchers; + } + + public org.zwobble.precisely.MatchResult match(java.lang.Object actual) { + return this.toMatcher().match(actual); + } + + public org.zwobble.precisely.TextTree describe() { + return this.toMatcher().describe(); + } + + private org.zwobble.precisely.Matcher toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher withValue(org.zwobble.precisely.Matcher value) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteral::value, (org.zwobble.precisely.Matcher) value)); + return new org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.typescript.ast.TypescriptStringLiteralMatcher body +} -- cgit v1.2.3