Skip to content

Commit

Permalink
Support return statement; avoid BigInt in ESLint < 6
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Oct 25, 2019
1 parent b51e5b1 commit e129aaa
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 42 deletions.
79 changes: 46 additions & 33 deletions lib/rules/no-inner-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,57 @@
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;
function checkMemberOrCallExpression (expression) {
if (expression.type !== 'MemberExpression' && expression.type !== 'CallExpression')
return;

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

let args = expect.arguments;
let [firstArgument] = args;
if (!firstArgument)
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;
}

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'
context.report({
node: firstArgument,
message: `\`${value}\` used in expect()`
});
}
return {
ReturnStatement(node) {
if (
node.argument && node.argument.type === 'CallExpression' &&
node.argument.callee
) {
value = `${firstArgument.operator}Infinity`;
} else {
return;
checkMemberOrCallExpression(node.argument);
} else if (node.argument && node.argument.type === 'MemberExpression') {
checkMemberOrCallExpression(node.argument);
}

context.report({
node: firstArgument,
message: `\`${value}\` used in expect()`
});
},
ExpressionStatement(node) {
let {expression} = node;
checkMemberOrCallExpression(expression);
}
};
};
48 changes: 39 additions & 9 deletions tests/lib/rules/no-inner-literal.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ ruleTester.run('no-inner-literal', rule, {
parserOptions: {
ecmaVersion: 2015
}
}, {
code: `
it('should have no problems', function () {
return expect(a).to.be.ok();
});
`
}, {
code: `
it('should have no problems', function () {
return expect(a).to.be.true;
});
`
}],

invalid: [{
Expand Down Expand Up @@ -50,14 +62,6 @@ ruleTester.run('no-inner-literal', rule, {
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: [{
Expand Down Expand Up @@ -93,5 +97,31 @@ ruleTester.run('no-inner-literal', rule, {
errors: [{
message: '`false` used in expect()'
}]
}]
}, {
code: `
it('should have no problems but does', function () {
return expect(false).to.equal(true);
});
`,
errors: [{
message: '`false` used in expect()'
}]
}, {
code: `
it('should have no problems but does', function () {
return expect(false).to.be.true;
});
`,
errors: [{
message: '`false` used in expect()'
}]
}, ...require('eslint/package.json').version.match(/^[1-5]\./) ? [] : [{
code: 'expect(132n).to.be.ok;',
errors: [{
message: '`132n` used in expect()'
}],
parserOptions: {
ecmaVersion: 2020
}
}]]
});

0 comments on commit e129aaa

Please sign in to comment.