• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ArkScript-lang / Ark / 12164232004

04 Dec 2024 04:37PM UTC coverage: 77.815% (+0.6%) from 77.19%
12164232004

push

github

SuperFola
feat(name resolution): allow fqn if the scope is only exporting symbols

1 of 3 new or added lines in 2 files covered. (33.33%)

256 existing lines in 11 files now uncovered.

5412 of 6955 relevant lines covered (77.81%)

9300.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.3
/src/arkscript/Formatter.cpp
1
#include <Ark/Constants.hpp>
2
#include <CLI/Formatter.hpp>
3

4
#include <fmt/core.h>
5
#include <fmt/color.h>
6

7
#include <Ark/Files.hpp>
8
#include <Ark/Exceptions.hpp>
9
#include <Ark/Compiler/Common.hpp>
10

11
using namespace Ark;
12
using namespace Ark::internal;
13

14
Formatter::Formatter(const bool dry_run) :
42✔
15
    m_dry_run(dry_run), m_parser(/* debug= */ 0, /* interpret= */ false), m_updated(false)
21✔
16
{}
42✔
17

18
Formatter::Formatter(std::string filename, const bool dry_run) :
42✔
19
    m_filename(std::move(filename)), m_dry_run(dry_run), m_parser(/* debug= */ 0, /* interpret= */ false), m_updated(false)
21✔
20
{}
42✔
21

22
void Formatter::run()
22✔
23
{
21✔
24
    try
25
    {
26
        const std::string code = Utils::readFile(m_filename);
21✔
27
        m_parser.process(m_filename, code);
21✔
28
        processAst(m_parser.ast());
21✔
29
        warnIfCommentsWereRemoved(code, ARK_NO_NAME_FILE);
21✔
30

×
31
        m_updated = code != m_output;
21✔
32
    }
22✔
33
    catch (const CodeError& e)
34
    {
35
        Diagnostics::generate(e);
×
36
    }
21✔
37
}
21✔
38

39
void Formatter::runWithString(const std::string& code)
21✔
40
{
21✔
41
    try
42
    {
1✔
43
        m_parser.process(ARK_NO_NAME_FILE, code);
21✔
44
        processAst(m_parser.ast());
21✔
45
        warnIfCommentsWereRemoved(code, ARK_NO_NAME_FILE);
21✔
46

47
        m_updated = code != m_output;
21✔
48
    }
21✔
49
    catch (const CodeError& e)
50
    {
×
51
        Diagnostics::generate(e);
×
52
    }
22✔
53
}
21✔
54

55
const std::string& Formatter::output() const
42✔
56
{
42✔
57
    return m_output;
42✔
58
}
59

60
bool Formatter::codeModified() const
×
61
{
×
62
    return m_updated;
1✔
63
}
64

65
void Formatter::processAst(const Node& ast)
42✔
66
{
42✔
67
    // remove useless surrounding begin (generated by the parser)
68
    if (isBeginBlock(ast))
42✔
69
    {
70
        for (std::size_t i = 1, end = ast.constList().size(); i < end; ++i)
176✔
71
        {
72
            const Node node = ast.constList()[i];
135✔
73
            if (shouldAddNewLineBetweenNodes(ast, i) && !m_output.empty())
134✔
74
                m_output += "\n";
18✔
75
            m_output += format(node, 0, false) + "\n";
134✔
76
        }
134✔
77
    }
42✔
78
    else
79
        m_output = format(ast, 0, false);
×
80

×
81
    if (!m_dry_run)
42✔
82
    {
83
        std::ofstream stream(m_filename);
×
84
        stream << m_output;
×
85
    }
×
86
}
42✔
87

