Skip to content

Commit

Permalink
refactor: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 12, 2018
1 parent 6afd2f4 commit 45396c1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 26 deletions.
77 changes: 64 additions & 13 deletions rules/__tests__/lowercase-description.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ const ruleTester = new RuleTester({
},
});

const errMessage =
'it(), test() and describe() descriptions should begin with lowercase';
const errors = [{ message: errMessage, column: 1, line: 1 }];

ruleTester.run('lowercase-description', rules['lowercase-description'], {
valid: [
"it(' ', function () {})",
Expand All @@ -36,44 +32,99 @@ ruleTester.run('lowercase-description', rules['lowercase-description'], {
'describe("<Foo/>", function () {})',
'describe("123 foo", function () {})',
'describe("42", function () {})',
'describe(function () {})',
],

invalid: [
{
code: "it('Foo', function () {})",
errors,
errors: [
{
message: '`it`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
{
code: 'it("Foo", function () {})',
errors,
errors: [
{
message: '`it`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
{
code: 'it(`Foo`, function () {})',
errors,
errors: [
{
message: '`it`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
{
code: "test('Foo', function () {})",
errors,
errors: [
{
message: '`test`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
{
code: 'test("Foo", function () {})',
errors,
errors: [
{
message: '`test`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
{
code: 'test(`Foo`, function () {})',
errors,
errors: [
{
message: '`test`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
{
code: "describe('Foo', function () {})",
errors,
errors: [
{
message: '`describe`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
{
code: 'describe("Foo", function () {})',
errors,
errors: [
{
message: '`describe`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
{
code: 'describe(`Foo`, function () {})',
errors,
errors: [
{
message: '`describe`s should begin with lowercase',
column: 1,
line: 1,
},
],
},
],
});
31 changes: 18 additions & 13 deletions rules/lowercase-description.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict';

const ruleMsg =
'it(), test() and describe() descriptions should begin with lowercase';

const isItTestOrDescribeFunction = node => {
return (
node.type === 'CallExpression' &&
Expand All @@ -23,24 +20,29 @@ const isItDescription = node => {
};

const testDescription = node => {
const type = node.arguments[0].type;
const firstArgument = node.arguments[0];
const type = firstArgument.type;

if (type === 'Literal') {
return node.arguments[0].value;
}
if (type === 'TemplateLiteral') {
return node.arguments[0].quasis[0].value.raw;
return firstArgument.value;
}

// `isItDescription` guarantees this is `type === 'TemplateLiteral'`
return firstArgument.quasis[0].value.raw;
};

const descriptionBeginsWithLowerCase = node => {
if (isItTestOrDescribeFunction(node) && isItDescription(node)) {
const description = testDescription(node);
if (!description[0]) {
return true;
return false;
}

if (description[0] !== description[0].toLowerCase()) {
return node.callee.name;
}
return description[0] === description[0].toLowerCase();
}
return true;
return false;
};

module.exports = {
Expand All @@ -53,9 +55,12 @@ module.exports = {
create(context) {
return {
CallExpression(node) {
if (!descriptionBeginsWithLowerCase(node)) {
const erroneousMethod = descriptionBeginsWithLowerCase(node);

if (erroneousMethod) {
context.report({
message: ruleMsg,
message: '`{{ method }}`s should begin with lowercase',
data: { method: erroneousMethod },
node: node,
});
}
Expand Down

0 comments on commit 45396c1

Please sign in to comment.