-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add expect-should-assertion rule (#12)
- Loading branch information
1 parent
3bfc17f
commit bcf714a
Showing
12 changed files
with
136 additions
and
14 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
27 changes: 27 additions & 0 deletions
27
lib/rules/expect-should-assertion/expect-should-assertions.md
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,27 @@ | ||
# expect-should-assertion | ||
|
||
The `"extends": "@jaredmcateer/shouldjs:recommended"` property in a configuration file enables this rule. | ||
|
||
Disallows should function variable to be called without chaining an assertion. | ||
|
||
Should.js does not assert solely by calling the function variable. | ||
|
||
You can configure the `shouldVarNames` in the `settings` property of the eslint config to limit which variable names will be checked. | ||
|
||
## Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
should(foo); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
should(foo).be.truthy(); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
It is not recommended to turn this rule off. |
25 changes: 25 additions & 0 deletions
25
lib/rules/expect-should-assertion/expect-should-assertions.test.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ESLintUtils } from "@typescript-eslint/utils"; | ||
import { | ||
ADD_ASSERTION_MESSAGE, | ||
expectShouldAssertion, | ||
EXPECT_SHOULD_ASSERTION, | ||
} from "./expect-should-assertions"; | ||
|
||
const ruleTester = new ESLintUtils.RuleTester({ | ||
parser: "@typescript-eslint/parser", | ||
}); | ||
|
||
ruleTester.run(EXPECT_SHOULD_ASSERTION, expectShouldAssertion, { | ||
valid: [ | ||
{ code: "should(foo).be.true();" }, | ||
{ code: "should(foo);", settings: { shouldVarNames: ["expect"] } }, | ||
], | ||
invalid: [ | ||
{ code: "should(foo);", errors: [{ messageId: ADD_ASSERTION_MESSAGE }] }, | ||
{ | ||
code: "expect();", | ||
settings: { shouldVarNames: ["expect"] }, | ||
errors: [{ messageId: ADD_ASSERTION_MESSAGE }], | ||
}, | ||
], | ||
}); |
52 changes: 52 additions & 0 deletions
52
lib/rules/expect-should-assertion/expect-should-assertions.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { AST_NODE_TYPES } from "@typescript-eslint/utils"; | ||
import { ConfigSettings } from "../../configs"; | ||
import { createRule } from "../../utils/create-rule"; | ||
|
||
export const EXPECT_SHOULD_ASSERTION = "expect-should-assertion"; | ||
export const ADD_ASSERTION_MESSAGE = "missingAssertionError"; | ||
|
||
type MessageIds = typeof ADD_ASSERTION_MESSAGE; | ||
|
||
export const expectShouldAssertion = createRule<[], MessageIds>({ | ||
name: EXPECT_SHOULD_ASSERTION, | ||
meta: { | ||
docs: { | ||
description: "Should.js function calls should be chained with an assertion.", | ||
recommended: "error", | ||
requiresTypeChecking: false, | ||
}, | ||
messages: { | ||
[ADD_ASSERTION_MESSAGE]: "Should.js function calls should be chained with an assertion.", | ||
}, | ||
schema: [], | ||
hasSuggestions: false, | ||
type: "problem", | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const validVarNames = (context.settings as ConfigSettings).shouldVarNames || ["should"]; | ||
|
||
return { | ||
/** | ||
* When encountering an CallExpression with expected name (i.e., should), | ||
* Check the parents if it is an ExpressionStatement then report an error. | ||
*/ | ||
CallExpression(node) { | ||
if (node?.callee?.type !== AST_NODE_TYPES.Identifier) return; | ||
|
||
// If CallExpression isn't an expected variable name then stop here | ||
const name = node.callee.name; | ||
if (!validVarNames?.find((varName) => varName === name)) return; | ||
|
||
// If we've hit an Expression statement it means that the Should.js | ||
// function var is being called without an assertion. | ||
if (node.parent?.type === AST_NODE_TYPES.ExpressionStatement) { | ||
context.report({ | ||
messageId: ADD_ASSERTION_MESSAGE, | ||
node, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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
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
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
bcf714a
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.