88
void Formatter::warnIfCommentsWereRemoved(const std::string& original_code, const std::string& filename)
42✔
89
{
42✔
90
    if (std::ranges::count(original_code, '#') != std::ranges::count(m_output, '#'))
42✔
91
    {
92
        fmt::println(
×
93
            "{}: one or more comments from the original source code seem to have been removed by mistake while formatting {}",
×
94
            fmt::styled("Warning", fmt::fg(fmt::color::dark_orange)),
×
95
            filename != ARK_NO_NAME_FILE ? filename : "file");
×
96
        fmt::println("Please fill an issue on GitHub: https://github.com/ArkScript-lang/Ark");
×
97
    }
×
98
}
42✔
99

100
bool Formatter::isListStartingWithKeyword(const Node& node, const Keyword keyword)
262✔
101
{
262✔
102
    return node.isListLike() && !node.constList().empty() && node.constList()[0].nodeType() == NodeType::Keyword && node.constList()[0].keyword() == keyword;
262✔
103
}
104

105
bool Formatter::isBeginBlock(const Node& node)
178✔
106
{
178✔
107
    return isListStartingWithKeyword(node, Keyword::Begin);
178✔
108
}
109

110
bool Formatter::isFuncDef(const Node& node)
64✔
111
{
64✔
112
    return isListStartingWithKeyword(node, Keyword::Fun);
64✔
113
}
114

115
bool Formatter::isFuncCall(const Node& node)
112✔
116
{
112✔
117
    return node.isListLike() && !node.constList().empty() && node.constList()[0].nodeType() == NodeType::Symbol;
112✔
118
}
119

120
std::size_t Formatter::lineOfLastNodeIn(const Node& node)
322✔
121
{
322✔
122
    if (node.isListLike() && !node.constList().empty())
322✔
123
    {
124
        std::size_t child_line = lineOfLastNodeIn(node.constList().back());
186✔
125
        if (child_line < node.line())
186✔
126
            return node.line();
×
127
        return child_line;
186✔
128
    }
187✔
129
    return node.line();
136✔
130
}
322✔
131

132
bool Formatter::shouldSplitOnNewline(const Node& node)
144✔
133
{
144✔
134
    const std::string formatted = format(node, 0, false);
144✔
135
    const std::string::size_type sz = formatted.find_first_of('\n');
144✔
136

137
    const bool is_long_line = !((sz < FormatterConfig::LongLineLength || (sz == std::string::npos && formatted.size() < FormatterConfig::LongLineLength)));
144✔
138
    if (node.comment().empty() && (isBeginBlock(node) || isFuncCall(node)))
144✔
139
        return false;
50✔
140
    if (is_long_line || (node.isListLike() && node.constList().size() > 1) || !node.comment().empty())
94✔
141
        return true;
20✔
142
    return false;
74✔
143
}
144✔
144

145
bool Formatter::shouldAddNewLineBetweenNodes(const Node& node, const std::size_t at)
228✔
146
{
228✔
147
    if (at <= 1)
228✔
148
        return false;
92✔
149

150
    const auto& list = node.constList();
136✔
151
    std::size_t previous_line = lineOfLastNodeIn(list[at - 1]);
136✔
152

153
    const auto& child = list[at];
136✔
154

155
    // If we have a node before the current one,
156
    // and the line count between the two nodes is more than 1,
157
    // maybe we should add a new line to preserve user spacing.
×
158
    // However, if the current node has a comment, do not add a new line, this is causing the spacing.
159
    if (child.line() - previous_line > 1 && child.comment().empty())
137✔
160
        return true;
16✔
161
    // If we do have a comment but the spacing is more than 2,
162
    // then add a newline to preserve user spacing.
163
    if (child.line() - previous_line > 2 && !child.comment().empty())
120✔
164
        return true;
2✔
165
    return false;
118✔
166
}
228✔
167

