Skip to content

Commit

Permalink
Implement printer cases for TS{Conditional,Infer}Type.
Browse files Browse the repository at this point in the history
Although (infer T) must frequently be parenthesized to avoid ambiguity,
Recast does not have to encode that logic into FastPath#needsParens,
because Babylon wraps such types with TSParenthesizedType. Cool!

babel/babel#7404
benjamn/ast-types@da0367b
microsoft/TypeScript#21316
microsoft/TypeScript#21496
  • Loading branch information
benjamn committed Feb 24, 2018
1 parent 6cf40b4 commit eb043a4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1778,6 +1778,27 @@ function genericPrintNoParens(path, options, print) {
case "TSIntersectionType":
return fromString(" & ").join(path.map(print, "types"));

case "TSConditionalType":
parts.push(
path.call(print, "checkType"),
" extends ",
path.call(print, "extendsType"),
" ? ",
path.call(print, "trueType"),
" : ",
path.call(print, "falseType")
);

return concat(parts);

case "TSInferType":
parts.push(
"infer ",
path.call(print, "typeParameter")
);

return concat(parts);

case "TSParenthesizedType":
return concat([
"(",
Expand Down

1 comment on commit eb043a4

@benjamn
Copy link
Owner Author

Choose a reason for hiding this comment

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

In hindsight, what I said about TSParenthesizedType was not really the good news I imagined, since obviously Recast will sometimes need to add parentheses when printing types like TSInferType, if not adding them would cause the code to parse incorrectly/differently. But at least TSParenthesizedType provides an AST-level tool that TypeScript developers can use to add parentheses explicitly—even/especially when they are “unnecessary” and only serve to signal the programmer’s intent.

Please sign in to comment.