From 27c0b6558a9531c3b87a2776b1553adf29834e87 Mon Sep 17 00:00:00 2001 From: Bryan Mishkin <698306+bmish@users.noreply.github.com> Date: Wed, 3 Aug 2022 23:49:32 -0400 Subject: [PATCH] fix: Improve violation reporting location for `no-unused-placeholders` (#279) * fix: reporting location for no-unused-placeholders * improve test --- lib/rules/no-unused-placeholders.js | 2 +- tests/lib/rules/no-missing-placeholders.js | 14 +++++++++++++- tests/lib/rules/no-unused-placeholders.js | 20 ++++++++++++++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/rules/no-unused-placeholders.js b/lib/rules/no-unused-placeholders.js index e877eb9d..97a66312 100644 --- a/lib/rules/no-unused-placeholders.js +++ b/lib/rules/no-unused-placeholders.js @@ -108,7 +108,7 @@ module.exports = { const key = utils.getKeyName(prop); if (!placeholdersInMessage.has(key)) { context.report({ - node: message, + node: prop, messageId: 'placeholderUnused', data: { unusedKey: key }, }); diff --git a/tests/lib/rules/no-missing-placeholders.js b/tests/lib/rules/no-missing-placeholders.js index 3390c75f..4a42b5ab 100644 --- a/tests/lib/rules/no-missing-placeholders.js +++ b/tests/lib/rules/no-missing-placeholders.js @@ -236,7 +236,19 @@ ruleTester.run('no-missing-placeholders', rule, { } }; `, - errors: [error('bar', 'Literal')], + errors: [ + error( + 'bar', + 'Literal', + // report on `message` + { + line: 6, + endLine: 6, + column: 24, + endColumn: 37, + } + ), + ], }, { code: ` diff --git a/tests/lib/rules/no-unused-placeholders.js b/tests/lib/rules/no-unused-placeholders.js index aad9d35b..a3c0f6c5 100644 --- a/tests/lib/rules/no-unused-placeholders.js +++ b/tests/lib/rules/no-unused-placeholders.js @@ -17,10 +17,11 @@ const RuleTester = require('eslint').RuleTester; * @param {string} unusedKey The placeholder that is unused * @returns {object} An expected error */ -function error(unusedKey, type = 'Literal') { +function error(unusedKey, extra) { return { - type, + type: 'Property', // The property in the report's `data` object for the unused placeholder. message: `The placeholder {{${unusedKey}}} is unused (does not exist in the actual message).`, + ...extra, }; } @@ -211,7 +212,18 @@ ruleTester.run('no-unused-placeholders', rule, { } }; `, - errors: [error('bar')], + errors: [ + error( + 'bar', + // report on property in data object + { + line: 7, + endLine: 7, + column: 23, + endColumn: 26, + } + ), + ], }, { // With `create` as variable. @@ -241,7 +253,7 @@ ruleTester.run('no-unused-placeholders', rule, { } }; `, - errors: [error('bar', 'Identifier')], + errors: [error('bar')], }, { code: `