168
std::string Formatter::format(const Node& node, std::size_t indent, bool after_newline)
1,374✔
169
{
1,374✔
170
    std::string output;
1,374✔
171
    if (!node.comment().empty())
1,374✔
172
    {
173
        output += formatComment(node.comment(), indent);
58✔
174
        after_newline = true;
58✔
175
    }
58✔
176
    if (after_newline)
1,374✔
177
        output += prefix(indent);
218✔
178

179
    switch (node.nodeType())
1,374✔
180
    {
678✔
181
        case NodeType::Symbol:
182
            output += node.string();
678✔
183
            break;
680✔
184
        case NodeType::Capture:
185
            output += "&" + node.string();
2✔
186
            break;
2✔
187
        case NodeType::Keyword:
188
            output += std::string(keywords[static_cast<std::size_t>(node.keyword())]);
×
189
            break;
42✔
190
        case NodeType::String:
191
            output += fmt::format("\"{}\"", node.string());
42✔
192
            break;
206✔
193
        case NodeType::Number:
194
            output += fmt::format("{}", node.number());
164✔
195
            break;
600✔
196
        case NodeType::List:
197
            output += formatBlock(node, indent, after_newline);
436✔
198
            break;
450✔
199
        case NodeType::Spread:
200
            output += fmt::format("...{}", node.string());
14✔
201
            break;
32✔
202
        case NodeType::Field:
203
        {
204
            std::string field = format(node.constList()[0], indent, false);
18✔
205
            for (std::size_t i = 1, end = node.constList().size(); i < end; ++i)
62✔
206
                field += "." + format(node.constList()[i], indent, false);
44✔
207
            output += field;
18✔
208
            break;
209
        }
38✔
210
        case NodeType::Macro:
211
            output += formatMacro(node, indent);
20✔
212
            break;
20✔
213
        // not handling Namespace nor Unused node types as those can not be generated by the parser
214
        case NodeType::Namespace:
215
            [[fallthrough]];
216
        case NodeType::Unused:
UNCOV
217
            break;
×
218
    }
1,374✔
219

220
    if (!node.commentAfter().empty())
1,374✔
221
        output += " " + formatComment(node.commentAfter(), /* indent= */ 0);
40✔
222

223
    return output;
1,374✔
224
}
1,374✔
225

226
std::string Formatter::formatComment(const std::string& comment, const std::size_t indent) const
110✔
227
{
110✔
228
    std::string output = prefix(indent);
110✔
229
    for (std::size_t i = 0, end = comment.size(); i < end; ++i)
1,654✔
230
    {
231
        output += comment[i];
1,544✔
232
        if (comment[i] == '\n' && i != end - 1)
1,544✔
233
            output += prefix(indent);
6✔
234
    }
1,544✔
235

236
    return output;
110✔
237
}
110✔
238

239
std::string Formatter::formatBlock(const Node& node, const std::size_t indent, const bool after_newline)
436✔
240
{
436✔
241
    if (node.constList().empty())
436✔
242
        return "()";
8✔
243

244
    const Node first = node.constList().front();
428✔
245
    if (first.nodeType() == NodeType::Keyword)
428✔
246
    {
247
        switch (first.keyword())
228✔
248
        {
30✔
249
            case Keyword::Fun:
250
                return formatFunction(node, indent);
94✔
251
            case Keyword::Let:
252
                [[fallthrough]];
253
            case Keyword::Mut:
254
                [[fallthrough]];
255
            case Keyword::Set:
256
                return formatVariable(node, indent);
106✔
257
            case Keyword::If:
258
                return formatCondition(node, indent);
52✔
259
            case Keyword::While:
260
                return formatLoop(node, indent);
68✔
261
            case Keyword::Begin:
262
                return formatBegin(node, indent, after_newline);
78✔
263
            case Keyword::Import:
264
                return formatImport(node, indent);
24✔
265
            case Keyword::Del:
266
                return formatDel(node, indent);
4✔
UNCOV
267
        }
×
268
        // HACK: should never reach, but the compiler insists that the function doesn't return in every code path
UNCOV
269
        return "";
×
270
    }
271
    return formatCall(node, indent);
200✔
272
}
436✔
273

