-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[codex] Cache list indentation regexes #3969
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: master
Are you sure you want to change the base?
Changes from all 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 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,19 @@ | ||||||||||
| const noopTest = { exec: () => null } as unknown as RegExp; | ||||||||||
|
|
||||||||||
| function cachedIndentRegex(createRegex: (indent: number) => RegExp) { | ||||||||||
| const cache: RegExp[] = []; | ||||||||||
| return (indent: number) => { | ||||||||||
| const cappedIndent = Math.min(3, indent - 1); | ||||||||||
| const cacheIndex = cappedIndent + 1; | ||||||||||
|
Comment on lines
+6
to
+7
Contributor
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. The current calculation of While the current tokenizer implementation might ensure
Suggested change
|
||||||||||
| let regex = cache[cacheIndex]; | ||||||||||
| if (!regex) { | ||||||||||
| regex = createRegex(cappedIndent); | ||||||||||
| cache[cacheIndex] = regex; | ||||||||||
| } | ||||||||||
| return regex; | ||||||||||
| }; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| function edit(regex: string | RegExp, opt = '') { | ||||||||||
| let source = typeof regex === 'string' ? regex : regex.source; | ||||||||||
| const obj = { | ||||||||||
|
|
@@ -78,12 +92,12 @@ export const other = { | |||||||||
| notSpaceStart: /^\S*/, | ||||||||||
| endingNewline: /\n$/, | ||||||||||
| listItemRegex: (bull: string) => new RegExp(`^( {0,3}${bull})((?:[\t ][^\\n]*)?(?:\\n|$))`), | ||||||||||
| nextBulletRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`), | ||||||||||
| hrRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`), | ||||||||||
| fencesBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}(?:\`\`\`|~~~)`), | ||||||||||
| headingBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}#`), | ||||||||||
| htmlBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}<(?:[a-z].*>|!--)`, 'i'), | ||||||||||
| blockquoteBeginRegex: (indent: number) => new RegExp(`^ {0,${Math.min(3, indent - 1)}}>`), | ||||||||||
| nextBulletRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}(?:[*+-]|\\d{1,9}[.)])((?:[ \t][^\\n]*)?(?:\\n|$))`)), | ||||||||||
| hrRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}((?:- *){3,}|(?:_ *){3,}|(?:\\* *){3,})(?:\\n+|$)`)), | ||||||||||
| fencesBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}(?:\`\`\`|~~~)`)), | ||||||||||
| headingBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}#`)), | ||||||||||
| htmlBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}<(?:[a-z].*>|!--)`, 'i')), | ||||||||||
| blockquoteBeginRegex: cachedIndentRegex((indent: number) => new RegExp(`^ {0,${indent}}>`)), | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| /** | ||||||||||
|
|
||||||||||
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.
While
indentis expected to be at least 1 in the current implementation ofTokenizer.ts, calculatingcappedIndentasMath.min(3, indent - 1)can result in a negative value ifindentis 0. This would causecreateRegex(-1)to be called, which leads to aSyntaxErrorwhen constructing theRegExp(e.g.,new RegExp("^ {0,-1}")).Additionally, using
Math.max(0, ...)ensures thatcacheIndexis always a non-negative integer, making the array access more predictable.