-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5cf874
commit cbd5dfc
Showing
6 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Prefer `String#slice()` over `String#substr()` and `String#substring()` | ||
|
||
[`String#substr()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr) and [`String#substring()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) are the two lesser known legacy ways to slice a string. It's better to use [`String#slice()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) as it's a more popular option with clearer behavior that has a consistent [`Array` counterpart](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice). | ||
|
||
This rule is fixible when no arguments are passed to the method. | ||
|
||
|
||
## Fail | ||
|
||
```js | ||
foo.substr(1, 2); | ||
foo.substring(1, 3); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
foo.slice(1, 3); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
const eslintTemplateVisitor = require('eslint-template-visitor'); | ||
const getDocsUrl = require('./utils/get-docs-url'); | ||
|
||
const templates = eslintTemplateVisitor(); | ||
|
||
const objectVariable = templates.variable(); | ||
const argumentsVariable = templates.spreadVariable(); | ||
|
||
const substrCallTemplate = templates.template`${objectVariable}.substr(${argumentsVariable})`; | ||
const substringCallTemplate = templates.template`${objectVariable}.substring(${argumentsVariable})`; | ||
|
||
const create = context => { | ||
const sourceCode = context.getSourceCode(); | ||
|
||
return templates.visitor({ | ||
[substrCallTemplate](node) { | ||
const objectNode = substrCallTemplate.context.getMatch(objectVariable); | ||
const argumentNodes = substrCallTemplate.context.getMatch(argumentsVariable); | ||
|
||
const problem = { | ||
node, | ||
message: 'Prefer `String#slice()` over `String#substr()`.' | ||
}; | ||
|
||
const canFix = argumentNodes.length === 0; | ||
|
||
if (canFix) { | ||
problem.fix = fixer => fixer.replaceText(node, sourceCode.getText(objectNode) + '.slice()'); | ||
} | ||
|
||
context.report(problem); | ||
}, | ||
|
||
[substringCallTemplate](node) { | ||
const objectNode = substringCallTemplate.context.getMatch(objectVariable); | ||
const argumentNodes = substringCallTemplate.context.getMatch(argumentsVariable); | ||
|
||
const problem = { | ||
node, | ||
message: 'Prefer `String#slice()` over `String#substring()`.' | ||
}; | ||
|
||
const canFix = argumentNodes.length === 0; | ||
|
||
if (canFix) { | ||
problem.fix = fixer => fixer.replaceText(node, sourceCode.getText(objectNode) + '.slice()'); | ||
} | ||
|
||
context.report(problem); | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocsUrl(__filename) | ||
}, | ||
fixable: 'code' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/prefer-string-slice'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const errors = [{ | ||
ruleId: 'prefer-string-slice' | ||
}]; | ||
|
||
ruleTester.run('prefer-string-slice', rule, { | ||
valid: [ | ||
'const substr = foo.substr', | ||
'const substring = foo.substring', | ||
|
||
'foo.slice()', | ||
'foo.slice(0)', | ||
'foo.slice(1, 2)', | ||
'foo.slice(-3, -2)' | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'foo.substr()', | ||
output: 'foo.slice()', | ||
errors | ||
}, | ||
{ | ||
code: '"foo".substr()', | ||
output: '"foo".slice()', | ||
errors | ||
}, | ||
|
||
{ | ||
code: 'foo.substr(start)', | ||
errors | ||
}, | ||
{ | ||
code: '"foo".substr(1)', | ||
errors | ||
}, | ||
{ | ||
code: 'foo.substr(start, length)', | ||
errors | ||
}, | ||
{ | ||
code: '"foo".substr(1, 2)', | ||
errors | ||
}, | ||
|
||
{ | ||
code: 'foo.substring()', | ||
output: 'foo.slice()', | ||
errors | ||
}, | ||
{ | ||
code: '"foo".substring()', | ||
output: '"foo".slice()', | ||
errors | ||
}, | ||
|
||
{ | ||
code: 'foo.substring(start)', | ||
errors | ||
}, | ||
{ | ||
code: '"foo".substring(1)', | ||
errors | ||
}, | ||
{ | ||
code: 'foo.substring(start, end)', | ||
errors | ||
}, | ||
{ | ||
code: '"foo".substring(1, 3)', | ||
errors | ||
} | ||
] | ||
}); |