Rewrite the handling of optional arguments in getJSToNativeConversionTemplate.

This moves all handling of `isOptional` (except `type.isCallback()`, which I believe @jdm is rewriting in #2204) into one place.