[d565449] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.AssignmentExpression = AssignmentExpression;
|
---|
| 7 | exports.Binary = Binary;
|
---|
| 8 | exports.BinaryExpression = BinaryExpression;
|
---|
| 9 | exports.ClassExpression = ClassExpression;
|
---|
[0c6b92a] | 10 | exports.ArrowFunctionExpression = exports.ConditionalExpression = ConditionalExpression;
|
---|
[d565449] | 11 | exports.DoExpression = DoExpression;
|
---|
| 12 | exports.FunctionExpression = FunctionExpression;
|
---|
| 13 | exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
|
---|
| 14 | exports.Identifier = Identifier;
|
---|
| 15 | exports.LogicalExpression = LogicalExpression;
|
---|
| 16 | exports.NullableTypeAnnotation = NullableTypeAnnotation;
|
---|
| 17 | exports.ObjectExpression = ObjectExpression;
|
---|
| 18 | exports.OptionalIndexedAccessType = OptionalIndexedAccessType;
|
---|
| 19 | exports.OptionalCallExpression = exports.OptionalMemberExpression = OptionalMemberExpression;
|
---|
| 20 | exports.SequenceExpression = SequenceExpression;
|
---|
| 21 | exports.TSSatisfiesExpression = exports.TSAsExpression = TSAsExpression;
|
---|
| 22 | exports.TSInferType = TSInferType;
|
---|
| 23 | exports.TSInstantiationExpression = TSInstantiationExpression;
|
---|
| 24 | exports.UnaryLike = exports.TSTypeAssertion = UnaryLike;
|
---|
| 25 | exports.TSIntersectionType = exports.TSUnionType = TSUnionType;
|
---|
| 26 | exports.IntersectionTypeAnnotation = exports.UnionTypeAnnotation = UnionTypeAnnotation;
|
---|
| 27 | exports.UpdateExpression = UpdateExpression;
|
---|
| 28 | exports.AwaitExpression = exports.YieldExpression = YieldExpression;
|
---|
| 29 | var _t = require("@babel/types");
|
---|
| 30 | var _index = require("./index.js");
|
---|
| 31 | const {
|
---|
| 32 | isArrayTypeAnnotation,
|
---|
| 33 | isBinaryExpression,
|
---|
| 34 | isCallExpression,
|
---|
| 35 | isForOfStatement,
|
---|
| 36 | isIndexedAccessType,
|
---|
| 37 | isMemberExpression,
|
---|
| 38 | isObjectPattern,
|
---|
| 39 | isOptionalMemberExpression,
|
---|
[0c6b92a] | 40 | isYieldExpression,
|
---|
| 41 | isStatement
|
---|
[d565449] | 42 | } = _t;
|
---|
| 43 | const PRECEDENCE = new Map([["||", 0], ["??", 0], ["|>", 0], ["&&", 1], ["|", 2], ["^", 3], ["&", 4], ["==", 5], ["===", 5], ["!=", 5], ["!==", 5], ["<", 6], [">", 6], ["<=", 6], [">=", 6], ["in", 6], ["instanceof", 6], [">>", 7], ["<<", 7], [">>>", 7], ["+", 8], ["-", 8], ["*", 9], ["/", 9], ["%", 9], ["**", 10]]);
|
---|
| 44 | function getBinaryPrecedence(node, nodeType) {
|
---|
| 45 | if (nodeType === "BinaryExpression" || nodeType === "LogicalExpression") {
|
---|
| 46 | return PRECEDENCE.get(node.operator);
|
---|
| 47 | }
|
---|
| 48 | if (nodeType === "TSAsExpression" || nodeType === "TSSatisfiesExpression") {
|
---|
| 49 | return PRECEDENCE.get("in");
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | function isTSTypeExpression(nodeType) {
|
---|
| 53 | return nodeType === "TSAsExpression" || nodeType === "TSSatisfiesExpression" || nodeType === "TSTypeAssertion";
|
---|
| 54 | }
|
---|
| 55 | const isClassExtendsClause = (node, parent) => {
|
---|
| 56 | const parentType = parent.type;
|
---|
| 57 | return (parentType === "ClassDeclaration" || parentType === "ClassExpression") && parent.superClass === node;
|
---|
| 58 | };
|
---|
| 59 | const hasPostfixPart = (node, parent) => {
|
---|
| 60 | const parentType = parent.type;
|
---|
| 61 | return (parentType === "MemberExpression" || parentType === "OptionalMemberExpression") && parent.object === node || (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression") && parent.callee === node || parentType === "TaggedTemplateExpression" && parent.tag === node || parentType === "TSNonNullExpression";
|
---|
| 62 | };
|
---|
| 63 | function NullableTypeAnnotation(node, parent) {
|
---|
| 64 | return isArrayTypeAnnotation(parent);
|
---|
| 65 | }
|
---|
| 66 | function FunctionTypeAnnotation(node, parent, tokenContext) {
|
---|
| 67 | const parentType = parent.type;
|
---|
[0c6b92a] | 68 | return (parentType === "UnionTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "ArrayTypeAnnotation" || Boolean(tokenContext & _index.TokenContext.arrowFlowReturnType)
|
---|
| 69 | );
|
---|
[d565449] | 70 | }
|
---|
| 71 | function UpdateExpression(node, parent) {
|
---|
| 72 | return hasPostfixPart(node, parent) || isClassExtendsClause(node, parent);
|
---|
| 73 | }
|
---|
[0c6b92a] | 74 | function needsParenBeforeExpressionBrace(tokenContext) {
|
---|
[d565449] | 75 | return Boolean(tokenContext & (_index.TokenContext.expressionStatement | _index.TokenContext.arrowBody));
|
---|
| 76 | }
|
---|
[0c6b92a] | 77 | function ObjectExpression(node, parent, tokenContext) {
|
---|
| 78 | return needsParenBeforeExpressionBrace(tokenContext);
|
---|
| 79 | }
|
---|
[d565449] | 80 | function DoExpression(node, parent, tokenContext) {
|
---|
| 81 | return !node.async && Boolean(tokenContext & _index.TokenContext.expressionStatement);
|
---|
| 82 | }
|
---|
| 83 | function Binary(node, parent) {
|
---|
| 84 | const parentType = parent.type;
|
---|
| 85 | if (node.type === "BinaryExpression" && node.operator === "**" && parentType === "BinaryExpression" && parent.operator === "**") {
|
---|
| 86 | return parent.left === node;
|
---|
| 87 | }
|
---|
| 88 | if (isClassExtendsClause(node, parent)) {
|
---|
| 89 | return true;
|
---|
| 90 | }
|
---|
| 91 | if (hasPostfixPart(node, parent) || parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "AwaitExpression") {
|
---|
| 92 | return true;
|
---|
| 93 | }
|
---|
| 94 | const parentPos = getBinaryPrecedence(parent, parentType);
|
---|
| 95 | if (parentPos != null) {
|
---|
| 96 | const nodePos = getBinaryPrecedence(node, node.type);
|
---|
| 97 | if (parentPos === nodePos && parentType === "BinaryExpression" && parent.right === node || parentPos > nodePos) {
|
---|
| 98 | return true;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 | return undefined;
|
---|
| 102 | }
|
---|
| 103 | function UnionTypeAnnotation(node, parent) {
|
---|
| 104 | const parentType = parent.type;
|
---|
| 105 | return parentType === "ArrayTypeAnnotation" || parentType === "NullableTypeAnnotation" || parentType === "IntersectionTypeAnnotation" || parentType === "UnionTypeAnnotation";
|
---|
| 106 | }
|
---|
| 107 | function OptionalIndexedAccessType(node, parent) {
|
---|
| 108 | return isIndexedAccessType(parent) && parent.objectType === node;
|
---|
| 109 | }
|
---|
| 110 | function TSAsExpression(node, parent) {
|
---|
| 111 | if ((parent.type === "AssignmentExpression" || parent.type === "AssignmentPattern") && parent.left === node) {
|
---|
| 112 | return true;
|
---|
| 113 | }
|
---|
| 114 | if (parent.type === "BinaryExpression" && (parent.operator === "|" || parent.operator === "&") && node === parent.left) {
|
---|
| 115 | return true;
|
---|
| 116 | }
|
---|
| 117 | return Binary(node, parent);
|
---|
| 118 | }
|
---|
| 119 | function TSUnionType(node, parent) {
|
---|
| 120 | const parentType = parent.type;
|
---|
[0c6b92a] | 121 | return parentType === "TSArrayType" || parentType === "TSOptionalType" || parentType === "TSIntersectionType" || parentType === "TSRestType";
|
---|
[d565449] | 122 | }
|
---|
| 123 | function TSInferType(node, parent) {
|
---|
| 124 | const parentType = parent.type;
|
---|
| 125 | return parentType === "TSArrayType" || parentType === "TSOptionalType";
|
---|
| 126 | }
|
---|
| 127 | function TSInstantiationExpression(node, parent) {
|
---|
| 128 | const parentType = parent.type;
|
---|
| 129 | return (parentType === "CallExpression" || parentType === "OptionalCallExpression" || parentType === "NewExpression" || parentType === "TSInstantiationExpression") && !!parent.typeParameters;
|
---|
| 130 | }
|
---|
| 131 | function BinaryExpression(node, parent, tokenContext, inForStatementInit) {
|
---|
| 132 | return node.operator === "in" && inForStatementInit;
|
---|
| 133 | }
|
---|
| 134 | function SequenceExpression(node, parent) {
|
---|
| 135 | const parentType = parent.type;
|
---|
[0c6b92a] | 136 | if (parentType === "SequenceExpression" || parentType === "ParenthesizedExpression" || parentType === "MemberExpression" && parent.property === node || parentType === "OptionalMemberExpression" && parent.property === node || parentType === "TemplateLiteral") {
|
---|
[d565449] | 137 | return false;
|
---|
| 138 | }
|
---|
[0c6b92a] | 139 | if (parentType === "ClassDeclaration") {
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 | if (parentType === "ForOfStatement") {
|
---|
| 143 | return parent.right === node;
|
---|
| 144 | }
|
---|
| 145 | if (parentType === "ExportDefaultDeclaration") {
|
---|
| 146 | return true;
|
---|
| 147 | }
|
---|
| 148 | return !isStatement(parent);
|
---|
[d565449] | 149 | }
|
---|
| 150 | function YieldExpression(node, parent) {
|
---|
| 151 | const parentType = parent.type;
|
---|
| 152 | return parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "UnaryExpression" || parentType === "SpreadElement" || hasPostfixPart(node, parent) || parentType === "AwaitExpression" && isYieldExpression(node) || parentType === "ConditionalExpression" && node === parent.test || isClassExtendsClause(node, parent) || isTSTypeExpression(parentType);
|
---|
| 153 | }
|
---|
| 154 | function ClassExpression(node, parent, tokenContext) {
|
---|
| 155 | return Boolean(tokenContext & (_index.TokenContext.expressionStatement | _index.TokenContext.exportDefault));
|
---|
| 156 | }
|
---|
| 157 | function UnaryLike(node, parent) {
|
---|
| 158 | return hasPostfixPart(node, parent) || isBinaryExpression(parent) && parent.operator === "**" && parent.left === node || isClassExtendsClause(node, parent);
|
---|
| 159 | }
|
---|
| 160 | function FunctionExpression(node, parent, tokenContext) {
|
---|
| 161 | return Boolean(tokenContext & (_index.TokenContext.expressionStatement | _index.TokenContext.exportDefault));
|
---|
| 162 | }
|
---|
| 163 | function ConditionalExpression(node, parent) {
|
---|
| 164 | const parentType = parent.type;
|
---|
| 165 | if (parentType === "UnaryExpression" || parentType === "SpreadElement" || parentType === "BinaryExpression" || parentType === "LogicalExpression" || parentType === "ConditionalExpression" && parent.test === node || parentType === "AwaitExpression" || isTSTypeExpression(parentType)) {
|
---|
| 166 | return true;
|
---|
| 167 | }
|
---|
| 168 | return UnaryLike(node, parent);
|
---|
| 169 | }
|
---|
| 170 | function OptionalMemberExpression(node, parent) {
|
---|
| 171 | return isCallExpression(parent) && parent.callee === node || isMemberExpression(parent) && parent.object === node;
|
---|
| 172 | }
|
---|
[0c6b92a] | 173 | function AssignmentExpression(node, parent, tokenContext) {
|
---|
| 174 | if (needsParenBeforeExpressionBrace(tokenContext) && isObjectPattern(node.left)) {
|
---|
[d565449] | 175 | return true;
|
---|
| 176 | } else {
|
---|
| 177 | return ConditionalExpression(node, parent);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | function LogicalExpression(node, parent) {
|
---|
| 181 | const parentType = parent.type;
|
---|
| 182 | if (isTSTypeExpression(parentType)) return true;
|
---|
| 183 | if (parentType !== "LogicalExpression") return false;
|
---|
| 184 | switch (node.operator) {
|
---|
| 185 | case "||":
|
---|
| 186 | return parent.operator === "??" || parent.operator === "&&";
|
---|
| 187 | case "&&":
|
---|
| 188 | return parent.operator === "??";
|
---|
| 189 | case "??":
|
---|
| 190 | return parent.operator !== "??";
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
[0c6b92a] | 193 | function Identifier(node, parent, tokenContext, _inForInit, getRawIdentifier) {
|
---|
[d565449] | 194 | var _node$extra;
|
---|
| 195 | const parentType = parent.type;
|
---|
| 196 | if ((_node$extra = node.extra) != null && _node$extra.parenthesized && parentType === "AssignmentExpression" && parent.left === node) {
|
---|
| 197 | const rightType = parent.right.type;
|
---|
| 198 | if ((rightType === "FunctionExpression" || rightType === "ClassExpression") && parent.right.id == null) {
|
---|
| 199 | return true;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
[0c6b92a] | 202 | if (getRawIdentifier && getRawIdentifier(node) !== node.name) {
|
---|
| 203 | return false;
|
---|
| 204 | }
|
---|
[d565449] | 205 | if (node.name === "let") {
|
---|
| 206 | const isFollowedByBracket = isMemberExpression(parent, {
|
---|
| 207 | object: node,
|
---|
| 208 | computed: true
|
---|
| 209 | }) || isOptionalMemberExpression(parent, {
|
---|
| 210 | object: node,
|
---|
| 211 | computed: true,
|
---|
| 212 | optional: false
|
---|
| 213 | });
|
---|
| 214 | if (isFollowedByBracket && tokenContext & (_index.TokenContext.expressionStatement | _index.TokenContext.forHead | _index.TokenContext.forInHead)) {
|
---|
| 215 | return true;
|
---|
| 216 | }
|
---|
| 217 | return Boolean(tokenContext & _index.TokenContext.forOfHead);
|
---|
| 218 | }
|
---|
| 219 | return node.name === "async" && isForOfStatement(parent, {
|
---|
| 220 | left: node,
|
---|
| 221 | await: false
|
---|
| 222 | });
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | //# sourceMappingURL=parentheses.js.map
|
---|