p42ai / js-assistant

120+ refactorings and code-assists for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=p42ai.refactor
MIT License
119 stars 7 forks source link

Convert to map has issues with the ... operator #27

Closed hediet closed 2 years ago

hediet commented 2 years ago
const combinedDiffs: { diff: RangeMapping; input: 1 | 2 }[] = [];

    for (const diffs of baseRange.input1Diffs) {
        combinedDiffs.push(...diffs.innerRangeMappings.map(diff => ({ diff, input: 1 as const })));
    }
    for (const diffs of baseRange.input2Diffs) {
        combinedDiffs.push(...diffs.innerRangeMappings.map(diff => ({ diff, input: 2 as const })));
    }

Convert to map ->

const combinedDiffs: { diff: RangeMapping; input: 1 | 2 }[] = baseRange.input1Diffs.map((diffs) => {
        return ...diffs.innerRangeMappings.map(diff => ({ diff, input: 1 as const }));
    });

    for (const diffs of baseRange.input2Diffs) {
        combinedDiffs.push(...diffs.innerRangeMappings.map(diff => ({ diff, input: 2 as const })));
    }

🐛

There is also flatMap!

lgrammel commented 2 years ago

Thanks for the bug report! I've released v1.117.2, which fixes this issue.

How would you use flatMap here ideally?

hediet commented 2 years ago
const combinedDiffs: { diff: RangeMapping; input: 1 | 2 }[] = baseRange.input1Diffs.flatMap((diffs) => {
        return diffs.innerRangeMappings.map(diff => ({ diff, input: 1 as const }));
    });
lgrammel commented 2 years ago

I've added .flatMap conversion in v1.120.3 (available when there is a spread element). Thanks for the idea!