Coverage Report

Created: 2025-08-28 06:26

/src/serenity/Userland/Libraries/LibJS/ParserError.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
3
 * Copyright (c) 2021-2022, David Tuin <davidot@serenityos.org>
4
 *
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <AK/StringView.h>
9
#include <AK/Vector.h>
10
#include <LibJS/ParserError.h>
11
#include <LibJS/Token.h>
12
13
namespace JS {
14
15
String ParserError::to_string() const
16
0
{
17
0
    if (!position.has_value())
18
0
        return MUST(String::from_byte_string(message));
19
0
    return MUST(String::formatted("{} (line: {}, column: {})", message, position.value().line, position.value().column));
20
0
}
21
22
ByteString ParserError::to_byte_string() const
23
0
{
24
0
    if (!position.has_value())
25
0
        return message;
26
0
    return ByteString::formatted("{} (line: {}, column: {})", message, position.value().line, position.value().column);
27
0
}
28
29
ByteString ParserError::source_location_hint(StringView source, char const spacer, char const indicator) const
30
0
{
31
0
    if (!position.has_value())
32
0
        return {};
33
    // We need to modify the source to match what the lexer considers one line - normalizing
34
    // line terminators to \n is easier than splitting using all different LT characters.
35
0
    ByteString source_string = source.replace("\r\n"sv, "\n"sv, ReplaceMode::All).replace("\r"sv, "\n"sv, ReplaceMode::All).replace(LINE_SEPARATOR_STRING, "\n"sv, ReplaceMode::All).replace(PARAGRAPH_SEPARATOR_STRING, "\n"sv, ReplaceMode::All);
36
0
    StringBuilder builder;
37
0
    builder.append(source_string.split_view('\n', SplitBehavior::KeepEmpty)[position.value().line - 1]);
38
0
    builder.append('\n');
39
0
    for (size_t i = 0; i < position.value().column - 1; ++i)
40
0
        builder.append(spacer);
41
0
    builder.append(indicator);
42
0
    return builder.to_byte_string();
43
0
}
44
45
}