|
| 1 | +import { RuleTester } from 'eslint' |
| 2 | +import rule from './eslint-log-printf-style.mjs' |
| 3 | + |
| 4 | +const ruleTester = new RuleTester({ |
| 5 | + languageOptions: { |
| 6 | + ecmaVersion: 2022, |
| 7 | + sourceType: 'module', |
| 8 | + }, |
| 9 | +}) |
| 10 | + |
| 11 | +ruleTester.run('eslint-log-printf-style', rule, { |
| 12 | + valid: [ |
| 13 | + // Printf-style formatting |
| 14 | + { code: 'log.debug("message %s", value)' }, |
| 15 | + { code: 'log.info("count: %d", count)' }, |
| 16 | + { code: 'log.warn("Error: %s with code %d", message, code)' }, |
| 17 | + { code: 'log.error("Failed with error %s", err)' }, |
| 18 | + |
| 19 | + // Simple string literals (no interpolation) |
| 20 | + { code: 'log.debug("simple message")' }, |
| 21 | + { code: 'log.info("no variables here")' }, |
| 22 | + |
| 23 | + // Template literals without expressions |
| 24 | + { code: 'log.debug(`simple template`)' }, |
| 25 | + |
| 26 | + // Not a log call |
| 27 | + { code: 'console.log(`template ${value}`)' }, // eslint-disable-line no-template-curly-in-string |
| 28 | + { code: 'foo.bar("test" + value)' }, |
| 29 | + ], |
| 30 | + |
| 31 | + invalid: [ |
| 32 | + // Callback-style logging |
| 33 | + { |
| 34 | + code: 'log.debug(() => "foo")', |
| 35 | + errors: [{ |
| 36 | + messageId: 'useFormat', |
| 37 | + data: { method: 'debug', badPattern: 'callback-style logging' }, |
| 38 | + }], |
| 39 | + }, |
| 40 | + { |
| 41 | + code: 'log.info(() => `message ${value}`)', // eslint-disable-line no-template-curly-in-string |
| 42 | + errors: [{ |
| 43 | + messageId: 'useFormat', |
| 44 | + data: { method: 'info', badPattern: 'callback-style logging' }, |
| 45 | + }], |
| 46 | + }, |
| 47 | + { |
| 48 | + code: 'log.warn(function() { return "message" })', |
| 49 | + errors: [{ |
| 50 | + messageId: 'useFormat', |
| 51 | + data: { method: 'warn', badPattern: 'callback-style logging' }, |
| 52 | + }], |
| 53 | + }, |
| 54 | + |
| 55 | + // Template literals with expressions |
| 56 | + { |
| 57 | + code: 'log.debug(`message ${value}`)', // eslint-disable-line no-template-curly-in-string |
| 58 | + errors: [{ |
| 59 | + messageId: 'useFormat', |
| 60 | + data: { method: 'debug', badPattern: 'template literals' }, |
| 61 | + }], |
| 62 | + }, |
| 63 | + { |
| 64 | + // eslint-disable-next-line no-template-curly-in-string |
| 65 | + code: 'log.warn(`Error: ${err.message} with code ${err.code}`)', |
| 66 | + errors: [{ |
| 67 | + messageId: 'useFormat', |
| 68 | + data: { method: 'warn', badPattern: 'template literals' }, |
| 69 | + }], |
| 70 | + }, |
| 71 | + |
| 72 | + // String concatenation |
| 73 | + { |
| 74 | + code: 'log.error("Error: " + message)', |
| 75 | + errors: [{ |
| 76 | + messageId: 'useFormat', |
| 77 | + data: { method: 'error', badPattern: 'string concatenation' }, |
| 78 | + }], |
| 79 | + }, |
| 80 | + { |
| 81 | + code: 'log.info("Count is " + count + " items")', |
| 82 | + errors: [{ |
| 83 | + messageId: 'useFormat', |
| 84 | + data: { method: 'info', badPattern: 'string concatenation' }, |
| 85 | + }], |
| 86 | + }, |
| 87 | + { |
| 88 | + code: 'log.warn("Metric queue exceeded limit (max: " + max + "). Dropping " + dropped + " measurements.")', |
| 89 | + errors: [{ |
| 90 | + messageId: 'useFormat', |
| 91 | + data: { method: 'warn', badPattern: 'string concatenation' }, |
| 92 | + }], |
| 93 | + }, |
| 94 | + ], |
| 95 | +}) |
0 commit comments