Skip to content

Commit

Permalink
feat: Report allowed methods for no-console rule (eslint#19306)
Browse files Browse the repository at this point in the history
* Renaming canProvideSuggestions() to canRemove().

* Implementing suggestions on console method replacement.

* Report allowed methods of console.

* Minor: formatting assetions for readability.

* Revert: no method replacement suggestions.

* Add missing data to assertion.

* Accepted message text adjustment

Co-authored-by: Milos Djermanovic <[email protected]>

---------

Co-authored-by: Milos Djermanovic <[email protected]>
  • Loading branch information
RobinTail and mdjermanovic authored Jan 3, 2025
1 parent 2e84213 commit e84e6e2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
4 changes: 3 additions & 1 deletion lib/rules/no-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ module.exports = {

messages: {
unexpected: "Unexpected console statement.",
limited: "Unexpected console statement. Only these console methods are allowed: {{ allowed }}.",
removeConsole: "Remove the console.{{ propertyName }}()."
}
},
Expand Down Expand Up @@ -169,7 +170,8 @@ module.exports = {
context.report({
node,
loc: node.loc,
messageId: "unexpected",
messageId: allowed.length ? "limited" : "unexpected",
data: { allowed: allowed.join(", ") },
suggest: canProvideSuggestions(node)
? [{
messageId: "removeConsole",
Expand Down
54 changes: 36 additions & 18 deletions tests/lib/rules/no-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ ruleTester.run("no-console", rule, {
code: "if (a) console.info(foo)",
options: [{ allow: ["warn"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "warn" },
type: "MemberExpression",
suggestions: null
}]
Expand All @@ -199,7 +200,8 @@ ruleTester.run("no-console", rule, {
code: "foo(console.warn)",
options: [{ allow: ["log"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "log" },
type: "MemberExpression",
suggestions: null
}]
Expand All @@ -208,7 +210,8 @@ ruleTester.run("no-console", rule, {
code: "console.log(foo)",
options: [{ allow: ["error"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "error" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -221,7 +224,8 @@ ruleTester.run("no-console", rule, {
code: "console.error(foo)",
options: [{ allow: ["warn"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "warn" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -234,7 +238,8 @@ ruleTester.run("no-console", rule, {
code: "console.info(foo)",
options: [{ allow: ["log"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "log" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -247,7 +252,8 @@ ruleTester.run("no-console", rule, {
code: "console.warn(foo)",
options: [{ allow: ["error"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "error" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -260,7 +266,8 @@ ruleTester.run("no-console", rule, {
code: "switch (a) { case 1: console.log(foo) }",
options: [{ allow: ["error"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "error" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -273,7 +280,8 @@ ruleTester.run("no-console", rule, {
code: "if (a) { console.info(foo) }",
options: [{ allow: ["warn"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "warn" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -287,7 +295,8 @@ ruleTester.run("no-console", rule, {
options: [{ allow: ["log"] }],
languageOptions: { ecmaVersion: "latest" },
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "log" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -302,7 +311,8 @@ ruleTester.run("no-console", rule, {
code: "if (a) console.log(foo)",
options: [{ allow: ["warn", "error"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "warn, error" },
type: "MemberExpression",
suggestions: null
}]
Expand All @@ -311,7 +321,8 @@ ruleTester.run("no-console", rule, {
code: "foo(console.info)",
options: [{ allow: ["warn", "error"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "warn, error" },
type: "MemberExpression",
suggestions: null
}]
Expand All @@ -320,7 +331,8 @@ ruleTester.run("no-console", rule, {
code: "console.log(foo)",
options: [{ allow: ["warn", "info"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "warn, info" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -333,7 +345,8 @@ ruleTester.run("no-console", rule, {
code: "console.error(foo)",
options: [{ allow: ["warn", "info", "log"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "warn, info, log" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -346,7 +359,8 @@ ruleTester.run("no-console", rule, {
code: "console.info(foo)",
options: [{ allow: ["warn", "error", "log"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "warn, error, log" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -359,7 +373,8 @@ ruleTester.run("no-console", rule, {
code: "console.warn(foo)",
options: [{ allow: ["info", "log"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "info, log" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -372,7 +387,8 @@ ruleTester.run("no-console", rule, {
code: "switch (a) { case 1: console.error(foo) }",
options: [{ allow: ["info", "log"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "info, log" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -385,7 +401,8 @@ ruleTester.run("no-console", rule, {
code: "if (a) { console.log(foo) }",
options: [{ allow: ["warn", "error"] }],
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "warn, error" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand All @@ -399,7 +416,8 @@ ruleTester.run("no-console", rule, {
options: [{ allow: ["log", "error", "warn"] }],
languageOptions: { ecmaVersion: "latest" },
errors: [{
messageId: "unexpected",
messageId: "limited",
data: { allowed: "log, error, warn" },
type: "MemberExpression",
suggestions: [{
messageId: "removeConsole",
Expand Down

0 comments on commit e84e6e2

Please sign in to comment.