-
Notifications
You must be signed in to change notification settings - Fork 47.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enforce no boolean or string constructors #9082
Changes from all commits
5f92d28
1e9812e
6ac5fd3
b9c96cf
2f63b0d
7d5cc2e
b963396
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* 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'; | ||
|
||
var rule = require('../no-primitive-constructors'); | ||
var RuleTester = require('eslint').RuleTester; | ||
var ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('eslint-rules/no-primitive-constructors', rule, { | ||
valid: [ | ||
'!!obj', | ||
"'' + obj", | ||
'+string', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'Boolean(obj)', | ||
errors: [ | ||
{ | ||
message: 'Do not use the Boolean constructor. To cast a value to a boolean, use double negation: !!value', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'String(obj)', | ||
errors: [ | ||
{ | ||
message: 'Do not use the String constructor. To cast a value to a string, concat it with the empty string (unless it\'s a symbol, which has different semantics): \'\' + value', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: 'Number(string)', | ||
errors: [ | ||
{ | ||
message: 'Do not use the Number constructor. To cast a value to a number, use the plus operator: +value', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* 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 report(node, name, msg) { | ||
context.report(node, `Do not use the ${name} constructor. ${msg}`); | ||
} | ||
|
||
function check(node) { | ||
const name = node.callee.name; | ||
switch (name) { | ||
case 'Boolean': | ||
report( | ||
node, | ||
name, | ||
'To cast a value to a boolean, use double negation: !!value' | ||
); | ||
break; | ||
case 'String': | ||
report( | ||
node, | ||
name, | ||
'To cast a value to a string, concat it with the empty string ' + | ||
'(unless it\'s a symbol, which has different semantics): ' + | ||
'\'\' + value' | ||
); | ||
break; | ||
case 'Number': | ||
report( | ||
node, | ||
name, | ||
'To cast a value to a number, use the plus operator: +value' | ||
); | ||
break; | ||
} | ||
} | ||
|
||
return { | ||
CallExpression: check, | ||
NewExpression: check, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,8 @@ if (__DEV__) { | |
'%s(...): Expected the last optional `callback` argument to be a ' + | ||
'function. Instead received: %s.', | ||
callerName, | ||
String(callback) | ||
// $FlowFixMe - Intentional cast to string | ||
'' + callback | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And here. |
||
); | ||
}; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,7 +167,8 @@ module.exports = function<T, P, I, TI, PI, C, CX, PL>( | |
callback === null || typeof callback === 'function', | ||
'render(...): Expected the last optional `callback` argument to be a ' + | ||
'function. Instead received: %s.', | ||
String(callback) | ||
// $FlowFixMe - Intentional cast to string | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to tell Flow it's intentional, or do we plan to leave these here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll ask the Flow folks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we casting this to a string anyway? It would be better to let the warning printer cast it to a string, so that it can choose how to visualize it. E.g. imagine |
||
'' + callback | ||
); | ||
} | ||
addTopLevelUpdate(current, nextState, callback, priorityLevel); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this isn't even used when this is a plain Object.