build/lib/treeshaking.ts/writeFile
function generateResult(ts: typeof import('typescript'), languageService: ts.LanguageService, shakeLevel: ShakeLevel): ITreeShakingResult {
	const program = languageService.getProgram();
	if (!program) {
		throw new Error('Could not get program from language service');
	}

	let result: ITreeShakingResult = {};
	const writeFile = (filePath: string, contents: string): void => {
		result[filePath] = contents;
	};

	program.getSourceFiles().forEach((sourceFile) => {
		const fileName = sourceFile.fileName;
		if (/^defaultLib:/.test(fileName)) {
			return;
		}
		const destination = fileName;
		if (/\.d\.ts$/.test(fileName)) {
			if (nodeOrChildIsBlack(sourceFile)) {
				writeFile(destination, sourceFile.text);
			}
			return;
		}

		let text = sourceFile.text;
		let result = '';

		function keep(node: ts.Node): void {
			result += text.substring(node.pos, node.end);
		}
		function write(data: string): void {
			result += data;
		}

		function writeMarkedNodes(node: ts.Node): void {
			if (getColor(node) === NodeColor.Black) {
				return keep(node);
			}

			// Always keep certain top-level statements
			if (ts.isSourceFile(node.parent)) {
				if (ts.isExpressionStatement(node) && ts.isStringLiteral(node.expression) && node.expression.text === 'use strict') {
					return keep(node);
				}

				if (ts.isVariableStatement(node) && nodeOrChildIsBlack(node)) {
					return keep(node);
				}
			}

			// Keep the entire import in import * as X cases
			if (ts.isImportDeclaration(node)) {
				if (node.importClause && node.importClause.namedBindings) {
					if (ts.isNamespaceImport(node.importClause.namedBindings)) {
						if (getColor(node.importClause.namedBindings) === NodeColor.Black) {
							return keep(node);
						}
					} else {
						let survivingImports: string[] = [];
						for (const importNode of node.importClause.namedBindings.elements) {
							if (getColor(importNode) === NodeColor.Black) {
								survivingImports.push(importNode.getFullText(sourceFile));
							}
						}
						const leadingTriviaWidth = node.getLeadingTriviaWidth();
						const leadingTrivia = sourceFile.text.substr(node.pos, leadingTriviaWidth);
						if (survivingImports.length > 0) {
							if (node.importClause && node.importClause.name && getColor(node.importClause) === NodeColor.Black) {
								return write(`${leadingTrivia}import ${node.importClause.name.text}, {${survivingImports.join(',')} } from${node.moduleSpecifier.getFullText(sourceFile)};`);
							}
							return write(`${leadingTrivia}import {${survivingImports.join(',')} } from${node.moduleSpecifier.getFullText(sourceFile)};`);
						} else {
							if (node.importClause && node.importClause.name && getColor(node.importClause) === NodeColor.Black) {
								return write(`${leadingTrivia}import ${node.importClause.name.text} from${node.moduleSpecifier.getFullText(sourceFile)};`);
							}
						}
					}
				} else {
					if (node.importClause && getColor(node.importClause) === NodeColor.Black) {
						return keep(node);
					}
				}
			}

			if (ts.isExportDeclaration(node)) {
				if (node.exportClause && node.moduleSpecifier && ts.isNamedExports(node.exportClause)) {
					let survivingExports: string[] = [];
					for (const exportSpecifier of node.exportClause.elements) {
						if (getColor(exportSpecifier) === NodeColor.Black) {
							survivingExports.push(exportSpecifier.getFullText(sourceFile));
						}
					}
					const leadingTriviaWidth = node.getLeadingTriviaWidth();
					const leadingTrivia = sourceFile.text.substr(node.pos, leadingTriviaWidth);
					if (survivingExports.length > 0) {
						return write(`${leadingTrivia}export {${survivingExports.join(',')} } from${node.moduleSpecifier.getFullText(sourceFile)};`);
					}
				}
			}

			if (shakeLevel === ShakeLevel.ClassMembers && (ts.isClassDeclaration(node) || ts.isInterfaceDeclaration(node)) && nodeOrChildIsBlack(node)) {
				let toWrite = node.getFullText();
				for (let i = node.members.length - 1; i >= 0; i--) {
					const member = node.members[i];
					if (getColor(member) === NodeColor.Black || !member.name) {
						// keep method
						continue;
					}

					let pos = member.pos - node.pos;
					let end = member.end - node.pos;
					toWrite = toWrite.substring(0, pos) + toWrite.substring(end);
				}
				return write(toWrite);
			}

			if (ts.isFunctionDeclaration(node)) {
				// Do not go inside functions if they haven't been marked
				return;
			}

			node.forEachChild(writeMarkedNodes);
		}

		if (getColor(sourceFile) !== NodeColor.Black) {
			if (!nodeOrChildIsBlack(sourceFile)) {
				// none of the elements are reachable => don't write this file at all!
				return;
			}
			sourceFile.forEachChild(writeMarkedNodes);
			result += sourceFile.endOfFileToken.getFullText(sourceFile);
		} else {
			result = text;
		}

		writeFile(destination, result);
	});

	return result;
}