Skip to content

Commit e84e6e2

Browse files
feat: Report allowed methods for no-console rule (#19306)
* 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]>
1 parent 2e84213 commit e84e6e2

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

lib/rules/no-console.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ module.exports = {
4949

5050
messages: {
5151
unexpected: "Unexpected console statement.",
52+
limited: "Unexpected console statement. Only these console methods are allowed: {{ allowed }}.",
5253
removeConsole: "Remove the console.{{ propertyName }}()."
5354
}
5455
},
@@ -169,7 +170,8 @@ module.exports = {
169170
context.report({
170171
node,
171172
loc: node.loc,
172-
messageId: "unexpected",
173+
messageId: allowed.length ? "limited" : "unexpected",
174+
data: { allowed: allowed.join(", ") },
173175
suggest: canProvideSuggestions(node)
174176
? [{
175177
messageId: "removeConsole",

tests/lib/rules/no-console.js

+36-18
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ ruleTester.run("no-console", rule, {
190190
code: "if (a) console.info(foo)",
191191
options: [{ allow: ["warn"] }],
192192
errors: [{
193-
messageId: "unexpected",
193+
messageId: "limited",
194+
data: { allowed: "warn" },
194195
type: "MemberExpression",
195196
suggestions: null
196197
}]
@@ -199,7 +200,8 @@ ruleTester.run("no-console", rule, {
199200
code: "foo(console.warn)",
200201
options: [{ allow: ["log"] }],
201202
errors: [{
202-
messageId: "unexpected",
203+
messageId: "limited",
204+
data: { allowed: "log" },
203205
type: "MemberExpression",
204206
suggestions: null
205207
}]
@@ -208,7 +210,8 @@ ruleTester.run("no-console", rule, {
208210
code: "console.log(foo)",
209211
options: [{ allow: ["error"] }],
210212
errors: [{
211-
messageId: "unexpected",
213+
messageId: "limited",
214+
data: { allowed: "error" },
212215
type: "MemberExpression",
213216
suggestions: [{
214217
messageId: "removeConsole",
@@ -221,7 +224,8 @@ ruleTester.run("no-console", rule, {
221224
code: "console.error(foo)",
222225
options: [{ allow: ["warn"] }],
223226
errors: [{
224-
messageId: "unexpected",
227+
messageId: "limited",
228+
data: { allowed: "warn" },
225229
type: "MemberExpression",
226230
suggestions: [{
227231
messageId: "removeConsole",
@@ -234,7 +238,8 @@ ruleTester.run("no-console", rule, {
234238
code: "console.info(foo)",
235239
options: [{ allow: ["log"] }],
236240
errors: [{
237-
messageId: "unexpected",
241+
messageId: "limited",
242+
data: { allowed: "log" },
238243
type: "MemberExpression",
239244
suggestions: [{
240245
messageId: "removeConsole",
@@ -247,7 +252,8 @@ ruleTester.run("no-console", rule, {
247252
code: "console.warn(foo)",
248253
options: [{ allow: ["error"] }],
249254
errors: [{
250-
messageId: "unexpected",
255+
messageId: "limited",
256+
data: { allowed: "error" },
251257
type: "MemberExpression",
252258
suggestions: [{
253259
messageId: "removeConsole",
@@ -260,7 +266,8 @@ ruleTester.run("no-console", rule, {
260266
code: "switch (a) { case 1: console.log(foo) }",
261267
options: [{ allow: ["error"] }],
262268
errors: [{
263-
messageId: "unexpected",
269+
messageId: "limited",
270+
data: { allowed: "error" },
264271
type: "MemberExpression",
265272
suggestions: [{
266273
messageId: "removeConsole",
@@ -273,7 +280,8 @@ ruleTester.run("no-console", rule, {
273280
code: "if (a) { console.info(foo) }",
274281
options: [{ allow: ["warn"] }],
275282
errors: [{
276-
messageId: "unexpected",
283+
messageId: "limited",
284+
data: { allowed: "warn" },
277285
type: "MemberExpression",
278286
suggestions: [{
279287
messageId: "removeConsole",
@@ -287,7 +295,8 @@ ruleTester.run("no-console", rule, {
287295
options: [{ allow: ["log"] }],
288296
languageOptions: { ecmaVersion: "latest" },
289297
errors: [{
290-
messageId: "unexpected",
298+
messageId: "limited",
299+
data: { allowed: "log" },
291300
type: "MemberExpression",
292301
suggestions: [{
293302
messageId: "removeConsole",
@@ -302,7 +311,8 @@ ruleTester.run("no-console", rule, {
302311
code: "if (a) console.log(foo)",
303312
options: [{ allow: ["warn", "error"] }],
304313
errors: [{
305-
messageId: "unexpected",
314+
messageId: "limited",
315+
data: { allowed: "warn, error" },
306316
type: "MemberExpression",
307317
suggestions: null
308318
}]
@@ -311,7 +321,8 @@ ruleTester.run("no-console", rule, {
311321
code: "foo(console.info)",
312322
options: [{ allow: ["warn", "error"] }],
313323
errors: [{
314-
messageId: "unexpected",
324+
messageId: "limited",
325+
data: { allowed: "warn, error" },
315326
type: "MemberExpression",
316327
suggestions: null
317328
}]
@@ -320,7 +331,8 @@ ruleTester.run("no-console", rule, {
320331
code: "console.log(foo)",
321332
options: [{ allow: ["warn", "info"] }],
322333
errors: [{
323-
messageId: "unexpected",
334+
messageId: "limited",
335+
data: { allowed: "warn, info" },
324336
type: "MemberExpression",
325337
suggestions: [{
326338
messageId: "removeConsole",
@@ -333,7 +345,8 @@ ruleTester.run("no-console", rule, {
333345
code: "console.error(foo)",
334346
options: [{ allow: ["warn", "info", "log"] }],
335347
errors: [{
336-
messageId: "unexpected",
348+
messageId: "limited",
349+
data: { allowed: "warn, info, log" },
337350
type: "MemberExpression",
338351
suggestions: [{
339352
messageId: "removeConsole",
@@ -346,7 +359,8 @@ ruleTester.run("no-console", rule, {
346359
code: "console.info(foo)",
347360
options: [{ allow: ["warn", "error", "log"] }],
348361
errors: [{
349-
messageId: "unexpected",
362+
messageId: "limited",
363+
data: { allowed: "warn, error, log" },
350364
type: "MemberExpression",
351365
suggestions: [{
352366
messageId: "removeConsole",
@@ -359,7 +373,8 @@ ruleTester.run("no-console", rule, {
359373
code: "console.warn(foo)",
360374
options: [{ allow: ["info", "log"] }],
361375
errors: [{
362-
messageId: "unexpected",
376+
messageId: "limited",
377+
data: { allowed: "info, log" },
363378
type: "MemberExpression",
364379
suggestions: [{
365380
messageId: "removeConsole",
@@ -372,7 +387,8 @@ ruleTester.run("no-console", rule, {
372387
code: "switch (a) { case 1: console.error(foo) }",
373388
options: [{ allow: ["info", "log"] }],
374389
errors: [{
375-
messageId: "unexpected",
390+
messageId: "limited",
391+
data: { allowed: "info, log" },
376392
type: "MemberExpression",
377393
suggestions: [{
378394
messageId: "removeConsole",
@@ -385,7 +401,8 @@ ruleTester.run("no-console", rule, {
385401
code: "if (a) { console.log(foo) }",
386402
options: [{ allow: ["warn", "error"] }],
387403
errors: [{
388-
messageId: "unexpected",
404+
messageId: "limited",
405+
data: { allowed: "warn, error" },
389406
type: "MemberExpression",
390407
suggestions: [{
391408
messageId: "removeConsole",
@@ -399,7 +416,8 @@ ruleTester.run("no-console", rule, {
399416
options: [{ allow: ["log", "error", "warn"] }],
400417
languageOptions: { ecmaVersion: "latest" },
401418
errors: [{
402-
messageId: "unexpected",
419+
messageId: "limited",
420+
data: { allowed: "log, error, warn" },
403421
type: "MemberExpression",
404422
suggestions: [{
405423
messageId: "removeConsole",

0 commit comments

Comments
 (0)