Skip to content

Conversation

@yanxue-22
Copy link
Contributor

@yanxue-22 yanxue-22 commented Oct 29, 2025

Ticket: DX-2214

This PR attempts to validate this following fix, which resolved an issue introduced by this Regression Commit.

Original Problem

The system encountered an error when parsing certain Arrow Functions with parameters (e.g., (codec) => t.array(codec)), specifically: Error: Error when parsing AdminUpdateShardKeyApiSpec: Unknown identifier codec.

What is parseFunctionBody

parseFunctionBody extracts and parses the body of an arrow function:

  • For expression bodies like () => t.string, it parses the expression t.string
  • For block bodies like () => { return t.string; }, it finds the return statement and parses its argument

The problem is that when parsing the body, any function parameters (like codec) are not in the symbol table (codecIdentifier has not been called yet)

The Fix

We only try inlining for arrow functions without parameters (that would have custom codecs).

Testing

  1. With custom codec defined: Verifies that arrayFromArrayOrSingle(NonEmptyString) correctly resolves to { type: 'array', items: {...} } using the custom codec
  2. Without custom codec: Verifies that the parser returns a ref instead of throwing "Unknown identifier codec"

@yanxue-22 yanxue-22 changed the title test: add comprehensive tests for arrow function fallback behavior test: add tests for arrow function fallback behavior Oct 29, 2025
@yanxue-22 yanxue-22 force-pushed the DX-2214-Test-Fix-For-Unknown-identifer-codec branch 6 times, most recently from bf9c320 to 465569a Compare November 4, 2025 01:07
@yanxue-22 yanxue-22 marked this pull request as ready for review November 4, 2025 01:14
@yanxue-22 yanxue-22 requested a review from a team as a code owner November 4, 2025 01:14
@starfy84 starfy84 requested a review from Copilot November 11, 2025 15:34
Copilot finished reviewing on behalf of starfy84 November 11, 2025 15:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a test to validate the fix for arrow function parameter parsing fallback behavior. The test ensures that when the parser fails to inline arrow function bodies with parameters, it correctly falls back to using predefined custom codec definitions instead of throwing an "Unknown identifier" error.

Key changes:

  • Adds a new test case that validates the parser's fallback mechanism for arrow functions with parameters
  • Test creates a realistic scenario with arrayFromArrayOrSingle(NonEmptyString) to verify the fallback works correctly

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@starfy84 starfy84 left a comment

Choose a reason for hiding this comment

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

@yanxue-22 I think copilot's suggestions broke your PR 😆

The suggestions are correct though. Using required doesn't give TestProject the correct types (it gets typed as any).

The test looks good! But I was wondering if the original fix was correct in the first place 🤔 . Or maybe there's a flaw with the original implementation. The Why the Fix Works section isn't really convincing me too much as to why exactly we need the fallback in the first place. We can go over this during collab!

Comment on lines 39 to 44
const knownImports = {
'.': {
arrayFromArrayOrSingle: (_: any, innerSchema: any) =>
E.right({ type: 'array', items: innerSchema }),
},
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like the type of knownImports doesn't align with the KnownImports type definition

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Not entirely confident either now that you point it out - will join collab hours tomorrow!

@yanxue-22 yanxue-22 marked this pull request as draft November 18, 2025 08:27
@yanxue-22 yanxue-22 force-pushed the DX-2214-Test-Fix-For-Unknown-identifer-codec branch 3 times, most recently from 831c67f to 9fcd9b9 Compare December 1, 2025 01:47
@yanxue-22 yanxue-22 marked this pull request as ready for review December 1, 2025 18:37
@yanxue-22 yanxue-22 requested a review from starfy84 December 1, 2025 22:45
Check if parseFunctionBody() succeeded before returning, allowing parser
to use custom codec definitions when function contains parameter references.

Ticket: DX-2418
@yanxue-22 yanxue-22 force-pushed the DX-2214-Test-Fix-For-Unknown-identifer-codec branch from 9fcd9b9 to 08ab81b Compare December 2, 2025 02:15
Comment on lines +513 to +515
const hasNoParameters = calleeInit.params.length === 0;
if (hasNoParameters) {
return parseFunctionBody(project, calleeSourceFile, calleeInit);
Copy link
Contributor

Choose a reason for hiding this comment

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

If I'm understanding this correctly; parseFunctionBody fails on functions with parameters. The fix is to only parse the function body if it has no parameters.

My question:

Is the fact that parseFunctionBody fails on arrow functions with parameters a flaw with the way we defined parseFunctionBody? Or is this intended behaviour?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

f I'm understanding this correctly; parseFunctionBody fails on functions with parameters. The fix is to only parse the function body if it has no parameters.

Correct

Copy link
Contributor Author

@yanxue-22 yanxue-22 Dec 3, 2025

Choose a reason for hiding this comment

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

Is the fact that parseFunctionBody fails on arrow functions with parameters a flaw with the way we defined parseFunctionBody? Or is this intended behaviour?

I think it is intended that parseFunctionBody only handles zero-parameter functions, since that was the case that surfaced the need for function. Arrow functions with parameters are left to be handled via custom codec definitions in knownImports, which I believe manually defines the schema transformation.

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.

3 participants