Skip to content

Commit

Permalink
- Add inner literal rule; fixes #17
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Oct 25, 2019
1 parent 444e1b7 commit 904b8c3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Enable the rules that you would like to use:
{
"rules": {
"chai-expect/no-inner-compare": 2,
"chai-expect/no-inner-literal": 2,
"chai-expect/missing-assertion": 2,
"chai-expect/terminating-properties": 2
}
Expand All @@ -54,8 +55,13 @@ and just extend the config:
## Rules

- `no-inner-compare` - Prevent using comparisons in the `expect()` argument
- `missing-assertion` - Prevent calling `expect(...)` without an assertion like `.to.be.ok`
- `terminating-properties` - Prevent calling `to.be.ok` and other assertion properties as functions
- `no-inner-literal` - Prevent using literals in the `expect()` argument
(`undefined`, `null`, `NaN`, `(+|-)Infinity`, `this`, booleans, numbers,
strings, and BigInt or regex literals)
- `missing-assertion` - Prevent calling `expect(...)` without an assertion
like `.to.be.ok`
- `terminating-properties` - Prevent calling `to.be.ok` and other assertion
properties as functions


### Additional configuration
Expand Down Expand Up @@ -83,6 +89,7 @@ The terminating-properties rule can be configured to ensure these (or other) add
}
```


## License

eslint-plugin-chai-expect is licensed under the [MIT License](http://www.opensource.org/licenses/mit-license.php).
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ module.exports = {
plugins: ['chai-expect'],
rules: {
'chai-expect/no-inner-compare': 'error',
'chai-expect/no-inner-literal': 'error',
'chai-expect/missing-assertion': 'error',
'chai-expect/terminating-properties': 'error'
}
}
},
rules: {
'no-inner-compare': require('./lib/rules/no-inner-compare'),
'no-inner-literal': require('./lib/rules/no-inner-literal'),
'missing-assertion': require('./lib/rules/missing-assertion'),
'terminating-properties': require('./lib/rules/terminating-properties')
}
Expand Down
46 changes: 46 additions & 0 deletions lib/rules/no-inner-literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict';

const findExpectCall = require('../util/find-expect-call');

module.exports = function(context) {
return {
ExpressionStatement(node) {
let {expression} = node;
if (expression.type !== 'MemberExpression' && expression.type !== 'CallExpression')
return;

let expect = findExpectCall(expression);
if (!expect)
return;

let args = expect.arguments;
let [firstArgument] = args;
if (!firstArgument)
return;

let value;
if (firstArgument.type === 'Literal' || firstArgument.type === 'BigIntLiteral') {
value = firstArgument.raw;
} else if (firstArgument.type === 'Identifier' && [
'undefined', 'NaN', 'Infinity'
].includes(firstArgument.name)) {
value = firstArgument.name;
} else if (firstArgument.type === 'ThisExpression') {
value = 'this';
} else if (
firstArgument.type === 'UnaryExpression' &&
firstArgument.argument.type === 'Identifier' &&
firstArgument.argument.name === 'Infinity'
) {
value = `${firstArgument.operator}Infinity`;
} else {
return;
}

context.report({
node: firstArgument,
message: `\`${value}\` used in expect()`
});
}
};
};
87 changes: 87 additions & 0 deletions tests/lib/rules/no-inner-literal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'use strict';

const rule = require('../../../lib/rules/no-inner-literal');
const {RuleTester} = require('eslint');

let ruleTester = new RuleTester();
ruleTester.run('no-inner-literal', rule, {
valid: [{
code: 'expect(a).to.be.ok;'
}, {
code: 'expect(a && b).to.be.ok;'
}, {
code: 'expect(a || b).to.be.ok;'
}, {
code: 'expect(a).to.equal(5);'
}],

invalid: [{
code: 'expect(undefined).to.be.ok;',
errors: [{
message: '`undefined` used in expect()'
}]
}, {
code: 'expect(null).to.be.ok;',
errors: [{
message: '`null` used in expect()'
}]
}, {
code: 'expect(true).to.be.ok;',
errors: [{
message: '`true` used in expect()'
}]
}, {
code: 'expect(52).to.be.ok;',
errors: [{
message: '`52` used in expect()'
}]
}, {
code: 'expect("string").to.be.ok;',
errors: [{
message: '`"string"` used in expect()'
}]
}, {
code: 'expect(132n).to.be.ok;',
errors: [{
message: '`132n` used in expect()'
}],
parserOptions: {
ecmaVersion: 2020
}
}, {
code: 'expect(/regex/).to.be.ok;',
errors: [{
message: '`/regex/` used in expect()'
}]
}, {
code: 'expect(NaN).to.be.ok;',
errors: [{
message: '`NaN` used in expect()'
}]
}, {
code: 'expect(Infinity).to.be.ok;',
errors: [{
message: '`Infinity` used in expect()'
}]
}, {
code: 'expect(+Infinity).to.be.ok;',
errors: [{
message: '`+Infinity` used in expect()'
}]
}, {
code: 'expect(-Infinity).to.be.ok;',
errors: [{
message: '`-Infinity` used in expect()'
}]
}, {
code: 'expect(this).to.be.ok;',
errors: [{
message: '`this` used in expect()'
}]
}, {
code: 'expect(false).to.equal(true);',
errors: [{
message: '`false` used in expect()'
}]
}]
});

0 comments on commit 904b8c3

Please sign in to comment.