Skip to content

Commit

Permalink
refactor: migrate all message to messageId (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
scagood authored Jul 17, 2024
1 parent ecbce9f commit b0fc13b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 17 deletions.
5 changes: 4 additions & 1 deletion rules/always-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ module.exports = {
additionalProperties: false,
},
],
messages: {
thenShouldReturnOrThrow: 'Each then() should return a value or throw',
},
},
create(context) {
const options = context.options[0] || {}
Expand Down Expand Up @@ -242,7 +245,7 @@ module.exports = {
const branch = funcInfo.branchInfoMap[id]
if (!branch.good) {
context.report({
message: 'Each then() should return a value or throw',
messageId: 'thenShouldReturnOrThrow',
node: branch.node,
})
}
Expand Down
5 changes: 4 additions & 1 deletion rules/avoid-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ module.exports = {
url: getDocsUrl('avoid-new'),
},
schema: [],
messages: {
avoidNew: 'Avoid creating new promises.',
},
},
create(context) {
return {
NewExpression(node) {
if (node.callee.name === 'Promise') {
context.report({ node, message: 'Avoid creating new promises.' })
context.report({ node, messageId: 'avoidNew' })
}
},
}
Expand Down
5 changes: 4 additions & 1 deletion rules/no-nesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module.exports = {
url: getDocsUrl('no-nesting'),
},
schema: [],
messages: {
avoidNesting: 'Avoid nesting promises.',
},
},
create(context) {
/**
Expand Down Expand Up @@ -112,7 +115,7 @@ module.exports = {

context.report({
node: node.callee.property,
message: 'Avoid nesting promises.',
messageId: 'avoidNesting',
})
},
}
Expand Down
5 changes: 4 additions & 1 deletion rules/no-new-statics.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = {
},
fixable: 'code',
schema: [],
messages: {
avoidNewStatic: "Avoid calling 'new' on 'Promise.{{ name }}()'",
},
},
create(context) {
return {
Expand All @@ -23,7 +26,7 @@ module.exports = {
) {
context.report({
node,
message: "Avoid calling 'new' on 'Promise.{{ name }}()'",
messageId: 'avoidNewStatic',
data: { name: node.callee.property.name },
fix(fixer) {
return fixer.replaceTextRange(
Expand Down
5 changes: 4 additions & 1 deletion rules/no-promise-in-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ module.exports = {
url: getDocsUrl('no-promise-in-callback'),
},
schema: [],
messages: {
avoidPromiseInCallback: 'Avoid using promises inside of callbacks.',
},
},
create(context) {
return {
Expand All @@ -34,7 +37,7 @@ module.exports = {
if (getAncestors(context, node).some(isInsideCallback)) {
context.report({
node: node.callee,
message: 'Avoid using promises inside of callbacks.',
messageId: 'avoidPromiseInCallback',
})
}
},
Expand Down
5 changes: 4 additions & 1 deletion rules/no-return-in-finally.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = {
url: getDocsUrl('no-return-in-finally'),
},
schema: [],
messages: {
avoidReturnInFinally: 'No return in finally',
},
},
create(context) {
return {
Expand All @@ -35,7 +38,7 @@ module.exports = {
) {
context.report({
node: node.callee.property,
message: 'No return in finally',
messageId: 'avoidReturnInFinally',
})
}
}
Expand Down
12 changes: 8 additions & 4 deletions rules/param-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ module.exports = {
additionalProperties: false,
},
],
messages: {
resolveParamNames:
'Promise constructor parameters must be named to match "{{ resolvePattern }}"',
rejectParamNames:
'Promise constructor parameters must be named to match "{{ rejectPattern }}"',
},
},
create(context) {
const options = context.options[0] || {}
Expand All @@ -45,8 +51,7 @@ module.exports = {
if (resolveParamName && !resolvePattern.test(resolveParamName)) {
context.report({
node: params[0],
message:
'Promise constructor parameters must be named to match "{{ resolvePattern }}"',
messageId: 'resolveParamNames',
data: {
resolvePattern: resolvePattern.source,
},
Expand All @@ -56,8 +61,7 @@ module.exports = {
if (rejectParamName && !rejectPattern.test(rejectParamName)) {
context.report({
node: params[1],
message:
'Promise constructor parameters must be named to match "{{ rejectPattern }}"',
messageId: 'rejectParamNames',
data: {
rejectPattern: rejectPattern.source,
},
Expand Down
5 changes: 4 additions & 1 deletion rules/prefer-await-to-then.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ module.exports = {
url: getDocsUrl('prefer-await-to-then'),
},
schema: [],
messages: {
preferAwaitToCallback: 'Prefer await to then()/catch()/finally().',
},
},
create(context) {
/** Returns true if node is inside yield or await expression. */
Expand Down Expand Up @@ -52,7 +55,7 @@ module.exports = {
) {
context.report({
node: node.property,
message: 'Prefer await to then()/catch()/finally().',
messageId: 'preferAwaitToCallback',
})
}
},
Expand Down
17 changes: 11 additions & 6 deletions rules/valid-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ module.exports = {
url: getDocsUrl('valid-params'),
},
schema: [],
messages: {
requireOneOptionalArgument:
'Promise.{{ name }}() requires 0 or 1 arguments, but received {{ numArgs }}',
requireOneArgument:
'Promise.{{ name }}() requires 1 argument, but received {{ numArgs }}',
requireTwoOptionalArguments:
'Promise.{{ name }}() requires 1 or 2 arguments, but received {{ numArgs }}',
},
},
create(context) {
return {
Expand All @@ -30,8 +38,7 @@ module.exports = {
if (numArgs > 1) {
context.report({
node,
message:
'Promise.{{ name }}() requires 0 or 1 arguments, but received {{ numArgs }}',
messageId: 'requireOneOptionalArgument',
data: { name, numArgs },
})
}
Expand All @@ -40,8 +47,7 @@ module.exports = {
if (numArgs < 1 || numArgs > 2) {
context.report({
node,
message:
'Promise.{{ name }}() requires 1 or 2 arguments, but received {{ numArgs }}',
messageId: 'requireTwoOptionalArguments',
data: { name, numArgs },
})
}
Expand All @@ -55,8 +61,7 @@ module.exports = {
if (numArgs !== 1) {
context.report({
node,
message:
'Promise.{{ name }}() requires 1 argument, but received {{ numArgs }}',
messageId: 'requireOneArgument',
data: { name, numArgs },
})
}
Expand Down

0 comments on commit b0fc13b

Please sign in to comment.