Skip to content

feat: add a helper to find if a function has a func/method call #302

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Dario-DC
Copy link
Contributor

@Dario-DC Dario-DC commented Apr 28, 2025

Checklist:

Closes #XXXXX

The main purpose of this helper would be to verify that specific built-in functions or methods (such as .sort() or sorted()) are not used by campers to solve DSA labs.

Not sure if we need it, but you can either choose to check that the method/function spam is called anywhere within a specific function foo with:

_Node(_code).block_has_call(spam, foo)

or verify that spam is called anywhere within the user's code:

_Node(_code).block_has_call(spam)

@Dario-DC Dario-DC requested a review from a team as a code owner April 28, 2025 18:57

self.assertTrue(node.func_has_call("spam", "sort"))
self.assertTrue(node.func_has_call("eggs", "get"))
self.assertFalse(node.func_has_call("srt", "sorted"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be confused by the description. Shouldn't this be caught as using sorted function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because it can search only within a function but sorted(...) is assigned to the variable srt (not function) in the global scope. I forgot to mention that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that makes sense. I'd add then another test for a case when sorted is used inside of function, to better show the difference, and have one test checking for function call (instead of just methods).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that would still be cheating with extra steps... would it be possible to prohibit that to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want that I can simply search the entire code instead of a function. What about something like this?

node.block_has_call('sorted', 'function') # finds sorted within function
node.block_has_call('sorted') # finds sorted in the entire code

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would that find also reassignments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should find any occurrences of the method/function being called.

@ilenia-magoni
Copy link

we should be able to test this with freeCodeCamp/freeCodeCamp#60285

@Dario-DC
Copy link
Contributor Author

we should be able to test this with freeCodeCamp/freeCodeCamp#60285

I tried a few times but it doesn't work for me

@ojeytonwilliams
Copy link
Contributor

ojeytonwilliams commented May 12, 2025

What's failing? I've tried

# in https://github.com/freeCodeCamp/freeCodeCamp/pull/60285
cd tools/client-plugins/browser-scripts/
pnpm link ../../../../curriculum-helpers/
pnpm build
cd ../../../
pnpm test:curriculum

and that seems fine. Obviously it doesn't use the new helpers, but there weren't any issues building or testing.

@Dario-DC
Copy link
Contributor Author

Dario-DC commented May 12, 2025

Even after pnpm link it keeps saying that Node does not have a block_has_call attribute. Probably my fault (?) freeCodeCamp/freeCodeCamp#60298

@ojeytonwilliams
Copy link
Contributor

Hey @Dario-DC these are the steps I took to get things working

  1. check out feat: add quicksort lab freeCodeCamp#60298 and run git rebase main (otherwise the pnpm versions don't match)
  2. check out this PR and pnpm build
  3. go to the main repo and cd tools/client-plugins/browser-scripts/
  4. pnpm link ../../../../curriculum-helpers/
  5. pnpm build
  6. go back to the root of the main repo and FCC_CHALLENGE_ID=681dbcca65bbda5f286dc0ca pnpm run test:curriculum

Copy link

@ilenia-magoni ilenia-magoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally managed to test, thanks @ojeytonwilliams

it also manages to catch this so great
image

LGTM

@Dario-DC
Copy link
Contributor Author

Thanks a lot @ojeytonwilliams 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants