Skip to content

Commit f9a1dd2

Browse files
committed
Don't warn when using browser globals
Create a script which scrapes web specifications to see what variables/classes/functions are global in web browsers. Teach quick-lint-js about these global variables to reduce false positives for browser code. Unfortunately, browsers have some global variables which might be common in user code, such as 'length' and 'name'. In the future, we might decide to trade some false positives for some false negatives. For now, be conservative and err on the side of false negatives by including all spec'd global variables. I didn't check if the globals can be reassigned or shadowed. Err on the side of false negatives by treating all these variables as writable and shadowable.
1 parent 59d630b commit f9a1dd2

File tree

12 files changed

+1568
-0
lines changed

12 files changed

+1568
-0
lines changed

docs/RELEASES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ Beta release.
3535

3636
### Fixed
3737

38+
* Using browser variables such as `document` and `window` no longer reports
39+
undesired undeclared variable warnings
3840
* `with` statements and `eval` no longer cause spurious undeclared variable
3941
warnings (implemented by [Himanshu][])
4042
* `++a[0];` no longer reports an assignment-to-const-variable error if `a` was

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ quick_lint_js_add_library(
5353
file-handle.cpp
5454
file-path.cpp
5555
file.cpp
56+
global-variables-browser.cpp
5657
gmo.cpp
5758
integer.cpp
5859
json.cpp
@@ -101,6 +102,7 @@ quick_lint_js_add_library(
101102
quick-lint-js/file-path.h
102103
quick-lint-js/file.h
103104
quick-lint-js/force-inline.h
105+
quick-lint-js/global-variables.h
104106
quick-lint-js/gmo.h
105107
quick-lint-js/have.h
106108
quick-lint-js/integer.h

src/configuration.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <algorithm>
55
#include <quick-lint-js/char8.h>
66
#include <quick-lint-js/configuration.h>
7+
#include <quick-lint-js/global-variables.h>
78
#include <quick-lint-js/lint.h>
89
#include <quick-lint-js/narrow-cast.h>
910
#include <quick-lint-js/unreachable.h>
@@ -23,6 +24,15 @@ ::simdjson::error_code get_bool_or_default(
2324
}
2425

2526
const global_declared_variable_set& configuration::globals() noexcept {
27+
if (this->add_global_group_browser_) {
28+
for (const char8** it = global_variables_browser; *it; ++it) {
29+
string8_view global(*it);
30+
if (!this->should_remove_global_variable(global)) {
31+
this->globals_.add_variable(global);
32+
}
33+
}
34+
}
35+
2636
if (this->add_global_group_ecmascript_) {
2737
const char8* writable_global_variables[] = {
2838
// ECMA-262 18.1 Value Properties of the Global Object
@@ -141,11 +151,16 @@ const std::optional<canonical_path>& configuration::config_file_path() const {
141151
}
142152

143153
void configuration::reset_global_groups() {
154+
this->add_global_group_browser_ = false;
144155
this->add_global_group_node_js_ = false;
145156
this->add_global_group_ecmascript_ = false;
146157
}
147158

148159
bool configuration::add_global_group(string8_view group_name) {
160+
if (group_name == u8"browser"sv) {
161+
this->add_global_group_browser_ = true;
162+
return true;
163+
}
149164
if (group_name == u8"ecmascript"sv) {
150165
this->add_global_group_ecmascript_ = true;
151166
return true;

0 commit comments

Comments
 (0)