From fdb3843bd618657dd09785150ddbe62de881e368 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 18 Feb 2020 05:11:07 +0800 Subject: [PATCH] - Remove regex literal argument; simplify --- lib/rules/valid-suite-description.js | 26 +++++++++++++++++--------- lib/rules/valid-test-description.js | 25 ++++++++++++++++--------- test/rules/valid-suite-description.js | 10 ---------- test/rules/valid-test-description.js | 10 ---------- 4 files changed, 33 insertions(+), 38 deletions(-) diff --git a/lib/rules/valid-suite-description.js b/lib/rules/valid-suite-description.js index 0e49cdc..0010f5a 100644 --- a/lib/rules/valid-suite-description.js +++ b/lib/rules/valid-suite-description.js @@ -8,18 +8,26 @@ const astUtils = require('../util/ast'); const defaultSuiteNames = [ 'describe', 'context', 'suite' ]; -function inlineOptions(context) { - const pattern = new RegExp(context.options[0], 'u'); - const suiteNames = context.options[1] ? context.options[1] : defaultSuiteNames; - const message = context.options[2]; +function inlineOptions(options) { + const [ + stringPattern, + suiteNames = defaultSuiteNames, + message + ] = options; + + const pattern = new RegExp(stringPattern, 'u'); return { pattern, suiteNames, message }; } function objectOptions(options) { - const pattern = new RegExp(options.pattern, 'u'); - const suiteNames = options.suiteNames ? options.suiteNames : defaultSuiteNames; - const message = options.message; + const { + pattern: stringPattern, + suiteNames = defaultSuiteNames, + message + } = options; + + const pattern = new RegExp(stringPattern, 'u'); return { pattern, suiteNames, message }; } @@ -58,9 +66,9 @@ module.exports = { create(context) { const options = context.options[0]; - const { pattern, suiteNames, message } = typeof options === 'object' && !(options instanceof RegExp) ? + const { pattern, suiteNames, message } = typeof options === 'object' ? objectOptions(options) : - inlineOptions(context); + inlineOptions(context.options); function isSuite(node) { return node.callee && node.callee.name && suiteNames.indexOf(node.callee.name) > -1; diff --git a/lib/rules/valid-test-description.js b/lib/rules/valid-test-description.js index daa68dc..07b2f58 100644 --- a/lib/rules/valid-test-description.js +++ b/lib/rules/valid-test-description.js @@ -9,18 +9,25 @@ const astUtils = require('../util/ast'); const defaultTestNames = [ 'it', 'test', 'specify' ]; -function inlineOptions(context) { - const pattern = context.options[0] ? new RegExp(context.options[0], 'u') : /^should/u; - const testNames = context.options[1] ? context.options[1] : defaultTestNames; - const message = context.options[2]; +function inlineOptions(options) { + const [ + stringPattern = '^should', + testNames = defaultTestNames, + message + ] = options; + + const pattern = new RegExp(stringPattern, 'u'); return { pattern, testNames, message }; } function objectOptions(options) { - const pattern = options.pattern ? new RegExp(options.pattern, 'u') : /^should/u; - const testNames = options.testNames ? options.testNames : defaultTestNames; - const message = options.message; + const { + pattern: stringPattern = '^should', + testNames = defaultTestNames, + message + } = options; + const pattern = new RegExp(stringPattern, 'u'); return { pattern, testNames, message }; } @@ -59,9 +66,9 @@ module.exports = { create(context) { const options = context.options[0]; - const { pattern, testNames, message } = typeof options === 'object' && !(options instanceof RegExp) ? + const { pattern, testNames, message } = typeof options === 'object' ? objectOptions(options) : - inlineOptions(context); + inlineOptions(context.options); function isTest(node) { return node.callee && node.callee.name && testNames.indexOf(node.callee.name) > -1; diff --git a/test/rules/valid-suite-description.js b/test/rules/valid-suite-description.js index 9dece9a..5e4976f 100644 --- a/test/rules/valid-suite-description.js +++ b/test/rules/valid-suite-description.js @@ -30,20 +30,10 @@ ruleTester.run('valid-suite-description', rules['valid-suite-description'], { options: [ '^[A-Z]', [ 'someFunction' ], 'some error message' ], code: 'someFunction("Should do something", function () { });' }, - { - options: [ /^[A-Z]/, [ 'someFunction' ], 'some error message' ], - code: 'someFunction("Should do something", function () { });' - }, { options: [ { pattern: '^[A-Z]', suiteNames: [ 'someFunction' ], message: 'some error message' } ], code: 'someFunction("Should do something", function () { });' }, - /* - { - options: [ { pattern: /^[A-Z]/, suiteNames: [ 'someFunction' ], message: 'some error message' } ], - code: 'someFunction("Should do something", function () { });' - }, - */ { options: [ {} ], code: 'someFunction("Should do something", function () { });' diff --git a/test/rules/valid-test-description.js b/test/rules/valid-test-description.js index d3a137d..52c651f 100644 --- a/test/rules/valid-test-description.js +++ b/test/rules/valid-test-description.js @@ -35,20 +35,10 @@ ruleTester.run('valid-test-description', rules['valid-test-description'], { options: [ '^should', [ 'someFunction' ], 'some error message' ], code: 'someFunction("should do something", function () { });' }, - { - options: [ /^should/, [ 'someFunction' ], 'some error message' ], - code: 'someFunction("should do something", function () { });' - }, { options: [ { pattern: '^should', testNames: [ 'someFunction' ], message: 'some error message' } ], code: 'someFunction("should do something", function () { });' }, - /* - { - options: [ { pattern: /^should/, testNames: [ 'someFunction' ], message: 'some error message' } ], - code: 'someFunction("should do something", function () { });' - }, - */ 'someOtherFunction();', { parserOptions: { ecmaVersion: 2017 },