Skip to content

Commit

Permalink
fix: reporting location in no-missing-message-ids
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Aug 4, 2022
1 parent e2f3deb commit 01003fa
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
10 changes: 6 additions & 4 deletions lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ module.exports = {
});
}

for (const { message, data } of reportMessagesAndDataArray.filter(
(obj) => obj.message
)) {
for (const {
message,
messageId,
data,
} of reportMessagesAndDataArray.filter((obj) => obj.message)) {
const messageStaticValue = getStaticValue(
message,
context.getScope()
Expand Down Expand Up @@ -112,7 +114,7 @@ module.exports = {

if (!matchingProperty) {
context.report({
node: message,
node: data || messageId || message,
messageId: 'placeholderDoesNotExist',
data: { missingKey: match[1] },
});
Expand Down
54 changes: 41 additions & 13 deletions tests/lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ const RuleTester = require('eslint').RuleTester;
* @param {string} missingKey The placeholder that is missing
* @returns {object} An expected error
*/
function error(missingKey, type = 'Literal') {
return { type, message: `The placeholder {{${missingKey}}} does not exist.` };
function error(missingKey, type, extra) {
return {
type,
message: `The placeholder {{${missingKey}}} does not exist.`,
...extra,
};
}

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -232,7 +236,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [error('bar', 'Literal')],
},
{
code: `
Expand All @@ -246,7 +250,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [error('bar', 'ObjectExpression')],
},
{
code: `
Expand All @@ -260,15 +264,15 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('hasOwnProperty')],
errors: [error('hasOwnProperty', 'ObjectExpression')],
},
{
code: `
module.exports = context => {
context.report(node, 'foo {{bar}}', { baz: 'qux' }); return {};
};
`,
errors: [error('bar')],
errors: [error('bar', 'ObjectExpression')],
},
{
// Message in variable.
Expand All @@ -278,15 +282,15 @@ ruleTester.run('no-missing-placeholders', rule, {
context.report(node, MESSAGE, { baz: 'qux' }); return {};
};
`,
errors: [error('bar', 'Identifier')],
errors: [error('bar', 'ObjectExpression')],
},
{
code: `
module.exports = context => {
context.report(node, { line: 1, column: 3 }, 'foo {{bar}}', { baz: 'baz' }); return {};
};
`,
errors: [error('bar')],
errors: [error('bar', 'ObjectExpression')],
},
{
code: `
Expand All @@ -300,7 +304,19 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [
error(
'bar',
'ObjectExpression',
// report on data
{
line: 7,
endLine: 7,
column: 21,
endColumn: 39,
}
),
],
},

{
Expand Down Expand Up @@ -340,7 +356,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [error('bar', 'ObjectExpression')],
},
{
// Suggestion and messageId
Expand All @@ -359,7 +375,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [error('bar', 'Literal')],
},
{
// `create` in variable.
Expand All @@ -373,7 +389,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
module.exports = { create };
`,
errors: [error('hasOwnProperty')],
errors: [error('hasOwnProperty', 'ObjectExpression')],
},
{
// messageId.
Expand All @@ -388,7 +404,19 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [
error(
'bar',
'Literal',
// report on the messageId
{
line: 7,
endLine: 7,
column: 26,
endColumn: 39,
}
),
],
},
],
});

0 comments on commit 01003fa

Please sign in to comment.