Skip to content

Commit 6b3fdbe

Browse files
committed
fix(fe): don't emit React-specific diags if Preact was imported
If both React and Preact modules are imported, it's ambiguous whether JSX references React or Preact. Be conservative and don't report any React-specific diagnostics in this case.
1 parent 7d5a279 commit 6b3fdbe

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

docs/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ Semantic Versioning.
2222
setting its thread name on FreeBSD.
2323
* React-specific JSX diagnostics, such as [E0193][] ("misspelled React
2424
attribute; write 'className' instead"), are now only reported when 'react' is
25-
imported. This fixes false warnings in Preact code. ([#1152][])
25+
imported and if 'preact' is not imported. This fixes false warnings in Preact
26+
code. ([#1152][])
2627

2728
## 3.0.0 (2024-01-01)
2829

src/quick-lint-js/fe/parse-statement.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5327,6 +5327,10 @@ void Parser::visited_module_import(const Token &module_name) {
53275327
starts_with(module_name_unescaped, u8"react-dom/"_sv)) {
53285328
this->imported_react_ = true;
53295329
}
5330+
if (module_name_unescaped == u8"preact"_sv ||
5331+
starts_with(module_name_unescaped, u8"preact/"_sv)) {
5332+
this->imported_preact_ = true;
5333+
}
53305334
}
53315335

53325336
void Parser::parse_and_visit_variable_declaration_statement(

src/quick-lint-js/fe/parse.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ void Parser::check_all_jsx_attributes() {
164164
break;
165165

166166
case Parser_JSX_Mode::auto_detect:
167+
if (this->imported_react_ + this->imported_preact_ > 1) {
168+
// Ambiguous. Don't report any diagnostics.
169+
break;
170+
}
167171
if (this->imported_react_) {
168172
goto react;
169173
}

src/quick-lint-js/fe/parse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,8 @@ class Parser {
11241124

11251125
// Heuristic. True if React.js was imported.
11261126
bool imported_react_ = false;
1127+
// Heuristic. True if Preact was imported.
1128+
bool imported_preact_ = false;
11271129

11281130
bool in_top_level_ = true;
11291131
bool in_async_function_ = false;

test/test-parse-jsx-react.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,15 @@ TEST_F(Test_Parse_JSX_React, diagnostic_if_auto_detect_and_react_is_imported) {
180180
u8"Diag_JSX_Event_Attribute_Should_Be_Camel_Case"_diag,
181181
u8"Diag_JSX_Attribute_Renamed_By_React"_diag, jsx_auto_detect_options);
182182
}
183+
184+
TEST_F(Test_Parse_JSX_React,
185+
no_diagnostic_if_auto_detect_and_preact_is_imported) {
186+
// Preact is incompatible with React.
187+
test_parse_and_visit_module(
188+
u8"import React from 'react';"_sv
189+
u8"c = <div onclick={handler} class=\"c\" />;"_sv
190+
u8"import Preact from 'preact';"_sv,
191+
no_diags, jsx_auto_detect_options);
192+
}
183193
}
184194
}

0 commit comments

Comments
 (0)