Skip to content

Commit

Permalink
fix: reporting location in no-missing-placeholders (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Aug 4, 2022
1 parent f5a5c24 commit 31ff45c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 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
49 changes: 37 additions & 12 deletions tests/lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ const RuleTester = require('eslint').RuleTester;
* @param {string} missingKey The placeholder that is missing
* @returns {object} An expected error
*/
function error(missingKey, type = 'Literal') {
function error(missingKey, type, extra) {
return {
type,
message: `The placeholder {{${missingKey}}} is missing (must provide it in the report's \`data\` object).`,
...extra,
};
}

Expand Down Expand Up @@ -235,7 +236,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [error('bar', 'Literal')],
},
{
code: `
Expand All @@ -249,7 +250,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [error('bar', 'ObjectExpression')],
},
{
code: `
Expand All @@ -263,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 @@ -281,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 @@ -303,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 @@ -343,7 +356,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [error('bar', 'ObjectExpression')],
},
{
// Suggestion and messageId
Expand All @@ -362,7 +375,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
errors: [error('bar')],
errors: [error('bar', 'Literal')],
},
{
// `create` in variable.
Expand All @@ -376,7 +389,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
module.exports = { create };
`,
errors: [error('hasOwnProperty')],
errors: [error('hasOwnProperty', 'ObjectExpression')],
},
{
// messageId.
Expand All @@ -391,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 31ff45c

Please sign in to comment.