source: trip-planner-front/node_modules/@babel/generator/lib/node/index.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.4 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.needsWhitespace = needsWhitespace;
7exports.needsWhitespaceBefore = needsWhitespaceBefore;
8exports.needsWhitespaceAfter = needsWhitespaceAfter;
9exports.needsParens = needsParens;
10
11var whitespace = require("./whitespace");
12
13var parens = require("./parentheses");
14
15var t = require("@babel/types");
16
17function 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
43const expandedParens = expandAliases(parens);
44const expandedWhitespaceNodes = expandAliases(whitespace.nodes);
45const expandedWhitespaceList = expandAliases(whitespace.list);
46
47function find(obj, node, parent, printStack) {
48 const fn = obj[node.type];
49 return fn ? fn(node, parent, printStack) : null;
50}
51
52function isOrHasCallExpression(node) {
53 if (t.isCallExpression(node)) {
54 return true;
55 }
56
57 return t.isMemberExpression(node) && isOrHasCallExpression(node.object);
58}
59
60function 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
87function needsWhitespaceBefore(node, parent) {
88 return needsWhitespace(node, parent, "before");
89}
90
91function needsWhitespaceAfter(node, parent) {
92 return needsWhitespace(node, parent, "after");
93}
94
95function 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}
Note: See TracBrowser for help on using the repository browser.