-
Notifications
You must be signed in to change notification settings - Fork 657
refactor(rome_js_analyze): has_truthy_value
#4073
Conversation
✅ Deploy Preview for docs-rometools canceled.
|
Parser conformance results on ubuntu-latestjs/262
jsx/babel
symbols/microsoft
ts/babel
ts/microsoft
|
131b6e0
to
478d894
Compare
/// | ||
/// Fhe function accepts a `closure` that will be used to for further check the value. | ||
pub fn has_truthy_value<F>(&self, closure: F) -> SyntaxResult<bool> | ||
where |
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 wasn't immediately clear to me what the closure is doing from reading the call sites only
What do you think of implementing this method as an extension method on SyntaxTokenText instead. Doing so would remove the need for the closure and the matching for many tokens could even be performed on the token kind rather than the text
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 had implemented the function without the use of the closure, but unfortunately, that degraded the DX, because I couldn't use the APIs I wanted.
On the other end, I didn't want to return SyntaxTokenText
instead of bool
because the function doesn't always return the text, but it actually checks if it contains a "truthy" value, so it felt weird.
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. I think I understand know what is happening. I wonder if it would be easier to understand if attribute has a function as_static_value() -> Option<StaticAttributeValue>
enum StaticAttributeValue {
Token(JsSyntaxToken),
// For strings. Or you can have `StringLiteral`, and `JsxString` variants if you don't want to duplicate the `inner_text` methods
String(JsSyntaxToken),
}
impl StaticAttributeValue {
fn is_truthy(&self) -> bool { ... }
fn text(&self) -> &str { .. }
fn is_falsy(&self) -> bool {}
}
It avoids the somewhat awkward combination of truthy
with any other check that the caller wants to perform.
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 spent a bit more time thinking about this and it should be possible to even go one step further:
- Change
inner_string_text
to return aSyntaxResult<QuotedString>
whereQuotedString
is a thin wrapper around a token
struct QuotedString(JsSyntaxToken);
impl QuotedString {
fn new(token: JsSyntaxToken) -> Self {
assert!(matches!(token.kind(), STRING_LITERAL_TOKEN | JSX_STRING_TOKEN | ...));
Self(token)
}
fn text(self) -> &str {
self.0.text_trimmed().trim_start(['"', '\'']).trim_end(['"', '\''])
}
fn quoted_text(&self) -> &str { self.0.text_trimmed() }
...
}
impl Deref for QuotedString {
type Target = str;
fn deref(&self) -> &Self::Target { self.text() }
}
- Introduce a new enum
JsStaticValue
enum StaticValue {
Boolean(JsSyntaxToken),
Null(JsSyntaxToken),
Undefined(JsSyntaxToken),
Number(JsSyntaxToken),
String(QuotedString),
TemplateChunk(JsSyntaxToken),
}
impl StaticValue {
fn is_truthy(&self) -> bool {... }
fn text(&self) -> &str { ... }
}
- Add a
AnyJsExpression::as_static_value()
method that returns Optionand do the same for
AnyJsxAttribute` - Stretch: Replace the
as_string_constant
method with a method that returnsOption<StringConstant>
value whereStringConstant is an enum. Finally, remove the
with_string_constantmethod (you can use
as_string_constant` directly.
enum StringConstant {
TemplateChunk(JsSyntaxToken),
String(QuotedString)
}
impl StringConstant {
fn text(&self) {
match self {
StringConstant::TemplateChunk(token) => token.text_trimmed(),
StringConstant::String(quoted) => quoted.text()
}
}
}
impl Deref for StringConstant { ... }
e3b3761
to
b3e3bf6
Compare
This PR is stale because it has been open 14 days with no activity. |
The continuos work is #4342, so I close this PR. |
Summary
This PR refactors some common logic among some rules; the new code is now implemented as API for the
JsxAttribute
.This is a pattern that is popping up in our rules, it was good to have a specialized function that checks value of the attributes.
Test Plan
Current snapshots should not change.
Documentation