1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.TokenContext = void 0;
|
---|
7 | exports.isLastChild = isLastChild;
|
---|
8 | exports.needsParens = needsParens;
|
---|
9 | exports.needsWhitespace = needsWhitespace;
|
---|
10 | exports.needsWhitespaceAfter = needsWhitespaceAfter;
|
---|
11 | exports.needsWhitespaceBefore = needsWhitespaceBefore;
|
---|
12 | var whitespace = require("./whitespace.js");
|
---|
13 | var parens = require("./parentheses.js");
|
---|
14 | var _t = require("@babel/types");
|
---|
15 | const {
|
---|
16 | FLIPPED_ALIAS_KEYS,
|
---|
17 | VISITOR_KEYS,
|
---|
18 | isCallExpression,
|
---|
19 | isDecorator,
|
---|
20 | isExpressionStatement,
|
---|
21 | isMemberExpression,
|
---|
22 | isNewExpression,
|
---|
23 | isParenthesizedExpression
|
---|
24 | } = _t;
|
---|
25 | const TokenContext = exports.TokenContext = {
|
---|
26 | expressionStatement: 1,
|
---|
27 | arrowBody: 2,
|
---|
28 | exportDefault: 4,
|
---|
29 | forHead: 8,
|
---|
30 | forInHead: 16,
|
---|
31 | forOfHead: 32,
|
---|
32 | arrowFlowReturnType: 64
|
---|
33 | };
|
---|
34 | function expandAliases(obj) {
|
---|
35 | const map = new Map();
|
---|
36 | function add(type, func) {
|
---|
37 | const fn = map.get(type);
|
---|
38 | map.set(type, fn ? function (node, parent, stack, inForInit, getRawIdentifier) {
|
---|
39 | var _fn;
|
---|
40 | return (_fn = fn(node, parent, stack, inForInit, getRawIdentifier)) != null ? _fn : func(node, parent, stack, inForInit, getRawIdentifier);
|
---|
41 | } : func);
|
---|
42 | }
|
---|
43 | for (const type of Object.keys(obj)) {
|
---|
44 | const aliases = FLIPPED_ALIAS_KEYS[type];
|
---|
45 | if (aliases) {
|
---|
46 | for (const alias of aliases) {
|
---|
47 | add(alias, obj[type]);
|
---|
48 | }
|
---|
49 | } else {
|
---|
50 | add(type, obj[type]);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | return map;
|
---|
54 | }
|
---|
55 | const expandedParens = expandAliases(parens);
|
---|
56 | const expandedWhitespaceNodes = expandAliases(whitespace.nodes);
|
---|
57 | function isOrHasCallExpression(node) {
|
---|
58 | if (isCallExpression(node)) {
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 | return isMemberExpression(node) && isOrHasCallExpression(node.object);
|
---|
62 | }
|
---|
63 | function needsWhitespace(node, parent, type) {
|
---|
64 | var _expandedWhitespaceNo;
|
---|
65 | if (!node) return false;
|
---|
66 | if (isExpressionStatement(node)) {
|
---|
67 | node = node.expression;
|
---|
68 | }
|
---|
69 | const flag = (_expandedWhitespaceNo = expandedWhitespaceNodes.get(node.type)) == null ? void 0 : _expandedWhitespaceNo(node, parent);
|
---|
70 | if (typeof flag === "number") {
|
---|
71 | return (flag & type) !== 0;
|
---|
72 | }
|
---|
73 | return false;
|
---|
74 | }
|
---|
75 | function needsWhitespaceBefore(node, parent) {
|
---|
76 | return needsWhitespace(node, parent, 1);
|
---|
77 | }
|
---|
78 | function needsWhitespaceAfter(node, parent) {
|
---|
79 | return needsWhitespace(node, parent, 2);
|
---|
80 | }
|
---|
81 | function needsParens(node, parent, tokenContext, inForInit, getRawIdentifier) {
|
---|
82 | var _expandedParens$get;
|
---|
83 | if (!parent) return false;
|
---|
84 | if (isNewExpression(parent) && parent.callee === node) {
|
---|
85 | if (isOrHasCallExpression(node)) return true;
|
---|
86 | }
|
---|
87 | if (isDecorator(parent)) {
|
---|
88 | return !isDecoratorMemberExpression(node) && !(isCallExpression(node) && isDecoratorMemberExpression(node.callee)) && !isParenthesizedExpression(node);
|
---|
89 | }
|
---|
90 | return (_expandedParens$get = expandedParens.get(node.type)) == null ? void 0 : _expandedParens$get(node, parent, tokenContext, inForInit, getRawIdentifier);
|
---|
91 | }
|
---|
92 | function isDecoratorMemberExpression(node) {
|
---|
93 | switch (node.type) {
|
---|
94 | case "Identifier":
|
---|
95 | return true;
|
---|
96 | case "MemberExpression":
|
---|
97 | return !node.computed && node.property.type === "Identifier" && isDecoratorMemberExpression(node.object);
|
---|
98 | default:
|
---|
99 | return false;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | function isLastChild(parent, child) {
|
---|
103 | const visitorKeys = VISITOR_KEYS[parent.type];
|
---|
104 | for (let i = visitorKeys.length - 1; i >= 0; i--) {
|
---|
105 | const val = parent[visitorKeys[i]];
|
---|
106 | if (val === child) {
|
---|
107 | return true;
|
---|
108 | } else if (Array.isArray(val)) {
|
---|
109 | let j = val.length - 1;
|
---|
110 | while (j >= 0 && val[j] === null) j--;
|
---|
111 | return j >= 0 && val[j] === child;
|
---|
112 | } else if (val) {
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | return false;
|
---|
117 | }
|
---|
118 |
|
---|
119 | //# sourceMappingURL=index.js.map
|
---|