Skip to content

Commit

Permalink
- Remove regex literal argument; simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Feb 17, 2020
1 parent 68a3c07 commit fdb3843
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
26 changes: 17 additions & 9 deletions lib/rules/valid-suite-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 16 additions & 9 deletions lib/rules/valid-test-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 0 additions & 10 deletions test/rules/valid-suite-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () { });'
Expand Down
10 changes: 0 additions & 10 deletions test/rules/valid-test-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down

0 comments on commit fdb3843

Please sign in to comment.