-
Notifications
You must be signed in to change notification settings - Fork 12
fix: JSDoc parameter parsing for deeply nested parameters #16
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
base: v0.0.2
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| expect(result.parameters?.[0].nested?.[2].defaultValue).toBe("true"); | ||
| }); | ||
|
|
||
| it("should parse deeply nested parameter properties (3+ levels)", () => { |
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.
I think we should add some test cases for edge cases like this to improve robustness. For example, what happens if the parent is declared after its children?
/**
* @param {Object} config.database.connection - Connection settings
* @param {string} config.database.connection.host - Database host
* @param {number} config.database.connection.port - Database port
* @param {string} config.database.name - Database name
* @param {Object} config.database - Database settings
* @param {Object} config - Configuration object
*/
export function configure(config: any): void {
// implementation
}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.
| private insertParamAtPath({ into, path, param }: { | ||
| into: ParameterData[]; | ||
| path: string[]; | ||
| param: ParameterData; | ||
| }): void { | ||
| const [currentSegment, ...remainingPath] = path; | ||
| if (currentSegment == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (isEmpty(remainingPath)) { | ||
| into.push({ ...param, name: currentSegment, nested: [] }); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| const existingChild = into.find((n) => n.name === currentSegment); | ||
| const child = existingChild ?? this.createPlaceholderObjectParam(currentSegment); | ||
| if (existingChild == null) { | ||
| into.push(child); | ||
| } | ||
|
|
||
| const nestedParam = { ...param, name: parts.slice(1).join(".") }; | ||
| parent.nested = parent.nested || []; | ||
| parent.nested.push(nestedParam); | ||
| this.insertParamAtPath({ | ||
| into: child.nested ?? [], | ||
| path: remainingPath, | ||
| param, | ||
| }); | ||
| } | ||
|
|
||
| private createPlaceholderObjectParam(name: string): ParameterData { | ||
| return { | ||
| name, | ||
| type: "Object", | ||
| description: "", | ||
| required: true, | ||
| defaultValue: undefined, | ||
| nested: [], | ||
| }; | ||
| } |
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.
I'm not sure this is the optimal way to write this method — it relies heavily on imperative code with mutations rather than declarative code that returns values. Also, looking at this call in isolation, it's hard to tell what the method actually does without seeing the implementation:
// Can we understand what this method does without the surrounding context?
this.insertParamAtPath({
into: child.nested ?? [],
path: remainingPath,
param,
});I think we can rewrite this to return a value instead, making it more declarative and predictable. Let me think about this for a moment.
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.
2e25ae6 to
211a2a6
Compare
bd5ad90 to
438196b
Compare
Changes
Fix JSDoc parser to correctly handle deeply nested parameter properties (3+ levels)
Example
AS-IS
TO-BE