Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bug in typescript-strip-types plugin #1408

@louisabraham

Description

@louisabraham

the repl uses a rollup plugin to remove types from .ts files

It has a bug when handling some recursive types like let a: Promise<int>;

reproduction here: https://svelte.dev/playground/ff43e6f9092b45798bd83a710d39799f?version=5.35.4

The error is

Cannot split a chunk that has already been edited

and comes from magic-string in the function ts_blank_space.

The problem is that that function is executed on both Promise<int> and int.

adding the commented lines fixed the bug for me but I'm not sure about the logic. For me it would be preferable to avoid calling ts_blank_space on overlapping positions.

function ts_blank_space(context: Context<any, { ms: MagicString }>, node: any): void {
	const { start, end } = node;
	// if (!context.state.ms.toString().slice(start, end).trim()) {
	// 	return;
	// }
	let i = start;
	while (i < end) {
		// Skip whitespace
		while (i < end && /\s/.test(context.state.ms.original[i])) i++;
		if (i >= end) break;
		// Find next whitespace or end
		let j = i + 1;
		while (j < end && !/\s/.test(context.state.ms.original[j])) j++;
		context.state.ms.overwrite(i, j, ' '.repeat(j - i));
		i = j;
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions