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