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
Reimplement jinja[spacing] to avoid use of regex #2306
Conversation
7c6879c
to
a9b348f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fascinating use of the jinja lexer.
In the lexer-related work I've been doing, I've been using TOKEN_*
constants from jinja:
https://github.com/pallets/jinja/blob/main/src/jinja2/lexer.py#L61-L109
Typically I do from jinja2 import lexer as j2tokens
and then use j2tokens.TOKEN_OPERATOR
or similar. What do you think of that idiom?
2f30328
to
782ba5a
Compare
41f24e3
to
9e9c208
Compare
@drybjed I am not sure how well you will take this but this change seems to uncover at least one genuine jinja2 syntax error in debops repository, but you might find yourself bit overwhelmed by the amount of feedback linter is providing: Take it easy! And to quote from thread,... I hope you get paid by the hour... ;) |
5900e3f
to
e8e1db2
Compare
@cognifloyd There are 4 reformatting test that are failing which I do not know how to fix. If you can have a look it would be great. That PR took all my weekend and I might really benefit from another pair of eyes to deal with these last ones. All these tests were results of different errors I found while testing formatting of jinja2 expression on all eco repositories, so we should be pleased about the results. Few minutes ago I got another idea,... what if I would try to use black itself to format the snippets, assuming that it might be used programately. Still, doing that would not be a joy as I would have to identify blocks of tokens and recombine them before I feed them to black,.. so maybe not a very good idea. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ew. Yes those are difficult. Here are some ideas to hopefully give you an idea of what to look at next.
@ssbarnea Thanks for the heads up. I'll test the proposed changes and make fixes ASAP. |
553472f
to
c4a1965
Compare
This change rewrites jinja2 rule to make it use jinaja lex parser and avoid use of regexes for parsing, as they cause too many false positives. Fixes: ansible#2301 ansible#2285 ansible#2241 ansible#2209 ansible#2208 ansible#2120
@ssbarnea I'm currently working on fixing all these warnings on |
@drybjed Thanks. I got some progress today on them but again failed to pass all tests. That jinja spacing is a bit of a can of worms. I already introduce a condition to skip the spacing checks when newlines are found inside the jinja blocks, basically because we assume user added them for indentation purposes. The scope of checking spaces was more towards simple blocks. |
This change rewrites jinja2 rule to make it use jinaja lexer and
avoid use of regexes for parsing, as they cause too many false
positives.
Fixes: #2301
Fixes: #2285
Fixes: #2241
Fixes: #2209
Fixes: #2208
Fixes: #2120