Skip to content

Commit 8d9eb3d

Browse files
authored
fix: from-map rule is not working on TS files (#50)
* fix: from-map rule is not working on TS files * fix: from-map rule is not working on TS files * fix: from-map rule is not working on TS files
1 parent 51e547a commit 8d9eb3d

File tree

6 files changed

+195
-71
lines changed

6 files changed

+195
-71
lines changed

package-lock.json

Lines changed: 88 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616
"@ava/babel": "^1.0.1",
1717
"@freaktechnik/eslint-config-node": "^7.1.0",
1818
"@freaktechnik/eslint-config-test": "^7.1.0",
19+
"@typescript-eslint/parser": "^2.25.0",
1920
"ava": "^3.5.2",
2021
"codecov": "^3.6.5",
2122
"eclint": "^2.8.1",
2223
"eslint": "^6.8.0",
2324
"eslint-ava-rule-tester": "^4.0.0",
2425
"eslint-plugin-eslint-plugin": "^2.2.1",
25-
"nyc": "^15.0.1"
26+
"nyc": "^15.0.1",
27+
"typescript": "^3.8.3"
2628
},
2729
"peerDependencies": {
2830
"eslint": ">=3.0.0"
@@ -49,6 +51,10 @@
4951
},
5052
"dependencies": {},
5153
"ava": {
52-
"babel": true
54+
"babel": true,
55+
"files": [
56+
"test/**/*",
57+
"!test/helpers"
58+
]
5359
}
5460
}

rules/from-map.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,32 @@ module.exports = {
7676
}
7777
// The original map callback from Array.from gets nested as a parameter to the callback from map.
7878
const lastCallback = getCallback(mapCallback, mapThisArgument, `${firstCallback}${restParameterString}`),
79-
restParameters = sourceCode.getText().slice(callback.end, parent.end);
79+
[
80+
callbackStartLocation
81+
, callbackEndLocation
82+
] = callback.range,
83+
[
84+
, parentEndLocation
85+
] = parent.range,
86+
[
87+
, nodeEndLocation
88+
] = node.range,
89+
restParameters = sourceCode.getText().slice(callbackEndLocation, parentEndLocation);
8090
return fixer.replaceTextRange([
81-
callback.start,
82-
node.end
91+
callbackStartLocation,
92+
nodeEndLocation
8393
], `${functionStart}${lastCallback}${functionEnd}${restParameters}`);
8494
}
8595

8696
// Move the map arguments to from.
87-
const [ firstArgument ] = node.arguments;
97+
const [ firstArgument ] = node.arguments,
98+
[ argumentStartLocation ] = firstArgument.range,
99+
[
100+
, parentEndLocation
101+
] = parent.range;
88102
return fixer.replaceTextRange([
89-
parent.end - FUNCTION_END.length,
90-
firstArgument.start
103+
parentEndLocation - FUNCTION_END.length,
104+
argumentStartLocation
91105
], PARAM_SEPARATOR);
92106
}
93107
});

test/helpers/from-map-test-cases.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
module.exports = {
2+
valid: [
3+
'array.map((t) => t.id)',
4+
'Array.from(iterable).some((t) => t !== "a")',
5+
'Array.from(iterable, (t) => t.id)'
6+
],
7+
invalid: [
8+
{
9+
code: 'Array.from(iterable).map((t) => t.id)',
10+
errors: [ {
11+
message: 'Use mapFn callback of Array.from instead of map()',
12+
column: 1,
13+
line: 1
14+
} ],
15+
output: 'Array.from(iterable, (t) => t.id)'
16+
},
17+
{
18+
code: 'Array.from(iterable, (t) => t.id, a).map((t) => t[0], b)',
19+
errors: [ {
20+
message: 'Use mapFn callback of Array.from instead of map()',
21+
column: 1,
22+
line: 1
23+
} ],
24+
output: 'Array.from(iterable, (t) => ((t) => t[0])(((t) => t.id)(t)), a)'
25+
},
26+
{
27+
code: 'Array.from(iterable, function(t) { return t.id; }, a).map((t) => t[0])',
28+
errors: [ {
29+
message: 'Use mapFn callback of Array.from instead of map()',
30+
column: 1,
31+
line: 1
32+
} ],
33+
output: 'Array.from(iterable, function(t) { return ((t) => t[0])((function(t) { return t.id; }).call(this, t)); }, a)'
34+
},
35+
{
36+
code: 'Array.from(iterable, function(t) { return t.id; }, a).map(function(t) { return t[0]; }, b)',
37+
errors: [ {
38+
message: 'Use mapFn callback of Array.from instead of map()',
39+
column: 1,
40+
line: 1
41+
} ],
42+
output: 'Array.from(iterable, function(t) { return (function(t) { return t[0]; }).call(b, (function(t) { return t.id; }).call(this, t)); }, a)'
43+
},
44+
{
45+
code: 'Array.from(iterable, function(u) { return u.id; }, a).map(function(t, i) { return t[0]; }, b)',
46+
errors: [ {
47+
message: 'Use mapFn callback of Array.from instead of map()',
48+
column: 1,
49+
line: 1
50+
} ],
51+
output: 'Array.from(iterable, function(t, i) { return (function(t, i) { return t[0]; }).call(b, (function(u) { return u.id; }).call(this, t, i), i); }, a)'
52+
},
53+
{
54+
code: 'Array.from(iterable, function(u, i) { return u.id; }, a).map(function(t) { return t[0]; }, b)',
55+
errors: [ {
56+
message: 'Use mapFn callback of Array.from instead of map()',
57+
column: 1,
58+
line: 1
59+
} ],
60+
output: 'Array.from(iterable, function(u, i) { return (function(t) { return t[0]; }).call(b, (function(u, i) { return u.id; }).call(this, u, i), i); }, a)'
61+
}
62+
]
63+
};

