-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add shim for ES Promise #33863
Closed
Closed
Add shim for ES Promise #33863
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/experimental-utils"; | ||
import { createRule } from "./utils"; | ||
|
||
export = createRule({ | ||
name: "async-type", | ||
meta: { | ||
docs: { | ||
description: ``, | ||
category: "Possible Errors", | ||
recommended: "error", | ||
}, | ||
messages: { | ||
asyncTypeError: `Async functions must have an explicit return type`, | ||
}, | ||
schema: [], | ||
type: "problem", | ||
}, | ||
defaultOptions: [], | ||
|
||
create(context) { | ||
const checkAsyncFunction = (node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression | TSESTree.ArrowFunctionExpression) => { | ||
if (node.async && !node.returnType) { | ||
context.report({ messageId: "asyncTypeError", node }); | ||
} | ||
}; | ||
|
||
const checkAsyncMethod = (node: TSESTree.MethodDefinition) => { | ||
if (node.value.type === AST_NODE_TYPES.FunctionExpression) { | ||
checkAsyncFunction(node.value); | ||
} | ||
}; | ||
|
||
return { | ||
[AST_NODE_TYPES.FunctionDeclaration]: checkAsyncFunction, | ||
[AST_NODE_TYPES.FunctionExpression]: checkAsyncFunction, | ||
[AST_NODE_TYPES.ArrowFunctionExpression]: checkAsyncFunction, | ||
[AST_NODE_TYPES.MethodDefinition]: checkAsyncMethod | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { RuleTester } from "./support/RuleTester"; | ||
import rule = require("../rules/async-type"); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
warnOnUnsupportedTypeScriptVersion: false, | ||
}, | ||
parser: require.resolve("@typescript-eslint/parser"), | ||
}); | ||
|
||
ruleTester.run("async-type", rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
async function foo(): Promise<void> {} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
const fn = async function(): Promise<void> {} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
const fn = async (): Promise<void> => {} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
class C { | ||
async method(): Promise<void> {} | ||
} | ||
`, | ||
}, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: `async function foo() {}`, | ||
errors: [{ messageId: "asyncTypeError" }] | ||
}, | ||
{ | ||
code: `const fn = async function() {}`, | ||
errors: [{ messageId: "asyncTypeError" }] | ||
}, | ||
{ | ||
code: `const fn = async () => {}`, | ||
errors: [{ messageId: "asyncTypeError" }] | ||
}, | ||
{ | ||
code: `class C { async method() {} }`, | ||
errors: [{ messageId: "asyncTypeError" }] | ||
}, | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint-disable async-type */ | ||
/// <reference types="node" /> | ||
|
||
import childProcess = require("child_process"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* @internal */ | ||
namespace ts { | ||
declare function setTimeout<A extends any[]>(handler: (...args: A) => void, timeout: number, ...args: A): any; | ||
|
||
export interface Promise<T> extends globalThis.Promise<T> { | ||
} | ||
|
||
export interface PromiseConstructor extends PromiseConstructorLike { | ||
new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason: unknown) => void) => void): Promise<T>; | ||
resolve<T>(value: T | PromiseLike<T>): Promise<T>; | ||
resolve(): Promise<void>; | ||
reject<T = never>(reason: unknown): Promise<T>; | ||
all<T>(promises: (T | PromiseLike<T>)[]): Promise<T[]>; | ||
race<T>(promises: (T | PromiseLike<T>)[]): Promise<T>; | ||
} | ||
|
||
export function createPromiseShim(): PromiseConstructor { | ||
class Promise<T> implements ts.Promise<T> { | ||
private _state: "pending" | "fulfilled" | "rejected" = "pending"; | ||
private _result: unknown; | ||
private _reactions: PromiseReaction[] = []; | ||
|
||
constructor(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason: any) => void) => void) { | ||
const { resolve, reject } = createResolvingFunctions(this); | ||
try { | ||
executor(resolve, reject); | ||
} | ||
catch (e) { | ||
reject(e); | ||
} | ||
} | ||
|
||
then<TResult1 = T, TResult2 = never>( | ||
onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, | ||
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null | ||
): Promise<TResult1 | TResult2> { | ||
return new Promise<TResult1 | TResult2>((resolve, reject) => { | ||
const reaction: PromiseReaction = { resolve, reject, onfulfilled, onrejected }; | ||
if (this._state === "pending") { | ||
this._reactions.push(reaction); | ||
} | ||
else { | ||
setTimeout(promiseReactionJob, 0, reaction, this._state === "fulfilled" ? "fulfill" : "reject", this._result); | ||
} | ||
}); | ||
} | ||
|
||
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): Promise<T | TResult> { | ||
return this.then(/*onfulfilled*/ undefined, onrejected); | ||
} | ||
|
||
static resolve(): Promise<void>; | ||
static resolve<T>(value: T | PromiseLike<T>): Promise<T>; | ||
static resolve<T>(value?: T | PromiseLike<T>): Promise<T> { | ||
return value instanceof this && value.constructor === this ? value : new Promise<T>(resolve => resolve(value)); | ||
} | ||
|
||
static reject<T = never>(reason: any): Promise<T> { | ||
return new Promise<T>((_, reject) => reject(reason)); | ||
} | ||
|
||
static all<T>(promises: (T | PromiseLike<T>)[]): Promise<T[]> { | ||
return new Promise<T[]>((resolve, reject) => { | ||
let count = promises.length; | ||
const values: T[] = Array<T>(count); | ||
for (let i = 0; i < promises.length; i++) { | ||
let called = false; | ||
this.resolve(promises[i]).then( | ||
value => { | ||
if (!called) { | ||
called = true; | ||
values[i] = value; | ||
count--; | ||
if (count === 0) { | ||
resolve(values); | ||
} | ||
} | ||
}, | ||
reject); | ||
} | ||
}); | ||
} | ||
|
||
static race<T>(promises: (T | PromiseLike<T>)[]): Promise<T> { | ||
return new Promise<T>((resolve, reject) => { | ||
for (const promise of promises) { | ||
this.resolve(promise).then(resolve, reject); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
interface PromiseReaction { | ||
resolve: (value: unknown) => void; | ||
reject: (reason: unknown) => void; | ||
onfulfilled?: ((value: unknown) => unknown) | null; | ||
onrejected?: ((reason: unknown) => unknown) | null; | ||
} | ||
|
||
function createResolvingFunctions<T>(promise: Promise<T>) { | ||
let called = false; | ||
return { | ||
resolve: (value: T | Promise<T>) => { | ||
if (!called) { | ||
called = true; | ||
try { | ||
if (promise === value) throw new TypeError(); | ||
// eslint-disable-next-line no-null/no-null | ||
const then = typeof value === "object" && value !== null && (<Promise<unknown>>value).then; | ||
if (typeof then !== "function") { | ||
settlePromise(promise, "fulfill", value); | ||
} | ||
else { | ||
setTimeout(resolveThenableJob, 0, promise, value, then); | ||
} | ||
} | ||
catch (e) { | ||
settlePromise(promise, "reject", e); | ||
} | ||
} | ||
}, | ||
reject: (reason: any) => { | ||
if (!called) { | ||
called = true; | ||
settlePromise(promise, "reject", reason); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
function settlePromise(promise: Promise<unknown>, verb: "fulfill" | "reject", value: unknown) { | ||
/* eslint-disable dot-notation */ | ||
const reactions = promise["_reactions"]; | ||
promise["_result"] = value; | ||
promise["_reactions"] = undefined!; | ||
promise["_state"] = verb === "fulfill" ? "fulfilled" : "rejected"; | ||
for (const reaction of reactions) { | ||
setTimeout(promiseReactionJob, 0, reaction, verb, value); | ||
} | ||
/* eslint-enable dot-notation */ | ||
} | ||
|
||
function resolveThenableJob<T>(promiseToResolve: Promise<T>, thenable: T, thenAction: Promise<T>["then"]) { | ||
const { resolve, reject } = createResolvingFunctions(promiseToResolve); | ||
try { | ||
thenAction.call(thenable, resolve, reject); | ||
} | ||
catch (e) { | ||
reject(e); | ||
} | ||
} | ||
|
||
function promiseReactionJob(reaction: PromiseReaction, verb: "fulfill" | "reject", argument: unknown) { | ||
const handler = verb === "fulfill" ? reaction.onfulfilled : reaction.onrejected; | ||
if (handler) { | ||
try { | ||
argument = handler(argument); | ||
verb = "fulfill"; | ||
} | ||
catch (e) { | ||
argument = e; | ||
verb = "reject"; | ||
} | ||
} | ||
|
||
const action = verb === "fulfill" ? reaction.resolve : reaction.reject; | ||
action(argument); | ||
} | ||
|
||
return Promise; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
"outFile": "../../built/local/shims.js" | ||
}, | ||
"files": [ | ||
"mapShim.ts" | ||
"mapShim.ts", | ||
"promiseShim.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.