Skip to content

Commit b8c59d3

Browse files
committed
fix(fe): pre-declare web types such as HeadersInit
TypeScript's dom.d.ts declares web-defined type aliases such as HeadersInit (used for fetch()). Therefore, TypeScript code in the wild relies on these pre-declared types. Teach quick-lint-js about these types, fixing false positive warnings.
1 parent 5b3895d commit b8c59d3

File tree

7 files changed

+115
-1
lines changed

7 files changed

+115
-1
lines changed

docs/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Semantic Versioning.
3030
code. ([#1152][])
3131
* Commas are now allowed after methods in interfaces in `.d.ts` files. (They
3232
were previously only allowed in `.ts` files.) ([#1171][])
33+
* quick-lint-js no longer reports warnings for web types such as `HeadersInit`
34+
in TypeScript code. ([#1180][])
3335

3436
## 3.0.0 (2024-01-01)
3537

@@ -1392,6 +1394,7 @@ Beta release.
13921394
[#1152]: https://github.com/quick-lint/quick-lint-js/issues/1152
13931395
[#1155]: https://github.com/quick-lint/quick-lint-js/issues/1155
13941396
[#1171]: https://github.com/quick-lint/quick-lint-js/issues/1171
1397+
[#1180]: https://github.com/quick-lint/quick-lint-js/issues/1180
13951398

13961399
[E0001]: https://quick-lint-js.com/errors/E0001/
13971400
[E0003]: https://quick-lint-js.com/errors/E0003/

src/quick-lint-js/configuration/configuration.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ bool Configuration::should_remove_global_variable(String8_View name) {
413413
group.globals == group_globals ? "" :
414414
group.non_shadowable_globals == group_globals ? "non_shadowable_" :
415415
group.non_writable_globals == group_globals ? "non_writable_" :
416+
group.type_only_globals == group_globals ? "type_only_" :
416417
"???";
417418
// clang-format on
418419
std::fprintf(

src/quick-lint-js/fe/global-variables-browser-generated.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,94 @@ const Char8 global_variables_web_worker[] =
12331233
u8"removeEventListener\0"
12341234
u8"self\0"
12351235
u8"terminate\0";
1236+
1237+
const Char8 global_variables_web_types[] =
1238+
u8"AlgorithmIdentifier\0"
1239+
u8"ArrayBufferView\0"
1240+
u8"BigInteger\0"
1241+
u8"BinaryData\0"
1242+
u8"BlobPart\0"
1243+
u8"BodyInit\0"
1244+
u8"BufferSource\0"
1245+
u8"COSEAlgorithmIdentifier\0"
1246+
u8"CSSKeywordish\0"
1247+
u8"CSSNumberish\0"
1248+
u8"CSSUnparsedSegment\0"
1249+
u8"CanvasImageSource\0"
1250+
u8"ClipboardItemData\0"
1251+
u8"ClipboardItemDataType\0"
1252+
u8"ClipboardItems\0"
1253+
u8"ConstrainBoolean\0"
1254+
u8"ConstrainDOMString\0"
1255+
u8"ConstrainDouble\0"
1256+
u8"ConstrainPoint2D\0"
1257+
u8"ConstrainULong\0"
1258+
u8"ContainerBasedOffset\0"
1259+
u8"CookieList\0"
1260+
u8"CryptoKeyID\0"
1261+
u8"DOMHighResTimeStamp\0"
1262+
u8"DOMTimeStamp\0"
1263+
u8"EventHandler\0"
1264+
u8"FileSystemWriteChunkType\0"
1265+
u8"Float32List\0"
1266+
u8"FormDataEntryValue\0"
1267+
u8"GLbitfield\0"
1268+
u8"GLboolean\0"
1269+
u8"GLbyte\0"
1270+
u8"GLclampf\0"
1271+
u8"GLenum\0"
1272+
u8"GLfloat\0"
1273+
u8"GLint\0"
1274+
u8"GLint64\0"
1275+
u8"GLintptr\0"
1276+
u8"GLshort\0"
1277+
u8"GLsizei\0"
1278+
u8"GLsizeiptr\0"
1279+
u8"GLubyte\0"
1280+
u8"GLuint\0"
1281+
u8"GLuint64\0"
1282+
u8"GLushort\0"
1283+
u8"GeometryNode\0"
1284+
u8"HTMLOrSVGImageElement\0"
1285+
u8"HTMLOrSVGScriptElement\0"
1286+
u8"HTMLString\0"
1287+
u8"HashAlgorithmIdentifier\0"
1288+
u8"HeadersInit\0"
1289+
u8"ImageBitmapSource\0"
1290+
u8"Int32List\0"
1291+
u8"LineAndPositionSetting\0"
1292+
u8"MediaProvider\0"
1293+
u8"Megabit\0"
1294+
u8"MessageEventSource\0"
1295+
u8"Millisecond\0"
1296+
u8"NamedCurve\0"
1297+
u8"OffscreenRenderingContext\0"
1298+
u8"OnBeforeUnloadEventHandler\0"
1299+
u8"OnErrorEventHandler\0"
1300+
u8"PasswordCredentialInit\0"
1301+
u8"PerformanceEntryList\0"
1302+
u8"PushMessageDataInit\0"
1303+
u8"RTCRtpTransform\0"
1304+
u8"ReadableStreamController\0"
1305+
u8"ReadableStreamReader\0"
1306+
u8"RenderingContext\0"
1307+
u8"ReportList\0"
1308+
u8"RequestInfo\0"
1309+
u8"RotationMatrixType\0"
1310+
u8"ScriptString\0"
1311+
u8"ScriptURLString\0"
1312+
u8"ScrollTimelineOffset\0"
1313+
u8"SmallCryptoKeyID\0"
1314+
u8"StartInDirectory\0"
1315+
u8"TexImageSource\0"
1316+
u8"TimerHandler\0"
1317+
u8"TrustedTimerHandler\0"
1318+
u8"TrustedType\0"
1319+
u8"Uint32List\0"
1320+
u8"UvmEntries\0"
1321+
u8"UvmEntry\0"
1322+
u8"XMLHttpRequestBodyInit\0"
1323+
u8"XRWebGLRenderingContext\0";
12361324
}
12371325

12381326
// quick-lint-js finds bugs in JavaScript programs.

src/quick-lint-js/fe/global-variables.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ const Global_Group global_groups[] = {
461461
{
462462
.name = u8"browser",
463463
.globals = global_variables_browser,
464+
.type_only_globals = global_variables_web_types,
464465
.globals_count = 990,
466+
.type_only_globals_count = 86,
465467
},
466468
{
467469
.name = u8"bun",
@@ -518,7 +520,9 @@ const Global_Group global_groups[] = {
518520
{
519521
.name = u8"web-worker",
520522
.globals = global_variables_web_worker,
523+
.type_only_globals = global_variables_web_types,
521524
.globals_count = 232,
525+
.type_only_globals_count = 86,
522526
},
523527
};
524528
static_assert(global_group_count == std::size(global_groups),

src/quick-lint-js/fe/global-variables.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern const Global_Group global_groups[];
2525

2626
extern const Char8 global_variables_browser[];
2727
extern const Char8 global_variables_web_worker[];
28+
extern const Char8 global_variables_web_types[];
2829
}
2930

3031
// quick-lint-js finds bugs in JavaScript programs.

test/test-variable-analyzer-globals.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ constexpr const Char8 *non_writable_global_variables[] = {
8989
};
9090

9191
constexpr const Char8 *type_only_global_variables[] = {
92+
// TypeScript
9293
u8"Awaited",
9394
u8"Capitalize",
9495
u8"ConstructorParameters",
@@ -110,6 +111,9 @@ constexpr const Char8 *type_only_global_variables[] = {
110111
u8"ThisType",
111112
u8"Uncapitalize",
112113
u8"Uppercase",
114+
115+
// Web
116+
u8"HeadersInit",
113117
};
114118

115119
constexpr const Char8 *nodejs_global_variables[] = {

tools/browser-globals/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ async function mainAsync() {
6868
"Worker",
6969
"WorkerGlobalScope",
7070
]),
71+
types: exposedGlobals.getGlobalsForNamespaces(["types"]),
7172
outputStream: process.stdout,
7273
});
7374
}
@@ -234,7 +235,10 @@ function collectExposedGlobals(globals, idlObject, allIDLObjects) {
234235
case "dictionary":
235236
case "enum":
236237
case "interface mixin":
238+
break;
239+
237240
case "typedef":
241+
globals.addGlobal("types", idlObject.name);
238242
break;
239243

240244
default:
@@ -415,7 +419,12 @@ class NullWriter extends stream.Writable {
415419
}
416420
}
417421

418-
function writeCPPFile({ browserGlobals, webWorkerGlobals, outputStream }) {
422+
function writeCPPFile({
423+
browserGlobals,
424+
webWorkerGlobals,
425+
types,
426+
outputStream,
427+
}) {
419428
function writeStrings(strings) {
420429
for (let string of strings) {
421430
if (!/^[A-Za-z0-9_$]+$/g.test(string)) {
@@ -444,6 +453,10 @@ const Char8 global_variables_browser[] =`);
444453
const Char8 global_variables_web_worker[] =`);
445454
writeStrings(webWorkerGlobals);
446455
outputStream.write(`;
456+
457+
const Char8 global_variables_web_types[] =`);
458+
writeStrings(types);
459+
outputStream.write(`;
447460
}
448461
449462
// quick-lint-js finds bugs in JavaScript programs.

0 commit comments

Comments
 (0)