added to remote repo
This commit is contained in:
1 parent
8697c9f405
commit
cc599b6e18
8049 files changed
+1096323
No files matched your search
+81
@@ -0,0 +1,81 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.TokenContext = void 0;
|
||||
exports.isLastChild = isLastChild;
|
||||
exports.parentNeedsParens = parentNeedsParens;
|
||||
var parens = require("./parentheses.js");
|
||||
var _t = require("@babel/types");
|
||||
var _nodes = require("../nodes.js");
|
||||
const {
|
||||
VISITOR_KEYS
|
||||
} = _t;
|
||||
const TokenContext = exports.TokenContext = {
|
||||
normal: 0,
|
||||
expressionStatement: 1,
|
||||
arrowBody: 2,
|
||||
exportDefault: 4,
|
||||
arrowFlowReturnType: 8,
|
||||
forInitHead: 16,
|
||||
forInHead: 32,
|
||||
forOfHead: 64,
|
||||
forInOrInitHeadAccumulate: 128,
|
||||
forInOrInitHeadAccumulatePassThroughMask: 128
|
||||
};
|
||||
for (const type of Object.keys(parens)) {
|
||||
const func = parens[type];
|
||||
if (_nodes.generatorInfosMap.has(type)) {
|
||||
_nodes.generatorInfosMap.get(type)[2] = func;
|
||||
}
|
||||
}
|
||||
function isOrHasCallExpression(node) {
|
||||
switch (node.type) {
|
||||
case "CallExpression":
|
||||
return true;
|
||||
case "MemberExpression":
|
||||
return isOrHasCallExpression(node.object);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function parentNeedsParens(node, parent, parentId) {
|
||||
switch (parentId) {
|
||||
case 112:
|
||||
if (parent.callee === node) {
|
||||
if (isOrHasCallExpression(node)) return true;
|
||||
}
|
||||
break;
|
||||
case 42:
|
||||
return !isDecoratorMemberExpression(node) && !(node.type === "CallExpression" && isDecoratorMemberExpression(node.callee)) && node.type !== "ParenthesizedExpression";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function isDecoratorMemberExpression(node) {
|
||||
switch (node.type) {
|
||||
case "Identifier":
|
||||
return true;
|
||||
case "MemberExpression":
|
||||
return !node.computed && node.property.type === "Identifier" && isDecoratorMemberExpression(node.object);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
function isLastChild(parent, child) {
|
||||
const visitorKeys = VISITOR_KEYS[parent.type];
|
||||
for (let i = visitorKeys.length - 1; i >= 0; i--) {
|
||||
const val = parent[visitorKeys[i]];
|
||||
if (val === child) {
|
||||
return true;
|
||||
} else if (Array.isArray(val)) {
|
||||
let j = val.length - 1;
|
||||
while (j >= 0 && val[j] === null) j--;
|
||||
return j >= 0 && val[j] === child;
|
||||
} else if (val) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"names":["parens","require","_t","_nodes","VISITOR_KEYS","TokenContext","exports","normal","expressionStatement","arrowBody","exportDefault","arrowFlowReturnType","forInitHead","forInHead","forOfHead","forInOrInitHeadAccumulate","forInOrInitHeadAccumulatePassThroughMask","type","Object","keys","func","generatorInfosMap","has","get","isOrHasCallExpression","node","object","parentNeedsParens","parent","parentId","callee","isDecoratorMemberExpression","computed","property","isLastChild","child","visitorKeys","i","length","val","Array","isArray","j"],"sources":["../../src/node/index.ts"],"sourcesContent":["import * as parens from \"./parentheses.ts\";\nimport { VISITOR_KEYS } from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nimport { generatorInfosMap } from \"../nodes.ts\";\n\nexport const enum TokenContext {\n normal = 0,\n expressionStatement = 1 << 0,\n arrowBody = 1 << 1,\n exportDefault = 1 << 2,\n arrowFlowReturnType = 1 << 3,\n forInitHead = 1 << 4,\n forInHead = 1 << 5,\n forOfHead = 1 << 6,\n // This flag lives across the token boundary, and will\n // be reset after forIn or forInit head is printed\n forInOrInitHeadAccumulate = 1 << 7,\n forInOrInitHeadAccumulatePassThroughMask = 0b10000000,\n}\n\nexport type NodeHandler<R> = (\n node: t.Node,\n // todo:\n // node: K extends keyof typeof t\n // ? Extract<typeof t[K], { type: \"string\" }>\n // : t.Node,\n parent: t.Node,\n parentId: number,\n tokenContext?: number,\n getRawIdentifier?: (node: t.Identifier) => string,\n) => R | undefined;\n\nfor (const type of Object.keys(parens) as (keyof typeof parens)[]) {\n const func = parens[type];\n if (generatorInfosMap.has(type)) {\n generatorInfosMap.get(type)![2] = func;\n }\n}\n\nfunction isOrHasCallExpression(node: t.Node): boolean {\n switch (node.type) {\n case \"CallExpression\":\n return true;\n case \"MemberExpression\":\n return isOrHasCallExpression(node.object);\n }\n return false;\n}\n\nexport function parentNeedsParens(\n node: t.Node,\n parent: any,\n parentId: number,\n): boolean {\n switch (parentId) {\n case __node(\"NewExpression\"):\n if (parent.callee === node) {\n if (isOrHasCallExpression(node)) return true;\n }\n break;\n case __node(\"Decorator\"):\n return (\n !isDecoratorMemberExpression(node) &&\n !(\n node.type === \"CallExpression\" &&\n isDecoratorMemberExpression(node.callee)\n ) &&\n node.type !== \"ParenthesizedExpression\"\n );\n }\n return false;\n}\n\nfunction isDecoratorMemberExpression(node: t.Node): boolean {\n switch (node.type) {\n case \"Identifier\":\n return true;\n case \"MemberExpression\":\n return (\n !node.computed &&\n node.property.type === \"Identifier\" &&\n isDecoratorMemberExpression(node.object)\n );\n default:\n return false;\n }\n}\n\nexport function isLastChild(parent: t.Node, child: t.Node) {\n const visitorKeys = VISITOR_KEYS[parent.type];\n for (let i = visitorKeys.length - 1; i >= 0; i--) {\n const val = (parent as any)[visitorKeys[i]] as t.Node | t.Node[] | null;\n if (val === child) {\n return true;\n } else if (Array.isArray(val)) {\n let j = val.length - 1;\n while (j >= 0 && val[j] === null) j--;\n return j >= 0 && val[j] === child;\n } else if (val) {\n return false;\n }\n }\n return false;\n}\n"],"mappings":";;;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,EAAA,GAAAD,OAAA;AAGA,IAAAE,MAAA,GAAAF,OAAA;AAAgD;EAHvCG;AAAY,IAAAF,EAAA;AAAA,MAKHG,YAAY,GAAAC,OAAA,CAAAD,YAAA;EAAAE,MAAA;EAAAC,mBAAA;EAAAC,SAAA;EAAAC,aAAA;EAAAC,mBAAA;EAAAC,WAAA;EAAAC,SAAA;EAAAC,SAAA;EAAAC,yBAAA;EAAAC,wCAAA;AAAA;AA2B9B,KAAK,MAAMC,IAAI,IAAIC,MAAM,CAACC,IAAI,CAACnB,MAAM,CAAC,EAA6B;EACjE,MAAMoB,IAAI,GAAGpB,MAAM,CAACiB,IAAI,CAAC;EACzB,IAAII,wBAAiB,CAACC,GAAG,CAACL,IAAI,CAAC,EAAE;IAC/BI,wBAAiB,CAACE,GAAG,CAACN,IAAI,CAAC,CAAE,CAAC,CAAC,GAAGG,IAAI;EACxC;AACF;AAEA,SAASI,qBAAqBA,CAACC,IAAY,EAAW;EACpD,QAAQA,IAAI,CAACR,IAAI;IACf,KAAK,gBAAgB;MACnB,OAAO,IAAI;IACb,KAAK,kBAAkB;MACrB,OAAOO,qBAAqB,CAACC,IAAI,CAACC,MAAM,CAAC;EAC7C;EACA,OAAO,KAAK;AACd;AAEO,SAASC,iBAAiBA,CAC/BF,IAAY,EACZG,MAAW,EACXC,QAAgB,EACP;EACT,QAAQA,QAAQ;IACd;MACE,IAAID,MAAM,CAACE,MAAM,KAAKL,IAAI,EAAE;QAC1B,IAAID,qBAAqB,CAACC,IAAI,CAAC,EAAE,OAAO,IAAI;MAC9C;MACA;IACF;MACE,OACE,CAACM,2BAA2B,CAACN,IAAI,CAAC,IAClC,EACEA,IAAI,CAACR,IAAI,KAAK,gBAAgB,IAC9Bc,2BAA2B,CAACN,IAAI,CAACK,MAAM,CAAC,CACzC,IACDL,IAAI,CAACR,IAAI,KAAK,yBAAyB;EAE7C;EACA,OAAO,KAAK;AACd;AAEA,SAASc,2BAA2BA,CAACN,IAAY,EAAW;EAC1D,QAAQA,IAAI,CAACR,IAAI;IACf,KAAK,YAAY;MACf,OAAO,IAAI;IACb,KAAK,kBAAkB;MACrB,OACE,CAACQ,IAAI,CAACO,QAAQ,IACdP,IAAI,CAACQ,QAAQ,CAAChB,IAAI,KAAK,YAAY,IACnCc,2BAA2B,CAACN,IAAI,CAACC,MAAM,CAAC;IAE5C;MACE,OAAO,KAAK;EAChB;AACF;AAEO,SAASQ,WAAWA,CAACN,MAAc,EAAEO,KAAa,EAAE;EACzD,MAAMC,WAAW,GAAGhC,YAAY,CAACwB,MAAM,CAACX,IAAI,CAAC;EAC7C,KAAK,IAAIoB,CAAC,GAAGD,WAAW,CAACE,MAAM,GAAG,CAAC,EALine truncated
|
||||
+298
@@ -0,0 +1,298 @@
|
||||
"use strict";
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.AssignmentExpression = AssignmentExpression;
|
||||
exports.BinaryExpression = BinaryExpression;
|
||||
exports.ClassExpression = ClassExpression;
|
||||
exports.ArrowFunctionExpression = exports.ConditionalExpression = ConditionalExpression;
|
||||
exports.DoExpression = DoExpression;
|
||||
exports.FunctionExpression = FunctionExpression;
|
||||
exports.FunctionTypeAnnotation = FunctionTypeAnnotation;
|
||||
exports.Identifier = Identifier;
|
||||
exports.LogicalExpression = LogicalExpression;
|
||||
exports.NullableTypeAnnotation = NullableTypeAnnotation;
|
||||
exports.ObjectExpression = ObjectExpression;
|
||||
exports.OptionalIndexedAccessType = OptionalIndexedAccessType;
|
||||
exports.OptionalCallExpression = exports.OptionalMemberExpression = OptionalMemberExpression;
|
||||
exports.SequenceExpression = SequenceExpression;
|
||||
exports.TSSatisfiesExpression = exports.TSAsExpression = TSAsExpression;
|
||||
exports.TSConditionalType = TSConditionalType;
|
||||
exports.TSConstructorType = exports.TSFunctionType = TSFunctionType;
|
||||
exports.TSInferType = TSInferType;
|
||||
exports.TSInstantiationExpression = TSInstantiationExpression;
|
||||
exports.TSIntersectionType = TSIntersectionType;
|
||||
exports.SpreadElement = exports.UnaryExpression = exports.TSTypeAssertion = UnaryLike;
|
||||
exports.TSTypeOperator = TSTypeOperator;
|
||||
exports.TSUnionType = TSUnionType;
|
||||
exports.IntersectionTypeAnnotation = exports.UnionTypeAnnotation = UnionTypeAnnotation;
|
||||
exports.UpdateExpression = UpdateExpression;
|
||||
exports.AwaitExpression = exports.YieldExpression = YieldExpression;
|
||||
var _t = require("@babel/types");
|
||||
var _index = require("./index.js");
|
||||
const {
|
||||
isMemberExpression,
|
||||
isOptionalMemberExpression,
|
||||
isYieldExpression,
|
||||
isStatement
|
||||
} = _t;
|
||||
const PRECEDENCE = new Map([["||", 0], ["??", 1], ["&&", 2], ["|", 3], ["^", 4], ["&", 5], ["==", 6], ["===", 6], ["!=", 6], ["!==", 6], ["<", 7], [">", 7], ["<=", 7], [">=", 7], ["in", 7], ["instanceof", 7], [">>", 8], ["<<", 8], [">>>", 8], ["+", 9], ["-", 9], ["*", 10], ["/", 10], ["%", 10], ["**", 11]]);
|
||||
function isTSTypeExpression(nodeId) {
|
||||
return nodeId === 156 || nodeId === 201 || nodeId === 209;
|
||||
}
|
||||
const isClassExtendsClause = (node, parent, parentId) => {
|
||||
return (parentId === 21 || parentId === 22) && parent.superClass === node;
|
||||
};
|
||||
const hasPostfixPart = (node, parent, parentId) => {
|
||||
switch (parentId) {
|
||||
case 108:
|
||||
case 132:
|
||||
return parent.object === node;
|
||||
case 17:
|
||||
case 130:
|
||||
case 112:
|
||||
return parent.callee === node;
|
||||
case 222:
|
||||
return parent.tag === node;
|
||||
case 191:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
function NullableTypeAnnotation(node, parent, parentId) {
|
||||
return parentId === 4;
|
||||
}
|
||||
function FunctionTypeAnnotation(node, parent, parentId, tokenContext) {
|
||||
return (parentId === 239 || parentId === 90 || parentId === 4 || (tokenContext & _index.TokenContext.arrowFlowReturnType) > 0
|
||||
);
|
||||
}
|
||||
function UpdateExpression(node, parent, parentId) {
|
||||
return hasPostfixPart(node, parent, parentId) || isClassExtendsClause(node, parent, parentId);
|
||||
}
|
||||
function needsParenBeforeExpressionBrace(tokenContext) {
|
||||
return (tokenContext & (_index.TokenContext.expressionStatement | _index.TokenContext.arrowBody)) > 0;
|
||||
}
|
||||
function ObjectExpression(node, parent, parentId, tokenContext) {
|
||||
return needsParenBeforeExpressionBrace(tokenContext);
|
||||
}
|
||||
function DoExpression(node, parent, parentId, tokenContext) {
|
||||
return (tokenContext & _index.TokenContext.expressionStatement) > 0 && !node.async;
|
||||
}
|
||||
function BinaryLike(node, parent, parentId, nodeType) {
|
||||
if (isClassExtendsClause(node, parent, parentId)) {
|
||||
return true;
|
||||
}
|
||||
if (hasPostfixPart(node, parent, parentId) || parentId === 238 || parentId === 145 || parentId === 8) {
|
||||
return true;
|
||||
}
|
||||
let parentPos;
|
||||
switch (parentId) {
|
||||
case 10:
|
||||
case 107:
|
||||
parentPos = PRECEDENCE.get(parent.operator);
|
||||
break;
|
||||
case 156:
|
||||
case 201:
|
||||
parentPos = 7;
|
||||
}
|
||||
if (parentPos !== undefined) {
|
||||
const nodePos = nodeType === 2 ? 7 : PRECEDENCE.get(node.operator);
|
||||
if (parentPos > nodePos) return true;
|
||||
if (parentPos === nodePos && parentId === 10 && (nodePos === 11 ? parent.left === node : parent.right === node)) {
|
||||
return true;
|
||||
}
|
||||
if (nodeType === 1 && parentId === 107 && (nodePos === 1 && parentPos !== 1 || parentPos === 1 && nodePos !== 1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function UnionTypeAnnotation(node, parent, parentId) {
|
||||
switch (parentId) {
|
||||
case 4:
|
||||
case 115:
|
||||
case 90:
|
||||
case 239:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function OptionalIndexedAccessType(node, parent, parentId) {
|
||||
return parentId === 84 && parent.objectType === node;
|
||||
}
|
||||
function TSAsExpression(node, parent, parentId) {
|
||||
if ((parentId === 6 || parentId === 7) && parent.left === node) {
|
||||
return true;
|
||||
}
|
||||
if (parentId === 10 && (parent.operator === "|" || parent.operator === "&") && node === parent.left) {
|
||||
return true;
|
||||
}
|
||||
return BinaryLike(node, parent, parentId, 2);
|
||||
}
|
||||
function TSConditionalType(node, parent, parentId) {
|
||||
switch (parentId) {
|
||||
case 155:
|
||||
case 195:
|
||||
case 211:
|
||||
case 212:
|
||||
return true;
|
||||
case 175:
|
||||
return parent.objectType === node;
|
||||
case 181:
|
||||
case 219:
|
||||
return parent.types[0] === node;
|
||||
case 161:
|
||||
return parent.checkType === node || parent.extendsType === node;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function TSUnionType(node, parent, parentId) {
|
||||
switch (parentId) {
|
||||
case 181:
|
||||
case 211:
|
||||
case 155:
|
||||
case 195:
|
||||
return true;
|
||||
case 175:
|
||||
return parent.objectType === node;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function TSIntersectionType(node, parent, parentId) {
|
||||
return parentId === 211 || TSTypeOperator(node, parent, parentId);
|
||||
}
|
||||
function TSInferType(node, parent, parentId) {
|
||||
if (TSTypeOperator(node, parent, parentId)) {
|
||||
return true;
|
||||
}
|
||||
if ((parentId === 181 || parentId === 219) && node.typeParameter.constraint && parent.types[0] === node) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function TSTypeOperator(node, parent, parentId) {
|
||||
switch (parentId) {
|
||||
case 155:
|
||||
case 195:
|
||||
return true;
|
||||
case 175:
|
||||
if (parent.objectType === node) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function TSInstantiationExpression(node, parent, parentId) {
|
||||
switch (parentId) {
|
||||
case 17:
|
||||
case 130:
|
||||
case 112:
|
||||
case 177:
|
||||
return (parent.typeParameters
|
||||
) != null;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function TSFunctionType(node, parent, parentId) {
|
||||
if (TSUnionType(node, parent, parentId)) return true;
|
||||
return parentId === 219 || parentId === 161 && (parent.checkType === node || parent.extendsType === node);
|
||||
}
|
||||
function BinaryExpression(node, parent, parentId, tokenContext) {
|
||||
if (BinaryLike(node, parent, parentId, 0)) return true;
|
||||
return (tokenContext & _index.TokenContext.forInOrInitHeadAccumulate) > 0 && node.operator === "in";
|
||||
}
|
||||
function LogicalExpression(node, parent, parentId) {
|
||||
return BinaryLike(node, parent, parentId, 1);
|
||||
}
|
||||
function SequenceExpression(node, parent, parentId) {
|
||||
if (parentId === 144 || parentId === 133 || parentId === 108 && parent.property === node || parentId === 132 && parent.property === node || parentId === 224) {
|
||||
return false;
|
||||
}
|
||||
if (parentId === 21) {
|
||||
return true;
|
||||
}
|
||||
if (parentId === 68) {
|
||||
return parent.right === node;
|
||||
}
|
||||
if (parentId === 60) {
|
||||
return true;
|
||||
}
|
||||
return !isStatement(parent);
|
||||
}
|
||||
function YieldExpression(node, parent, parentId) {
|
||||
return parentId === 10 || parentId === 107 || parentId === 238 || parentId === 145 || hasPostfixPart(node, parent, parentId) || parentId === 8 && isYieldExpression(node) || parentId === 28 && node === parent.test || isClassExtendsClause(node, parent, parentId) || isTSTypeExpression(parentId);
|
||||
}
|
||||
function ClassExpression(node, parent, parentId, tokenContext) {
|
||||
return (tokenContext & (_index.TokenContext.expressionStatement | _index.TokenContext.exportDefault)) > 0;
|
||||
}
|
||||
function UnaryLike(node, parent, parentId) {
|
||||
return hasPostfixPart(node, parent, parentId) || parentId === 10 && parent.operator === "**" && parent.left === node || isClassExtendsClause(node, parent, parentId);
|
||||
}
|
||||
function FunctionExpression(node, parent, parentId, tokenContext) {
|
||||
return (tokenContext & (_index.TokenContext.expressionStatement | _index.TokenContext.exportDefault)) > 0;
|
||||
}
|
||||
function ConditionalExpression(node, parent, parentId) {
|
||||
switch (parentId) {
|
||||
case 238:
|
||||
case 145:
|
||||
case 10:
|
||||
case 107:
|
||||
case 8:
|
||||
return true;
|
||||
case 28:
|
||||
if (parent.test === node) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (isTSTypeExpression(parentId)) {
|
||||
return true;
|
||||
}
|
||||
return UnaryLike(node, parent, parentId);
|
||||
}
|
||||
function OptionalMemberExpression(node, parent, parentId) {
|
||||
switch (parentId) {
|
||||
case 17:
|
||||
return parent.callee === node;
|
||||
case 108:
|
||||
return parent.object === node;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function AssignmentExpression(node, parent, parentId, tokenContext) {
|
||||
if (needsParenBeforeExpressionBrace(tokenContext) && node.left.type === "ObjectPattern") {
|
||||
return true;
|
||||
}
|
||||
return ConditionalExpression(node, parent, parentId);
|
||||
}
|
||||
function Identifier(node, parent, parentId, tokenContext, getRawIdentifier) {
|
||||
var _node$extra;
|
||||
if (getRawIdentifier && getRawIdentifier(node) !== node.name) {
|
||||
return false;
|
||||
}
|
||||
if (parentId === 6 && (_node$extra = node.extra) != null && _node$extra.parenthesized && parent.left === node) {
|
||||
const rightType = parent.right.type;
|
||||
if ((rightType === "FunctionExpression" || rightType === "ClassExpression") && parent.right.id == null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (tokenContext & _index.TokenContext.forOfHead || (parentId === 108 || parentId === 132) && tokenContext & (_index.TokenContext.expressionStatement | _index.TokenContext.forInitHead | _index.TokenContext.forInHead)) {
|
||||
if (node.name === "let") {
|
||||
const isFollowedByBracket = isMemberExpression(parent, {
|
||||
object: node,
|
||||
computed: true
|
||||
}) || isOptionalMemberExpression(parent, {
|
||||
object: node,
|
||||
computed: true,
|
||||
optional: false
|
||||
});
|
||||
if (isFollowedByBracket && tokenContext & (_index.TokenContext.expressionStatement | _index.TokenContext.forInitHead | _index.TokenContext.forInHead)) {
|
||||
return true;
|
||||
}
|
||||
return (tokenContext & _index.TokenContext.forOfHead) > 0;
|
||||
}
|
||||
}
|
||||
return parentId === 68 && parent.left === node && node.name === "async" && !parent.await;
|
||||
}
|
||||
|
||||
//# sourceMappingURL=parentheses.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"names":["_t","require","_index","isMemberExpression","isOptionalMemberExpression","isYieldExpression","isStatement","PRECEDENCE","Map","isTSTypeExpression","nodeId","isClassExtendsClause","node","parent","parentId","superClass","hasPostfixPart","object","callee","tag","NullableTypeAnnotation","FunctionTypeAnnotation","tokenContext","TokenContext","arrowFlowReturnType","UpdateExpression","needsParenBeforeExpressionBrace","expressionStatement","arrowBody","ObjectExpression","DoExpression","async","BinaryLike","nodeType","parentPos","get","operator","undefined","nodePos","left","right","UnionTypeAnnotation","OptionalIndexedAccessType","objectType","TSAsExpression","TSConditionalType","types","checkType","extendsType","TSUnionType","TSIntersectionType","TSTypeOperator","TSInferType","typeParameter","constraint","TSInstantiationExpression","typeParameters","TSFunctionType","BinaryExpression","forInOrInitHeadAccumulate","LogicalExpression","SequenceExpression","property","YieldExpression","test","ClassExpression","exportDefault","UnaryLike","FunctionExpression","ConditionalExpression","OptionalMemberExpression","AssignmentExpression","type","Identifier","getRawIdentifier","_node$extra","name","extra","parenthesized","rightType","id","forOfHead","forInitHead","forInHead","isFollowedByBracket","computed","optional","await"],"sources":["../../src/node/parentheses.ts"],"sourcesContent":["import {\n isMemberExpression,\n isOptionalMemberExpression,\n isYieldExpression,\n isStatement,\n} from \"@babel/types\";\nimport type * as t from \"@babel/types\";\n\nimport { TokenContext } from \"./index.ts\";\n\nconst PRECEDENCE = new Map([\n [\"||\", 0],\n [\"??\", 1],\n [\"&&\", 2],\n [\"|\", 3],\n [\"^\", 4],\n [\"&\", 5],\n [\"==\", 6],\n [\"===\", 6],\n [\"!=\", 6],\n [\"!==\", 6],\n [\"<\", 7],\n [\">\", 7],\n [\"<=\", 7],\n [\">=\", 7],\n [\"in\", 7],\n [\"instanceof\", 7],\n [\">>\", 8],\n [\"<<\", 8],\n [\">>>\", 8],\n [\"+\", 9],\n [\"-\", 9],\n [\"*\", 10],\n [\"/\", 10],\n [\"%\", 10],\n [\"**\", 11],\n]);\n\nfunction isTSTypeExpression(nodeId: number) {\n return (\n nodeId === __node(\"TSAsExpression\") ||\n nodeId === __node(\"TSSatisfiesExpression\") ||\n nodeId === __node(\"TSTypeAssertion\")\n );\n}\n\nconst isClassExtendsClause = (\n node: t.Node,\n parent: any,\n parentId: number,\n): parent is t.Class => {\n return (\n (parentId === __node(\"ClassDeclaration\") ||\n parentId === __node(\"ClassExpression\")) &&\n parent.superClass === node\n );\n};\n\nconst hasPostfixPart = (node: t.Node, parent: any, parentId: number) => {\n switch (parentId) {\n case __node(\"MemberExpression\"):\n case __node(\"OptionalMemberExpression\"):\n return parent.object === node;\n case __node(\"CallExpression\"):\n case __node(\"OptionalCallExpression\"):\n case __node(\"NewExpression\"):\n return parent.callee === node;\n case __node(\"TaggedTemplateExpression\"):\n return parent.tag === node;\n case __node(\"TSNonNullExpression\"):\n return true;\n }\n\n return false;\n};\n\nexport function NullableTypeAnnotation(\n node: t.NullableTypeAnnotation,\n parent: any,\n parentId: number,\n): boolean {\n return parentId === __node(\"ArrayTypeAnnotation\");\n}\n\nexport function FunctionTypeAnnotation(\n node: t.FunctionTypeAnnotation,\n parent: any,\n parentId: number,\n tokenContext: number,\n): boolean {\n return (\n // (() => A) | (() => B)\n parentId === __node(\"UnionTypeAnnotation\") ||\n // (() => A) & (() => B)\n parentId === __node(\"IntersectionTypeAnnotation\") ||\n // (() => A)[]\n parentId === __node(\"ArrayTypeAnnotation\") ||\n (tokenContext & TokenContext.arrowFlowReturnType) > 0\n );\n}\n\nexport function UpdateExpression(\n node: t.UpdateExpression,\n parent: any,\n parentId: number,\n): boolean {\n return (\n hasPostfixPart(node, parent, parentId) ||\n isClassExtendsClause(node, parent, parentId)\n );\n}\n\nfunction needsParenBeforeExpressionBrace(tokenContext: number) {\n return (\n (tokenContext &\n (TokenContext.expressionStatement | TokenContext.arrowBody)) >\n 0\n );\n}\n\nexport function ObjectExpression(\n node: t.ObjectExpression,\n parent: any,\n parentId: number,\n tokenContext: number,\n): boolean {\n return needsParenBeforeExpressionBrace(tokenContext);\n}\n\nexport function DoExpression(\n node: t.DoExpression,\n parent: any,\n parentId: number,\n tokenContext: number,\n): boolean {\n // `async do` can start an expression statement\n return (tokenContext & TokenContext.expressionStatement) > 0 && !node.async;\n}\n\nconst enum BinaryLikeType {\n Binary = 0,\n Logical = 1,\n TypeScript = 2,\n}\n\nfunction BinaryLike(\n node: t.Binary | t.TSAsExpression | t.TSSatisfiesExpression,\n parent: any,\n parentId: number,\n nodeType: BinaryLikeType,\n): boolean {\n if (isClassExtendsClause(node, parent, parentId)) {\n return tLine truncated
|
||||
Reference in new issue
Block a user