Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forbidden to set this for setTimeout #2511

Merged
merged 2 commits into from Jun 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions js/timers.ts
Expand Up @@ -181,6 +181,13 @@ function fireTimers(): void {

export type Args = unknown[];

// @internal
justjavac marked this conversation as resolved.
Show resolved Hide resolved
function checkThis<T>(thisArg: T): void {
justjavac marked this conversation as resolved.
Show resolved Hide resolved
if (thisArg !== null && thisArg !== undefined && thisArg !== window) {
throw new TypeError("Illegal invocation");
}
}

function setTimer(
cb: (...args: Args) => void,
delay: number,
Expand Down Expand Up @@ -226,6 +233,8 @@ export function setTimeout(
delay: number,
...args: Args
): number {
// @ts-ignore
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What error are you ignoring and why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I used this in a function(not a class)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly a helpful tip: you can put the reasoning for a ts-ignore on the same line:

example:

// @ts-ignore : because I used this in a function (not a class)
checkThis(this);

Copy link
Contributor

@kitsonk kitsonk Jun 13, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defining this in the arguments would remove this error. Under strict mode, where this cannot be inferred, you have to be explicit. It isn't an error to ignore.

function setTimer(
	this: unknown,
    cb: (...args: Args) => void,
	delay: number,
	...args: unknown[]
): number {
	checkThis(this);
	// ...
}

checkThis(this);
return setTimer(cb, delay, args, false);
}

Expand All @@ -235,6 +244,8 @@ export function setInterval(
delay: number,
...args: Args
): number {
// @ts-ignore
checkThis(this);
return setTimer(cb, delay, args, true);
}

Expand Down
54 changes: 54 additions & 0 deletions js/timers_test.ts
Expand Up @@ -177,3 +177,57 @@ test(async function timeoutCallbackThis(): Promise<void> {
setTimeout(obj.foo, 1);
await promise;
});

test(async function timeoutBindThis(): Promise<void> {
function noop(): void {}

const thisCheckPassed = [null, undefined, window, globalThis];

const thisCheckFailed = [
0,
"",
true,
false,
{},
[],
"foo",
(): void => {},
Object.prototype
];

thisCheckPassed.forEach(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(thisArg: any): void => {
let hasThrown = 0;
try {
setTimeout.call(thisArg, noop, 1);
hasThrown = 1;
} catch (err) {
if (err instanceof TypeError) {
hasThrown = 2;
} else {
hasThrown = 3;
}
}
assertEquals(hasThrown, 1);
}
);

thisCheckFailed.forEach(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(thisArg: any): void => {
let hasThrown = 0;
try {
setTimeout.call(thisArg, noop, 1);
hasThrown = 1;
} catch (err) {
if (err instanceof TypeError) {
hasThrown = 2;
} else {
hasThrown = 3;
}
}
assertEquals(hasThrown, 2);
}
);
});