Skip to content

Commit f402e49

Browse files
committed
Fix parsing of (x && <y/>)
(x && <y/>) is reporting E0026 (missing operand for operator) between '&&' and '<'. This is because we only parse JSX if the precedence allows binary operators. This requirement is a mistake; we should parse JSX regardless of the precedence. Fix the requirement, fixing the false positive.
1 parent 4e72a14 commit f402e49

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

docs/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66
quick-lint-js' version numbers are arbitrary. quick-lint-js does *not* adhere to
77
Semantic Versioning.
88

9+
## Unreleased
10+
11+
### Fixed
12+
13+
* `x && <Element/>` no longer falsely reports [E0026][] (missing operand for
14+
operator). (Thanks to [Piotr Dąbrowski][] for reporting.)
15+
916
## 2.1.0 (2022-02-09)
1017

1118
[Downloads](https://c.quick-lint-js.com/releases/2.1.0/)
@@ -392,6 +399,7 @@ Beta release.
392399
[Lee Wannacott]: https://github.com/LeeWannacott
393400
[Matheus de Sousa]: https://github.com/keyehzy
394401
[Nico Sonack]: https://github.com/herrhotzenplotz
402+
[Piotr Dąbrowski]: https://github.com/yhnavein
395403
[Shivam Mehta]: https://github.com/maniac-en
396404
[coc.nvim]: https://github.com/neoclide/coc.nvim
397405
[config-global-groups]: https://quick-lint-js.com/config/#global-groups
@@ -404,6 +412,7 @@ Beta release.
404412
[E0016]: https://quick-lint-js.com/errors/E0016/
405413
[E0019]: https://quick-lint-js.com/errors/E0019/
406414
[E0020]: https://quick-lint-js.com/errors/E0020/
415+
[E0026]: https://quick-lint-js.com/errors/E0026/
407416
[E0036]: https://quick-lint-js.com/errors/E0036/
408417
[E0038]: https://quick-lint-js.com/errors/E0038/
409418
[E0040]: https://quick-lint-js.com/errors/E0040/

src/quick-lint-js/parse-expression-inl.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,16 +611,15 @@ expression* parser::parse_primary_expression(Visitor& v, precedence prec) {
611611
return function;
612612
}
613613
}
614+
if (this->peek().type == token_type::less) {
615+
// <MyComponent /> (JSX)
616+
return this->parse_jsx_expression(v);
617+
}
614618
expression* ast =
615619
this->make_expression<expression::_missing>(this->peek().span());
616620
if (prec.binary_operators) {
617-
if (this->peek().type == token_type::less) {
618-
// <MyComponent /> (JSX)
619-
return this->parse_jsx_expression(v);
620-
} else {
621-
this->error_reporter_->report(
622-
error_missing_operand_for_operator{this->peek().span()});
623-
}
621+
this->error_reporter_->report(
622+
error_missing_operand_for_operator{this->peek().span()});
624623
}
625624
return ast;
626625
}

test/test-parse-expression-jsx.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,13 @@ TEST_F(test_parse_expression, tag_with_member_expression) {
320320
ASSERT_EQ(summarize(ast), "jsxmemberelement((a-b, c-d))");
321321
}
322322
}
323+
324+
TEST_F(test_parse_expression, jsx_with_binary_operator) {
325+
{
326+
expression* ast = this->parse_expression(u8"x && <div />"_sv, jsx_options);
327+
ASSERT_EQ(summarize(ast), "binary(var x, jsxelement(div))");
328+
}
329+
}
323330
}
324331
}
325332

0 commit comments

Comments
 (0)