|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { printComments, printDanglingComments } = require("../../main/comments"); |
| 4 | +const { |
| 5 | + builders: { concat, join, line, hardline, softline, group, indent, ifBreak }, |
| 6 | +} = require("../../document"); |
| 7 | +const { hasTrailingComment, hasTrailingLineComment } = require("../utils"); |
| 8 | +const { getTypeParametersGroupId } = require("./type-parameters"); |
| 9 | + |
| 10 | +function printClass(path, options, print) { |
| 11 | + const n = path.getValue(); |
| 12 | + const parts = []; |
| 13 | + |
| 14 | + if (n.abstract) { |
| 15 | + parts.push("abstract "); |
| 16 | + } |
| 17 | + |
| 18 | + parts.push("class"); |
| 19 | + |
| 20 | + // Keep old behaviour of extends in same line |
| 21 | + // If there is only on extends and there are not comments |
| 22 | + const groupMode = |
| 23 | + (n.id && hasTrailingComment(n.id)) || |
| 24 | + (n.superClass && |
| 25 | + n.superClass.comments && |
| 26 | + n.superClass.comments.length !== 0) || |
| 27 | + (n.extends && n.extends.length !== 0) || // DeclareClass |
| 28 | + (n.mixins && n.mixins.length !== 0) || |
| 29 | + (n.implements && n.implements.length !== 0); |
| 30 | + |
| 31 | + const partsGroup = []; |
| 32 | + const extendsParts = []; |
| 33 | + |
| 34 | + if (n.id) { |
| 35 | + partsGroup.push(" ", path.call(print, "id")); |
| 36 | + } |
| 37 | + |
| 38 | + partsGroup.push(path.call(print, "typeParameters")); |
| 39 | + |
| 40 | + if (n.superClass) { |
| 41 | + const printed = concat([ |
| 42 | + "extends ", |
| 43 | + printSuperClass(path, options, print), |
| 44 | + path.call(print, "superTypeParameters"), |
| 45 | + ]); |
| 46 | + const printedWithComments = path.call( |
| 47 | + (superClass) => printComments(superClass, () => printed, options), |
| 48 | + "superClass" |
| 49 | + ); |
| 50 | + if (groupMode) { |
| 51 | + extendsParts.push(line, group(printedWithComments)); |
| 52 | + } else { |
| 53 | + extendsParts.push(" ", printedWithComments); |
| 54 | + } |
| 55 | + } else { |
| 56 | + extendsParts.push(printList(path, options, print, "extends")); |
| 57 | + } |
| 58 | + |
| 59 | + extendsParts.push(printList(path, options, print, "mixins")); |
| 60 | + extendsParts.push(printList(path, options, print, "implements")); |
| 61 | + |
| 62 | + if (groupMode) { |
| 63 | + const printedExtends = concat(extendsParts); |
| 64 | + if (shouldIndentOnlyHeritageClauses(n)) { |
| 65 | + parts.push( |
| 66 | + group( |
| 67 | + concat( |
| 68 | + partsGroup.concat(ifBreak(indent(printedExtends), printedExtends)) |
| 69 | + ) |
| 70 | + ) |
| 71 | + ); |
| 72 | + } else { |
| 73 | + parts.push(group(indent(concat(partsGroup.concat(printedExtends))))); |
| 74 | + } |
| 75 | + } else { |
| 76 | + parts.push(...partsGroup, ...extendsParts); |
| 77 | + } |
| 78 | + |
| 79 | + parts.push(" ", path.call(print, "body")); |
| 80 | + |
| 81 | + return concat(parts); |
| 82 | +} |
| 83 | + |
| 84 | +function hasMultipleHeritage(node) { |
| 85 | + return ( |
| 86 | + ["superClass", "extends", "mixins", "implements"].filter( |
| 87 | + (key) => !!node[key] |
| 88 | + ).length > 1 |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +function shouldIndentOnlyHeritageClauses(node) { |
| 93 | + return ( |
| 94 | + node.typeParameters && |
| 95 | + !hasTrailingLineComment(node.typeParameters) && |
| 96 | + !hasMultipleHeritage(node) |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +function printList(path, options, print, listName) { |
| 101 | + const n = path.getValue(); |
| 102 | + if (!n[listName] || n[listName].length === 0) { |
| 103 | + return ""; |
| 104 | + } |
| 105 | + |
| 106 | + const printedLeadingComments = printDanglingComments( |
| 107 | + path, |
| 108 | + options, |
| 109 | + /* sameIndent */ true, |
| 110 | + ({ marker }) => marker === listName |
| 111 | + ); |
| 112 | + return concat([ |
| 113 | + shouldIndentOnlyHeritageClauses(n) |
| 114 | + ? ifBreak(" ", line, { |
| 115 | + groupId: getTypeParametersGroupId(n.typeParameters), |
| 116 | + }) |
| 117 | + : line, |
| 118 | + printedLeadingComments, |
| 119 | + printedLeadingComments && hardline, |
| 120 | + listName, |
| 121 | + group( |
| 122 | + indent( |
| 123 | + concat([line, join(concat([",", line]), path.map(print, listName))]) |
| 124 | + ) |
| 125 | + ), |
| 126 | + ]); |
| 127 | +} |
| 128 | + |
| 129 | +function printSuperClass(path, options, print) { |
| 130 | + const printed = path.call(print, "superClass"); |
| 131 | + const parent = path.getParentNode(); |
| 132 | + if (parent && parent.type === "AssignmentExpression") { |
| 133 | + return concat([ |
| 134 | + ifBreak("("), |
| 135 | + indent(concat([softline, printed])), |
| 136 | + softline, |
| 137 | + ifBreak(")"), |
| 138 | + ]); |
| 139 | + } |
| 140 | + return printed; |
| 141 | +} |
| 142 | + |
| 143 | +module.exports = { printClass }; |
0 commit comments