-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the rejected promises part of no-throw to new rule no-reject (#118)
* Add the no-reject rule * Revert changes to the readme * Revert "update `no-throw` to not allow Promise.reject() (#116)" This reverts commit 4efdf16. * Add docs * Remove previously removed line from readme
- Loading branch information
1 parent
f40c59f
commit 704fb3a
Showing
6 changed files
with
83 additions
and
32 deletions.
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,30 @@ | ||
import * as ts from "typescript"; | ||
import * as Lint from "tslint"; | ||
import { | ||
createInvalidNode, | ||
CheckNodeResult, | ||
createCheckNodeRule | ||
} from "./shared/check-node"; | ||
|
||
type Options = {}; | ||
|
||
// tslint:disable-next-line:variable-name | ||
export const Rule = createCheckNodeRule( | ||
checkNode, | ||
"Unexpected reject, return an error instead." | ||
); | ||
|
||
function checkNode( | ||
node: ts.Node, | ||
_ctx: Lint.WalkContext<Options> | ||
): CheckNodeResult { | ||
if ( | ||
ts.isPropertyAccessExpression(node) && | ||
ts.isIdentifier(node.expression) && | ||
node.expression.text === "Promise" && | ||
node.name.text === "reject" | ||
) { | ||
return { invalidNodes: [createInvalidNode(node, [])] }; | ||
} | ||
return { invalidNodes: [] }; | ||
} |
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,18 @@ | ||
const error = new Error(); | ||
|
||
function foo(): Promise<number> { | ||
if (Math.random() > 0.5) { | ||
return Promise.reject(new Error("bar")) | ||
~~~~~~~~~~~~~~ [failure] | ||
} | ||
return Promise.resolve(10) | ||
} | ||
|
||
function bar(): Promise<number | Error> { | ||
if (Math.random() > 0.5) { | ||
return Promise.resolve(new Error("foo")) | ||
} | ||
return Promise.resolve(10) | ||
} | ||
|
||
[failure]: Unexpected reject, return an error instead. |
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,6 @@ | ||
{ | ||
"rulesDirectory": ["../../../../rules"], | ||
"rules": { | ||
"no-reject": true | ||
} | ||
} |
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