Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new user/beginner friendly errors system #505

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

NiumXp
Copy link
Contributor

@NiumXp NiumXp commented Jul 1, 2024

Hi, this is one of my drafts (maybe 7 months ago) focused on beginners (and quality of life) that I wrote for norminette when @matthieu42Network was accepting my PRs, I'm not sure if he's still available, but recently it seems like new people became collaborators, this made me happy and willing to continue with contributions.

New errors formatting

To use the new format is not necessary a new configuration, just run your norminette:

(.venv) niumxp@DESKTOP-LRN507C:~/oss/norminette$ cat -A test.c
#define exit 42$
$
int^IMain(void) {$
^Ireturn exit;$
}$
(.venv) niumxp@DESKTOP-LRN507C:~/oss/norminette$ norminette test.c
test.c:1:1 INVALID_HEADER Missing or invalid 42 header
test.c:1:9 MACRO_NAME_CAPITAL
     1 | #define exit 42
       |         ^^^^ Macro name must be capitalized
test.c:3:5 FORBIDDEN_CHAR_NAME
     3 | int Main(void) {
       |     ^^^^ user defined identifiers should contain only lowercase characters, digits or '_'
test.c:3:16 BRACE_NEWLINE
     3 | int Main(void) {
       |                ^ Expected newline before brace
test.c:4:12 RETURN_PARENTHESIS
     4 |     return exit;
       |            ^^^^ Return value must be in parenthesis

I added a red color to the errors:
image

@xtrm-en
Copy link

xtrm-en commented Sep 11, 2024

Subjective nit, this should probably have a bit more padding for readability's sake. Other than that, looks pretty amazing :)

Copy link

@otto-mata otto-mata left a comment

Choose a reason for hiding this comment

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

Overall a very good new feature, the norminette would really benefit from its addition!
Some changes are still necessary in my opinion, but please you are absolutely free to discuss your choices!

Bests,
tblochet

Comment on lines 189 to +210
class HumanizedErrorsFormatter(_formatter):
def __str__(self) -> str:
output = ''
for file in self.files:
for error in file.errors:
highlight = error.highlights[0]
# Location
output += f"\x1b[;97m{file.path}:{highlight.lineno}:{highlight.column}\x1b[0m"
output += ' ' + error.name
if not highlight.length:
output += ' ' + error.text
if highlight.length:
# Line
output += f"\n {highlight.lineno:>5} | {file[highlight.lineno,].translated}"
# Arrow
output += "\n | " + ' ' * (highlight.column - 1)
output += f"\x1b[0;91m{'^' * (highlight.length or 0)} {highlight.hint or error.text}\x1b[0m"
output += '\n'
return output


class ShortErrorsFormatter(_formatter):

Choose a reason for hiding this comment

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

It might be a better idea to create another _formatter class, rather than modifying the existing default class.
maybe class CompilerLikeErrorsFormatter

Comment on lines +98 to +100
# if len(item) == 2:
# lineno, column = item
# return self._line_to_index[lineno + column]

Choose a reason for hiding this comment

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

Unnecessary comments

Choose a reason for hiding this comment

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

See last comment, potentially breaking change

@@ -151,6 +151,7 @@ Error: TOO_MANY_WS (line: 7, col: 1): Extra whitespaces for indent
Error: TAB_REPLACE_SPACE (line: 7, col: 2): Found tab when expecting space
Error: TOO_MANY_WS (line: 9, col: 1): Extra whitespaces for indent level
Error: MACRO_NAME_CAPITAL (line: 9, col: 10): Macro name must be capitalized
Error: SPC_BEFORE_NL (line: 26, col: 2): Space before newline

Choose a reason for hiding this comment

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

This is a potentially breaking change, it should be in a separate PR

Comment on lines -75 to +79
context.new_error("RETURN_PARENTHESIS", context.peek_token(tmp))
token = context.peek_token(tmp)
error = Error.from_name("RETURN_PARENTHESIS")
error.add_highlight(token)
context.errors.add(error)

Choose a reason for hiding this comment

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

It might be better to do a complete refacto on this,
There are about 145 more context.new_error() still being used.
Maybe in another PR ?

@@ -11,7 +11,8 @@
from norminette.registry import Registry
from norminette.errors import JSONErrorsFormatter
from norminette.errors import Error, Errors, Highlight as H
from norminette.errors import HumanizedErrorsFormatter
from norminette.errors import ShortErrorsFormatter
# from norminette.errors import HumanizedErrorsFormatter

Choose a reason for hiding this comment

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

Unnecessary comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants