Skip to content

Commit d7461a3

Browse files
committed
use utils
1 parent af5886c commit d7461a3

5 files changed

+76
-71
lines changed

lib/rules/no-missing-message-ids.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = {
7171
values.forEach((val) => {
7272
if (
7373
val.type === 'Literal' &&
74-
val.value !== null &&
74+
typeof val.value === 'string' &&
7575
val.value !== '' &&
7676
!utils.getMessageIdNodeById(
7777
val.value,

lib/rules/no-missing-placeholders.js

+24-34
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,16 @@ module.exports = {
3232

3333
create(context) {
3434
const sourceCode = context.getSourceCode();
35+
const { scopeManager } = sourceCode;
36+
3537
let contextIdentifiers;
3638

37-
// ----------------------------------------------------------------------
38-
// Public
39-
// ----------------------------------------------------------------------
39+
const ruleInfo = utils.getRuleInfo(sourceCode);
40+
const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager);
4041

4142
return {
4243
Program(ast) {
43-
contextIdentifiers = utils.getContextIdentifiers(
44-
sourceCode.scopeManager,
45-
ast
46-
);
44+
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
4745
},
4846
CallExpression(node) {
4947
if (
@@ -57,38 +55,30 @@ module.exports = {
5755
return;
5856
}
5957

60-
const info = utils.getRuleInfo(sourceCode);
61-
const metaNode = info.meta;
62-
const messagesNode =
63-
metaNode &&
64-
metaNode.properties &&
65-
metaNode.properties.find(
66-
(p) => p.type === 'Property' && utils.getKeyName(p) === 'messages'
67-
);
68-
6958
const reportMessagesAndDataArray =
7059
utils.collectReportViolationAndSuggestionData(reportInfo);
7160

72-
// Check for any potential instances where we can use the messageId to fill in the message for convenience.
73-
reportMessagesAndDataArray.forEach((obj) => {
74-
if (
75-
!obj.message &&
76-
obj.messageId &&
77-
obj.messageId.type === 'Literal'
78-
) {
79-
const correspondingMessage =
80-
messagesNode &&
81-
messagesNode.value.properties &&
82-
messagesNode.value.properties.find(
83-
(p) =>
84-
p.type === 'Property' &&
85-
utils.getKeyName(p) === obj.messageId.value
61+
if (messagesNode) {
62+
// Check for any potential instances where we can use the messageId to fill in the message for convenience.
63+
reportMessagesAndDataArray.forEach((obj) => {
64+
if (
65+
!obj.message &&
66+
obj.messageId &&
67+
obj.messageId.type === 'Literal' &&
68+
typeof obj.messageId.value === 'string'
69+
) {
70+
const correspondingMessage = utils.getMessageIdNodeById(
71+
obj.messageId.value,
72+
ruleInfo,
73+
scopeManager,
74+
context.getScope()
8675
);
87-
if (correspondingMessage) {
88-
obj.message = correspondingMessage.value;
76+
if (correspondingMessage) {
77+
obj.message = correspondingMessage.value;
78+
}
8979
}
90-
}
91-
});
80+
});
81+
}
9282

9383
for (const { message, data } of reportMessagesAndDataArray.filter(
9484
(obj) => obj.message

lib/rules/no-unused-placeholders.js

+24-34
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,16 @@ module.exports = {
3131

3232
create(context) {
3333
const sourceCode = context.getSourceCode();
34+
const { scopeManager } = sourceCode;
35+
3436
let contextIdentifiers;
3537

36-
// ----------------------------------------------------------------------
37-
// Public
38-
// ----------------------------------------------------------------------
38+
const ruleInfo = utils.getRuleInfo(sourceCode);
39+
const messagesNode = utils.getMessagesNode(ruleInfo, scopeManager);
3940

4041
return {
4142
Program(ast) {
42-
contextIdentifiers = utils.getContextIdentifiers(
43-
sourceCode.scopeManager,
44-
ast
45-
);
43+
contextIdentifiers = utils.getContextIdentifiers(scopeManager, ast);
4644
},
4745
CallExpression(node) {
4846
if (
@@ -56,38 +54,30 @@ module.exports = {
5654
return;
5755
}
5856

59-
const info = utils.getRuleInfo(sourceCode);
60-
const metaNode = info.meta;
61-
const messagesNode =
62-
metaNode &&
63-
metaNode.properties &&
64-
metaNode.properties.find(
65-
(p) => p.type === 'Property' && utils.getKeyName(p) === 'messages'
66-
);
67-
6857
const reportMessagesAndDataArray =
6958
utils.collectReportViolationAndSuggestionData(reportInfo);
7059

71-
// Check for any potential instances where we can use the messageId to fill in the message for convenience.
72-
reportMessagesAndDataArray.forEach((obj) => {
73-
if (
74-
!obj.message &&
75-
obj.messageId &&
76-
obj.messageId.type === 'Literal'
77-
) {
78-
const correspondingMessage =
79-
messagesNode &&
80-
messagesNode.value.properties &&
81-
messagesNode.value.properties.find(
82-
(p) =>
83-
p.type === 'Property' &&
84-
utils.getKeyName(p) === obj.messageId.value
60+
if (messagesNode) {
61+
// Check for any potential instances where we can use the messageId to fill in the message for convenience.
62+
reportMessagesAndDataArray.forEach((obj) => {
63+
if (
64+
!obj.message &&
65+
obj.messageId &&
66+
obj.messageId.type === 'Literal' &&
67+
typeof obj.messageId.value === 'string'
68+
) {
69+
const correspondingMessage = utils.getMessageIdNodeById(
70+
obj.messageId.value,
71+
ruleInfo,
72+
scopeManager,
73+
context.getScope()
8574
);
86-
if (correspondingMessage) {
87-
obj.message = correspondingMessage.value;
75+
if (correspondingMessage) {
76+
obj.message = correspondingMessage.value;
77+
}
8878
}
89-
}
90-
});
79+
});
80+
}
9181

9282
for (const { message, data } of reportMessagesAndDataArray.filter(
9383
(obj) => obj.message

tests/lib/rules/no-missing-placeholders.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ ruleTester.run('no-missing-placeholders', rule, {
124124
}
125125
};
126126
`,
127-
// messageId but the message property doesn't exist yet.
127+
// messageId but the message doesn't exist in `meta.messages`.
128128
`
129129
module.exports = {
130130
meta: {
@@ -135,7 +135,7 @@ ruleTester.run('no-missing-placeholders', rule, {
135135
}
136136
};
137137
`,
138-
// messageId but no messages object doesn't exist yet.
138+
// messageId but no `meta.messages`.
139139
`
140140
module.exports = {
141141
meta: { },
@@ -144,6 +144,14 @@ ruleTester.run('no-missing-placeholders', rule, {
144144
}
145145
};
146146
`,
147+
// messageId but no `meta`.
148+
`
149+
module.exports = {
150+
create(context) {
151+
context.report({ node, messageId: 'myMessageId' });
152+
}
153+
};
154+
`,
147155
// messageId with correctly-used placeholder.
148156
`
149157
module.exports = {

tests/lib/rules/no-unused-placeholders.js

+17
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,23 @@ ruleTester.run('no-unused-placeholders', rule, {
161161
}
162162
};
163163
`,
164+
// messageId but no `meta.messages`.
165+
`
166+
module.exports = {
167+
meta: {},
168+
create(context) {
169+
context.report({ node, messageId: 'myMessageId' });
170+
}
171+
};
172+
`,
173+
// messageId but no `meta`.
174+
`
175+
module.exports = {
176+
create(context) {
177+
context.report({ node, messageId: 'myMessageId' });
178+
}
179+
};
180+
`,
164181
// messageId with correctly-used placeholder.
165182
`
166183
module.exports = {

0 commit comments

Comments
 (0)