Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the assertion message is a string #314

Merged
merged 2 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/assertion-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const test = require('ava');
test(t => {
t.is(value); // Not enough arguments
t.is(value, expected, message, extra); // Too many arguments
t.is(value, expected, false); // Assertion message is not a string
});

/* eslint ava/assertion-arguments: ["error", {"message": "always"}] */
Expand Down
27 changes: 26 additions & 1 deletion rules/assertion-arguments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const {visitIf} = require('enhance-visitors');
const {getStaticValue, isOpeningParenToken, isCommaToken} = require('eslint-utils');
const {getStaticValue, isOpeningParenToken, isCommaToken, findVariable} = require('eslint-utils');
const util = require('../util');
const createAvaRule = require('../create-ava-rule');

Expand Down Expand Up @@ -203,6 +203,13 @@ function noComments(sourceCode, ...nodes) {
});
}

function isString(node) {
const {type} = node;
return type === 'TemplateLiteral' ||
type === 'TaggedTemplateExpression' ||
(type === 'Literal' && typeof node.value === 'string');
}

const create = context => {
const ava = createAvaRule();
const options = context.options[0] || {};
Expand Down Expand Up @@ -271,6 +278,24 @@ const create = context => {

checkArgumentOrder({node, assertion: firstNonSkipMember, context});
}

if (gottenArgs === nArgs.max && nArgs.min !== nArgs.max) {
let lastArg = node.arguments[node.arguments.length - 1];

if (lastArg.type === 'Identifier') {
const variable = findVariable(context.getScope(), lastArg);
let value;
for (const ref of variable.references) {
value = ref.writeExpr || value;
}

lastArg = value;
}

if (!isString(lastArg)) {
report(node, 'Assertion message should be a string.');
}
}
})
});
};
Expand Down
15 changes: 13 additions & 2 deletions test/assertion-arguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const outOfOrderError = (line, column, endLine, endColumn) => ({
message: 'Expected values should come after actual values.',
line, column, endLine, endColumn
});
const messageIsNotStringError = 'Assertion message should be a string.';

const header = 'const test = require(\'ava\');';

Expand Down Expand Up @@ -331,7 +332,11 @@ ruleTester.run('assertion-arguments', rule, {
testCase(false, 't.regex(\'static\', new RegExp(\'[dynamic]+\'));'),
testCase(false, 't.regex(dynamic, /[static]/);'),
testCase(false, 't.notRegex(\'static\', new RegExp(\'[dynamic]+\'));'),
testCase(false, 't.notRegex(dynamic, /[static]/);')
testCase(false, 't.notRegex(dynamic, /[static]/);'),

// Lookup message type
testCase(false, 'const message = \'ok\'; t.assert(true, message);'),
testCase(false, 'const message = \'ok\'; t.is(42, 42, message);')
],
invalid: [
// Not enough arguments
Expand Down Expand Up @@ -538,6 +543,12 @@ ruleTester.run('assertion-arguments', rule, {
outOfOrderError(1, 13, 1, 23 + expression.length),
{output: `t.deepEqual(${expression}, 'static');`}
)
)
),

// Message is not string
testCase(false, 't.assert(true, true);', messageIsNotStringError),
testCase(false, 't.deepEqual({}, {}, 42);', messageIsNotStringError),
testCase(false, 't.fail({});', messageIsNotStringError),
testCase(false, 'let message = "ok"; message = false; t.assert(true, message);', messageIsNotStringError)
]
});