-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
feat: add helper for matching function strings #144
feat: add helper for matching function strings #144
Conversation
c7c5ff6
to
129fa1c
Compare
Can we add an option to not look for the closing bracket? That is, I would want to be able to test: const naomi = (love) => {
return love ** 2
} With something like const regex = new RexExp(`${__helpers.functionRegex("naomi", ["love"], {closed: false})}\s*return\s*love\s*\*\*\s*2)`);
assert.match(code, regex); |
Sure! Just to check, do you want something that matches everything up to and including the opening bracket (if there is one)? e.g. let codeOne = `const naomi = (love) => {
return love ** 2
}`
let codeTwo = `function naomi(love) {
return love ** 3
}`
let trickyArrowCode = `const naomi = love => love**2`
let startRE = functionRegex("naomi", ["love"], { closed: false });
startRE.test(codeOne); // true
startRE.test(codeTwo): // true
let fullRE = new RegExp(startRE.source + /\s*return\s*love\s*\*\*\s*2}/.source)
fullRE.test(codeOne); // true
fullRE.test(codeTwo); // false
let weirdRE = new RegExp(startRE.source + /\s*return\s*love/.source)
weirdRE.test(codeOne); // true
weirdRE.test(codeTwo); // true
let arrowRE = new RegExp(startRE.source + /\s*love\*\*2/.source)
arrowRE.test(trickyArrowCode); // true |
@naomi-lgbt I'm revisiting this, are those specs more or less what you wanted? |
Yes that looks great! Thanks! |
RegEx was awkwardly neither RegExp nor Regex
It's just a regex, there's no point testing how people might use it.
bf4cb9d
to
f3a5352
Compare
Hey @naomi-lgbt I think it's worked as intended now. There's still the ugly case where it captures too much (described in the docs), but I can't think of a way around that just using regexes. At least not in the time I allocated myself for this! It should work well enough for our uses and tide us over until we can incorporate the ast-based tooling Shaun created. |
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.
This should be fine as is
Checklist:
Update index.md
)TODO
The docs should explain this pretty well, but the gist is: this function provides a regex to match function expressions/declarations and arrow functions.