Preserve parens with keyof, enforce parens with arrows with type params (#2503)

* Force parens around argument of simple arrow function if type annotation is present

* Preserve parens inside keyof operator
master
Lucas Azzola 2017-07-17 21:32:33 +10:00 committed by Christopher Chedeau
parent b3a4785cec
commit d62d1c5c56
8 changed files with 34 additions and 2 deletions

View File

@ -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", "!<rootDir>/node_modules/"],
coveragePathIgnorePatterns: [
"<rootDir>/src/doc-debug.js",
"<rootDir>/src/clean-ast.js",

View File

@ -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;
}

View File

@ -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 &&

View File

@ -379,3 +379,10 @@ const initializeSnapshotState = (
) => new SnapshotState(testFile, update, testPath, expand);
`;
exports[`type_params.js 1`] = `
<T>(a) => { }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<T>(a) => {};
`;

View File

@ -0,0 +1 @@
<T>(a) => { }

View File

@ -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;
`;

View File

@ -0,0 +1 @@
run_spec(__dirname, { parser: "typescript" });

View File

@ -0,0 +1,5 @@
type A = keyof (T | U);
type B = keyof (X & Y);
type C = keyof T | U;
type D = keyof X & Y;