Skip to content
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

resolves #1719 add context and node_name accessor in the type definition #1718

Merged
merged 1 commit into from
Jan 6, 2024
Merged

resolves #1719 add context and node_name accessor in the type definition #1718

merged 1 commit into from
Jan 6, 2024

Conversation

RayOffiah
Copy link
Contributor

Hi there 👋🏾

In previous versions of the API, it was possible to change the context and name of a block/node by reassigning it in the source code.

block.context = block.node_name = newName

I noticed that the ability to do this has been removed from the API.

It isn't a function that is going to get used a lot, but some code I wrote a while back does rely on it.

This PR will restore the functionality, but uses proper set functions to change the values.

@ggrossetie
Copy link
Member

Hey! Could you please open an issue so we can discuss this change?

I noticed that the ability to do this has been removed from the API.

I don't recall removing this API 🤔

it was possible to change the context and name of a block/node by reassigning it in the source code.
It isn't a function that is going to get used a lot, but some code I wrote a while back does rely on it.

Could you please provide an example (in the issue) that rely on it?

@RayOffiah
Copy link
Contributor Author

Yep, no problem.

I'll work up an issue tomorrow.

Comment on lines 1104 to 1106
AbstractNode.prototype.setNodeName = function (nodeName) {
this.node_name = nodeName
}
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I'm going to warn you, we are really getting into some internal state here. The node_name variable has never been formalized and allowing it to be changed is really undefined. If anything, setting the context should set the node_name so it remains internal. But for an inline node, the node_name is "inline_" + the context.

Copy link
Member

Choose a reason for hiding this comment

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

I'm reluctant to add it to the "porcelain" API. I would be in favour of adding two fields in the TypeScript definition node_name and context.

That way block.context = block.node_name = newName won't raise a warning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mmm, yes. 🤔

It sounds like I'm skating very close to breaking stuff, now.

I have a vague memory of having to set the node name in the JavaScript API, but that wasn't needed for the Ruby API. I'll try a bit of test code to see if I can do this without fiddling with the node name.

Or perhaps I need to look at doing this a different way. Rather than alter the context, it would be safer to remove olist block and replace it with a colist built from scratch. (Would be more work though).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm reluctant to add it to the "porcelain" API. I would be in favour of adding two fields in the TypeScript definition node_name and context.

That way block.context = block.node_name = newName won't raise a warning.

They would be picked up by the underlying Ruby API if I just add two fields in the TypeScript file?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I think so.
As far as I recall, Opal is using un-prefixed fields for attr_reader.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, that seems to work 👍🏾

@mojavelinux
Copy link
Member

mojavelinux commented Jan 2, 2024 via email

@ggrossetie
Copy link
Member

@RayOffiah Looks good!

Could you please add a test in: https://github.com/asciidoctor/asciidoctor.js/blob/main/packages/core/types/tests.ts. It should verify that declaring node_name and context as fields is enough.

@mojavelinux
Copy link
Member

mojavelinux commented Jan 4, 2024

I still don't think node_name should be exposed. Instead, setContext should set context and node_name. In Ruby, context is a symbol and node_name is a string, but in JavaScript there are no symbols (of the Ruby variety), so these two fields can just be synced. There's no reason that context and node_name should be different. This was purely an internal optimization to split them.

See https://github.com/asciidoctor/asciidoctor/blob/8abd937672303fce561757f3f3d71cb52875ae71/lib/asciidoctor/abstract_node.rb#L36

@RayOffiah
Copy link
Contributor Author

@RayOffiah Looks good!

Could you please add a test in: https://github.com/asciidoctor/asciidoctor.js/blob/main/packages/core/types/tests.ts. It should verify that declaring node_name and context as fields is enough.

Okay, will do. 👍🏾

context and node_name now exposed as properties.
add setContext function to API
@ggrossetie
Copy link
Member

All good!
@mojavelinux I think we can add a setContext that sets both node_name and context at a later point in time.

@ggrossetie ggrossetie changed the title add setContext function to API resolves #1719 add context and node_name accessor in the type definition Jan 6, 2024
@ggrossetie ggrossetie merged commit afddcea into asciidoctor:main Jan 6, 2024
10 checks passed
@mojavelinux
Copy link
Member

I've made my case ;)

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.

None yet

3 participants