Skip to content

Commit

Permalink
Fix: False negative in prefer-message-ids rule (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Jul 30, 2021
1 parent e4b0f92 commit c5c4b62
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/rules/prefer-message-ids.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const utils = require('../utils');
const { getStaticValue } = require('eslint-utils');

// ------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -17,11 +18,15 @@ module.exports = {
fixable: null,
schema: [],
messages: {
messagesMissing: '`meta.messages` must contain at least one violation message.',
foundMessage: 'Use `messageId` instead of `message`.',
},
},

create (context) {
const sourceCode = context.getSourceCode();
const info = utils.getRuleInfo(sourceCode);

let contextIdentifiers;

// ----------------------------------------------------------------------
Expand All @@ -31,6 +36,30 @@ module.exports = {
return {
Program (ast) {
contextIdentifiers = utils.getContextIdentifiers(context, ast);

if (info === null || info.meta === null) {
return;
}

const metaNode = info.meta;
const messagesNode =
metaNode &&
metaNode.properties &&
metaNode.properties.find(p => p.type === 'Property' && utils.getKeyName(p) === 'messages');

if (!messagesNode) {
context.report({ node: metaNode, messageId: 'messagesMissing' });
return;
}

const staticValue = getStaticValue(messagesNode.value, context.getScope());
if (!staticValue) {
return;
}

if (typeof staticValue.value === 'object' && staticValue.value.constructor === Object && Object.keys(staticValue.value).length === 0) {
context.report({ node: messagesNode.value, messageId: 'messagesMissing' });
}
},
CallExpression (node) {
if (
Expand Down
75 changes: 75 additions & 0 deletions tests/lib/rules/prefer-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ ruleTester.run('prefer-message-ids', rule, {
]
});
`,

// `meta.messages` has a message
`
module.exports = {
meta: { messages: { someMessageId: 'some message' } },
create(context) {
context.report({ node, messageId: 'someMessageId' });
}
};
`,
// `meta.messages` has a message (in variable)
`
const messages = { someMessageId: 'some message' };
module.exports = {
meta: { messages },
create(context) {
context.report({ node, messageId: 'someMessageId' });
}
};
`,
],

invalid: [
Expand Down Expand Up @@ -100,5 +120,60 @@ ruleTester.run('prefer-message-ids', rule, {
`,
errors: [{ messageId: 'foundMessage', type: 'Property' }],
},

{
// `meta.messages` missing
code: `
module.exports = {
meta: { description: 'foo' },
create(context) { }
};
`,
errors: [{ messageId: 'messagesMissing', type: 'ObjectExpression' }],
},
{
// `meta.messages` empty
code: `
module.exports = {
meta: {
description: 'foo',
messages: {},
},
create(context) { }
};
`,
errors: [{ messageId: 'messagesMissing', type: 'ObjectExpression' }],
},
{
// `meta.messages` empty (in variable)
code: `
const messages = {};
module.exports = {
meta: {
description: 'foo',
messages,
},
create(context) { }
};
`,
errors: [{ messageId: 'messagesMissing', type: 'Identifier' }],
},
{
// `meta.messages` missing and using `message`
code: `
module.exports = {
meta: {
description: 'foo',
},
create(context) {
context.report({ node, message: 'foo' });
}
};
`,
errors: [
{ messageId: 'messagesMissing', type: 'ObjectExpression' },
{ messageId: 'foundMessage', type: 'Property' },
],
},
],
});

0 comments on commit c5c4b62

Please sign in to comment.