-
Notifications
You must be signed in to change notification settings - Fork 16
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
Allow idents to start with _
#244
Allow idents to start with _
#244
Conversation
(None, name, []) if name.chars().next().map_or(true, char::is_lowercase) => { | ||
(None, name, []) | ||
if name | ||
.chars() | ||
.next() | ||
.map_or(true, |c| c == '_' || c.is_lowercase()) => |
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.
I'm kinda confused if it is okay that this function returns PathOrIdentifier::Identifier("_")
for the placeholder with this change. Before, PathOrIdentifier::Path(vec!["_"])
would be returned. Actually, both look wrong to me.
Can you have a look what happens of you add a case ↓ before the changed one?
(None, "_", _) => fail.parse_next(initial_i),
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.
it didn't change anything as far as I could see.
Good catch! Interesting that this case did not occur earlier. |
09e1534
to
05ae1ba
Compare
The original patch was not enough so I greatly extended it. It is now much more useful. :) |
Sorry, I'm not quite happy with the second commit. IMHO on the left-hand side of an assignment, the target, placeholders should always be allowed without restrictions. Maybe you are are only interested in the side-effect of an assignment Maybe you could remove the 2nd commit from this PR and move it into a new one, and I'll have a look at it, then? |
Good point. Wouldn't you prefer that I update the second commit to allow it instead? |
I have no strong opinion. :) We can keep it in one PR. |
Then leave me some time so I can check what actual cases are allowed on the lhs. |
Take as much time as you need. :) |
I think I've opened this can of worms with my initial issue on Askama: https://github.com/djc/askama/issues/1105 (I've tested all of the following examples on the current pull request commit GuillaumeGomez@05ae1ba) In essence, for variable names I think the following cases should be covered as they are allowed by Rust (and most other programming languages):
Thus, it would be nice before merging this pull-request, to cover a few more corner-cases. It would be nice (unless it conflicts with the Jinja syntax) that all Rust valid identifiers (as specified in https://doc.rust-lang.org/reference/identifiers.html) to be considered valid identifiers for |
For this one, please open another issue. For the rest, I'll add a regression test for them. |
05ae1ba
to
c2b0db0
Compare
I re-allowed the few cases that should not be forbidden. :) |
rinja_derive/src/generator.rs
Outdated
@@ -1118,6 +1118,7 @@ impl<'a> Generator<'a> { | |||
_ => Ok(false), | |||
} | |||
} | |||
Target::Placeholder("_") => Ok(false), |
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.
Isn't wildcard ..
handling needed, too?
Target::Rest(var) => match &**var {
Some(var) => "what's done in Target::Name(_)",
None => Ok(false),
}
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.
Very good point!
c2b0db0
to
3d13a6e
Compare
Added handling for |
rinja_derive/src/generator.rs
Outdated
@@ -997,6 +997,7 @@ impl<'a> Generator<'a> { | |||
_ => Ok(false), | |||
} | |||
} | |||
Target::Placeholder("_" | "..") => Ok(false), |
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.
The second case will never be true, though. Target::Placeholder(_)
only ever captures "_"
, and Target::Rest(_)
captures ..
or .. @ var
.
Both variant should probably contain a WithSpan<'a, ()>
instead of the captured token, to make the meaning more clear.
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.
Ok, gonna clean up this code because there is no point in keeping content if it's not used.
3d13a6e
to
53f15d8
Compare
rinja_parser/src/target.rs
Outdated
Target::Name("_") | Target::Placeholder(_) => { | ||
return Err(winnow::error::ErrMode::Cut(ErrorContext::new( | ||
"reserved keyword `_` cannot be used here", | ||
i, | ||
))); | ||
} |
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.
IMHO, this use of placeholders is fine. Maybe I want to {% let _ = iter.next() %}
or {% let [_] = slice.try_into().unwrap() %}
, because I want to test something in my code, generate an effect, whatever. Sorry, but I think whole function can the removed, because I don't want to restrict what can be skipped on the lhs of an assignment. :-/
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.
Ok, let's go then. :)
Ah, we have a new license in our dependency tree apparently. |
e160721
to
2ec7740
Compare
allow = ["Apache-2.0", "MIT", "MIT-0", "Unicode-DFS-2016"] | ||
allow = ["Apache-2.0", "MIT", "MIT-0", "Unicode-3.0"] |
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.
Yeah, the license was changed "about 5 hours ago" :)
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.
Lucky me. :')
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.
Thank you very much for the change! :)
Until now, we didn't allow
_x
as ident. Fixed it. :)