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

Fixed a printer crash caused by empty parameter modifiers #60537

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

fixes crash reported here: #60527 (comment)

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Nov 19, 2024
@typescript-bot
Copy link
Collaborator

This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise.

@@ -1114,7 +1114,7 @@ export function createSyntacticTypeNodeBuilder(
function ensureParameter(p: ParameterDeclaration, context: SyntacticTypeNodeBuilderContext) {
return factory.updateParameterDeclaration(
p,
[],
p.modifiers,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The crash was caused by getPos rying to read .__pos that is meant to be added by onBeforeEmitNodeArray (called by emitDecoratorsAndModifiers, called by emitParameter). But since this list was empty it just bailed on it:

function emitDecoratorsAndModifiers(node: Node, modifiers: NodeArray<ModifierLike> | undefined, allowDecorators: boolean) {
if (modifiers?.length) {

This fix tries to reuse existing p.modifiers (it feels like ensureParameter shouldn't try to los information). Since the parameters in question don't have any modifiers an undefined gets "reused". So then the printer doesn't crash on lacking .__pos because it doesn't even have see the related node array as it wasn't created in the first place.

This, of course, means that this can still crash, in a similar way, on empty modifiers if they are ever returned.

I think it would be best to keep the current fix (it avoids allocating a redundant empty array) but also call onBeforeEmitNodeArray on empty modifiers in emitDecoratorsAndModifiers. I glanced through the whole emitter.ts and it seems like it's the only function that avoids it. Do you think there is some extra test case to be written that would prove it needs to be done there?

This, of course, means that any similar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

2 participants