diff --git a/jest.config.js b/jest.config.js index 043f5859..506ae3af 100644 --- a/jest.config.js +++ b/jest.config.js @@ -8,7 +8,7 @@ module.exports = { testRegex: "jsfmt\\.spec\\.js$|__tests__/.*\\.js$", testPathIgnorePatterns: ["tests/new_react", "tests/more_react"], collectCoverage: ENABLE_COVERAGE, - collectCoverageFrom: ["src/**/*.js", "index.js"], + collectCoverageFrom: ["src/**/*.js", "index.js", "!/node_modules/"], coveragePathIgnorePatterns: [ "/src/doc-debug.js", "/src/clean-ast.js", diff --git a/src/fast-path.js b/src/fast-path.js index fae6826d..2262cd8f 100644 --- a/src/fast-path.js +++ b/src/fast-path.js @@ -346,13 +346,15 @@ FastPath.prototype.needsParens = function(options) { } case "TSParenthesizedType": { + const grandParent = this.getParentNode(1); if ( (parent.type === "TypeParameter" || parent.type === "VariableDeclarator" || parent.type === "TypeAnnotation" || parent.type === "GenericTypeAnnotation") && (node.typeAnnotation.type === "TypeAnnotation" && - node.typeAnnotation.typeAnnotation.type !== "TSFunctionType") + node.typeAnnotation.typeAnnotation.type !== "TSFunctionType" && + grandParent.type !== "TSTypeOperator") ) { return false; } diff --git a/src/printer.js b/src/printer.js index 06281e80..3db182b2 100644 --- a/src/printer.js +++ b/src/printer.js @@ -3126,6 +3126,7 @@ function canPrintParamsWithoutParens(node) { return ( node.params.length === 1 && !node.rest && + !node.typeParameters && node.params[0].type === "Identifier" && !node.params[0].typeAnnotation && !node.params[0].comments && diff --git a/tests/arrows/__snapshots__/jsfmt.spec.js.snap b/tests/arrows/__snapshots__/jsfmt.spec.js.snap index ba9b9c32..4791bd03 100644 --- a/tests/arrows/__snapshots__/jsfmt.spec.js.snap +++ b/tests/arrows/__snapshots__/jsfmt.spec.js.snap @@ -379,3 +379,10 @@ const initializeSnapshotState = ( ) => new SnapshotState(testFile, update, testPath, expand); `; + +exports[`type_params.js 1`] = ` +(a) => { } +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +(a) => {}; + +`; diff --git a/tests/arrows/type_params.js b/tests/arrows/type_params.js new file mode 100644 index 00000000..fae332d3 --- /dev/null +++ b/tests/arrows/type_params.js @@ -0,0 +1 @@ +(a) => { } diff --git a/tests/typescript_keyof/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_keyof/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..00b2ed24 --- /dev/null +++ b/tests/typescript_keyof/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`keyof.ts 1`] = ` +type A = keyof (T | U); +type B = keyof (X & Y); +type C = keyof T | U; +type D = keyof X & Y; + +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +type A = keyof (T | U); +type B = keyof (X & Y); +type C = keyof T | U; +type D = keyof X & Y; + +`; diff --git a/tests/typescript_keyof/jsfmt.spec.js b/tests/typescript_keyof/jsfmt.spec.js new file mode 100644 index 00000000..bc085c48 --- /dev/null +++ b/tests/typescript_keyof/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" }); diff --git a/tests/typescript_keyof/keyof.ts b/tests/typescript_keyof/keyof.ts new file mode 100644 index 00000000..a96d901c --- /dev/null +++ b/tests/typescript_keyof/keyof.ts @@ -0,0 +1,5 @@ +type A = keyof (T | U); +type B = keyof (X & Y); +type C = keyof T | U; +type D = keyof X & Y; +