274
std::string Formatter::formatFunction(const Node& node, const std::size_t indent)
30✔
275
{
30✔
276
    const Node args_node = node.constList()[1];
30✔
277
    const Node body_node = node.constList()[2];
30✔
278

279
    std::string formatted_args;
30✔
280

281
    if (!args_node.comment().empty())
30✔
282
    {
283
        formatted_args += "\n";
2✔
284
        formatted_args += formatComment(args_node.comment(), indent + 1);
2✔
285
        formatted_args += prefix(indent + 1);
2✔
286
    }
2✔
287
    else
288
        formatted_args += " ";
28✔
289

290
    if (args_node.isListLike())
30✔
291
    {
292
        bool comment_in_args = false;
28✔
293
        std::string args;
28✔
294
        for (std::size_t i = 0, end = args_node.constList().size(); i < end; ++i)
52✔
295
        {
296
            const Node arg_i = args_node.constList()[i];
24✔
297
            if (!arg_i.comment().empty())
24✔
298
                comment_in_args = true;
4✔
299

300
            args += format(arg_i, indent + (comment_in_args ? 1 : 0), comment_in_args);
24✔
301
            if (i != end - 1)
24✔
302
                args += comment_in_args ? '\n' : ' ';
8✔
303
        }
24✔
304

305
        formatted_args += fmt::format("({}{})", (comment_in_args ? "\n" : ""), args);
28✔
306
    }
28✔
307
    else
308
        formatted_args += format(args_node, indent, false);
2✔
309

310
    if (!shouldSplitOnNewline(body_node) && args_node.comment().empty())
30✔
311
        return fmt::format("(fun{} {})", formatted_args, format(body_node, indent + 1, false));
18✔
312
    return fmt::format("(fun{}\n{})", formatted_args, format(body_node, indent + 1, true));
12✔
313
}
30✔
314

315
std::string Formatter::formatVariable(const Node& node, const std::size_t indent)
64✔
316
{
64✔
317
    std::string keyword = std::string(keywords[static_cast<std::size_t>(node.constList()[0].keyword())]);
64✔
318

319
    const Node body_node = node.constList()[2];
64✔
320
    const std::string formatted_bind = format(node.constList()[1], indent, false);
64✔
321

322
    // we don't want to add another indentation level here, because it would result in a (let a (fun ()\n{indent+=4}...))
323
    if (isFuncDef(body_node))
64✔
324
        return fmt::format("({} {} {})", keyword, formatted_bind, format(body_node, indent, false));
6✔
325
    if (!shouldSplitOnNewline(body_node))
58✔
326
        return fmt::format("({} {} {})", keyword, formatted_bind, format(body_node, indent + 1, false));
56✔
327
    return fmt::format("({} {}\n{})", keyword, formatted_bind, format(body_node, indent + 1, true));
2✔
328
}
64✔
329

330
std::string Formatter::formatCondition(const Node& node, const std::size_t indent, const bool is_macro)
48✔
331
{
48✔
332
    const Node cond_node = node.constList()[1];
48✔
333
    const Node then_node = node.constList()[2];
48✔
334

335
    bool cond_on_newline = false;
48✔
336
    std::string formatted_cond = format(cond_node, indent + 1, false);
48✔
337
    if (formatted_cond.find('\n') != std::string::npos)
48✔
338
        cond_on_newline = true;
2✔
339

340
    std::string if_cond_formatted = fmt::format(
96✔
341
        "({}if{}{}",
48✔
342
        is_macro ? "$" : "",
48✔
343
        cond_on_newline ? "\n" : " ",
48✔
344
        formatted_cond);
345

346
    const bool split_then_newline = shouldSplitOnNewline(then_node);
48✔
347

348
    // (if cond then)
349
    if (node.constList().size() == 3)
48✔
350
    {
351
        if (cond_on_newline || split_then_newline)
22✔
352
            return fmt::format("{}\n{})", if_cond_formatted, format(then_node, indent + 1, true));
2✔
353
        return fmt::format("{} {})", if_cond_formatted, format(then_node, indent + 1, false));
20✔
354
    }
355
    // (if cond then else)
356
    return fmt::format(
26✔
357
        "{}\n{}\n{}{})",
26✔
358
        if_cond_formatted,
359
        format(then_node, indent + 1, true),
26✔
360
        format(node.constList()[3], indent + 1, true),
26✔
361
        node.constList()[3].commentAfter().empty() ? "" : ("\n" + prefix(indent)));
26✔
362
}
48✔
363

