Skip to content

Commit

Permalink
Fix: some rules crashing if tests array has missing elements (fixes #35
Browse files Browse the repository at this point in the history
…). (#36)

rules: no-identical-tests, test-case-property-ordering, test-case-shorthand-string.
  • Loading branch information
aladdin-add committed Aug 26, 2017
1 parent 7cabcdc commit e3a14e1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lib/rules/no-identical-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ module.exports = {
*@returns {boolean} if eq, return true, else return false.
*/
function eq (testA, testB) {
// https://github.com/not-an-aardvark/eslint-plugin-eslint-plugin/issues/35
if (!testA || !testB) {
return testA === testB;
}

if (testA.type !== testB.type) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/test-case-property-ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = {
utils.getTestInfo(context, ast).forEach(testRun => {
[testRun.valid, testRun.invalid].forEach(tests => {
(tests || []).forEach(test => {
const properties = test.properties || [];
const properties = (test && test.properties) || [];
const keyNames = properties.map(utils.getKeyName);

for (let i = 0, lastChecked; i < keyNames.length; i++) {
Expand Down
20 changes: 11 additions & 9 deletions lib/rules/test-case-shorthand-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,17 @@ module.exports = {
*/
function reportTestCases (cases) {
const caseInfoList = cases.map(testCase => {
if (testCase.type === 'Literal' || testCase.type === 'TemplateLiteral') {
return { node: testCase, shorthand: true, needsLongform: false };
}
if (testCase.type === 'ObjectExpression') {
return {
node: testCase,
shorthand: false,
needsLongform: !(testCase.properties.length === 1 && utils.getKeyName(testCase.properties[0]) === 'code'),
};
if (testCase) {
if (testCase.type === 'Literal' || testCase.type === 'TemplateLiteral') {
return { node: testCase, shorthand: true, needsLongform: false };
}
if (testCase.type === 'ObjectExpression') {
return {
node: testCase,
shorthand: false,
needsLongform: !(testCase.properties.length === 1 && utils.getKeyName(testCase.properties[0]) === 'code'),
};
}
}
return null;
}).filter(Boolean);
Expand Down

0 comments on commit e3a14e1

Please sign in to comment.