|
| 1 | +import * as fs from "fs"; |
| 2 | +import jsYaml from "js-yaml"; |
| 3 | +import { fromMarkdown } from "mdast-util-from-markdown"; |
| 4 | +import { frontmatterFromMarkdown } from "mdast-util-frontmatter"; |
| 5 | +import { frontmatter } from "micromark-extension-frontmatter"; |
| 6 | +import ts from "typescript"; |
| 7 | +import type * as unist from "unist"; |
| 8 | + |
| 9 | +// commandLineParser.ts maps option names -> current (old) diagnostic |
| 10 | +// names. Lookup new descriptions in website Markdown and rewrite |
| 11 | +// diagnostic names accordingly. |
| 12 | +const commandLineParserFileName = |
| 13 | + "TypeScript/src/compiler/commandLineParser.ts"; |
| 14 | +const commandLineParserText = String( |
| 15 | + fs.readFileSync(commandLineParserFileName) |
| 16 | +); |
| 17 | +const sourceFile = ts.createSourceFile( |
| 18 | + commandLineParserFileName, |
| 19 | + commandLineParserText, |
| 20 | + 0 |
| 21 | +); |
| 22 | +const newMessages: { [oldName: string]: string } = {}; |
| 23 | +ts.forEachChild(sourceFile, visit); |
| 24 | +fs.writeFileSync( |
| 25 | + commandLineParserFileName, |
| 26 | + commandLineParserText.replace( |
| 27 | + new RegExp(String.raw`\b(?:${Object.keys(newMessages).join("|")})\b`, "g"), |
| 28 | + (oldName) => convertPropertyName(newMessages[oldName]) |
| 29 | + ) |
| 30 | +); |
| 31 | + |
| 32 | +// Replace old messages with new |
| 33 | +Object.assign( |
| 34 | + newMessages, |
| 35 | + Object.fromEntries( |
| 36 | + Object.values(newMessages).map((newMessage) => [ |
| 37 | + convertPropertyName(newMessage), |
| 38 | + newMessage, |
| 39 | + ]) |
| 40 | + ) |
| 41 | +); |
| 42 | +const diagnosticMessagesFileName = |
| 43 | + "TypeScript/src/compiler/diagnosticMessages.json"; |
| 44 | +const diagnosticMessagesText = String( |
| 45 | + fs.readFileSync(diagnosticMessagesFileName) |
| 46 | +); |
| 47 | +const diagnosticMessages = JSON.parse(diagnosticMessagesText); |
| 48 | +const oldNames = new Set( |
| 49 | + Object.keys(diagnosticMessages).map((oldMessage) => |
| 50 | + convertPropertyName(oldMessage) |
| 51 | + ) |
| 52 | +); |
| 53 | +fs.writeFileSync( |
| 54 | + diagnosticMessagesFileName, |
| 55 | + ( |
| 56 | + JSON.stringify( |
| 57 | + Object.fromEntries( |
| 58 | + Object.entries(diagnosticMessages) |
| 59 | + .map(([oldMessage, data]) => { |
| 60 | + const oldName = convertPropertyName(oldMessage); |
| 61 | + const newMessage = newMessages[oldName]; |
| 62 | + const newName = newMessage && convertPropertyName(newMessage); |
| 63 | + // Careful not to insert duplicates |
| 64 | + return ( |
| 65 | + (newName === oldName || !oldNames.has(newName)) && [ |
| 66 | + newMessage || oldMessage, |
| 67 | + data, |
| 68 | + ] |
| 69 | + ); |
| 70 | + }) |
| 71 | + .filter( |
| 72 | + (entry): entry is Exclude<typeof entry, false> => entry as never |
| 73 | + ) |
| 74 | + ), |
| 75 | + undefined, |
| 76 | + 4 |
| 77 | + ) + "\n" |
| 78 | + ).replace(/\n/g, "\r\n") //.replaceAll("\n", "\r\n") |
| 79 | +); |
| 80 | + |
| 81 | +// Visit nodes like the following: |
| 82 | +// { |
| 83 | +// name: "optionName", |
| 84 | +// description: Diagnostics.Voila_la_description |
| 85 | +// } |
| 86 | +function visit(node: ts.Node) { |
| 87 | + if (!ts.isObjectLiteralExpression(node)) { |
| 88 | + ts.forEachChild(node, visit); |
| 89 | + return; |
| 90 | + } |
| 91 | + const name = getPropertyAssignment(node, "name"); |
| 92 | + if (!name || !ts.isStringLiteral(name.initializer)) return; |
| 93 | + const description = getPropertyAssignment(node, "description"); |
| 94 | + if ( |
| 95 | + !description || |
| 96 | + !ts.isPropertyAccessExpression(description.initializer) || |
| 97 | + !ts.isIdentifier(description.initializer.expression) || |
| 98 | + description.initializer.expression.text !== "Diagnostics" |
| 99 | + ) |
| 100 | + return; |
| 101 | + const optionName = name.initializer.text; |
| 102 | + const oldName = description.initializer.name.text; |
| 103 | + const newMessage = getDescription(optionName); |
| 104 | + if (!newMessage) return; |
| 105 | + newMessages[oldName] = newMessage; |
| 106 | +} |
| 107 | + |
| 108 | +function getPropertyAssignment(obj: ts.ObjectLiteralExpression, name: string) { |
| 109 | + return obj.properties.find( |
| 110 | + (property): property is ts.PropertyAssignment => |
| 111 | + ts.isPropertyAssignment(property) && |
| 112 | + ts.isIdentifier(property.name) && |
| 113 | + property.name.text === name |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +// Get new description from website Markdown |
| 118 | +function getDescription(optionName: string) { |
| 119 | + const fileName = new URL( |
| 120 | + `../copy/en/options/${optionName}.md`, |
| 121 | + import.meta.url |
| 122 | + ); |
| 123 | + if (!fs.existsSync(fileName)) return; |
| 124 | + const doc = fs.readFileSync(fileName); |
| 125 | + const optionTree = fromMarkdown(doc, { |
| 126 | + extensions: [frontmatter()], |
| 127 | + mdastExtensions: [frontmatterFromMarkdown()], |
| 128 | + }); |
| 129 | + // Get frontmatter |
| 130 | + const yaml = optionTree.children.find( |
| 131 | + (yaml): yaml is typeof yaml & { type: "yaml" } => yaml.type === "yaml" |
| 132 | + ); |
| 133 | + if (!yaml) return; |
| 134 | + const data = jsYaml.load(yaml.value) as { oneline: string }; |
| 135 | + // Strip Markdown |
| 136 | + const descriptionTree = fromMarkdown(data.oneline); |
| 137 | + return toString(descriptionTree); |
| 138 | +} |
| 139 | + |
| 140 | +function toString(node: Partial<unist.Parent>): string { |
| 141 | + return node.type === "inlineCode" |
| 142 | + ? `'${node.value}'` |
| 143 | + : (node.value as string) || |
| 144 | + node.children?.map((child) => toString(child)).join("") || |
| 145 | + ""; |
| 146 | +} |
| 147 | + |
| 148 | +// https://github.com/microsoft/TypeScript/blob/e00b5ecd406b3d299ca69ef6780cc22ef0ecef4a/scripts/processDiagnosticMessages.ts#L106-L124 |
| 149 | +function convertPropertyName(origName: string): string { |
| 150 | + let result = origName |
| 151 | + .split("") |
| 152 | + .map((char) => { |
| 153 | + if (char === "*") return "_Asterisk"; |
| 154 | + if (char === "/") return "_Slash"; |
| 155 | + if (char === ":") return "_Colon"; |
| 156 | + return /\w/.test(char) ? char : "_"; |
| 157 | + }) |
| 158 | + .join(""); |
| 159 | + |
| 160 | + // get rid of all multi-underscores |
| 161 | + result = result.replace(/_+/g, "_"); |
| 162 | + |
| 163 | + // remove any leading underscore, unless it is followed by a number. |
| 164 | + result = result.replace(/^_([^\d])/, "$1"); |
| 165 | + |
| 166 | + // get rid of all trailing underscores. |
| 167 | + result = result.replace(/_$/, ""); |
| 168 | + |
| 169 | + return result; |
| 170 | +} |
0 commit comments