@samitouri / QOS-React / commits / 5f92d28e52

New ESLint rule that warns against Boolean and String constructors

Andrew Clark committed Feb 28, 2017 at 13:04 UTC 5f92d28e5299a59e120a993bfd4c74dc4ec3a6c6
3 files changed +37
.eslintrc.js
+1
@@ -55,6 +55,7 @@ module.exports = {
55 // CUSTOM RULES
56 // the second argument of warning/invariant should be a literal string
57 'react-internal/warning-and-invariant-args': ERROR,
58 + 'react-internal/boolean-and-string-constructors': ERROR,
59 },
60
61 globals: {
eslint-rules/boolean-and-string-constructors.js new
+35
@@ -0,0 +1,35 @@
1 +/**
2 + * Copyright 2015-present, Facebook, Inc.
3 + * All rights reserved.
4 + *
5 + * This source code is licensed under the BSD-style license found in the
6 + * LICENSE file in the root directory of this source tree. An additional grant
7 + * of patent rights can be found in the PATENTS file in the same directory.
8 + *
9 + * @emails react-core
10 +*/
11 +
12 +'use strict';
13 +
14 +module.exports = function(context) {
15 + function check(node) {
16 + const name = node.callee.name;
17 + let msg = null;
18 + switch (name) {
19 + case 'Boolean':
20 + msg = 'To cast a value to a boolean, use double negation: !!value';
21 + break;
22 + case 'String':
23 + msg = 'To cast a value to a string, concat it with the empty string: \'\' + value';
24 + break;
25 + }
26 + if (msg) {
27 + context.report(node, `Do not use the ${name} constructor. ${msg}`);
28 + }
29 + }
30 +
31 + return {
32 + CallExpression: check,
33 + NewExpression: check,
34 + };
35 +};
eslint-rules/index.js
+1
@@ -3,5 +3,6 @@
3 module.exports = {
4 rules: {
5 'warning-and-invariant-args': require('./warning-and-invariant-args'),
6 + 'boolean-and-string-constructors': require('./boolean-and-string-constructors'),
7 },
8 };