-
-
Notifications
You must be signed in to change notification settings - Fork 109
[codex] harden formatter regex replacements #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,13 @@ export function createErrorMessagePrettifier( | |
| const rules = await getRules(codeBlock); | ||
| let output = message; | ||
|
|
||
| for (const { pattern, replacer } of rules) { | ||
| for (const rule of rules) { | ||
| if ("replace" in rule) { | ||
| output = await rule.replace(output); | ||
| continue; | ||
| } | ||
|
|
||
| const { pattern, replacer } = rule; | ||
| let result = ""; | ||
| let lastIndex = 0; | ||
| for (const match of output.matchAll(pattern)) { | ||
|
|
@@ -30,10 +36,63 @@ export function createErrorMessagePrettifier( | |
| }; | ||
| } | ||
|
|
||
| type Rule = { | ||
| pattern: RegExp; | ||
| replacer: (...args: any[]) => string | Promise<string>; | ||
| }; | ||
| type Rule = | ||
| | { | ||
| pattern: RegExp; | ||
| replacer: (...args: any[]) => string | Promise<string>; | ||
| } | ||
| | { | ||
| replace: (message: string) => string | Promise<string>; | ||
| }; | ||
|
|
||
| async function replaceQuotedStringLiteralTypes( | ||
| output: string, | ||
| codeBlock: CodeBlockFn | ||
| ): Promise<string> { | ||
| let result = ""; | ||
| let lastIndex = 0; | ||
| let searchIndex = 0; | ||
|
|
||
| while (searchIndex < output.length) { | ||
| let startIndex = -1; | ||
| for (let i = searchIndex; i < output.length - 2; i++) { | ||
| if ( | ||
| /\s/.test(output.charAt(i)) && | ||
| output.charAt(i + 1) === "'" && | ||
| output.charAt(i + 2) === '"' | ||
| ) { | ||
| startIndex = i; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (startIndex === -1) break; | ||
|
|
||
| const contentStart = startIndex + 3; | ||
| let endIndex = contentStart; | ||
| while (true) { | ||
| endIndex = output.indexOf(`"'`, endIndex); | ||
| if (endIndex === -1) break; | ||
| if (output[endIndex - 1] !== "\\") break; | ||
| endIndex += 1; | ||
| } | ||
|
|
||
| if (endIndex === -1) break; | ||
|
|
||
| result += output.slice(lastIndex, startIndex); | ||
| result += await formatTypeBlock( | ||
| "", | ||
| `"${output.slice(contentStart, endIndex)}"`, | ||
| codeBlock | ||
| ); | ||
|
|
||
| const matchEnd = endIndex + 2; | ||
| lastIndex = matchEnd < output.length ? matchEnd + 1 : matchEnd; | ||
| searchIndex = lastIndex; | ||
| } | ||
|
|
||
| return result + output.slice(lastIndex); | ||
| } | ||
|
|
||
| async function getRules(codeBlock: CodeBlockFn): Promise<Rule[]> { | ||
| const formatTypeScriptBlock = (code: string) => codeBlock(code, "typescript"); | ||
|
|
@@ -51,17 +110,17 @@ async function getRules(codeBlock: CodeBlockFn): Promise<Rule[]> { | |
|
|
||
| return [ | ||
| { | ||
| pattern: /(?:\s)'"(.*?)(?<!\\)"'(?:\s|:|.|$)/g, | ||
| replacer: async (p1: string) => formatTypeBlock("", `"${p1}"`, codeBlock), | ||
| replace: (output: string) => | ||
| replaceQuotedStringLiteralTypes(output, codeBlock), | ||
| }, | ||
| { | ||
| pattern: /['“](declare module )['”](.*)['“];['”]/g, | ||
| pattern: /['“](declare module )['”]([^'“”\r\n]*)['“];['”]/g, | ||
| replacer: (p1: string, p2: string) => | ||
| formatTypeScriptBlock(`${p1} "${p2}"`), | ||
| }, | ||
| { | ||
| pattern: | ||
| /(is missing the following properties from type\s?)'(.*)': ((?:#?\w+, )*(?:(?!and)\w+)?)/g, | ||
| /(is missing the following properties from type\s?)'([^'\r\n]*)': ((?:#?\w+, )*(?:(?!and)\w+)?)/g, | ||
| replacer: async (pre: string, type: string, post: string) => { | ||
| const formattedType = await formatTypeBlock("", type, codeBlock); | ||
| const list = post | ||
|
|
@@ -73,7 +132,7 @@ async function getRules(codeBlock: CodeBlockFn): Promise<Rule[]> { | |
| }, | ||
| }, | ||
| { | ||
| pattern: /(types) ['“](.*?)['”] and ['“](.*?)['”][.]?/gi, | ||
| pattern: /(types) ['“]([^'“”\r\n]*)['”] and ['“]([^'“”\r\n]*)['”][.]?/gi, | ||
| replacer: async (p1: string, p2: string, p3: string) => { | ||
| const [left, right] = await Promise.all([ | ||
| formatTypeBlock(p1, p2, codeBlock), | ||
|
|
@@ -83,7 +142,8 @@ async function getRules(codeBlock: CodeBlockFn): Promise<Rule[]> { | |
| }, | ||
| }, | ||
| { | ||
| pattern: /type annotation must be ['“](.*?)['”] or ['“](.*?)['”][.]?/gi, | ||
| pattern: | ||
| /type annotation must be ['“]([^'“”\r\n]*)['”] or ['“]([^'“”\r\n]*)['”][.]?/gi, | ||
| replacer: async (p1: string, p2: string, p3: string | number) => { | ||
| if (typeof p3 === "string") { | ||
| const [left, right] = await Promise.all([ | ||
|
|
@@ -100,7 +160,7 @@ async function getRules(codeBlock: CodeBlockFn): Promise<Rule[]> { | |
| }, | ||
| }, | ||
| { | ||
| pattern: /(Overload \d of \d), ['“](.*?)['”], /gi, | ||
| pattern: /(Overload \d of \d), ['“]([^'“”\r\n]*)['”], /gi, | ||
| replacer: async (p1: string, p2: string) => | ||
| `${p1}${await formatTypeBlock("", p2, codeBlock)}`, | ||
| }, | ||
|
|
@@ -114,18 +174,18 @@ async function getRules(codeBlock: CodeBlockFn): Promise<Rule[]> { | |
| }, | ||
| { | ||
| pattern: | ||
| /(module|file|file name|imported via) ['"“](.*?)['"“](?=[\s(.|,]|$)/gi, | ||
| /(module|file|file name|imported via) ['"“]([^'"“”\r\n]*)['"“](?=[\s(.|,]|$)/gi, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This character class now forbids Useful? React with 👍 / 👎. |
||
| replacer: async (p1: string, p2: string) => | ||
| formatTypeBlock(p1, `"${p2}"`, codeBlock), | ||
| }, | ||
| { | ||
| pattern: | ||
| /(type|type alias|interface|module|file|file name|class|method's|subtype of constraint) ['“](.*?)['“](?=[\s(.|,)]|$)/gi, | ||
| /\b(type|type alias|interface|module|file|file name|class|method's|subtype of constraint) ['“]((?:[^'“\r\n]|['“](?![\s(.|,)]|$))*)['“](?=[\s(.|,)]|$)/gi, | ||
| replacer: (p1: string, p2: string) => formatTypeOrModuleBlock(p1, p2), | ||
| }, | ||
| { | ||
| pattern: | ||
| /['“]([^>]*)['”] (type|interface|return type|file|module|is (not )?assignable)/gi, | ||
| /['“]([^'“”>\r\n]*)['”] (type|interface|return type|file|module|is (not )?assignable)/gi, | ||
| replacer: async (p1: string, p2: string) => | ||
| `${await formatTypeOrModuleBlock("", p1)} ${p2}`, | ||
| }, | ||
|
|
@@ -136,16 +196,16 @@ async function getRules(codeBlock: CodeBlockFn): Promise<Rule[]> { | |
| }, | ||
| { | ||
| pattern: | ||
| /['“](import|export|require|in|continue|break|let|false|true|const|new|throw|await|for await|[0-9]+)( ?.*?)['”]/g, | ||
| /['“](import|export|require|in|continue|break|let|false|true|const|new|throw|await|for await|[0-9]+)( ?[^'“”\r\n]*)['”]/g, | ||
| replacer: (p1: string, p2: string) => formatTypeScriptBlock(`${p1}${p2}`), | ||
| }, | ||
| { | ||
| pattern: /(return|operator) ['“](.*?)['”]/gi, | ||
| pattern: /(return|operator) ['“]([^'“”\r\n]*)['”]/gi, | ||
| replacer: (p1: string, p2: string) => | ||
| `${p1} ${formatTypeScriptBlock(p2)}`, | ||
| }, | ||
| { | ||
| pattern: /(?<!\w)'((?:(?!["]).)*?)'(?!\w)/g, | ||
| pattern: /(?<!\w)'([^'"\r\n]*)'(?!\w)/g, | ||
| replacer: (p1: string) => ` ${codeBlock(p1)} `, | ||
| }, | ||
| ]; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new
replaceQuotedStringLiteralTypesscanner searches for the closing"'withindexOfand never stops at\n, so it now matches across line boundaries where the previous regex did not. In any multi-line diagnostic that contains'"on one line and"'later, this rule can consume unrelated lines into one replacement and also drop the next character viamatchEnd + 1, which corrupts the rendered message rather than just formatting one literal.Useful? React with 👍 / 👎.