364
std::string Formatter::formatLoop(const Node& node, const std::size_t indent)
10✔
365
{
10✔
366
    const Node cond_node = node.constList()[1];
10✔
367
    const Node body_node = node.constList()[2];
10✔
368

369
    bool cond_on_newline = false;
10✔
370
    std::string formatted_cond = format(cond_node, indent + 1, false);
10✔
371
    if (formatted_cond.find('\n') != std::string::npos)
10✔
372
        cond_on_newline = true;
2✔
373

374
    if (cond_on_newline || shouldSplitOnNewline(body_node))
10✔
375
        return fmt::format(
8✔
376
            "(while{}{}\n{})",
4✔
377
            cond_on_newline ? "\n" : " ",
4✔
378
            formatted_cond,
379
            format(body_node, indent + 1, true));
4✔
380
    return fmt::format(
6✔
381
        "(while {} {})",
6✔
382
        formatted_cond,
383
        format(body_node, indent + 1, false));
6✔
384
}
10✔
385

386
std::string Formatter::formatBegin(const Node& node, const std::size_t indent, const bool after_newline)
58✔
387
{
58✔
388
    // only the keyword begin is present
389
    if (node.constList().size() == 1)
58✔
390
        return "{}";
8✔
391

392
    // after a new line, we need to increment our indentation level
393
    // if the block is a top level one, we also need to increment indentation level
394
    const std::size_t inner_indentation = indent + (after_newline ? 1 : 0) + (indent == 0 ? 1 : 0);
50✔
395

396
    std::string output = "{\n";
50✔
397
    // skip begin keyword
398
    for (std::size_t i = 1, end = node.constList().size(); i < end; ++i)
144✔
399
    {
400
        const Node child = node.constList()[i];
94✔
401
        // we want to preserve the node grouping by the user, but remove useless duplicate new line
402
        // but that shouldn't apply to the first node of the block
403
        if (shouldAddNewLineBetweenNodes(node, i) && i > 1)
94✔
UNCOV
404
            output += "\n";
×
405

406
        output += format(child, inner_indentation, true);
94✔
407
        if (i != end - 1)
94✔
408
            output += "\n";
44✔
409
    }
94✔
410

411
    // if the last node has a comment, add a new line
412
    if (!node.constList().empty() && !node.constList().back().commentAfter().empty())
50✔
413
        output += "\n" + prefix(indent) + "}";
2✔
414
    else
415
        output += " }";
48✔
416
    return output;
50✔
417
}
58✔
418

419
std::string Formatter::formatImport(const Node& node, const std::size_t indent)
20✔
420
{
20✔
421
    const Node package_node = node.constList()[1];
20✔
422
    std::string package;
20✔
423

424
    if (!package_node.comment().empty())
20✔
425
        package += "\n" + formatComment(package_node.comment(), indent + 1) + prefix(indent + 1);
2✔
426
    else
427
        package += " ";
18✔
428

429
    for (std::size_t i = 0, end = package_node.constList().size(); i < end; ++i)
58✔
430
    {
431
        package += format(package_node.constList()[i], indent + 1, false);
38✔
432
        if (i != end - 1)
38✔
433
            package += ".";
18✔
434
    }
38✔
435

436
    const Node symbols = node.constList()[2];
20✔
437
    if (symbols.nodeType() == NodeType::Symbol && symbols.string() == "*")
20✔
438
        package += ":*";
2✔
439
    else  // symbols is a list
440
    {
441
        if (const auto& sym_list = symbols.constList(); !sym_list.empty())
28✔
442
        {
443
            const bool comment_after_last = !sym_list.back().commentAfter().empty();
10✔
444

445
            for (const auto& sym : sym_list)
24✔
446
            {
447
                if (sym.comment().empty())
14✔
448
                {
449
                    if (comment_after_last)
8✔
450
                        package += "\n" + prefix(indent + 1) + ":" + sym.string();
2✔
451
                    else
452
                        package += " :" + sym.string();
6✔
453
                }
8✔
454
                else
455
                    package += "\n" + formatComment(sym.comment(), indent + 1) + prefix(indent + 1) + ":" + sym.string();
6✔
456
            }
14✔
457

458
            if (comment_after_last)
10✔
459
            {
460
                package += " " + formatComment(sym_list.back().commentAfter(), /* indent= */ 0);
2✔
461
                package += "\n" + prefix(indent + 1);
2✔
462
            }
2✔
463
        }
10✔
464
    }
465

466
    return fmt::format("(import{})", package);
20✔
467
}
20✔
468

