Skip to content

Create consistency with vanilla TS on canEmitSimpleArrowHead #878

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

Closed

Conversation

maschwenk
Copy link
Contributor

@maschwenk maschwenk commented May 18, 2025

I'm doing some regression testing against Typescript Go for emitted javascript, I'm noticing little nuances that are creating differences in emitted output. This is one of those cases.

The logic for canEmitSimpleArrowHead is slightly different where Typescript 5.8 considers whether the parent has modifiers (like async) whereas the Go implementation did not

https://github.com/microsoft/TypeScript/blob/83dc0bb2ed91fe0815ab28dc3ff95fae7425e413/src/compiler/emitter.ts#L4597-L4611

function canEmitSimpleArrowHead(parentNode: FunctionTypeNode | ConstructorTypeNode | ArrowFunction, parameters: NodeArray<ParameterDeclaration>) {
        const parameter = singleOrUndefined(parameters);
        return parameter
            && parameter.pos === parentNode.pos // may not have parsed tokens between parent and parameter
            && isArrowFunction(parentNode) // only arrow functions may have simple arrow head
            && !parentNode.type // arrow function may not have return type annotation
            && !some(parentNode.modifiers) // parent may not have decorators or modifiers
            && !some(parentNode.typeParameters) // parent may not have type parameters
            && !some(parameter.modifiers) // parameter may not have decorators or modifiers
            && !parameter.dotDotDotToken // parameter may not be rest
            && !parameter.questionToken // parameter may not be optional
            && !parameter.type // parameter may not have a type annotation
            && !parameter.initializer // parameter may not have an initializer
            && isIdentifier(parameter.name); // parameter name must be identifier
    }

@Copilot Copilot AI review requested due to automatic review settings May 18, 2025 15:29
Copy link
Contributor

@Copilot 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 updates the logic for canEmitSimpleArrowHead in the printer to align with TypeScript 5.8 behavior by explicitly checking for the presence of modifiers on both the parent and the parameter.

  • Introduces explicit nil checks for parent and parameter modifiers
  • Reorders validation checks to improve consistency with the vanilla TypeScript implementation

@maschwenk
Copy link
Contributor Author

Exact output consistency has been communicated as non goal (which makes sense), so closing

@maschwenk maschwenk closed this May 30, 2025
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

1 participant