Skip to content

Commit

Permalink
Handle string interpolation table expression
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Nov 19, 2023
1 parent c872ba6 commit 712aa76
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed handling of floor division (`//`) syntax when only Luau FFlag is enabled
- Fixed missing space when table is inside of Luau interpolated string expression (`{{` is invalid syntax)

## [0.19.1] - 2023-11-15

Expand Down
11 changes: 10 additions & 1 deletion src/formatters/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,9 +823,18 @@ fn format_interpolated_string(
let literal = format_token_reference(ctx, &segment.literal, shape);
shape = shape + literal.to_string().len();

let expression = format_expression(ctx, &segment.expression, shape);
let mut expression = format_expression(ctx, &segment.expression, shape);
shape = shape.take_last_line(&expression);

// If expression is a table constructor, then ensure a space is added beforehand
// since `{{` syntax is not permitted
if let Expression::TableConstructor { .. } = expression {
expression =
expression.update_leading_trivia(FormatTriviaType::Append(vec![Token::new(
TokenType::spaces(1),
)]))
}

segments.push(InterpolatedStringSegment {
literal,
expression,
Expand Down
1 change: 1 addition & 0 deletions tests/inputs-luau/string-interpolation-table.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
local x = `{ {1} }`
7 changes: 7 additions & 0 deletions tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/tests.rs
expression: format(&contents)
input_file: tests/inputs-luau/string-interpolation-table.lua
---
local x = `{ { 1 }}`

0 comments on commit 712aa76

Please sign in to comment.