From d064cb6fe260670f5c3999b663ba7c5c0b3fc42c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=9B=E5=AE=9A=E8=B0=94=E7=9A=84=E7=8C=AB?= Date: Sun, 27 Aug 2017 03:20:38 +0800 Subject: [PATCH] Fix: some rules crash if tests array has missing elements (fixes #35). rules: no-identical-tests, test-case-property-ordering, test-case-shorthand-string. --- lib/rules/no-identical-tests.js | 5 +++++ lib/rules/test-case-property-ordering.js | 2 +- lib/rules/test-case-shorthand-strings.js | 20 +++++++++++--------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/rules/no-identical-tests.js b/lib/rules/no-identical-tests.js index 10b6d99d..d278b3de 100644 --- a/lib/rules/no-identical-tests.js +++ b/lib/rules/no-identical-tests.js @@ -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; } diff --git a/lib/rules/test-case-property-ordering.js b/lib/rules/test-case-property-ordering.js index 7c16a66e..c1f60f02 100644 --- a/lib/rules/test-case-property-ordering.js +++ b/lib/rules/test-case-property-ordering.js @@ -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++) { diff --git a/lib/rules/test-case-shorthand-strings.js b/lib/rules/test-case-shorthand-strings.js index 863f86ad..f20a8c2f 100644 --- a/lib/rules/test-case-shorthand-strings.js +++ b/lib/rules/test-case-shorthand-strings.js @@ -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);