469
std::string Formatter::formatDel(const Node& node, const std::size_t indent)
4✔
470
{
4✔
471
    std::string formatted_sym = format(node.constList()[1], indent + 1, false);
4✔
472
    if (formatted_sym.find('\n') != std::string::npos)
4✔
473
        return fmt::format("(del\n{})", formatted_sym);
2✔
474
    return fmt::format("(del {})", formatted_sym);
2✔
475
}
4✔
476

477
std::string Formatter::formatCall(const Node& node, const std::size_t indent)
200✔
478
{
200✔
479
    bool is_list = false;
200✔
480
    if (!node.constList().empty() && node.constList().front().nodeType() == NodeType::Symbol &&
374✔
481
        node.constList().front().string() == "list")
174✔
482
        is_list = true;
4✔
483

484
    bool is_multiline = false;
200✔
485

486
    std::vector<std::string> formatted_args;
200✔
487
    for (std::size_t i = 1, end = node.constList().size(); i < end; ++i)
516✔
488
    {
489
        formatted_args.push_back(format(node.constList()[i], indent, false));
316✔
490
        // if we have at least one argument taking multiple lines, split them all on their own line
491
        if (formatted_args.back().find('\n') != std::string::npos || !node.constList()[i].commentAfter().empty())
316✔
492
            is_multiline = true;
12✔
493
    }
316✔
494

495
    std::string output = is_list ? "[" : ("(" + format(node.constList()[0], indent, false));
200✔
496
    for (std::size_t i = 0, end = formatted_args.size(); i < end; ++i)
516✔
497
    {
498
        const std::string formatted_node = formatted_args[i];
316✔
499
        if (is_multiline)
316✔
500
            output += "\n" + format(node.constList()[i + 1], indent + 1, true);
20✔
501
        else
502
            output += (is_list && i == 0 ? "" : " ") + formatted_node;
296✔
503
    }
316✔
504
    if (!node.constList().back().commentAfter().empty())
200✔
505
        output += "\n" + prefix(indent);
6✔
506
    output += is_list ? "]" : ")";
200✔
507
    return output;
200✔
508
}
200✔
509

510
std::string Formatter::formatMacro(const Node& node, const std::size_t indent)
20✔
511
{
20✔
512
    if (isListStartingWithKeyword(node, Keyword::If))
20✔
513
        return formatCondition(node, indent, /* is_macro= */ true);
6✔
514

515
    std::string output = "($ ";
14✔
516
    bool after_newline = false;
14✔
517

518
    for (std::size_t i = 0, end = node.constList().size(); i < end; ++i)
54✔
519
    {
520
        output += format(node.constList()[i], indent + 1, after_newline);
40✔
521
        after_newline = false;
40✔
522

523
        if (!node.constList()[i].commentAfter().empty())
40✔
524
        {
525
            output += "\n";
4✔
526
            after_newline = true;
4✔
527
        }
4✔
528
        else if (i != end - 1)
36✔
529
            output += " ";
24✔
530
    }
40✔
531

532
    return output + ")";
14✔
533
}
20✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc