fix(typescript): preserve necessary parens for TSOptionalType (#5056)

master
Ika 2018-09-06 09:04:37 +08:00 committed by GitHub
parent ff4ef426e6
commit 461e6035c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View File

@ -317,7 +317,8 @@ function needsParens(path, options) {
parent.type === "TSTypeReference") &&
(node.typeAnnotation.type === "TSTypeAnnotation" &&
node.typeAnnotation.typeAnnotation.type !== "TSFunctionType" &&
grandParent.type !== "TSTypeOperator")
grandParent.type !== "TSTypeOperator" &&
grandParent.type !== "TSOptionalType")
) {
return false;
}

View File

@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`complex.ts - typescript-verify 1`] = `
type T = [("a" | "b")?];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type T = [("a" | "b")?];
`;
exports[`simple.ts - typescript-verify 1`] = `
type X = [number, string?];
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -0,0 +1 @@
type T = [("a" | "b")?];