Skip to content

Commit

Permalink
feat: add expect-should-assertion rule (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredmcateer committed Apr 12, 2022
1 parent 3bfc17f commit bcf714a
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 14 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# eslint-plugin-shouldjs

Rules that apply to testing with the [should-js](https://shouldjs.github.io/) library.
Rules that apply to testing with the [should.js](https://shouldjs.github.io/) library.

## Installation

Expand Down Expand Up @@ -30,7 +30,7 @@ Add `@jaredmcateer/shouldjs` to the plugins section of your `.eslintrc` configur

### Settings

By default the only allowed variable name for should is `should`, this can be changed by providing an array to `shouldVarNames` in the eslint settings.
By default the only allowed variable name for Should.js is `should`, this can be changed by providing an array to `shouldVarNames` in the eslint settings.

```json
{
Expand Down Expand Up @@ -65,6 +65,7 @@ Alternative you can use the recommended settings

- [should-var-names](lib/rules/should-var-name/should-var-name.md)
- [no-property-assertion](lib/rules/no-property-assertions/no-property-assertion.md)
- [expect-should-assertion](lib/rules/expect-should-assertion/expect-should-assertions.md)

## Acknowledgements

Expand Down
2 changes: 2 additions & 0 deletions lib/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConfigSettings } from ".";
import { EXPECT_SHOULD_ASSERTION } from "../rules/expect-should-assertion/expect-should-assertions";
import { NO_PROPERTY_ASSERTIONS } from "../rules/no-property-assertions/no-property-assertions";
import { SHOULD_VAR_NAME } from "../rules/should-var-name/should-var-name";

Expand All @@ -8,6 +9,7 @@ export const recommended = {
rules: {
[`@jaredmcateer/shouldjs/${NO_PROPERTY_ASSERTIONS}`]: ["error"],
[`@jaredmcateer/shouldjs/${SHOULD_VAR_NAME}`]: ["error"],
[`@jaredmcateer/shouldjs/${EXPECT_SHOULD_ASSERTION}`]: ["error"],
},

settings: {
Expand Down
27 changes: 27 additions & 0 deletions lib/rules/expect-should-assertion/expect-should-assertions.md
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 lib/rules/expect-should-assertion/expect-should-assertions.test.ts
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 lib/rules/expect-should-assertion/expect-should-assertions.ts
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,
});
}
},
};
},
});
5 changes: 5 additions & 0 deletions lib/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
expectShouldAssertion,
EXPECT_SHOULD_ASSERTION,
} from "./expect-should-assertion/expect-should-assertions";
import {
noPropertyAssertions,
NO_PROPERTY_ASSERTIONS,
Expand All @@ -7,4 +11,5 @@ import { shouldVarName, SHOULD_VAR_NAME } from "./should-var-name/should-var-nam
export const rules = {
[NO_PROPERTY_ASSERTIONS]: noPropertyAssertions,
[SHOULD_VAR_NAME]: shouldVarName,
[EXPECT_SHOULD_ASSERTION]: expectShouldAssertion,
};
4 changes: 2 additions & 2 deletions lib/rules/no-property-assertions/no-property-assertion.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

The `"extends": "@jaredmcateer/shouldjs:recommended"` property in a configuration file enables this rule.

Disallows ending a should-js chain with a property instead of a method.
Disallows ending a Should.js chain with a property instead of a method.

Should-js does not use properties for assertions but it is a common gotcha with certain assertions.
Should.js does not use properties for assertions but it is a common gotcha with certain assertions.

When checking by the CallExpression you can configure the `shouldVarNames` in the `settings` property of the eslint config to limit which variable names will be checked.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ ruleTester.run(NO_PROPERTY_ASSERTIONS, noPropertyAssertions, {
{ code: "foo.should.be.true();" },
{ code: "should(foo).be.false();" },
{ code: "foo.should.not.have.been.eql(bar);" },
// This is an error but handled by another rule
{ code: "should(false);" },
{ code: "myCustomVar(foo).should.be.eql(bar);", settings: { shouldVarNames: ["myCustomVar"] } },
],
invalid: [
Expand Down
11 changes: 9 additions & 2 deletions lib/rules/no-property-assertions/no-property-assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export const noPropertyAssertions = createRule<[], MessageIds>({
name: NO_PROPERTY_ASSERTIONS,
meta: {
docs: {
description: "Should-js assertions should be methods.",
description: "Should.js assertions should be methods.",
recommended: "error",
requiresTypeChecking: false,
},
messages: {
[PROPERTY_ASSERTION_ERROR]: "Should-js assertions should be methods.",
[PROPERTY_ASSERTION_ERROR]: "Should.js assertions should be methods.",
},
schema: [],
hasSuggestions: false,
Expand Down Expand Up @@ -58,6 +58,13 @@ export const noPropertyAssertions = createRule<[], MessageIds>({
const name = node.callee.name;
if (!validVarNames?.find((varName) => varName === name)) return;

if (node.parent?.type === AST_NODE_TYPES.ExpressionStatement) {
// If we've hit an Expression statement it means that the Should.js
// function var is being called without an assertion, this is handled
// by the expect-should-assertion rule.
return;
}

// CallExpression matches function variable name, begin traversing AST to check.
checkChain(node.parent, context);
},
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/should-var-name/should-var-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The `"extends": "@jaredmcateer/shouldjs:recommended"` property in a configuration file enables this rule.

Disallows assigning a variable for should that isn't part of the approved list. This is set by the `shouldVarNames` array in settings property of the eslint config.
Disallows assigning a variable for Should.js that isn't part of the approved list. This is set by the `shouldVarNames` array in settings property of the eslint config.

## Rule Details

Expand Down Expand Up @@ -32,4 +32,4 @@ import should from "should";

## When Not To Use It

It is not recommended to turn this rule off. Other rules in the plugin rely on the variable name when using the should function to decide whether to lint by assigning it an unknown value those rules will not be run.
It is not recommended to turn this rule off. Other rules in the plugin rely on the variable name when using the Should.js function to decide whether to lint by assigning it an unknown value those rules will not be run.
8 changes: 4 additions & 4 deletions lib/rules/should-var-name/should-var-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export const shouldVarName = createRule<[], MessageIds>({
name: SHOULD_VAR_NAME,
meta: {
docs: {
description: "Only allows the configured variable name for should-js variable name.",
description: "Only allows the configured variable name for Should.js variable name.",
recommended: "error",
requiresTypeChecking: false,
},
messages: {
[INVALID_VAR_NAME]: "Invalid variable name for should-js.",
[INVALID_VAR_NAME]: "Invalid variable name for Should.js.",
[SUGGEST_FUNCTION_VAR_RENAME]: "Rename variable to: {{name}}",
},
schema: [],
Expand Down Expand Up @@ -60,7 +60,7 @@ export const shouldVarName = createRule<[], MessageIds>({
CallExpression(node) {
if (node?.callee?.type !== AST_NODE_TYPES.Identifier) return;

// If CallExpression isn't requiring should then stop here
// If CallExpression isn't requiring Should.js then stop here
const isRequire = node.callee.name === "require";
const isOneArg = isRequire && node.arguments.length === 1;
const packageArg = isOneArg && node.arguments[0];
Expand All @@ -87,7 +87,7 @@ export const shouldVarName = createRule<[], MessageIds>({
ImportDeclaration(node) {
if (node?.source?.type !== AST_NODE_TYPES.Literal) return;

// If ImportDeclaration isn't requiring should then stop here
// If ImportDeclaration isn't requiring Should.js then stop here
if (node.source.value !== "should") return;
const defaultSpecifier = node.specifiers.find(
(s) => s.type === AST_NODE_TYPES.ImportDefaultSpecifier
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "@jaredmcateer/eslint-plugin-shouldjs",
"version": "1.0.0",
"description": "A collection of Should-js rules",
"description": "A collection of Should.js rules",
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin",
"should",
"shouldjs",
"should-js"
"should-js",
"should.js"
],
"author": "Jared McAteer <jared.mcateer+npm@gmail.com>",
"main": "dist/index.js",
Expand Down

1 comment on commit bcf714a

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Lines Statements Branches Functions
Coverage: 100%
93.02% (80/86) 100% (16/16) 78.78% (78/99)

Please sign in to comment.