mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
New ESLint rule that warns against Boolean and String constructors
This commit is contained in:
@@ -55,6 +55,7 @@ module.exports = {
|
||||
// CUSTOM RULES
|
||||
// the second argument of warning/invariant should be a literal string
|
||||
'react-internal/warning-and-invariant-args': ERROR,
|
||||
'react-internal/boolean-and-string-constructors': ERROR,
|
||||
},
|
||||
|
||||
globals: {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*
|
||||
* @emails react-core
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
module.exports = function(context) {
|
||||
function check(node) {
|
||||
const name = node.callee.name;
|
||||
let msg = null;
|
||||
switch (name) {
|
||||
case 'Boolean':
|
||||
msg = 'To cast a value to a boolean, use double negation: !!value';
|
||||
break;
|
||||
case 'String':
|
||||
msg = 'To cast a value to a string, concat it with the empty string: \'\' + value';
|
||||
break;
|
||||
}
|
||||
if (msg) {
|
||||
context.report(node, `Do not use the ${name} constructor. ${msg}`);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
CallExpression: check,
|
||||
NewExpression: check,
|
||||
};
|
||||
};
|
||||
@@ -3,5 +3,6 @@
|
||||
module.exports = {
|
||||
rules: {
|
||||
'warning-and-invariant-args': require('./warning-and-invariant-args'),
|
||||
'boolean-and-string-constructors': require('./boolean-and-string-constructors'),
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user