Skip to content

Commit

Permalink
prefer-trim-start-end: Fix error message, improve report location (#823)
Browse files Browse the repository at this point in the history
Co-authored-by: fisker <lionkay@gmail.com>
  • Loading branch information
zawys and fisker committed Sep 11, 2020
1 parent a78cea3 commit b35c261
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 10 deletions.
19 changes: 11 additions & 8 deletions rules/prefer-trim-start-end.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@ const methods = new Map([
['trimRight', 'trimEnd']
]);

const selector = methodSelector({
names: ['trimLeft', 'trimRight'],
length: 0
});
const selector = [
methodSelector({
names: ['trimLeft', 'trimRight'],
length: 0
}),
'> MemberExpression.callee',
'> Identifier.property'
].join(' ');

const messages = {};
for (const [method, replacement] of methods.entries()) {
messages[method] = `Prefer \`String#${method}()\` over \`String#${replacement}()\`.`;
messages[method] = `Prefer \`String#${replacement}()\` over \`String#${method}()\`.`;
}

const create = context => {
return {
[selector](node) {
const {property} = node.callee;
const method = property.name;
const method = node.name;
context.report({
node,
messageId: method,
fix: fixer => fixer.replaceText(property, methods.get(method))
fix: fixer => fixer.replaceText(node, methods.get(method))
});
}
};
Expand Down
51 changes: 49 additions & 2 deletions test/prefer-trim-start-end.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import {outdent} from 'outdent';
import visualizeRuleTester from './utils/visualize-rule-tester';
import rule from '../rules/prefer-trim-start-end';

const ruleTester = avaRuleTester(test, {
Expand All @@ -16,7 +18,7 @@ const errorTrimRight = {
messageId: 'trimRight'
};

ruleTester.run('prefer-flat-map', rule, {
ruleTester.run('prefer-trim-start-end', rule, {
valid: [
'foo.trimStart()',
'foo.trimEnd()',
Expand All @@ -32,7 +34,13 @@ ruleTester.run('prefer-flat-map', rule, {
'foo.bar();',
// More argument(s)
'foo.trimLeft(extra);',
'foo.trimLeft(...argumentsArray)'
'foo.trimLeft(...argumentsArray)',
// `trimLeft` is in argument
'foo.bar(trimLeft)',
'foo.bar(foo.trimLeft)',
// `trimLeft` is in `MemberExpression.object`
'trimLeft.foo()',
'foo.trimLeft.bar()'
],
invalid: [
{
Expand All @@ -45,10 +53,49 @@ ruleTester.run('prefer-flat-map', rule, {
output: 'foo.trimEnd()',
errors: [errorTrimRight]
},
{
code: 'trimLeft.trimRight()',
output: 'trimLeft.trimEnd()',
errors: [errorTrimRight]
},
{
code: 'foo.trimLeft.trimRight()',
output: 'foo.trimLeft.trimEnd()',
errors: [errorTrimRight]
},
{
code: '"foo".trimLeft()',
output: '"foo".trimStart()',
errors: [errorTrimLeft]
},
{
code: outdent`
foo
// comment
.trimRight/* comment */(
/* comment */
)
`,
output: outdent`
foo
// comment
.trimEnd/* comment */(
/* comment */
)
`,
errors: [errorTrimRight]
}
]
});

const visualizeTester = visualizeRuleTester(test);
visualizeTester.run('prefer-trim-start-end', rule, [
'foo.trimLeft()',
outdent`
foo
// comment
.trimRight/* comment */(
/* comment */
)
`
]);
27 changes: 27 additions & 0 deletions test/snapshots/prefer-trim-start-end.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Snapshot report for `test/prefer-trim-start-end.js`

The actual snapshot is saved in `prefer-trim-start-end.js.snap`.

Generated by [AVA](https://avajs.dev).

## prefer-trim-start-end - #1

> Snapshot 1
`␊
> 1 | foo.trimLeft()␊
| ^^^^^^^^ Prefer `String#trimStart()` over `String#trimLeft()`.␊
`

## prefer-trim-start-end - #2

> Snapshot 1
`␊
1 | foo␊
2 | // comment␊
> 3 | .trimRight/* comment */(␊
| ^^^^^^^^^ Prefer `String#trimEnd()` over `String#trimRight()`.␊
4 | /* comment */␊
5 | )␊
`
Binary file added test/snapshots/prefer-trim-start-end.js.snap
Binary file not shown.

0 comments on commit b35c261

Please sign in to comment.