Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
prefer-trim-start-end
rule (#440)
- Loading branch information
1 parent
a496e96
commit 29e3b13
Showing
5 changed files
with
128 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,19 @@ | ||
# Prefer `String#trimStart()` / `String#trimEnd()` over `String#trimLeft()` / `String#trimRight()` | ||
|
||
[`String#trimLeft()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimLeft) and [`String#trimRight()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimRight) are aliases of [`String#trimStart()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart) and [`String#trimEnd()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd) | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
const foo = bar.trimStart(); | ||
const foo = bar.trimEnd(); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const foo = bar.trimLeft(); | ||
const foo = bar.trimRight(); | ||
``` |
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,55 @@ | ||
'use strict'; | ||
const getDocumentationUrl = require('./utils/get-documentation-url'); | ||
|
||
const methods = new Map([ | ||
['trimLeft', 'trimStart'], | ||
['trimRight', 'trimEnd'] | ||
]); | ||
|
||
const messages = {}; | ||
|
||
for (const [method, replacement] of methods.entries()) { | ||
messages[method] = `Prefer \`String#${method}()\` over \`String#${replacement}()\`.`; | ||
} | ||
|
||
const create = context => { | ||
return { | ||
CallExpression(node) { | ||
const {callee, arguments: arguments_} = node; | ||
|
||
if ( | ||
callee.type !== 'MemberExpression' || | ||
callee.property.type !== 'Identifier' || | ||
arguments_.length !== 0 | ||
) { | ||
return; | ||
} | ||
|
||
const method = callee.property.name; | ||
|
||
if (!methods.has(method)) { | ||
return; | ||
} | ||
|
||
const replacement = methods.get(method); | ||
|
||
context.report({ | ||
node, | ||
messageId: method, | ||
fix: fixer => fixer.replaceText(callee.property, replacement) | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
url: getDocumentationUrl(__filename) | ||
}, | ||
fixable: 'code', | ||
messages | ||
} | ||
}; |
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,51 @@ | ||
import test from 'ava'; | ||
import avaRuleTester from 'eslint-ava-rule-tester'; | ||
import rule from '../rules/prefer-trim-start-end'; | ||
|
||
const ruleTester = avaRuleTester(test, { | ||
env: { | ||
es6: true | ||
} | ||
}); | ||
|
||
const errorTrimLeft = { | ||
ruleId: 'prefer-trim-start-end', | ||
messageId: 'trimLeft' | ||
}; | ||
|
||
const errorTrimRight = { | ||
ruleId: 'prefer-trim-start-end', | ||
messageId: 'trimRight' | ||
}; | ||
|
||
ruleTester.run('prefer-flat-map', rule, { | ||
valid: [ | ||
'foo.trimStart()', | ||
'foo.trimEnd()', | ||
// Extra arguments | ||
'foo.trimLeft(1)', | ||
// New | ||
'new foo.trimLeft()', | ||
// Function call | ||
'trimLeft()', | ||
// Not call | ||
'foo.trimLeft' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'foo.trimLeft()', | ||
output: 'foo.trimStart()', | ||
errors: [errorTrimLeft] | ||
}, | ||
{ | ||
code: 'foo.trimRight()', | ||
output: 'foo.trimEnd()', | ||
errors: [errorTrimRight] | ||
}, | ||
{ | ||
code: '"foo".trimLeft()', | ||
output: '"foo".trimStart()', | ||
errors: [errorTrimLeft] | ||
} | ||
] | ||
}); |