From c7400c10aadadb53d45b12fb35084addfaedcb57 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: Tue, 18 Jul 2017 22:05:55 +0800 Subject: [PATCH 1/2] Fix: prefer-output-null crashes, when a test case is not object. --- lib/rules/prefer-output-null.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/rules/prefer-output-null.js b/lib/rules/prefer-output-null.js index 9f31645a..5d033d3f 100644 --- a/lib/rules/prefer-output-null.js +++ b/lib/rules/prefer-output-null.js @@ -40,8 +40,11 @@ module.exports = { * @returns {Node} found node; if not found, return null; */ function getTestInfo (key) { - const res = test.properties.filter(item => item.key.name === key); - return res.length ? res[res.length - 1] : null; + if (test.type === 'ObjectExpression') { + const res = test.properties.filter(item => item.key.name === key); + return res.length ? res[res.length - 1] : null; + } + return key === 'code' ? test : null; } const code = getTestInfo('code'); From 08ad38c831b79cd931dc6885159187b3e929db45 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: Tue, 18 Jul 2017 22:20:30 +0800 Subject: [PATCH 2/2] Chore: add valid test. --- tests/lib/rules/prefer-output-null.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/lib/rules/prefer-output-null.js b/tests/lib/rules/prefer-output-null.js index 24ee3eae..0d705fe9 100644 --- a/tests/lib/rules/prefer-output-null.js +++ b/tests/lib/rules/prefer-output-null.js @@ -37,6 +37,15 @@ ruleTester.run('prefer-output-null', rule, { ] }); `, + ` + new RuleTester().run('foo', bar, { + valid: [ + 'foo' + ], + invalid: [] + }); + `, + ], invalid: [