Skip to content

Improve violation reporting location in no-missing-placeholders #280

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
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
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,
}
),
],
},
],
});