| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @emails react-core |
| 8 | */ |
| 9 | |
| 10 | 'use strict'; |
| 11 | |
| 12 | const rule = require('../no-production-logging'); |
| 13 | const {RuleTester} = require('eslint'); |
| 14 | const ruleTester = new RuleTester(); |
| 15 | |
| 16 | ruleTester.run('no-production-logging', rule, { |
| 17 | valid: [ |
| 18 | { |
| 19 | code: ` |
| 20 | if (__DEV__) { |
| 21 | console.error('Oh no'); |
| 22 | } |
| 23 | `, |
| 24 | }, |
| 25 | { |
| 26 | code: ` |
| 27 | if (__DEV__) { |
| 28 | console.error('Hello %s', foo) |
| 29 | } |
| 30 | `, |
| 31 | }, |
| 32 | { |
| 33 | code: ` |
| 34 | if (__DEV__) { |
| 35 | console.error('Hello %s %s', foo, bar) |
| 36 | } |
| 37 | `, |
| 38 | }, |
| 39 | { |
| 40 | code: ` |
| 41 | if (__DEV__) { |
| 42 | console.warn('Oh no'); |
| 43 | } |
| 44 | `, |
| 45 | }, |
| 46 | { |
| 47 | code: ` |
| 48 | if (__DEV__) { |
| 49 | console.warn('Oh no'); |
| 50 | } |
| 51 | `, |
| 52 | }, |
| 53 | // This is OK too because it's wrapped outside: |
| 54 | { |
| 55 | code: ` |
| 56 | if (__DEV__) { |
| 57 | if (potato) { |
| 58 | while (true) { |
| 59 | console.error('Oh no'); |
| 60 | } |
| 61 | } |
| 62 | }`, |
| 63 | }, |
| 64 | { |
| 65 | code: ` |
| 66 | var f; |
| 67 | if (__DEV__) { |
| 68 | f = function() { |
| 69 | if (potato) { |
| 70 | while (true) { |
| 71 | console.error('Oh no'); |
| 72 | } |
| 73 | } |
| 74 | }; |
| 75 | }`, |
| 76 | }, |
| 77 | // Don't do anything with these: |
| 78 | { |
| 79 | code: 'normalFunctionCall(test);', |
| 80 | }, |
| 81 | { |
| 82 | code: 'invariant(test);', |
| 83 | }, |
| 84 | { |
| 85 | code: ` |
| 86 | if (__DEV__) { |
| 87 | normalFunctionCall(test); |
| 88 | } |
| 89 | `, |
| 90 | }, |
| 91 | // This is OK because of the outer if. |
| 92 | { |
| 93 | code: ` |
| 94 | if (__DEV__) { |
| 95 | if (foo) { |
| 96 | if (__DEV__) { |
| 97 | } else { |
| 98 | console.error('Oh no'); |
| 99 | } |
| 100 | } |
| 101 | }`, |
| 102 | }, |
| 103 | { |
| 104 | // This is an escape hatch that makes it fire in production. |
| 105 | code: ` |
| 106 | console['error']('Oh no'); |
| 107 | `, |
| 108 | }, |
| 109 | ], |
| 110 | invalid: [ |
| 111 | { |
| 112 | code: "console.error('Oh no');", |
| 113 | output: "if (__DEV__) {console.error('Oh no')};", |
| 114 | errors: [ |
| 115 | { |
| 116 | message: `Wrap console.error() in an "if (__DEV__) {}" check`, |
| 117 | }, |
| 118 | ], |
| 119 | }, |
| 120 | { |
| 121 | code: "console.warn('Oh no');", |
| 122 | output: "if (__DEV__) {console.warn('Oh no')};", |
| 123 | errors: [ |
| 124 | { |
| 125 | message: `Wrap console.warn() in an "if (__DEV__) {}" check`, |
| 126 | }, |
| 127 | ], |
| 128 | }, |
| 129 | { |
| 130 | code: "console.warn('Oh no')", |
| 131 | output: "if (__DEV__) {console.warn('Oh no')}", |
| 132 | errors: [ |
| 133 | { |
| 134 | message: `Wrap console.warn() in an "if (__DEV__) {}" check`, |
| 135 | }, |
| 136 | ], |
| 137 | }, |
| 138 | { |
| 139 | code: ` |
| 140 | if (potato) { |
| 141 | console.warn('Oh no'); |
| 142 | } |
| 143 | `, |
| 144 | output: ` |
| 145 | if (potato) { |
| 146 | if (__DEV__) {console.warn('Oh no')}; |
| 147 | } |
| 148 | `, |
| 149 | errors: [ |
| 150 | { |
| 151 | message: `Wrap console.warn() in an "if (__DEV__) {}" check`, |
| 152 | }, |
| 153 | ], |
| 154 | }, |
| 155 | { |
| 156 | code: ` |
| 157 | if (__DEV__ || potato && true) { |
| 158 | console.error('Oh no'); |
| 159 | } |
| 160 | `, |
| 161 | output: ` |
| 162 | if (__DEV__ || potato && true) { |
| 163 | if (__DEV__) {console.error('Oh no')}; |
| 164 | } |
| 165 | `, |
| 166 | errors: [ |
| 167 | { |
| 168 | message: `Wrap console.error() in an "if (__DEV__) {}" check`, |
| 169 | }, |
| 170 | ], |
| 171 | }, |
| 172 | { |
| 173 | code: ` |
| 174 | if (banana && __DEV__ && potato && kitten) { |
| 175 | console.error('Oh no'); |
| 176 | } |
| 177 | `, |
| 178 | output: ` |
| 179 | if (banana && __DEV__ && potato && kitten) { |
| 180 | if (__DEV__) {console.error('Oh no')}; |
| 181 | } |
| 182 | `, |
| 183 | // Technically this code is valid but we prefer |
| 184 | // explicit standalone __DEV__ blocks that stand out. |
| 185 | errors: [ |
| 186 | { |
| 187 | message: `Wrap console.error() in an "if (__DEV__) {}" check`, |
| 188 | }, |
| 189 | ], |
| 190 | }, |
| 191 | { |
| 192 | code: ` |
| 193 | if (!__DEV__) { |
| 194 | console.error('Oh no'); |
| 195 | } |
| 196 | `, |
| 197 | output: ` |
| 198 | if (!__DEV__) { |
| 199 | if (__DEV__) {console.error('Oh no')}; |
| 200 | } |
| 201 | `, |
| 202 | errors: [ |
| 203 | { |
| 204 | message: `Wrap console.error() in an "if (__DEV__) {}" check`, |
| 205 | }, |
| 206 | ], |
| 207 | }, |
| 208 | { |
| 209 | code: ` |
| 210 | if (foo || x && __DEV__) { |
| 211 | console.error('Oh no'); |
| 212 | } |
| 213 | `, |
| 214 | output: ` |
| 215 | if (foo || x && __DEV__) { |
| 216 | if (__DEV__) {console.error('Oh no')}; |
| 217 | } |
| 218 | `, |
| 219 | errors: [ |
| 220 | { |
| 221 | message: `Wrap console.error() in an "if (__DEV__) {}" check`, |
| 222 | }, |
| 223 | ], |
| 224 | }, |
| 225 | { |
| 226 | code: ` |
| 227 | if (__DEV__) { |
| 228 | } else { |
| 229 | console.error('Oh no'); |
| 230 | } |
| 231 | `, |
| 232 | output: ` |
| 233 | if (__DEV__) { |
| 234 | } else { |
| 235 | if (__DEV__) {console.error('Oh no')}; |
| 236 | } |
| 237 | `, |
| 238 | errors: [ |
| 239 | { |
| 240 | message: `Wrap console.error() in an "if (__DEV__) {}" check`, |
| 241 | }, |
| 242 | ], |
| 243 | }, |
| 244 | { |
| 245 | code: ` |
| 246 | if (__DEV__) { |
| 247 | } else { |
| 248 | if (__DEV__) { |
| 249 | } else { |
| 250 | console.error('Oh no'); |
| 251 | } |
| 252 | } |
| 253 | `, |
| 254 | output: ` |
| 255 | if (__DEV__) { |
| 256 | } else { |
| 257 | if (__DEV__) { |
| 258 | } else { |
| 259 | if (__DEV__) {console.error('Oh no')}; |
| 260 | } |
| 261 | } |
| 262 | `, |
| 263 | errors: [ |
| 264 | { |
| 265 | message: `Wrap console.error() in an "if (__DEV__) {}" check`, |
| 266 | }, |
| 267 | ], |
| 268 | }, |
| 269 | { |
| 270 | code: ` |
| 271 | if (__DEV__) { |
| 272 | console.log('Oh no'); |
| 273 | } |
| 274 | `, |
| 275 | errors: [ |
| 276 | { |
| 277 | message: 'Unexpected use of console', |
| 278 | }, |
| 279 | ], |
| 280 | }, |
| 281 | { |
| 282 | code: ` |
| 283 | if (__DEV__) { |
| 284 | console.log.apply(console, 'Oh no'); |
| 285 | } |
| 286 | `, |
| 287 | errors: [ |
| 288 | { |
| 289 | message: 'Unexpected use of console', |
| 290 | }, |
| 291 | ], |
| 292 | }, |
| 293 | ], |
| 294 | }); |