Skip to content
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

Closed
wants to merge 12 commits into from

Conversation

matijs
Copy link
Contributor

@matijs matijs commented Oct 24, 2016

Prefer String.slice instead of String.substr or String.substring.

  • Documentation
  • Process feedback
  • Add fixer

if (name === 'substr' || name === 'substring') {
context.report({
node,
message: 'Use String.slice instead of String.substr or String.substring.'
Copy link
Contributor

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}\`.

Copy link
Contributor Author

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 :)

@matijs matijs changed the title Prefer string slice Prefer string slice - fixes #65 Oct 24, 2016
@sindresorhus sindresorhus changed the title Prefer string slice - fixes #65 Add prefer-string-slice rule - fixes #65 Oct 26, 2016
@@ -0,0 +1,16 @@
# Prefer the use of `String.slice` instead of `String.substr` or `String.substring`
Copy link
Owner

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 of String#substr or String#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()`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

. => # (like the header)

Copy link
Owner

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) {
Copy link
Owner

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);
Copy link
Owner

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}\`.`
Copy link
Owner

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}\`.`

@sindresorhus
Copy link
Owner

Nice start! :)

Would be interesting if it could show in the error message what needs changing to switch to .slice(). Mainly when you're going from .substr as it uses length instead of end as the second argument.

Should also be fixable for literal arguments.

@matijs
Copy link
Contributor Author

matijs commented Oct 26, 2016

@sindresorhus thanks for the feedback! Thoughts on fixing so far… (WIP)

String#substr with one argument

  1. Argument can be anything
  2. String.substr(a) can be replaced by String.slice(a)

String#substring with one argument

  1. Argument has to be a Literal
  2. String.substring(a) with a >= 0 can be replaced by String.slice(a)
  3. String.substring(a) with a < 0 can be replaced by `String.slice(0), this probably never happens

String#substr with two arguments

  1. Arguments have to be Literal
  2. String.substr(a, b) with b > 0 can be replaced by String.slice(a, a + b)
  3. String.substr(a, b) with b <= 0 is unfixable

String#substring with two arguments

  1. Arguments have to be Literal and > 0
  2. String.substring(a, b) with a >= 0 && b >= 0 && can be replaced
  3. if a <= b, replace by String.slice(a, b)
  4. else replace by String.slice(b, a)

@sindresorhus
Copy link
Owner

sindresorhus commented Oct 26, 2016

Sounds good

This was referenced Oct 26, 2016
@@ -0,0 +1,18 @@
# Prefer the use of `String#slice` instead of `String#substr` or `String#substring`
Copy link
Owner

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)
Copy link
Owner

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`
Copy link
Owner

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);`
Copy link
Owner

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.

Copy link
Contributor Author

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}\`.`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add () here too.

Copy link
Contributor Author

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.

@matijs
Copy link
Contributor Author

matijs commented Nov 2, 2016

Still working on this, just a bit short on time at the moment.

@SamVerschueren
Copy link
Contributor

@matijs No problem, just continue when you find the time :).

sindresorhus referenced this pull request in sindresorhus/camelcase Nov 8, 2016
@sindresorhus
Copy link
Owner

Hey @matijs. Still interested in finishing this? :)

@matijs
Copy link
Contributor Author

matijs commented Jan 23, 2017

Yes, very much! Life is calming down a little bit, I hope to spend some time on this towards the end of the week.

@sindresorhus
Copy link
Owner

@matijs Still interested? Otherwise I'll close this.

@matijs
Copy link
Contributor Author

matijs commented May 12, 2017

I am, but I just don't have the time at the moment :/

@sindresorhus
Copy link
Owner

Ok. No worries. I'll close this, but happy to reopen if you get a chance to finish this at some point :)

@florianb
Copy link
Contributor

florianb commented Dec 4, 2017

@sindresorhus, @matijs - If no one objects, i'll take the PR over.

@sindresorhus
Copy link
Owner

@florianb Go ahead :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants