Skip to content

Commit a470b27

Browse files
avivkellerCopilot
andauthored
fix(regexp): re-use common patterns (#376)
Co-authored-by: Copilot <[email protected]>
1 parent 7149528 commit a470b27

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/utils/parser/constants.mjs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,44 @@ export const DOC_MDN_BASE_URL_JS_GLOBALS = `${DOC_MDN_BASE_URL_JS}Reference/Glob
1818
// These are regular expressions used to determine if a given Markdown heading
1919
// is a specific type of API Doc entry (e.g., Event, Class, Method, etc)
2020
// and to extract the inner content of said Heading to be used as the API doc entry name
21+
const CAMEL_CASE = '\\w+(?:\\.\\w+)*';
22+
const FUNCTION_CALL = '\\([^)]*\\)';
23+
24+
// Matches "bar":
25+
// Group 1: foo[bar]
26+
// Group 2: foo.bar
27+
const PROPERTY = `${CAMEL_CASE}(?:(\\[${CAMEL_CASE}\\])|\\.(\\w+))`;
28+
2129
export const DOC_API_HEADING_TYPES = [
2230
{
2331
type: 'method',
24-
regex:
25-
// Group 1: foo[bar]()
26-
// Group 2: foo.bar()
27-
// Group 3: foobar()
28-
/^`?(?:\w*(?:(\[[^\]]+\])|(?:\.(\w+)))|(\w+))\([^)]*\)`?$/i,
32+
regex: new RegExp(`^\`?${PROPERTY}${FUNCTION_CALL}\`?$`, 'i'),
2933
},
3034
{ type: 'event', regex: /^Event: +`?['"]?([^'"]+)['"]?`?$/i },
3135
{
3236
type: 'class',
33-
regex:
34-
/^Class: +`?([A-Z]\w+(?:\.[A-Z]\w+)*(?: +extends +[A-Z]\w+(?:\.[A-Z]\w+)*)?)`?$/i,
37+
regex: new RegExp(
38+
`Class: +\`?(${CAMEL_CASE}(?: extends +${CAMEL_CASE})?)\`?$`,
39+
'i'
40+
),
3541
},
3642
{
3743
type: 'ctor',
38-
regex: /^(?:Constructor: +)?`?new +([A-Z]\w+(?:\.[A-Z]\w+)*)\([^)]*\)`?$/i,
44+
regex: new RegExp(
45+
`^(?:Constructor: +)?\`?new +(${CAMEL_CASE})${FUNCTION_CALL}\`?$`,
46+
'i'
47+
),
3948
},
4049
{
4150
type: 'classMethod',
42-
regex:
43-
/^Static method: +`?[A-Z]\w+(?:\.[A-Z]\w+)*(?:(\[\w+\.\w+\])|\.(\w+))\([^)]*\)`?$/i,
51+
regex: new RegExp(
52+
`^Static method: +\`?${PROPERTY}${FUNCTION_CALL}\`?$`,
53+
'i'
54+
),
4455
},
4556
{
4657
type: 'property',
47-
regex:
48-
/^(?:Class property: +)?`?[A-Z]\w+(?:\.[A-Z]\w+)*(?:(\[\w+\.\w+\])|\.(\w+))`?$/i,
58+
regex: new RegExp(`^(?:Class property: +)?\`?${PROPERTY}\`?$`, 'i'),
4959
},
5060
];
5161

0 commit comments

Comments
 (0)