tsquery icon indicating copy to clipboard operation
tsquery copied to clipboard

Losing empty lines when using replace function

Open PowerSupply opened this issue 1 year ago • 1 comments

Problem description When using replace function to transform code the updated code is said to be printed with the TypeScript Printer.

This seems to get rid of empty lines which are not recovered when formatting the updated code with e.g. Prettier. This results in many diffs in the updated code compared to the original code, even when the same formatter was used before and after.

Suggested solution Wouldn't it be possible to just do a string replacement if the input to the replace function is a string (not a node)? This would guarantee that the parts of the original string that are not replaced remain the same.

PowerSupply avatar Apr 25 '24 20:04 PowerSupply

Here is a wrapper function that does the thing:

import { query } from '@phenomnomnominal/tsquery';

export function exactReplace(originalCode: string, selector: string, mappingFunc: (a: string) => string) {
    let offset = 0;

    query(originalCode, selector).forEach(({ pos, end }) => {
        pos = pos + offset;
        end = end + offset;
        const match = originalCode.substring(pos, end);
        const replacement = mappingFunc(match);
        originalCode = replaceSubstring(originalCode, pos, end, replacement);
        offset = offset + (replacement.length - match.length);
    });

    return originalCode;
}

function replaceSubstring(str: string, start: number, end: number, replacement: string) {
    return str.substring(0, start) + replacement + str.substring(end);
}

PowerSupply avatar Apr 25 '24 21:04 PowerSupply