sanctuary-js / sanctuary-def

Run-time type system for JavaScript
MIT License
294 stars 23 forks source link

simplify code for formatting type signatures #264

Closed davidchambers closed 5 years ago

davidchambers commented 5 years ago

Prior to this change, parameterized types would be formatted with enclosing parens. $.Array (a), for example, would be formatted as '(Array a)'. In many contexts these parens could safely be stripped, but doing so required convoluted conditional logic:

return unless (
  t.type === FUNCTION || t.type === RECORD || isEmpty (t.keys),
  stripOutermostParens,
  ...
);
return unless (t.type === RECORD ||
                 isEmpty (t.keys) ||
                 t.type === FUNCTION && isEmpty (propPath) ||
                 !(isEmpty (propPath)),
               stripOutermostParens,
               ...);
var repr = show (type);
var parenthesized = repr.slice (0, 1) + repr.slice (-1) === '()';
return formatType4 (function(s) {
  return parenthesized && repr !== '()' && s.length === repr.length ?
    _ ('(') + r ('^') (s.slice (1, -1)) + _ (')') :
    r ('^') (s);
});

This pull request redefines a responsibility of parameterized types in a small but impactful way: rather than stripping parens if possible, parameterized types now wrap inner types in parens if necessary.