Skip to content

Commit 5eb8d42

Browse files
committed
Merge remote-tracking branch 'origin/codex/4247-rails-ts-types' into codex/4248-rpc-helper
* origin/codex/4247-rails-ts-types: Reject raw template interpolation
2 parents 37f50ef + 35dd2c4 commit 5eb8d42

3 files changed

Lines changed: 15 additions & 0 deletions

File tree

react_on_rails/lib/react_on_rails/typescript_response_types.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module TypeScriptResponseTypes
1515
RAW_TYPE_GROUP_CLOSES = { "(" => ")", "[" => "]", "{" => "}", "<" => ">" }.freeze
1616
RAW_TYPE_GROUP_ENDS = RAW_TYPE_GROUP_CLOSES.values.freeze
1717
RAW_TYPE_STRING_LITERAL_PATTERN = /'(?:\\.|[^'\\])*'|"(?:\\.|[^"\\])*"|`(?:\\.|[^`\\])*`/
18+
RAW_TYPE_TEMPLATE_INTERPOLATION_PATTERN = /\$\{/
1819
RAW_TYPE_NESTING_TOKEN_PATTERN = /#{RAW_TYPE_STRING_LITERAL_PATTERN.source}|=>|[({\[<]|[)}\]>]|,/
1920
RAW_TYPE_UNSAFE_LINE_BREAK_PATTERN = /[\r\n\u2028\u2029]/
2021
RAW_TYPE_UNSAFE_NON_STRING_PATTERN = %r{;|//|/\*|\*/}
@@ -68,6 +69,7 @@ def validate_fields!(fields)
6869

6970
def unsafe_raw_type_expression?(expression)
7071
expression.match?(RAW_TYPE_UNSAFE_LINE_BREAK_PATTERN) ||
72+
expression.match?(RAW_TYPE_TEMPLATE_INTERPOLATION_PATTERN) ||
7173
expression.gsub(RAW_TYPE_STRING_LITERAL_PATTERN, "").match?(RAW_TYPE_UNSAFE_NON_STRING_PATTERN) ||
7274
unsafe_raw_type_nesting?(expression)
7375
end

react_on_rails/sig/react_on_rails/typescript_response_types.rbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module ReactOnRails
55
IDENTIFIER_PATTERN: Regexp
66
OPTION_KEYS: Array[Symbol]
77
WRAPPER_TYPE_KEYS: Array[Symbol]
8+
RAW_TYPE_TEMPLATE_INTERPOLATION_PATTERN: Regexp
89
RESERVED_TYPE_NAMES: Array[String]
910
GENERATED_TYPE_NAMES: Array[String]
1011
SCALAR_TYPES: Hash[Symbol, String]

react_on_rails/spec/react_on_rails/typescript_response_types_raw_validation_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@
6565
end.to raise_error(ReactOnRails::Error, /single-line type expressions/)
6666
end
6767

68+
it "rejects raw TypeScript template literal interpolation" do
69+
response_types.define_response(
70+
"events.show",
71+
type_name: "EventsShowResponse",
72+
fields: { slug: { raw: "`project-${string}`" } }
73+
)
74+
75+
expect do
76+
response_types.to_d_ts
77+
end.to raise_error(ReactOnRails::Error, /single-line type expressions/)
78+
end
79+
6880
it "rejects raw TypeScript expressions with ECMAScript line separators" do
6981
response_types.define_response(
7082
"events.show",

0 commit comments

Comments
 (0)