| 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 | module.exports = { |
| 13 | meta: { |
| 14 | schema: [], |
| 15 | }, |
| 16 | create(context) { |
| 17 | function report(node, name, msg) { |
| 18 | context.report(node, `Do not use the ${name} constructor. ${msg}`); |
| 19 | } |
| 20 | |
| 21 | function check(node) { |
| 22 | const name = node.callee.name; |
| 23 | switch (name) { |
| 24 | case 'Boolean': |
| 25 | report( |
| 26 | node, |
| 27 | name, |
| 28 | 'To cast a value to a boolean, use double negation: !!value' |
| 29 | ); |
| 30 | break; |
| 31 | case 'String': |
| 32 | if (node.type === 'NewExpression') { |
| 33 | context.report( |
| 34 | node, |
| 35 | "Do not use `new String()`. Use String() without new (or '' + value for perf-sensitive code)." |
| 36 | ); |
| 37 | } |
| 38 | break; |
| 39 | case 'Number': |
| 40 | report( |
| 41 | node, |
| 42 | name, |
| 43 | 'To cast a value to a number, use the plus operator: +value' |
| 44 | ); |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return { |
| 50 | CallExpression: check, |
| 51 | NewExpression: check, |
| 52 | }; |
| 53 | }, |
| 54 | }; |