-
-
Notifications
You must be signed in to change notification settings - Fork 385
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
Add prefer-string-slice
rule - fixes #65
#67
Conversation
if (name === 'substr' || name === 'substring') { | ||
context.report({ | ||
node, | ||
message: 'Use String.slice instead of String.substr or String.substring.' |
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.
Use \`String.slice\` instead of \`String.${name}\`.
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.
Ah, of course, much better, sorted :)
prefer-string-slice
rule - fixes #65
@@ -0,0 +1,16 @@ | |||
# Prefer the use of `String.slice` instead of `String.substr` or `String.substring` |
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.
Prefer the use of
String#slice
instead ofString#substr
orString#substring
@@ -0,0 +1,16 @@ | |||
# Prefer the use of `String.slice` instead of `String.substr` or `String.substring` | |||
|
|||
Prefer a convention of using `String.slice` instead of `String.substr` or `String.substring`. `String.slice()` has clearer behavior and has a counterpart with arrays. It is also better to be consistent. Anything that can be done with `String.substr()` or `String.substring()` can be done with `String.slice()`. |
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.
Linkify these methods to their MDN page.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/slice
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.
.
=> #
(like the header)
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.
Also link to the article I shared in the original issue about the difference.
const create = context => { | ||
return { | ||
Identifier: node => { | ||
if (['substr', 'substring'].indexOf(node.name) > -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.
> -1
=> !== -1
Identifier: node => { | ||
if (['substr', 'substring'].indexOf(node.name) > -1) { | ||
const args = node.parent.parent.arguments; | ||
console.dir(args[0].type); |
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 left a console.dir in here.
console.dir(args[0].type); | ||
context.report({ | ||
node, | ||
message: `Use \`String.slice\` instead of \`String.${node.name}\`.` |
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.
message: `Use \`String#slice\` instead of \`String#${node.name}\`.`
Nice start! :) Would be interesting if it could show in the error message what needs changing to switch to Should also be fixable for literal arguments. |
@sindresorhus thanks for the feedback! Thoughts on fixing so far… (WIP)
|
Sounds good |
@@ -0,0 +1,18 @@ | |||
# Prefer the use of `String#slice` instead of `String#substr` or `String#substring` |
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.
We should use ()
here. E.g. String#slice()
|
||
Prefer a convention of using [`String#slice`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/slice) instead of [`String#substr`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substr) or [`String#substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring). `String#slice()` has clearer behavior and has a counterpart with arrays. It is also better to be consistent. Anything that can be done with `String#substr()` or `String#substring()` can be done with `String#slice()`. | ||
|
||
Read more in [this Stack Overflow question](http://stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring) |
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.
Should end in a .
@@ -61,6 +62,7 @@ Configure it in `package.json`. | |||
- [no-array-instanceof](docs/rules/no-array-instanceof.md) - Require `Array.isArray()` instead of `instanceof Array`. *(fixable)* | |||
- [no-new-buffer](docs/rules/no-new-buffer.md) - Enforce the use of `Buffer.from()` and `Buffer.alloc()` instead of the deprecated `new Buffer()`. *(fixable)* | |||
- [no-hex-escape](docs/rules/no-hex-escape.md) - Enforce the use of unicode escapes instead of hexadecimal escapes. *(fixable)* | |||
- [prefer-string-slice](docs/rules/prefer-string-slice) - Prefer the use of `String.slice` instead of `String.substr` or `String.substring` |
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.
Description should be the same as the docs title.
valid: [ | ||
'const foo = bar.slice(1)', | ||
`const foo = bar.slice(baz, 2);`, | ||
`const foo = bar.slice(baz - 1, 2);` |
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.
No point in using template literal here.
Same elsewhere in this file.
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.
Laziness, will fix.
if (['substr', 'substring'].indexOf(node.name) !== -1) { | ||
context.report({ | ||
node, | ||
message: `Use \`String#slice\` instead of \`String#${node.name}\`.` |
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.
Add ()
here too.
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.
Rewrote this bit quite a bit, fixed in the rewrite.
Still working on this, just a bit short on time at the moment. |
@matijs No problem, just continue when you find the time :). |
Hey @matijs. Still interested in finishing this? :) |
Yes, very much! Life is calming down a little bit, I hope to spend some time on this towards the end of the week. |
@matijs Still interested? Otherwise I'll close this. |
I am, but I just don't have the time at the moment :/ |
Ok. No worries. I'll close this, but happy to reopen if you get a chance to finish this at some point :) |
@sindresorhus, @matijs - If no one objects, i'll take the PR over. |
@florianb Go ahead :) |
Prefer
String.slice
instead ofString.substr
orString.substring
.