Skip to content

Proposal: Add Closure Compiler Syntax Plugin #7661

@arv

Description

@arv

Choose one: Feature request

Input Code

// Closure Compiler input/output
/** @return {T} */
function f() {
   return /** @type {T} */ (someExpression);
}

The parentheses are important to Closure Compiler and it defines a Cast expression

https://github.com/google/closure-compiler/wiki/Types-in-the-Closure-Type-System#type-casts

https://github.com/google/closure-compiler/blob/79deb15554ae0903b21fa9dc94746a879ae8ef12/src/com/google/javascript/jscomp/parsing/IRFactory.java#L783-L788

Expected Behavior

Keep the parens on a paren expression after a JSDoc comment.

Current Behavior

The ParenExpression is "transformed" to an Expression. (Not really transformed but Babel does not output unnecessary parens).

Possible Solution

Introduce a syntax plugin that detects the case where we have a paren expression preceded by a JSDoc comment with @type in it and create a CcCastExpression instead. Have the generator for the CcCastExpression output the comment, parens and the expression.

Context

Cannot use Babel to do JSX transformation because we use Closure Compiler for type checking and minimization.

At this time I'm not suggesting adding any other Closure Compiler features. I was not planning on parsing the actual type in the JSDoc but future improvements to this plugin could do that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    outdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions