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