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 2 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.

4 changes: 3 additions & 1 deletion 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.0",
"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.0"
"nyc": "^15.0.0",
"typescript": "^3.8.3"
},
"peerDependencies": {
"eslint": ">=3.0.0"
Expand Down
10 changes: 5 additions & 5 deletions rules/from-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ 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);
restParameters = sourceCode.getText().slice(callback.loc.end.column, parent.loc.end.column);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use callback.range[1] instead of loc.end.column. Same goes for all ranges. range[0] is the start, range[1] is the end. I believe the current approach only works because all unit tests only operate on the first line.

return fixer.replaceTextRange([
callback.start,
node.end
callback.loc.start.column,
node.loc.end.column
], `${functionStart}${lastCallback}${functionEnd}${restParameters}`);
}

// Move the map arguments to from.
const [ firstArgument ] = node.arguments;
return fixer.replaceTextRange([
parent.end - FUNCTION_END.length,
firstArgument.start
parent.loc.end.column - FUNCTION_END.length,
firstArgument.loc.start.column
], PARAM_SEPARATOR);
}
});
Expand Down
75 changes: 75 additions & 0 deletions test/rules/from-map-ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import AvaRuleTester from "eslint-ava-rule-tester";
import test from "ava";
import rule from "../../rules/from-map";

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

ruleTester.run('from-map', rule, {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the actual rule tests should be shared with a helper module, so adding a test for the rule automatically checks for both parsers.

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)'
}
]
});