1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.needsWhitespace = needsWhitespace;
|
---|
7 | exports.needsWhitespaceBefore = needsWhitespaceBefore;
|
---|
8 | exports.needsWhitespaceAfter = needsWhitespaceAfter;
|
---|
9 | exports.needsParens = needsParens;
|
---|
10 |
|
---|
11 | var whitespace = require("./whitespace");
|
---|
12 |
|
---|
13 | var parens = require("./parentheses");
|
---|
14 |
|
---|
15 | var t = require("@babel/types");
|
---|
16 |
|
---|
17 | function expandAliases(obj) {
|
---|
18 | const newObj = {};
|
---|
19 |
|
---|
20 | function add(type, func) {
|
---|
21 | const fn = newObj[type];
|
---|
22 | newObj[type] = fn ? function (node, parent, stack) {
|
---|
23 | const result = fn(node, parent, stack);
|
---|
24 | return result == null ? func(node, parent, stack) : result;
|
---|
25 | } : func;
|
---|
26 | }
|
---|
27 |
|
---|
28 | for (const type of Object.keys(obj)) {
|
---|
29 | const aliases = t.FLIPPED_ALIAS_KEYS[type];
|
---|
30 |
|
---|
31 | if (aliases) {
|
---|
32 | for (const alias of aliases) {
|
---|
33 | add(alias, obj[type]);
|
---|
34 | }
|
---|
35 | } else {
|
---|
36 | add(type, obj[type]);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | return newObj;
|
---|
41 | }
|
---|
42 |
|
---|
43 | const expandedParens = expandAliases(parens);
|
---|
44 | const expandedWhitespaceNodes = expandAliases(whitespace.nodes);
|
---|
45 | const expandedWhitespaceList = expandAliases(whitespace.list);
|
---|
46 |
|
---|
47 | function find(obj, node, parent, printStack) {
|
---|
48 | const fn = obj[node.type];
|
---|
49 | return fn ? fn(node, parent, printStack) : null;
|
---|
50 | }
|
---|
51 |
|
---|
52 | function isOrHasCallExpression(node) {
|
---|
53 | if (t.isCallExpression(node)) {
|
---|
54 | return true;
|
---|
55 | }
|
---|
56 |
|
---|
57 | return t.isMemberExpression(node) && isOrHasCallExpression(node.object);
|
---|
58 | }
|
---|
59 |
|
---|
60 | function needsWhitespace(node, parent, type) {
|
---|
61 | if (!node) return 0;
|
---|
62 |
|
---|
63 | if (t.isExpressionStatement(node)) {
|
---|
64 | node = node.expression;
|
---|
65 | }
|
---|
66 |
|
---|
67 | let linesInfo = find(expandedWhitespaceNodes, node, parent);
|
---|
68 |
|
---|
69 | if (!linesInfo) {
|
---|
70 | const items = find(expandedWhitespaceList, node, parent);
|
---|
71 |
|
---|
72 | if (items) {
|
---|
73 | for (let i = 0; i < items.length; i++) {
|
---|
74 | linesInfo = needsWhitespace(items[i], node, type);
|
---|
75 | if (linesInfo) break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (typeof linesInfo === "object" && linesInfo !== null) {
|
---|
81 | return linesInfo[type] || 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | return 0;
|
---|
85 | }
|
---|
86 |
|
---|
87 | function needsWhitespaceBefore(node, parent) {
|
---|
88 | return needsWhitespace(node, parent, "before");
|
---|
89 | }
|
---|
90 |
|
---|
91 | function needsWhitespaceAfter(node, parent) {
|
---|
92 | return needsWhitespace(node, parent, "after");
|
---|
93 | }
|
---|
94 |
|
---|
95 | function needsParens(node, parent, printStack) {
|
---|
96 | if (!parent) return false;
|
---|
97 |
|
---|
98 | if (t.isNewExpression(parent) && parent.callee === node) {
|
---|
99 | if (isOrHasCallExpression(node)) return true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | return find(expandedParens, node, parent, printStack);
|
---|
103 | } |
---|