Skip to content

Commit 0f8779e

Browse files
authored
Refactor prefer-trim-start-end with selector (#518)
1 parent 013816f commit 0f8779e

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

rules/prefer-trim-start-end.js

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,30 @@
11
'use strict';
22
const getDocumentationUrl = require('./utils/get-documentation-url');
3+
const methodSelector = require('./utils/method-selector');
34

5+
const selector = methodSelector({length: 0});
46
const methods = new Map([
57
['trimLeft', 'trimStart'],
68
['trimRight', 'trimEnd']
79
]);
810

911
const messages = {};
10-
1112
for (const [method, replacement] of methods.entries()) {
1213
messages[method] = `Prefer \`String#${method}()\` over \`String#${replacement}()\`.`;
1314
}
1415

1516
const create = context => {
1617
return {
17-
CallExpression(node) {
18-
const {callee, arguments: arguments_} = node;
19-
20-
if (
21-
callee.type !== 'MemberExpression' ||
22-
callee.property.type !== 'Identifier' ||
23-
arguments_.length !== 0
24-
) {
25-
return;
18+
[selector](node) {
19+
const {property} = node.callee;
20+
const method = property.name;
21+
if (methods.has(method)) {
22+
context.report({
23+
node,
24+
messageId: method,
25+
fix: fixer => fixer.replaceText(property, methods.get(method))
26+
});
2627
}
27-
28-
const method = callee.property.name;
29-
30-
if (!methods.has(method)) {
31-
return;
32-
}
33-
34-
const replacement = methods.get(method);
35-
36-
context.report({
37-
node,
38-
messageId: method,
39-
fix: fixer => fixer.replaceText(callee.property, replacement)
40-
});
4128
}
4229
};
4330
};

rules/utils/method-selector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = options => {
1111
'[callee.type="MemberExpression"]',
1212
'[callee.computed=false]',
1313
'[callee.property.type="Identifier"]',
14-
`[callee.property.name="${name}"]`,
14+
name ? `[callee.property.name="${name}"]` : '',
1515
`[arguments.length=${length}]`,
1616
...(
1717
allowSpreadElement ?

test/prefer-trim-start-end.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ ruleTester.run('prefer-flat-map', rule, {
2222
valid: [
2323
'foo.trimStart()',
2424
'foo.trimEnd()',
25-
// Extra arguments
26-
'foo.trimLeft(1)',
27-
// New
28-
'new foo.trimLeft()',
29-
// Function call
30-
'trimLeft()',
31-
// Not call
32-
'foo.trimLeft'
25+
// Not `CallExpression`
26+
'new foo.trimLeft();',
27+
// Not `MemberExpression`
28+
'trimLeft();',
29+
// `callee.property` is not a `Identifier`
30+
'foo[\'trimLeft\']();',
31+
// Computed
32+
'foo[trimLeft]();',
33+
// Not `trimLeft`/`trimRight`
34+
'foo.bar();',
35+
// More argument(s)
36+
'foo.trimLeft(extra);',
37+
'foo.trimLeft(...argumentsArray)'
3338
],
3439
invalid: [
3540
{

0 commit comments

Comments
 (0)