import { writeFileSync } from "fs" ; import { join } from "path" ; import { generateCombinedTypeTestCode } from "./generate-combined-type-test-code" ; import { markdownTable } from "./markdown-util" ; import { TypescriptType } from "./type-test" ; import { visitComparisonsInTestCode } from "./visit-type-comparisons" ; import { CompilerOptions } from "typescript" ; import { PRIMITIVE_TYPES , SPECIAL_TYPES } from "../type-combinations.spec" ; param types param compilerOptions export function generateAssignmentTableMarkdown ( types , compilerOptions = { } ) { const typeResults = new Map < string , Map < string , boolean > > ( ) ; const testCode = generateCombinedTypeTestCode ( types , types ) ; visitComparisonsInTestCode ( testCode , compilerOptions , ( { assignable , typeAString , typeBString } ) => { const typeResult = typeResults . get ( typeAString ) || new Map ( ) ; typeResult . set ( typeBString , assignable ) ; typeResults . set ( typeAString , typeResult ) ; } ) ; const headerRow = [ "typeB ➡️\ntypeA ⬇️" , ... Array . from ( typeResults . keys ( ) ) ] ; const rows = Array . from ( typeResults . entries ( ) ) . map ( ( [ title , typeResult ] ) => [ title , ... Array . from ( typeResult . values ( ) ) . map ( assignable => ( assignable ? "✅" : "❌" ) ) ] ) ; const tableRows = [ headerRow , ... rows ] ; return markdownTable ( tableRows ) ; } param types param path export function generateAssignmentMarkdown ( types , path = "./assignments.md" ) { const strictTableMarkdown = generateAssignmentTableMarkdown ( types , { strict : true } ) ; const nonStrictTableMarkdown = generateAssignmentTableMarkdown ( types , { strict : false } ) ; return `# Assignments 
This table illustrates which types can be assigned to each other.

Each cell shows if the assignment \`typeA = typeB\` is valid.

	## Assignments with strict options:
	${ strictTableMarkdown }
	
	## Assignments with non-strict options:
	${ nonStrictTableMarkdown }
	` ; } param types param path export function writeAssignmentMarkdown ( path = "./assignments.md" ) { const markdown = generateAssignmentMarkdown ( [ ... PRIMITIVE_TYPES , ... SPECIAL_TYPES , "{}" , "void" ] ) ; const absolutePath = join ( process . cwd ( ) , path ) ; console . log ( `Writing comparison table to ${ absolutePath }` ) ; writeFileSync ( absolutePath , markdown ) ; }