LibJS: Set Token's m_offset to the value's start index

This makes much more sense than the current way of setting it to the
Lexer's m_position after consuming the full value.
This commit is contained in:
Linus Groh 2022-01-18 23:44:33 +00:00
parent c1d3b557d5
commit 95a9f12b97
Notes: sideshowbarker 2024-07-17 20:38:22 +09:00
2 changed files with 9 additions and 4 deletions

View file

@ -832,7 +832,7 @@ Token Lexer::next()
m_filename,
m_line_number,
m_line_column - 1,
m_position);
value_start + 1);
m_hit_invalid_unicode.clear();
// Do not produce any further tokens.
VERIFY(is_eof());
@ -845,7 +845,7 @@ Token Lexer::next()
m_filename,
value_start_line_number,
value_start_column_number,
m_position);
value_start - 1);
}
if (identifier.has_value())
@ -889,7 +889,7 @@ Token Lexer::force_slash_as_regex()
m_filename,
m_current_token.line_number(),
m_current_token.line_column(),
m_position);
value_start - 1);
if constexpr (LEXER_DEBUG) {
dbgln("------------------------------");

View file

@ -2138,7 +2138,12 @@ RefPtr<BindingPattern> Parser::synthesize_binding_pattern(Expression const& expr
return error.position.has_value() && range.contains(*error.position);
});
// Make a parser and parse the source for this expression as a binding pattern.
auto source = m_state.lexer.source().substring_view(expression.source_range().start.offset - 2, expression.source_range().end.offset - expression.source_range().start.offset);
// NOTE: There's currently a fundamental problem that we pass the *next* (a.k.a. `current_token`)
// token's position to most nodes' SourceRange when using `rule_start.position(), position()`.
// This means that `source` will contain the subsequent token's trivia, if any (which is fine).
auto source_start_offset = expression.source_range().start.offset;
auto source_end_offset = expression.source_range().end.offset;
auto source = m_state.lexer.source().substring_view(source_start_offset, source_end_offset - source_start_offset);
Lexer lexer { source, m_state.lexer.filename(), expression.source_range().start.line, expression.source_range().start.column };
Parser parser { lexer };