-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* - Add inner literal rule; fixes #17 * Support `return` statement; avoid BigInt in ESLint < 6
- Loading branch information
Showing
4 changed files
with
197 additions
and
2 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
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,59 @@ | ||
'use strict'; | ||
|
||
const findExpectCall = require('../util/find-expect-call'); | ||
|
||
module.exports = function(context) { | ||
function checkMemberOrCallExpression (expression) { | ||
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()` | ||
}); | ||
} | ||
return { | ||
ReturnStatement(node) { | ||
if ( | ||
node.argument && node.argument.type === 'CallExpression' && | ||
node.argument.callee | ||
) { | ||
checkMemberOrCallExpression(node.argument); | ||
} else if (node.argument && node.argument.type === 'MemberExpression') { | ||
checkMemberOrCallExpression(node.argument); | ||
} | ||
}, | ||
ExpressionStatement(node) { | ||
let {expression} = node; | ||
checkMemberOrCallExpression(expression); | ||
} | ||
}; | ||
}; |
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,127 @@ | ||
'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);' | ||
}, { | ||
code: 'expect(`template literal`).to.equal(5);', | ||
parserOptions: { | ||
ecmaVersion: 2015 | ||
} | ||
}, { | ||
code: 'expect(tagged`template literal`).to.equal(5);', | ||
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: [{ | ||
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(/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()' | ||
}] | ||
}, { | ||
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 | ||
} | ||
}]] | ||
}); |