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

no-invalid-debug-function-arguments: Use dynamic error message #365

Merged
merged 1 commit into from Dec 29, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/rules/no-invalid-debug-function-arguments.js
Expand Up @@ -7,7 +7,10 @@ const emberUtils = require('../utils/ember');
// Rule Definition
//------------------------------------------------------------------------------

const ERROR_MESSAGE = "Usage of Ember's `assert` / `warn` / `deprecate` function has its arguments passed in the wrong order. `String description` should come before `Boolean condition`.";
function getErrorMessage(debugFunction) {
return `Usage of Ember's \`${debugFunction}\` function has its arguments passed in the wrong order. \`String description\` should come before \`Boolean condition\`.`;
}

const DEBUG_FUNCTIONS = ['assert', 'deprecate', 'warn'];

module.exports = {
Expand All @@ -20,7 +23,7 @@ module.exports = {
fixable: null
},

ERROR_MESSAGE,
getErrorMessage,
DEBUG_FUNCTIONS,

create(context) {
Expand All @@ -29,7 +32,7 @@ module.exports = {
if (isDebugFunctionWithReversedArgs(node)) {
context.report({
node,
message: ERROR_MESSAGE
message: getErrorMessage(getDebugFunction(node))
});
}
}
Expand All @@ -47,8 +50,11 @@ function isDebugFunctionWithReversedArgs(node) {
}

function isDebugFunction(node) {
// Example: Detects `Ember.assert()` or `assert()`.
return DEBUG_FUNCTIONS.some(debugFunction => emberUtils.isModule(node, debugFunction));
return getDebugFunction(node) !== undefined;
}

function getDebugFunction(node) {
return DEBUG_FUNCTIONS.find(debugFunction => emberUtils.isModule(node, debugFunction));
}

function isString(node) {
Expand Down
16 changes: 8 additions & 8 deletions tests/lib/rules/no-invalid-debug-function-arguments.js
Expand Up @@ -5,7 +5,7 @@
const rule = require('../../../lib/rules/no-invalid-debug-function-arguments');
const RuleTester = require('eslint').RuleTester;

const { DEBUG_FUNCTIONS, ERROR_MESSAGE } = rule;
const { DEBUG_FUNCTIONS, getErrorMessage } = rule;

//------------------------------------------------------------------------------
// Tests
Expand Down Expand Up @@ -67,31 +67,31 @@ const VALID_USAGES = [
const INVALID_USAGES = flatten(DEBUG_FUNCTIONS.map(debugFunction => [
{
code: `${debugFunction}(true, 'My description.');`,
errors: [{ message: ERROR_MESSAGE, type: 'CallExpression' }]
errors: [{ message: getErrorMessage(debugFunction), type: 'CallExpression' }]
},
{
code: `Ember.${debugFunction}(true, 'My description.');`,
errors: [{ message: ERROR_MESSAGE, type: 'CallExpression' }]
errors: [{ message: getErrorMessage(debugFunction), type: 'CallExpression' }]
},
{
code: `${debugFunction}(true, 'My description.', { id: 'some-id' });`,
errors: [{ message: ERROR_MESSAGE, type: 'CallExpression' }]
errors: [{ message: getErrorMessage(debugFunction), type: 'CallExpression' }]
},
{
code: `Ember.${debugFunction}(true, 'My description.', { id: 'some-id' });`,
errors: [{ message: ERROR_MESSAGE, type: 'CallExpression' }]
errors: [{ message: getErrorMessage(debugFunction), type: 'CallExpression' }]
},
{
code: `${debugFunction}(true, \`My \${123} description.\`);`,
errors: [{ message: ERROR_MESSAGE, type: 'CallExpression' }]
errors: [{ message: getErrorMessage(debugFunction), type: 'CallExpression' }]
},
{
code: `const CONDITION = true; ${debugFunction}(CONDITION, 'My description.');`,
errors: [{ message: ERROR_MESSAGE, type: 'CallExpression' }]
errors: [{ message: getErrorMessage(debugFunction), type: 'CallExpression' }]
},
{
code: `const CONDITION = true; ${debugFunction}(CONDITION, \`My \${123} description.\`);`,
errors: [{ message: ERROR_MESSAGE, type: 'CallExpression' }]
errors: [{ message: getErrorMessage(debugFunction), type: 'CallExpression' }]
}
]));

Expand Down