Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fixtures/small/prism/heredoc_invalid_utf8_escape_actual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<<~EOF
line with \xFF escape
foo
EOF
4 changes: 4 additions & 0 deletions fixtures/small/prism/heredoc_invalid_utf8_escape_expected.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<<~EOF
line with \xFF escape
foo
EOF
14 changes: 8 additions & 6 deletions librubyfmt/src/format_prism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
parser_state::{FormattingContext, HashType, ParserState},
render_targets::MultilineHandling,
types::SourceOffset,
util::{const_to_str, loc_to_str, loc_to_string, u8_to_str},
util::{const_to_str, loc_to_str, loc_to_string},
};

pub fn format_node<'src>(ps: &mut ParserState<'src>, node: prism::Node<'src>) {
Expand Down Expand Up @@ -909,12 +909,14 @@ fn format_inner_string<'src>(
.iter()
.filter_map(|part| {
if let Some(node) = part.as_string_node() {
let raw = loc_to_str(node.content_loc());
let unescaped = u8_to_str(node.unescaped());
let raw = node.content_loc().as_slice();
let unescaped = node.unescaped();

// Count leading whitespace in each
let raw_leading = raw.len() - raw.trim_start().len();
let unescaped_leading = unescaped.len() - unescaped.trim_start().len();
let raw_leading = raw.iter().take_while(|&&b| b == b' ' || b == b'\t').count();
let unescaped_leading = unescaped
.iter()
.take_while(|&&b| b == b' ' || b == b'\t')
Comment on lines +915 to +918
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that Sorbet's heredoc bits in the parser have to take into account a tab being 8 spaces. Do we have to do that same accounting here as well?

Answering my own question, I guess not, because the number of spaces and tabs in the raw and unescaped are essentially coming from the same source, so things should just match up?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think in this case since they're the same source, it doesn't really matter, and I think if you had a tab that was beyond the common indent, it would be a part of the string contents and just get rendered anyways.

.count();

// The difference is the common indent (if raw has more leading whitespace)
if raw_leading > unescaped_leading {
Expand Down