Skip to content

Commit

Permalink
fix(fe): pre-declare web types such as HeadersInit
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
strager committed Jan 11, 2024
1 parent 5b3895d commit b8c59d3
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Semantic Versioning.
code. ([#1152][])
* Commas are now allowed after methods in interfaces in `.d.ts` files. (They
were previously only allowed in `.ts` files.) ([#1171][])
* quick-lint-js no longer reports warnings for web types such as `HeadersInit`
in TypeScript code. ([#1180][])

## 3.0.0 (2024-01-01)

Expand Down Expand Up @@ -1392,6 +1394,7 @@ Beta release.
[#1152]: https://github.com/quick-lint/quick-lint-js/issues/1152
[#1155]: https://github.com/quick-lint/quick-lint-js/issues/1155
[#1171]: https://github.com/quick-lint/quick-lint-js/issues/1171
[#1180]: https://github.com/quick-lint/quick-lint-js/issues/1180

[E0001]: https://quick-lint-js.com/errors/E0001/
[E0003]: https://quick-lint-js.com/errors/E0003/
Expand Down
1 change: 1 addition & 0 deletions src/quick-lint-js/configuration/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ bool Configuration::should_remove_global_variable(String8_View name) {
group.globals == group_globals ? "" :
group.non_shadowable_globals == group_globals ? "non_shadowable_" :
group.non_writable_globals == group_globals ? "non_writable_" :
group.type_only_globals == group_globals ? "type_only_" :
"???";
// clang-format on
std::fprintf(
Expand Down
88 changes: 88 additions & 0 deletions src/quick-lint-js/fe/global-variables-browser-generated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,94 @@ const Char8 global_variables_web_worker[] =
u8"removeEventListener\0"
u8"self\0"
u8"terminate\0";

const Char8 global_variables_web_types[] =
u8"AlgorithmIdentifier\0"
u8"ArrayBufferView\0"
u8"BigInteger\0"
u8"BinaryData\0"
u8"BlobPart\0"
u8"BodyInit\0"
u8"BufferSource\0"
u8"COSEAlgorithmIdentifier\0"
u8"CSSKeywordish\0"
u8"CSSNumberish\0"
u8"CSSUnparsedSegment\0"
u8"CanvasImageSource\0"
u8"ClipboardItemData\0"
u8"ClipboardItemDataType\0"
u8"ClipboardItems\0"
u8"ConstrainBoolean\0"
u8"ConstrainDOMString\0"
u8"ConstrainDouble\0"
u8"ConstrainPoint2D\0"
u8"ConstrainULong\0"
u8"ContainerBasedOffset\0"
u8"CookieList\0"
u8"CryptoKeyID\0"
u8"DOMHighResTimeStamp\0"
u8"DOMTimeStamp\0"
u8"EventHandler\0"
u8"FileSystemWriteChunkType\0"
u8"Float32List\0"
u8"FormDataEntryValue\0"
u8"GLbitfield\0"
u8"GLboolean\0"
u8"GLbyte\0"
u8"GLclampf\0"
u8"GLenum\0"
u8"GLfloat\0"
u8"GLint\0"
u8"GLint64\0"
u8"GLintptr\0"
u8"GLshort\0"
u8"GLsizei\0"
u8"GLsizeiptr\0"
u8"GLubyte\0"
u8"GLuint\0"
u8"GLuint64\0"
u8"GLushort\0"
u8"GeometryNode\0"
u8"HTMLOrSVGImageElement\0"
u8"HTMLOrSVGScriptElement\0"
u8"HTMLString\0"
u8"HashAlgorithmIdentifier\0"
u8"HeadersInit\0"
u8"ImageBitmapSource\0"
u8"Int32List\0"
u8"LineAndPositionSetting\0"
u8"MediaProvider\0"
u8"Megabit\0"
u8"MessageEventSource\0"
u8"Millisecond\0"
u8"NamedCurve\0"
u8"OffscreenRenderingContext\0"
u8"OnBeforeUnloadEventHandler\0"
u8"OnErrorEventHandler\0"
u8"PasswordCredentialInit\0"
u8"PerformanceEntryList\0"
u8"PushMessageDataInit\0"
u8"RTCRtpTransform\0"
u8"ReadableStreamController\0"
u8"ReadableStreamReader\0"
u8"RenderingContext\0"
u8"ReportList\0"
u8"RequestInfo\0"
u8"RotationMatrixType\0"
u8"ScriptString\0"
u8"ScriptURLString\0"
u8"ScrollTimelineOffset\0"
u8"SmallCryptoKeyID\0"
u8"StartInDirectory\0"
u8"TexImageSource\0"
u8"TimerHandler\0"
u8"TrustedTimerHandler\0"
u8"TrustedType\0"
u8"Uint32List\0"
u8"UvmEntries\0"
u8"UvmEntry\0"
u8"XMLHttpRequestBodyInit\0"
u8"XRWebGLRenderingContext\0";
}

// quick-lint-js finds bugs in JavaScript programs.
Expand Down
4 changes: 4 additions & 0 deletions src/quick-lint-js/fe/global-variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,9 @@ const Global_Group global_groups[] = {
{
.name = u8"browser",
.globals = global_variables_browser,
.type_only_globals = global_variables_web_types,
.globals_count = 990,
.type_only_globals_count = 86,
},
{
.name = u8"bun",
Expand Down Expand Up @@ -518,7 +520,9 @@ const Global_Group global_groups[] = {
{
.name = u8"web-worker",
.globals = global_variables_web_worker,
.type_only_globals = global_variables_web_types,
.globals_count = 232,
.type_only_globals_count = 86,
},
};
static_assert(global_group_count == std::size(global_groups),
Expand Down
1 change: 1 addition & 0 deletions src/quick-lint-js/fe/global-variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern const Global_Group global_groups[];

extern const Char8 global_variables_browser[];
extern const Char8 global_variables_web_worker[];
extern const Char8 global_variables_web_types[];
}

// quick-lint-js finds bugs in JavaScript programs.
Expand Down
4 changes: 4 additions & 0 deletions test/test-variable-analyzer-globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ constexpr const Char8 *non_writable_global_variables[] = {
};

constexpr const Char8 *type_only_global_variables[] = {
// TypeScript
u8"Awaited",
u8"Capitalize",
u8"ConstructorParameters",
Expand All @@ -110,6 +111,9 @@ constexpr const Char8 *type_only_global_variables[] = {
u8"ThisType",
u8"Uncapitalize",
u8"Uppercase",

// Web
u8"HeadersInit",
};

constexpr const Char8 *nodejs_global_variables[] = {
Expand Down
15 changes: 14 additions & 1 deletion tools/browser-globals/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ async function mainAsync() {
"Worker",
"WorkerGlobalScope",
]),
types: exposedGlobals.getGlobalsForNamespaces(["types"]),
outputStream: process.stdout,
});
}
Expand Down Expand Up @@ -234,7 +235,10 @@ function collectExposedGlobals(globals, idlObject, allIDLObjects) {
case "dictionary":
case "enum":
case "interface mixin":
break;

case "typedef":
globals.addGlobal("types", idlObject.name);
break;

default:
Expand Down Expand Up @@ -415,7 +419,12 @@ class NullWriter extends stream.Writable {
}
}

function writeCPPFile({ browserGlobals, webWorkerGlobals, outputStream }) {
function writeCPPFile({
browserGlobals,
webWorkerGlobals,
types,
outputStream,
}) {
function writeStrings(strings) {
for (let string of strings) {
if (!/^[A-Za-z0-9_$]+$/g.test(string)) {
Expand Down Expand Up @@ -444,6 +453,10 @@ const Char8 global_variables_browser[] =`);
const Char8 global_variables_web_worker[] =`);
writeStrings(webWorkerGlobals);
outputStream.write(`;
const Char8 global_variables_web_types[] =`);
writeStrings(types);
outputStream.write(`;
}
// quick-lint-js finds bugs in JavaScript programs.
Expand Down

0 comments on commit b8c59d3

Please sign in to comment.