Repository: sveltejs/svelte
File: packages/svelte/tests/sourcemaps/test.ts (TypeScript)

Code before:
				source = input;
			}

			const original = locate(source, source.indexOf(str, idx));
			if (!original)
				throw new Error(`Could not find '${str}'${idx > 0 ? ` after index ${idx}` : ''} in input`);
			return original;
		}

		/** Find line/column of string in generated code */
		function find_generated(str: string, idx = 0) {
			const generated = output_locator(output.indexOf(str, idx));
			if (!generated)
				throw new Error(`Could not find '${str}'${idx > 0 ? ` after index ${idx}` : ''} in output`);
			return generated;
		}

		const decoded = decode(map.mappings);

		/*
		Decoded sourcemap mappings contain absolute line/column numbers.
		Negative values are invalid and can cause errors in downstream processing
		*/
		for (let l = 0; l < decoded.length; l++) {
			for (let m of decoded[l]) {
				if (m.find((i) => i < 0)) {
					throw new Error(
						`Invalid mapping with negative value ${JSON.stringify(m)} at line ${l} of the decoded mappings of ${info} sourcemap\n${JSON.stringify(map)}`
					);
				}
			}
		}

		try {
			for (let entry of entries) {
				entry = typeof entry === 'string' ? { str: entry } : entry;

				const str = entry.str;

				// Find generated line/column
				const generated_str = entry.strGenerated ?? str;
				if (entry.strGenerated === null) {
					if (!output.includes(generated_str)) continue;
				}
				let generated = find_generated(generated_str);
				if (entry.idxGenerated) {
					let i = entry.idxGenerated;
					while (i-- > 0) {
						generated = find_generated(generated_str, generated.character + 1);
					}
				}

Review comment (suggestion):
```suggestion
				if (m.some((i) => i < 0)) {
```

Code after:
				source = input;
			}

			const original = locate(source, source.indexOf(str, idx));
			if (!original)
				throw new Error(`Could not find '${str}'${idx > 0 ? ` after index ${idx}` : ''} in input`);
			return original;
		}

		/** Find line/column of string in generated code */
		function find_generated(str: string, idx = 0) {
			const generated = output_locator(output.indexOf(str, idx));
			if (!generated)
				throw new Error(`Could not find '${str}'${idx > 0 ? ` after index ${idx}` : ''} in output`);
			return generated;
		}

		const decoded = decode(map.mappings);

		/*
		Decoded sourcemap mappings contain absolute line/column numbers.
		Negative values are invalid and can cause errors in downstream processing
		*/
		for (let l = 0; l < decoded.length; l++) {
			for (let m of decoded[l]) {
				if (m.some((i) => i < 0)) {
					throw new Error(
						`Invalid mapping with negative value ${JSON.stringify(m)} at line ${l} of the decoded mappings of ${info} sourcemap\n${JSON.stringify(map)}`
					);
				}
			}
		}

		try {
			for (let entry of entries) {
				entry = typeof entry === 'string' ? { str: entry } : entry;

				const str = entry.str;

				// Find generated line/column
				const generated_str = entry.strGenerated ?? str;
				if (entry.strGenerated === null) {
					if (!output.includes(generated_str)) continue;
				}
				let generated = find_generated(generated_str);
				if (entry.idxGenerated) {
					let i = entry.idxGenerated;
					while (i-- > 0) {
						generated = find_generated(generated_str, generated.character + 1);
					}
				}