Skip to content

Commit

Permalink
Merge pull request #237 from lo1tuma/template-strings
Browse files Browse the repository at this point in the history
Check static template strings in valid-test-description and valid-suite-description
  • Loading branch information
lo1tuma authored Feb 18, 2020
2 parents 7eea93d + 6255546 commit 1c3a545
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 20 deletions.
10 changes: 6 additions & 4 deletions lib/rules/valid-suite-description.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict';

const { getStringIfConstant } = require('eslint-utils');

/**
* @fileoverview Match suite descriptions to match a pre-configured regular expression
* @author Alexander Afanasyev
*/

const astUtils = require('../util/ast');
const defaultSuiteNames = [ 'describe', 'context', 'suite' ];

function inlineOptions(options) {
Expand Down Expand Up @@ -76,10 +77,11 @@ module.exports = {

function hasValidSuiteDescription(mochaCallExpression) {
const args = mochaCallExpression.arguments;
const description = args[0];
const descriptionArgument = args[0];
const description = getStringIfConstant(descriptionArgument, context.getScope());

if (astUtils.isStringLiteral(description)) {
return pattern.test(description.value);
if (description) {
return pattern.test(description);
}

return true;
Expand Down
9 changes: 5 additions & 4 deletions lib/rules/valid-test-description.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

const { getStringIfConstant } = require('eslint-utils');

/**
* @fileoverview Match test descriptions to match a pre-configured regular expression
* @author Alexander Afanasyev
*/

const astUtils = require('../util/ast');

const defaultTestNames = [ 'it', 'test', 'specify' ];

function inlineOptions(options) {
Expand Down Expand Up @@ -77,9 +77,10 @@ module.exports = {
function hasValidTestDescription(mochaCallExpression) {
const args = mochaCallExpression.arguments;
const testDescriptionArgument = args[0];
const description = getStringIfConstant(testDescriptionArgument, context.getScope());

if (astUtils.isStringLiteral(testDescriptionArgument)) {
return pattern.test(testDescriptionArgument.value);
if (description) {
return pattern.test(description);
}

return true;
Expand Down
5 changes: 0 additions & 5 deletions lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ function isMochaFunctionCall(node, scope) {
return isTestCase(node) || isDescribe(node) || isHookCall(node);
}

function isStringLiteral(node) {
return node && node.type === 'Literal' && typeof node.value === 'string';
}

function hasParentMochaFunctionCall(functionExpression) {
return isTestCase(functionExpression.parent) || isHookCall(functionExpression.parent);
}
Expand All @@ -130,7 +126,6 @@ module.exports = {
isMochaFunctionCall,
isHookCall,
isSuiteConfigCall,
isStringLiteral,
hasParentMochaFunctionCall,
findReturnStatement,
isReturnOfUndefined
Expand Down
29 changes: 22 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"changelog": "pr-log"
},
"dependencies": {
"eslint-utils": "^2.0.0",
"ramda": "^0.26.1"
},
"devDependencies": {
Expand Down
30 changes: 30 additions & 0 deletions test/rules/valid-suite-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ ruleTester.run('valid-suite-description', rules['valid-suite-description'], {
parserOptions: { ecmaVersion: 2017 },
options: [ '^Foo' ],
code: 'describe(`Foo with template strings`, function () {});'
},
{
parserOptions: { ecmaVersion: 2019 },
options: [ '^Foo' ],
code: 'describe(anyTag`with template strings`, function () {});'
},
{
parserOptions: { ecmaVersion: 2019 },
options: [ '^Foo' ],
code: 'describe(`${dynamicVar} with template strings`, function () {});'
}

],
Expand Down Expand Up @@ -89,6 +99,26 @@ ruleTester.run('valid-suite-description', rules['valid-suite-description'], {
errors: [
{ message: 'some error message' }
]
},
{
options: [ '^[A-Z]' ],
code: 'describe(`this is a test`, function () { });',
parserOptions: {
ecmaVersion: 2019
},
errors: [
{ message: 'Invalid "describe()" description found.', line: 1, column: 1 }
]
},
{
options: [ '^[A-Z]' ],
code: 'const foo = "this"; describe(`${foo} is a test`, function () { });',
parserOptions: {
ecmaVersion: 2019
},
errors: [
{ message: 'Invalid "describe()" description found.', line: 1, column: 21 }
]
}
]
});
26 changes: 26 additions & 0 deletions test/rules/valid-test-description.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ ruleTester.run('valid-test-description', rules['valid-test-description'], {
{
parserOptions: { ecmaVersion: 2017 },
code: 'it(`should work with template strings`, function () {});'
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'it(foo`work with template strings`, function () {});'
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'it(`${foo} work with template strings`, function () {});'
}
],

Expand Down Expand Up @@ -113,6 +121,24 @@ ruleTester.run('valid-test-description', rules['valid-test-description'], {
errors: [
{ message: 'Invalid "it()" description found.' }
]
},
{
code: 'it(`this is a test`, function () { });',
parserOptions: {
ecmaVersion: 2019
},
errors: [
{ message: 'Invalid "it()" description found.', line: 1, column: 1 }
]
},
{
code: 'const foo = "this"; it(`${foo} is a test`, function () { });',
parserOptions: {
ecmaVersion: 2019
},
errors: [
{ message: 'Invalid "it()" description found.', line: 1, column: 21 }
]
}
]
});

0 comments on commit 1c3a545

Please sign in to comment.