Skip to content

fix: from-map rule is not working on TS files #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
"@ava/babel": "^1.0.1",
"@freaktechnik/eslint-config-node": "^7.1.0",
"@freaktechnik/eslint-config-test": "^7.1.0",
"@typescript-eslint/parser": "^2.25.0",
"ava": "^3.5.2",
"codecov": "^3.6.5",
"eclint": "^2.8.1",
"eslint": "^6.8.0",
"eslint-ava-rule-tester": "^4.0.0",
"eslint-plugin-eslint-plugin": "^2.2.1",
"nyc": "^15.0.1"
"nyc": "^15.0.1",
"typescript": "^3.8.3"
},
"peerDependencies": {
"eslint": ">=3.0.0"
Expand All @@ -49,6 +51,10 @@
},
"dependencies": {},
"ava": {
"babel": true
"babel": true,
"files": [
"test/**/*",
"!test/helpers"
]
}
}
26 changes: 20 additions & 6 deletions rules/from-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,32 @@ module.exports = {
}
// The original map callback from Array.from gets nested as a parameter to the callback from map.
const lastCallback = getCallback(mapCallback, mapThisArgument, `${firstCallback}${restParameterString}`),
restParameters = sourceCode.getText().slice(callback.end, parent.end);
[
callbackStartLocation
, callbackEndLocation
] = callback.range,
[
, parentEndLocation
] = parent.range,
[
, nodeEndLocation
] = node.range,
restParameters = sourceCode.getText().slice(callbackEndLocation, parentEndLocation);
return fixer.replaceTextRange([
callback.start,
node.end
callbackStartLocation,
nodeEndLocation
], `${functionStart}${lastCallback}${functionEnd}${restParameters}`);
}

// Move the map arguments to from.
const [ firstArgument ] = node.arguments;
const [ firstArgument ] = node.arguments,
[ argumentStartLocation ] = firstArgument.range,
[
, parentEndLocation
] = parent.range;
return fixer.replaceTextRange([
parent.end - FUNCTION_END.length,
firstArgument.start
parentEndLocation - FUNCTION_END.length,
argumentStartLocation
], PARAM_SEPARATOR);
}
});
Expand Down
63 changes: 63 additions & 0 deletions test/helpers/from-map-test-cases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module.exports = {
valid: [
'array.map((t) => t.id)',
'Array.from(iterable).some((t) => t !== "a")',
'Array.from(iterable, (t) => t.id)'
],
invalid: [
{
code: 'Array.from(iterable).map((t) => t.id)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
output: 'Array.from(iterable, (t) => t.id)'
},
{
code: 'Array.from(iterable, (t) => t.id, a).map((t) => t[0], b)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
output: 'Array.from(iterable, (t) => ((t) => t[0])(((t) => t.id)(t)), a)'
},
{
code: 'Array.from(iterable, function(t) { return t.id; }, a).map((t) => t[0])',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
output: 'Array.from(iterable, function(t) { return ((t) => t[0])((function(t) { return t.id; }).call(this, t)); }, a)'
},
{
code: 'Array.from(iterable, function(t) { return t.id; }, a).map(function(t) { return t[0]; }, b)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
output: 'Array.from(iterable, function(t) { return (function(t) { return t[0]; }).call(b, (function(t) { return t.id; }).call(this, t)); }, a)'
},
{
code: 'Array.from(iterable, function(u) { return u.id; }, a).map(function(t, i) { return t[0]; }, b)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
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)'
},
{
code: 'Array.from(iterable, function(u, i) { return u.id; }, a).map(function(t) { return t[0]; }, b)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
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)'
}
]
};
14 changes: 14 additions & 0 deletions test/rules/from-map-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import AvaRuleTester from "eslint-ava-rule-tester";
import test from "ava";
import rule from "../../rules/from-map";
import testCases from '../helpers/from-map-test-cases';

const ruleTester = new AvaRuleTester(test, {
parser: require.resolve('@typescript-eslint/parser'),
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module'
}
});

ruleTester.run('from-map', rule, testCases);
65 changes: 2 additions & 63 deletions test/rules/from-map.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,12 @@
import test from 'ava';
import AvaRuleTester from 'eslint-ava-rule-tester';
import rule from '../../rules/from-map';
import testCases from "../helpers/from-map-test-cases";

const ruleTester = new AvaRuleTester(test, {
parserOptions: {
ecmaVersion: 2015
}
});

ruleTester.run('from-map', rule, {
valid: [
'array.map((t) => t.id)',
'Array.from(iterable).some((t) => t !== "a")',
'Array.from(iterable, (t) => t.id)'
],
invalid: [
{
code: 'Array.from(iterable).map((t) => t.id)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
output: 'Array.from(iterable, (t) => t.id)'
},
{
code: 'Array.from(iterable, (t) => t.id, a).map((t) => t[0], b)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
output: 'Array.from(iterable, (t) => ((t) => t[0])(((t) => t.id)(t)), a)'
},
{
code: 'Array.from(iterable, function(t) { return t.id; }, a).map((t) => t[0])',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
output: 'Array.from(iterable, function(t) { return ((t) => t[0])((function(t) { return t.id; }).call(this, t)); }, a)'
},
{
code: 'Array.from(iterable, function(t) { return t.id; }, a).map(function(t) { return t[0]; }, b)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
output: 'Array.from(iterable, function(t) { return (function(t) { return t[0]; }).call(b, (function(t) { return t.id; }).call(this, t)); }, a)'
},
{
code: 'Array.from(iterable, function(u) { return u.id; }, a).map(function(t, i) { return t[0]; }, b)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
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)'
},
{
code: 'Array.from(iterable, function(u, i) { return u.id; }, a).map(function(t) { return t[0]; }, b)',
errors: [ {
message: 'Use mapFn callback of Array.from instead of map()',
column: 1,
line: 1
} ],
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)'
}
]
});
ruleTester.run('from-map', rule, testCases);