| 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 | |
| 8 | import chalk from 'chalk'; |
| 9 | |
| 10 | const JsFileExtensionRE = /(js|ts|jsx|tsx)$/; |
| 11 | const NextConfigFileRE = /^next\.config\.(js|mjs)$/; |
| 12 | const StrictModeRE = /<(React\.StrictMode|StrictMode)>/; |
| 13 | const NextStrictModeRE = /reactStrictMode:\s*true/; |
| 14 | let StrictModeUsage = false; |
| 15 | |
| 16 | export default { |
| 17 | run(source: string, path: string): void { |
| 18 | if (StrictModeUsage) { |
| 19 | return; |
| 20 | } |
| 21 | |
| 22 | if (NextConfigFileRE.exec(path) !== null) { |
| 23 | StrictModeUsage = NextStrictModeRE.exec(source) !== null; |
| 24 | } else if (JsFileExtensionRE.exec(path) !== null) { |
| 25 | StrictModeUsage = StrictModeRE.exec(source) !== null; |
| 26 | } |
| 27 | }, |
| 28 | |
| 29 | report(): void { |
| 30 | if (StrictModeUsage) { |
| 31 | console.log(chalk.green('StrictMode usage found.')); |
| 32 | } else { |
| 33 | console.log(chalk.red('StrictMode usage not found.')); |
| 34 | } |
| 35 | }, |
| 36 | }; |