feat(nodes): add ConstantNode support with Anchor parsing#958
feat(nodes): add ConstantNode support with Anchor parsing#958tanmay4l wants to merge 1 commit intocodama-idl:mainfrom
Conversation
🦋 Changeset detectedLatest commit: caa2700 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Hi, thanks for the PR! This is looking pretty good. The only thing I'm unsure of is whether on not the On one hand, this would make the On the other hand, this make the What do you think? |
hmm interesting point about ConstantValueNode. I kept them separate because program constants need name field, which ConstantValueNode doesn't have. also constants[0].constant.value feels awkward. But I see your point about reuse. |
|
Hey sorry for the late reply, I was actually gonna review this properly this week. Are you still okay keeping this PR open? |
sure haha ( ill resolve conflict ) |
lorisleiva
left a comment
There was a problem hiding this comment.
Thanks for this PR and again sorry for the super later review.
I've made a few comments, lmk what you think.
I also think you're missing an important test file in the packages/visitors-core/test/nodes folder (e.g. look at the packages/visitors-core/test/nodes/DefinedTypeNode.test.ts file for reference).
P.S.: Don't forget to rebase.
| readonly type: TType; | ||
| readonly value: TValue; |
There was a problem hiding this comment.
Btw I've decided, I think it's better to keep the design as-is. 🙌
Wrapping a ConstantValueNode inside feels a bit dirty just for the sake of removing a little duplication.
| // so we use u8 to represent the type of each element | ||
| const type = | ||
| idl.type === 'bytes' | ||
| ? numberTypeNode('u8') |
There was a problem hiding this comment.
Are you sure this is correct? This represents a single u8 number. Should this not just be bytesTypeNode() since the parseConstantValue returns a bytesValueNode('base16', ...) anyway?
I also wonder if we should check the parsed value against the unparsed type to make sure they match together. Wdyt?
| // so we use u8 to represent the type of each element | ||
| const type = | ||
| idl.type === 'bytes' | ||
| ? numberTypeNode('u8') |
| value: '[116, 101, 115, 116]', // "test" in bytes | ||
| }); | ||
|
|
||
| expect(node).toEqual(constantNode('seedPrefix', numberTypeNode('u8'), bytesValueNode('base16', '74657374'))); |
There was a problem hiding this comment.
Yeah see this should actually just use the bytesTypeNode() type here to match bytesValueNode(...).
The BytesTypeNode basically just means, it's a type representing raw-bytes without any size constraints. BytesValueNode helps describing the value for these bytes.
| // Type should be parsed, value should be string | ||
| expect(node.name).toBe('appName'); | ||
| expect(node.value).toEqual(stringValueNode('MyApp')); |
| ### Numeric Constant | ||
|
|
||
| ```ts | ||
| const maxSize = constantNode('MAX_SIZE', numberTypeNode('u32'), numberValueNode(100)); |
There was a problem hiding this comment.
Let's stay consistent here with the casing so MAX_SIZE should be maxSize. Let's also use const node = to be consistent with the rest of the documentation.
| ```ts | ||
| const maxItems = constantNode('MAX_ITEMS', numberTypeNode('u64'), numberValueNode(1000)); | ||
| // Note: Add docs via the ConstantNode interface if needed | ||
| ``` |
There was a problem hiding this comment.
This example needs to use the 4th parameter to show how to document a constant.
| // Note: Add docs via the ConstantNode interface if needed | ||
| ``` | ||
|
|
||
| ## Parsing from Anchor IDL |
There was a problem hiding this comment.
Let's remove that section. We don't do that for any other node. Anchor parsing is bounded in its own package.
| outExtension() { | ||
| return { js: `.cjs` }; | ||
| }, | ||
| treeshake: false, |
There was a problem hiding this comment.
Let's remove this change unless this is a blocker for this PR.
| test('it preserves docs on constantNode', () => { | ||
| const node = constantNode('myConstant', numberTypeNode('u32'), numberValueNode(42), ['My docs']); | ||
| const result = visit(node, identityVisitor()); | ||
| assertIsNode(result, 'constantNode'); | ||
| expect(result.docs).toEqual(['My docs']); | ||
| }); |
There was a problem hiding this comment.
This is a bit too specific of a test for that file. I'd remove it altogether.
#69 cc @lorisleiva