Skip to content

Commit

Permalink
feat(formatter): warning when the formatter deletes comment(s) by mis…
Browse files Browse the repository at this point in the history
…take
  • Loading branch information
SuperFola committed Jun 15, 2024
1 parent 250e332 commit 6639646
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- fuzzing step in the CI
- better error reporting on unknown import
- check on number of arguments passed to `type`
- warning when the formatter deletes comment(s) by mistake

### Changed
- instructions are on 4 bytes: 1 byte for the instruction, 1 byte of padding, 2 bytes for an immediate argument
Expand Down
1 change: 1 addition & 0 deletions include/CLI/Formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Formatter final
std::string m_output;

void processAst(const Ark::internal::Node& ast);
void warnIfCommentsWereRemoved(const std::string& original_code, const std::string& filename);

static bool isListStartingWithKeyword(const Ark::internal::Node& node, Ark::internal::Keyword keyword);
static bool isBeginBlock(const Ark::internal::Node& node);
Expand Down
15 changes: 15 additions & 0 deletions src/arkscript/Formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <CLI/Formatter.hpp>

#include <fmt/core.h>
#include <termcolor/proxy.hpp>

#include <Ark/Files.hpp>
#include <Ark/Exceptions.hpp>
Expand All @@ -25,6 +26,7 @@ void Formatter::run()
const std::string code = Utils::readFile(m_filename);
m_parser.process(m_filename, code);
processAst(m_parser.ast());
warnIfCommentsWereRemoved(code, ARK_NO_NAME_FILE);
}
catch (const CodeError& e)
{
Expand All @@ -38,6 +40,7 @@ void Formatter::runWithString(const std::string& code)
{
m_parser.process(ARK_NO_NAME_FILE, code);
processAst(m_parser.ast());
warnIfCommentsWereRemoved(code, ARK_NO_NAME_FILE);
}
catch (const CodeError& e)
{
Expand Down Expand Up @@ -75,6 +78,18 @@ void Formatter::processAst(const Node& ast)
}
}

void Formatter::warnIfCommentsWereRemoved(const std::string& original_code, const std::string& filename)
{
if (std::ranges::count(original_code, '#') != std::ranges::count(m_output, '#'))
{
std::cout << termcolor::yellow << "Warning" << termcolor::reset << ": one or more comments from the original source code seem to have been removed by mistake while formatting ";
if (filename != ARK_NO_NAME_FILE)
std::cout << "'" << filename << "'" << std::endl;
else
std::cout << "file" << std::endl;
std::cout << "Please fill an issue on GitHub: https://github.com/ArkScript-lang/Ark" << std::endl;
}
}

bool Formatter::isListStartingWithKeyword(const Node& node, const Keyword keyword)
{
Expand Down

0 comments on commit 6639646

Please sign in to comment.