Repository navigation
feat: add enum support to Explorer - #606
Open
rasadregmi wants to merge 1 commit into
Open
rasadregmi wants to merge 1 commit into
rasadregmi wants to merge 1 commit into
Conversation
Closes freeCodeCamp#605 `Explorer` could not see TypeScript enums at all, so curriculum tests had to fall back on regular expressions. Those regexes reject valid code -- for example the Digital Pet Game lab asserts /\s*enum\s+PetMood\s*{[\s*\w+,\s*]*HAPPY[\s*\w+,\s*]*} which matches `HAPPY,` but fails on `HAPPY = 'HAPPY',` because the character class excludes `=` and quotes. Adds the same treatment the other node kinds already receive: - `findMembers` returns `EnumDeclaration.members`, so `getAll()` can reach enum members - `enums` maps enum declarations by name, mirroring `classes`/`interfaces` - `enumMembers` maps members of a single enum, mirroring `methods` - `value` returns an enum member's initializer, or an empty `Explorer` - a new `enumMember` parse context so `matches()` works The parse context is required, not incidental: an `EnumMember` is neither a `TypeNode` nor an `Expression`, so it falls through every existing `CONTEXT_GUARDS` entry to `"source"` and gets compared against an `ExpressionStatement`, which never matches. Regular, `const` and exported enums are all `EnumDeclaration` statements, so all three are found. Members are keyed with `name.getText()` rather than the `(name as Identifier).text` cast used elsewhere, because `ComputedPropertyName` has no `text` property and would collapse onto a single bogus `undefined` key.
Contributor
|
the issue is not open for contributors, please look at issues with the help wanted label at this time we will not be able to review your PR |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist:
Update index.md)Closes #605
This PR adds enum support to
Explorer, which currently has no way to see TypeScript enums at all.Because of that, curriculum tests have to assert enums with regular expressions. A concrete example is the Digital Pet Game lab:
https://github.com/freeCodeCamp/freeCodeCamp/blob/main/curriculum/challenges/english/blocks/lab-digital-pet-game/68c362b379059c388d3874f2.md
Eight assertions in that lab (lines 360-402) each check one member like this:
The character class
[\s*\w+,\s*]only allows word characters, commas and*, so this passes forHAPPY,but rejects the equally validHAPPY = 'HAPPY',— the=, the quotes and the spaces around them all fall outside the class.With enum support the same check becomes a single assertion:
Why this is better in practice:
HAPPY = 'HAPPY'valueexposes the initializer, soenumMembers.HAPPY.value.matches("'HAPPY'")worksImplementation notes:
findMembersnow returnsEnumDeclaration.members; its return type is widened to includeEnumMember, which does not extendClassElementenumsandenumMembersaccessors, mirroringclassesandmethodsvaluereturns an enum member's initializer, or an emptyExplorerenumMemberparse context somatches()worksThat last one is required rather than incidental. An
EnumMemberis neither aTypeNodenor anExpression, so it falls through every existingCONTEXT_GUARDSentry to"source", where it gets compared against anExpressionStatement— which never matches.Regular,
constand exported enums are allEnumDeclarationstatements, so all three are found. Members are keyed withname.getText()rather than the(name as Identifier).textcast used elsewhere in the file, becauseComputedPropertyNamehas notextproperty and would collapse every computed member onto a single bogusundefinedkey.Tests cover regular/
const/exported enums, initialized and uninitialized members, string, numeric and computed initializers, scope isolation, non-identifier member names, and the exactHAPPY = 'HAPPY'case the lab regex rejects. The new tests were confirmed to fail before the fix and pass after it.pnpm test:unit(344 tests) andpnpm test:python(137 tests) pass, along withpnpm lintandpnpm build.On documentation: I documented the new accessors using the same inline comment convention every other accessor in
explorer.tsfollows. I did not add a docs page becauseExplorerdoesn't have one today —docs/has noExplorerreferences at all anddocs/javascript.mdis a 3-line stub. Happy to add one if you'd prefer that, though it would mean a new file plus aSUMMARY.mdnav entry.