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 operatormaster
parent
b3a4785cec
commit
d62d1c5c56
|
@ -8,7 +8,7 @@ module.exports = {
|
||||||
testRegex: "jsfmt\\.spec\\.js$|__tests__/.*\\.js$",
|
testRegex: "jsfmt\\.spec\\.js$|__tests__/.*\\.js$",
|
||||||
testPathIgnorePatterns: ["tests/new_react", "tests/more_react"],
|
testPathIgnorePatterns: ["tests/new_react", "tests/more_react"],
|
||||||
collectCoverage: ENABLE_COVERAGE,
|
collectCoverage: ENABLE_COVERAGE,
|
||||||
collectCoverageFrom: ["src/**/*.js", "index.js"],
|
collectCoverageFrom: ["src/**/*.js", "index.js", "!<rootDir>/node_modules/"],
|
||||||
coveragePathIgnorePatterns: [
|
coveragePathIgnorePatterns: [
|
||||||
"<rootDir>/src/doc-debug.js",
|
"<rootDir>/src/doc-debug.js",
|
||||||
"<rootDir>/src/clean-ast.js",
|
"<rootDir>/src/clean-ast.js",
|
||||||
|
|
|
@ -346,13 +346,15 @@ FastPath.prototype.needsParens = function(options) {
|
||||||
}
|
}
|
||||||
|
|
||||||
case "TSParenthesizedType": {
|
case "TSParenthesizedType": {
|
||||||
|
const grandParent = this.getParentNode(1);
|
||||||
if (
|
if (
|
||||||
(parent.type === "TypeParameter" ||
|
(parent.type === "TypeParameter" ||
|
||||||
parent.type === "VariableDeclarator" ||
|
parent.type === "VariableDeclarator" ||
|
||||||
parent.type === "TypeAnnotation" ||
|
parent.type === "TypeAnnotation" ||
|
||||||
parent.type === "GenericTypeAnnotation") &&
|
parent.type === "GenericTypeAnnotation") &&
|
||||||
(node.typeAnnotation.type === "TypeAnnotation" &&
|
(node.typeAnnotation.type === "TypeAnnotation" &&
|
||||||
node.typeAnnotation.typeAnnotation.type !== "TSFunctionType")
|
node.typeAnnotation.typeAnnotation.type !== "TSFunctionType" &&
|
||||||
|
grandParent.type !== "TSTypeOperator")
|
||||||
) {
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -3126,6 +3126,7 @@ function canPrintParamsWithoutParens(node) {
|
||||||
return (
|
return (
|
||||||
node.params.length === 1 &&
|
node.params.length === 1 &&
|
||||||
!node.rest &&
|
!node.rest &&
|
||||||
|
!node.typeParameters &&
|
||||||
node.params[0].type === "Identifier" &&
|
node.params[0].type === "Identifier" &&
|
||||||
!node.params[0].typeAnnotation &&
|
!node.params[0].typeAnnotation &&
|
||||||
!node.params[0].comments &&
|
!node.params[0].comments &&
|
||||||
|
|
|
@ -379,3 +379,10 @@ const initializeSnapshotState = (
|
||||||
) => new SnapshotState(testFile, update, testPath, expand);
|
) => new SnapshotState(testFile, update, testPath, expand);
|
||||||
|
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
exports[`type_params.js 1`] = `
|
||||||
|
<T>(a) => { }
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
<T>(a) => {};
|
||||||
|
|
||||||
|
`;
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
<T>(a) => { }
|
|
@ -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;
|
||||||
|
|
||||||
|
`;
|
|
@ -0,0 +1 @@
|
||||||
|
run_spec(__dirname, { parser: "typescript" });
|
|
@ -0,0 +1,5 @@
|
||||||
|
type A = keyof (T | U);
|
||||||
|
type B = keyof (X & Y);
|
||||||
|
type C = keyof T | U;
|
||||||
|
type D = keyof X & Y;
|
||||||
|
|
Loading…
Reference in New Issue