test/rules/from-map-ts.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import AvaRuleTester from "eslint-ava-rule-tester";
2+
import test from "ava";
3+
import rule from "../../rules/from-map";
4+
import testCases from '../helpers/from-map-test-cases';
5+
6+
const ruleTester = new AvaRuleTester(test, {
7+
parser: require.resolve('@typescript-eslint/parser'),
8+
parserOptions: {
9+
ecmaVersion: 2020,
10+
sourceType: 'module'
11+
}
12+
});
13+
14+
ruleTester.run('from-map', rule, testCases);

test/rules/from-map.js

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,12 @@
11
import test from 'ava';
22
import AvaRuleTester from 'eslint-ava-rule-tester';
33
import rule from '../../rules/from-map';
4+
import testCases from "../helpers/from-map-test-cases";
45

56
const ruleTester = new AvaRuleTester(test, {
67
parserOptions: {
78
ecmaVersion: 2015
89
}
910
});
1011

11-
ruleTester.run('from-map', rule, {
12-
valid: [
13-
'array.map((t) => t.id)',
14-
'Array.from(iterable).some((t) => t !== "a")',
15-
'Array.from(iterable, (t) => t.id)'
16-
],
17-
invalid: [
18-
{
19-
code: 'Array.from(iterable).map((t) => t.id)',
20-
errors: [ {
21-
message: 'Use mapFn callback of Array.from instead of map()',
22-
column: 1,
23-
line: 1
24-
} ],
25-
output: 'Array.from(iterable, (t) => t.id)'
26-
},
27-
{
28-
code: 'Array.from(iterable, (t) => t.id, a).map((t) => t[0], b)',
29-
errors: [ {
30-
message: 'Use mapFn callback of Array.from instead of map()',
31-
column: 1,
32-
line: 1
33-
} ],
34-
output: 'Array.from(iterable, (t) => ((t) => t[0])(((t) => t.id)(t)), a)'
35-
},
36-
{
37-
code: 'Array.from(iterable, function(t) { return t.id; }, a).map((t) => t[0])',
38-
errors: [ {
39-
message: 'Use mapFn callback of Array.from instead of map()',
40-
column: 1,
41-
line: 1
42-
} ],
43-
output: 'Array.from(iterable, function(t) { return ((t) => t[0])((function(t) { return t.id; }).call(this, t)); }, a)'
44-
},
45-
{
46-
code: 'Array.from(iterable, function(t) { return t.id; }, a).map(function(t) { return t[0]; }, b)',
47-
errors: [ {
48-
message: 'Use mapFn callback of Array.from instead of map()',
49-
column: 1,
50-
line: 1
51-
} ],
52-
output: 'Array.from(iterable, function(t) { return (function(t) { return t[0]; }).call(b, (function(t) { return t.id; }).call(this, t)); }, a)'
53-
},
54-
{
55-
code: 'Array.from(iterable, function(u) { return u.id; }, a).map(function(t, i) { return t[0]; }, b)',
56-
errors: [ {
57-
message: 'Use mapFn callback of Array.from instead of map()',
58-
column: 1,
59-
line: 1
60-
} ],
61-
output: 'Array.from(iterable, function(t, i) { return (function(t, i) { return t[0]; }).call(b, (function(u) { return u.id; }).call(this, t, i), i); }, a)'
62-
},
63-
{
64-
code: 'Array.from(iterable, function(u, i) { return u.id; }, a).map(function(t) { return t[0]; }, b)',
65-
errors: [ {
66-
message: 'Use mapFn callback of Array.from instead of map()',
67-
column: 1,
68-
line: 1
69-
} ],
70-
output: 'Array.from(iterable, function(u, i) { return (function(t) { return t[0]; }).call(b, (function(u, i) { return u.id; }).call(this, u, i), i); }, a)'
71-
}
72-
]
73-
});
12+
ruleTester.run('from-map', rule, testCases);

0 commit comments

Comments
 (0)