Skip to content

Update: ensure report-message-format checks formatting in meta.messages #72

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

Merged
merged 1 commit into from
Dec 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions lib/rules/report-message-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,45 @@ module.exports = {
const pattern = new RegExp(context.options[0] || '');
let contextIdentifiers;

/**
* Report a message node if it doesn't match the given formatting
* @param {ASTNode} message The message AST node
* @returns {void}
*/
function processMessageNode (message) {
if (
(message.type === 'Literal' && typeof message.value === 'string' && !pattern.test(message.value)) ||
(message.type === 'TemplateLiteral' && message.quasis.length === 1 && !pattern.test(message.quasis[0].value.cooked))
) {
context.report({
node: message,
message: "Report message does not match the pattern '{{pattern}}'.",
data: { pattern: context.options[0] || '' },
});
}
}

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

return {
Program (node) {
contextIdentifiers = utils.getContextIdentifiers(context, node);
const ruleInfo = utils.getRuleInfo(context.getSourceCode().ast);
const messagesObject = ruleInfo &&
ruleInfo.meta &&
ruleInfo.meta.type === 'ObjectExpression' &&
ruleInfo.meta.properties.find(prop => prop.type === 'Property' && utils.getKeyName(prop) === 'messages');

if (!messagesObject || messagesObject.value.type !== 'ObjectExpression') {
return;
}

messagesObject.value.properties
.filter(prop => prop.type === 'Property')
.map(prop => prop.value)
.forEach(processMessageNode);
},
CallExpression (node) {
if (
Expand All @@ -49,16 +81,7 @@ module.exports = {
return;
}

if (
(message.type === 'Literal' && typeof message.value === 'string' && !pattern.test(message.value)) ||
(message.type === 'TemplateLiteral' && message.quasis.length === 1 && !pattern.test(message.quasis[0].value.cooked))
) {
context.report({
node: message,
message: "Report message does not match the pattern '{{pattern}}'.",
data: { pattern: context.options[0] || '' },
});
}
processMessageNode(message);
}
},
};
Expand Down
27 changes: 27 additions & 0 deletions tests/lib/rules/report-message-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ ruleTester.run('report-message-format', rule, {
`,
options: ['foo'],
},
{
code: `
module.exports = {
meta: {
messages: {
message1: 'foo bar',
message2: 'bar foobar'
}
},
create: context => ({})
}
`,
options: ['foo'],
},
],

invalid: [
Expand Down Expand Up @@ -176,6 +190,19 @@ ruleTester.run('report-message-format', rule, {
`,
options: ['foo'],
},
{
code: `
module.exports = {
meta: {
messages: {
message1: 'bar'
}
},
create: context => ({})
};
`,
options: ['foo'],
},
].map(invalidCase => {
return Object.assign({
errors: [{ message: `Report message does not match the pattern '${invalidCase.options[0]}'.` }],
Expand Down