-
-
Notifications
You must be signed in to change notification settings - Fork 420
prefer-code-point: Report cases that String.fromCharCode not called
#2766
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
Conversation
rules/prefer-code-point.js
Outdated
| optional: false, | ||
| })) { | ||
| return ['fromCodePoint', node.callee.object.property]; | ||
| } |
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.
This is not the correct solution, we should change the listerner to MemberExpresion.
Meaning, no matter how String.fromCharCode is used, it should be reported.
const x = String.fromCharCode
String.fromCharCode + 1
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.
And the String.fromCharCode(code) condition above should be removed.
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.
const create = () => ({
CallExpression(node) {
// Report `charCodeAt` call
},
MemberExpression() {
// Report `String.fromCharCode`
}
});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.
You can even try to use GlobalReferenceTracker which handles more cases, but it's hard to understand. I prefer MemberExpression check for now which is good enough.
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.
my bad 🤦 , fixed in e075d07, also added extra test case for const x = String.fromCharCode
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.
The logic is correct now, thank you!
We can improve the code quantity a little bit,
- We can reuse the problem reporting part. something like
function getProblem(node, replacement){...}should be good. getReplacement...()can be inlined since they dont need return "replacement", we already know which case we are dealing.
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.
done
e075d07 to
9a10b46
Compare
9a10b46 to
6b1c893
Compare
prefer-code-point: fix String.fromCharCode.bind(String) caseprefer-code-point: Report cases that String.fromCharCode not called
Fixed #2613 bug with
String.fromCharCode.bind(String), i hope I understood this comment correctly