Skip to content

Commit 6255546

Browse files
committed
Check static template strings in valid-test-description and valid-suite-description
1 parent 7eea93d commit 6255546

7 files changed

+90
-20
lines changed

lib/rules/valid-suite-description.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict';
22

3+
const { getStringIfConstant } = require('eslint-utils');
4+
35
/**
46
* @fileoverview Match suite descriptions to match a pre-configured regular expression
57
* @author Alexander Afanasyev
68
*/
79

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

1112
function inlineOptions(options) {
@@ -76,10 +77,11 @@ module.exports = {
7677

7778
function hasValidSuiteDescription(mochaCallExpression) {
7879
const args = mochaCallExpression.arguments;
79-
const description = args[0];
80+
const descriptionArgument = args[0];
81+
const description = getStringIfConstant(descriptionArgument, context.getScope());
8082

81-
if (astUtils.isStringLiteral(description)) {
82-
return pattern.test(description.value);
83+
if (description) {
84+
return pattern.test(description);
8385
}
8486

8587
return true;

lib/rules/valid-test-description.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict';
22

3+
const { getStringIfConstant } = require('eslint-utils');
4+
35
/**
46
* @fileoverview Match test descriptions to match a pre-configured regular expression
57
* @author Alexander Afanasyev
68
*/
79

8-
const astUtils = require('../util/ast');
9-
1010
const defaultTestNames = [ 'it', 'test', 'specify' ];
1111

1212
function inlineOptions(options) {
@@ -77,9 +77,10 @@ module.exports = {
7777
function hasValidTestDescription(mochaCallExpression) {
7878
const args = mochaCallExpression.arguments;
7979
const testDescriptionArgument = args[0];
80+
const description = getStringIfConstant(testDescriptionArgument, context.getScope());
8081

81-
if (astUtils.isStringLiteral(testDescriptionArgument)) {
82-
return pattern.test(testDescriptionArgument.value);
82+
if (description) {
83+
return pattern.test(description);
8384
}
8485

8586
return true;

lib/util/ast.js

-5
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ function isMochaFunctionCall(node, scope) {
100100
return isTestCase(node) || isDescribe(node) || isHookCall(node);
101101
}
102102

103-
function isStringLiteral(node) {
104-
return node && node.type === 'Literal' && typeof node.value === 'string';
105-
}
106-
107103
function hasParentMochaFunctionCall(functionExpression) {
108104
return isTestCase(functionExpression.parent) || isHookCall(functionExpression.parent);
109105
}
@@ -130,7 +126,6 @@ module.exports = {
130126
isMochaFunctionCall,
131127
isHookCall,
132128
isSuiteConfigCall,
133-
isStringLiteral,
134129
hasParentMochaFunctionCall,
135130
findReturnStatement,
136131
isReturnOfUndefined

package-lock.json

+22-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"changelog": "pr-log"
2323
},
2424
"dependencies": {
25+
"eslint-utils": "^2.0.0",
2526
"ramda": "^0.26.1"
2627
},
2728
"devDependencies": {

test/rules/valid-suite-description.js

+30
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ ruleTester.run('valid-suite-description', rules['valid-suite-description'], {
4343
parserOptions: { ecmaVersion: 2017 },
4444
options: [ '^Foo' ],
4545
code: 'describe(`Foo with template strings`, function () {});'
46+
},
47+
{
48+
parserOptions: { ecmaVersion: 2019 },
49+
options: [ '^Foo' ],
50+
code: 'describe(anyTag`with template strings`, function () {});'
51+
},
52+
{
53+
parserOptions: { ecmaVersion: 2019 },
54+
options: [ '^Foo' ],
55+
code: 'describe(`${dynamicVar} with template strings`, function () {});'
4656
}
4757

4858
],
@@ -89,6 +99,26 @@ ruleTester.run('valid-suite-description', rules['valid-suite-description'], {
8999
errors: [
90100
{ message: 'some error message' }
91101
]
102+
},
103+
{
104+
options: [ '^[A-Z]' ],
105+
code: 'describe(`this is a test`, function () { });',
106+
parserOptions: {
107+
ecmaVersion: 2019
108+
},
109+
errors: [
110+
{ message: 'Invalid "describe()" description found.', line: 1, column: 1 }
111+
]
112+
},
113+
{
114+
options: [ '^[A-Z]' ],
115+
code: 'const foo = "this"; describe(`${foo} is a test`, function () { });',
116+
parserOptions: {
117+
ecmaVersion: 2019
118+
},
119+
errors: [
120+
{ message: 'Invalid "describe()" description found.', line: 1, column: 21 }
121+
]
92122
}
93123
]
94124
});

test/rules/valid-test-description.js

+26
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ ruleTester.run('valid-test-description', rules['valid-test-description'], {
4343
{
4444
parserOptions: { ecmaVersion: 2017 },
4545
code: 'it(`should work with template strings`, function () {});'
46+
},
47+
{
48+
parserOptions: { ecmaVersion: 2019 },
49+
code: 'it(foo`work with template strings`, function () {});'
50+
},
51+
{
52+
parserOptions: { ecmaVersion: 2019 },
53+
code: 'it(`${foo} work with template strings`, function () {});'
4654
}
4755
],
4856

@@ -113,6 +121,24 @@ ruleTester.run('valid-test-description', rules['valid-test-description'], {
113121
errors: [
114122
{ message: 'Invalid "it()" description found.' }
115123
]
124+
},
125+
{
126+
code: 'it(`this is a test`, function () { });',
127+
parserOptions: {
128+
ecmaVersion: 2019
129+
},
130+
errors: [
131+
{ message: 'Invalid "it()" description found.', line: 1, column: 1 }
132+
]
133+
},
134+
{
135+
code: 'const foo = "this"; it(`${foo} is a test`, function () { });',
136+
parserOptions: {
137+
ecmaVersion: 2019
138+
},
139+
errors: [
140+
{ message: 'Invalid "it()" description found.', line: 1, column: 21 }
141+
]
116142
}
117143
]
118144
});

0 commit comments

Comments
 (0)