Skip to content

Commit

Permalink
use utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Jul 4, 2022
1 parent af5886c commit d7461a3
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 71 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-missing-message-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = {
values.forEach((val) => {
if (
val.type === 'Literal' &&
val.value !== null &&
typeof val.value === 'string' &&
val.value !== '' &&
!utils.getMessageIdNodeById(
val.value,
Expand Down
58 changes: 24 additions & 34 deletions lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ module.exports = {

create(context) {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;

let contextIdentifiers;

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const ruleInfo = utils.getRuleInfo(sourceCode);
const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager);

return {
Program(ast) {
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast
);
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
},
CallExpression(node) {
if (
Expand All @@ -57,38 +55,30 @@ module.exports = {
return;
}

const info = utils.getRuleInfo(sourceCode);
const metaNode = info.meta;
const messagesNode =
metaNode &&
metaNode.properties &&
metaNode.properties.find(
(p) => p.type === 'Property' && utils.getKeyName(p) === 'messages'
);

const reportMessagesAndDataArray =
utils.collectReportViolationAndSuggestionData(reportInfo);

// Check for any potential instances where we can use the messageId to fill in the message for convenience.
reportMessagesAndDataArray.forEach((obj) => {
if (
!obj.message &&
obj.messageId &&
obj.messageId.type === 'Literal'
) {
const correspondingMessage =
messagesNode &&
messagesNode.value.properties &&
messagesNode.value.properties.find(
(p) =>
p.type === 'Property' &&
utils.getKeyName(p) === obj.messageId.value
if (messagesNode) {
// Check for any potential instances where we can use the messageId to fill in the message for convenience.
reportMessagesAndDataArray.forEach((obj) => {
if (
!obj.message &&
obj.messageId &&
obj.messageId.type === 'Literal' &&
typeof obj.messageId.value === 'string'
) {
const correspondingMessage = utils.getMessageIdNodeById(
obj.messageId.value,
ruleInfo,
scopeManager,
context.getScope()
);
if (correspondingMessage) {
obj.message = correspondingMessage.value;
if (correspondingMessage) {
obj.message = correspondingMessage.value;
}
}
}
});
});
}

for (const { message, data } of reportMessagesAndDataArray.filter(
(obj) => obj.message
Expand Down
58 changes: 24 additions & 34 deletions lib/rules/no-unused-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,16 @@ module.exports = {

create(context) {
const sourceCode = context.getSourceCode();
const { scopeManager } = sourceCode;

let contextIdentifiers;

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------
const ruleInfo = utils.getRuleInfo(sourceCode);
const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager);

return {
Program(ast) {
contextIdentifiers = utils.getContextIdentifiers(
sourceCode.scopeManager,
ast
);
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
},
CallExpression(node) {
if (
Expand All @@ -56,38 +54,30 @@ module.exports = {
return;
}

const info = utils.getRuleInfo(sourceCode);
const metaNode = info.meta;
const messagesNode =
metaNode &&
metaNode.properties &&
metaNode.properties.find(
(p) => p.type === 'Property' && utils.getKeyName(p) === 'messages'
);

const reportMessagesAndDataArray =
utils.collectReportViolationAndSuggestionData(reportInfo);

// Check for any potential instances where we can use the messageId to fill in the message for convenience.
reportMessagesAndDataArray.forEach((obj) => {
if (
!obj.message &&
obj.messageId &&
obj.messageId.type === 'Literal'
) {
const correspondingMessage =
messagesNode &&
messagesNode.value.properties &&
messagesNode.value.properties.find(
(p) =>
p.type === 'Property' &&
utils.getKeyName(p) === obj.messageId.value
if (messagesNode) {
// Check for any potential instances where we can use the messageId to fill in the message for convenience.
reportMessagesAndDataArray.forEach((obj) => {
if (
!obj.message &&
obj.messageId &&
obj.messageId.type === 'Literal' &&
typeof obj.messageId.value === 'string'
) {
const correspondingMessage = utils.getMessageIdNodeById(
obj.messageId.value,
ruleInfo,
scopeManager,
context.getScope()
);
if (correspondingMessage) {
obj.message = correspondingMessage.value;
if (correspondingMessage) {
obj.message = correspondingMessage.value;
}
}
}
});
});
}

for (const { message, data } of reportMessagesAndDataArray.filter(
(obj) => obj.message
Expand Down
12 changes: 10 additions & 2 deletions tests/lib/rules/no-missing-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
// messageId but the message property doesn't exist yet.
// messageId but the message doesn't exist in `meta.messages`.
`
module.exports = {
meta: {
Expand All @@ -135,7 +135,7 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
// messageId but no messages object doesn't exist yet.
// messageId but no `meta.messages`.
`
module.exports = {
meta: { },
Expand All @@ -144,6 +144,14 @@ ruleTester.run('no-missing-placeholders', rule, {
}
};
`,
// messageId but no `meta`.
`
module.exports = {
create(context) {
context.report({ node, messageId: 'myMessageId' });
}
};
`,
// messageId with correctly-used placeholder.
`
module.exports = {
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/no-unused-placeholders.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ ruleTester.run('no-unused-placeholders', rule, {
}
};
`,
// messageId but no `meta.messages`.
`
module.exports = {
meta: {},
create(context) {
context.report({ node, messageId: 'myMessageId' });
}
};
`,
// messageId but no `meta`.
`
module.exports = {
create(context) {
context.report({ node, messageId: 'myMessageId' });
}
};
`,
// messageId with correctly-used placeholder.
`
module.exports = {
Expand Down

0 comments on commit d7461a3

Please sign in to comment.