New ESLint rule that warns against Boolean and String constructors

This commit is contained in:
Andrew Clark
2017-02-28 13:04:33 -08:00
parent a269e7ec3f
commit 5f92d28e52
3 changed files with 37 additions and 0 deletions
+1
View File
@@ -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,
};
};
+1
View File
@@ -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'),
},
};