Skip to content

Commit

Permalink
Merge pull request #316 from lo1tuma/fix-no-empty-desc2
Browse files Browse the repository at this point in the history
Allow dynamic expressions as description argument in no-empty-description
  • Loading branch information
lo1tuma committed Dec 14, 2021
2 parents 1747811 + 3fea088 commit a6377da
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
28 changes: 21 additions & 7 deletions lib/rules/no-empty-description.js
Expand Up @@ -14,22 +14,31 @@ function objectOptions(options = {}) {
return { testNames, message };
}

function isTemplateString(node) {
return [ 'TaggedTemplateExpression', 'TemplateLiteral' ].includes(node && node.type);
}

function isIdentifier(node) {
return node && node.type === 'Identifier';
function isLiteral(node) {
return node && node.type === 'Literal';
}

function isStaticallyAnalyzableDescription(node, extractedText) {
if (extractedText === null) {
return !(isTemplateString(node) || isIdentifier(node));
return isLiteral(node);
}

return true;
}

function isValidDescriptionArgumentNode(node) {
if (!node) {
return false;
}

return [
'Literal', 'TemplateLiteral', 'TaggedTemplateExpression',
'Identifier', 'MemberExpression', 'CallExpression',
'LogicalExpression', 'BinaryExpression', 'ConditionalExpression',
'UnaryExpression', 'SpreadElement', 'AwaitExpression',
'YieldExpression', 'NewExpression' ].includes(node.type);
}

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -69,6 +78,11 @@ module.exports = {

function isNonEmptyDescription(mochaCallExpression) {
const description = mochaCallExpression.arguments[0];

if (!isValidDescriptionArgumentNode(description)) {
return false;
}

const text = getStringIfConstant(description, context.getScope());

if (!isStaticallyAnalyzableDescription(description, text)) {
Expand Down
44 changes: 44 additions & 0 deletions test/rules/no-empty-description.js
Expand Up @@ -31,6 +31,36 @@ ruleTester.run('no-empty-description', rules['no-empty-description'], {

'var dynamicTitle = "foo"; it(dynamicTitle, function() {});',
'it(dynamicTitle, function() {});',
'var dynamicTitle = "foo"; it(dynamicTitle.replace("foo", ""), function() {});',
'it(dynamicTitle.replace("foo", ""), function() {});',
'it(42, function() { })',
'it(true, function() { })',
'it(null, function() { })',
'it(new Foo(), function() { })',
'it(foo.bar, function() { })',
'it("foo".toUpperCase(), function() { })',
{
parserOptions: { ecmaVersion: 2020 },
code: 'it(foo ?? "bar", function() { })'
},
'it(foo || "bar", function() { })',
'it(foo ? "bar" : "baz", function() { })',
'it(foo ? bar : baz, function() { })',
'it(typeof foo, function() { })',
'it(foo + bar, function() { })',
'it("foo" + "bar", function() { })',
{
parserOptions: { ecmaVersion: 2019 },
code: 'it(...args)'
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'async function a() { it(await foo, function () {}); }'
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'function* g() { it(yield foo, function () {}); }'
},

'notTest()',

Expand All @@ -53,6 +83,10 @@ ruleTester.run('no-empty-description', rules['no-empty-description'], {
{
parserOptions: { ecmaVersion: 2019 },
code: 'const dynamicTitle = "foo"; it(dynamicTitle, function() {});'
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'const dynamicTitle = "foo"; it(dynamicTitle.replace("foo", ""), function() {});'
}
],

Expand Down Expand Up @@ -88,6 +122,16 @@ ruleTester.run('no-empty-description', rules['no-empty-description'], {
parserOptions: { ecmaVersion: 2019 },
code: 'const foo = ""; it(foo);',
errors: [ { message: defaultErrorMessage, line: 1, column: 17 } ]
},
{
parserOptions: { ecmaVersion: 2019 },
code: 'const foo = { bar: "" }; it(foo.bar);',
errors: [ { message: defaultErrorMessage, line: 1, column: 26 } ]
},
{
parserOptions: { ecmaVersion: 2020 },
code: 'it(foo?.bar);',
errors: [ { message: defaultErrorMessage, line: 1, column: 1 } ]
}
]

Expand Down

0 comments on commit a6377da

Please sign in to comment.