UNPKG

4.84 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.nodes = void 0;
7var _t = require("@babel/types");
8const {
9 FLIPPED_ALIAS_KEYS,
10 isArrayExpression,
11 isAssignmentExpression,
12 isBinary,
13 isBlockStatement,
14 isCallExpression,
15 isFunction,
16 isIdentifier,
17 isLiteral,
18 isMemberExpression,
19 isObjectExpression,
20 isOptionalCallExpression,
21 isOptionalMemberExpression,
22 isStringLiteral
23} = _t;
24function crawlInternal(node, state) {
25 if (!node) return state;
26 if (isMemberExpression(node) || isOptionalMemberExpression(node)) {
27 crawlInternal(node.object, state);
28 if (node.computed) crawlInternal(node.property, state);
29 } else if (isBinary(node) || isAssignmentExpression(node)) {
30 crawlInternal(node.left, state);
31 crawlInternal(node.right, state);
32 } else if (isCallExpression(node) || isOptionalCallExpression(node)) {
33 state.hasCall = true;
34 crawlInternal(node.callee, state);
35 } else if (isFunction(node)) {
36 state.hasFunction = true;
37 } else if (isIdentifier(node)) {
38 state.hasHelper = state.hasHelper || node.callee && isHelper(node.callee);
39 }
40 return state;
41}
42function crawl(node) {
43 return crawlInternal(node, {
44 hasCall: false,
45 hasFunction: false,
46 hasHelper: false
47 });
48}
49function isHelper(node) {
50 if (!node) return false;
51 if (isMemberExpression(node)) {
52 return isHelper(node.object) || isHelper(node.property);
53 } else if (isIdentifier(node)) {
54 return node.name === "require" || node.name.charCodeAt(0) === 95;
55 } else if (isCallExpression(node)) {
56 return isHelper(node.callee);
57 } else if (isBinary(node) || isAssignmentExpression(node)) {
58 return isIdentifier(node.left) && isHelper(node.left) || isHelper(node.right);
59 } else {
60 return false;
61 }
62}
63function isType(node) {
64 return isLiteral(node) || isObjectExpression(node) || isArrayExpression(node) || isIdentifier(node) || isMemberExpression(node);
65}
66const nodes = exports.nodes = {
67 AssignmentExpression(node) {
68 const state = crawl(node.right);
69 if (state.hasCall && state.hasHelper || state.hasFunction) {
70 return state.hasFunction ? 1 | 2 : 2;
71 }
72 },
73 SwitchCase(node, parent) {
74 return (!!node.consequent.length || parent.cases[0] === node ? 1 : 0) | (!node.consequent.length && parent.cases[parent.cases.length - 1] === node ? 2 : 0);
75 },
76 LogicalExpression(node) {
77 if (isFunction(node.left) || isFunction(node.right)) {
78 return 2;
79 }
80 },
81 Literal(node) {
82 if (isStringLiteral(node) && node.value === "use strict") {
83 return 2;
84 }
85 },
86 CallExpression(node) {
87 if (isFunction(node.callee) || isHelper(node)) {
88 return 1 | 2;
89 }
90 },
91 OptionalCallExpression(node) {
92 if (isFunction(node.callee)) {
93 return 1 | 2;
94 }
95 },
96 VariableDeclaration(node) {
97 for (let i = 0; i < node.declarations.length; i++) {
98 const declar = node.declarations[i];
99 let enabled = isHelper(declar.id) && !isType(declar.init);
100 if (!enabled && declar.init) {
101 const state = crawl(declar.init);
102 enabled = isHelper(declar.init) && state.hasCall || state.hasFunction;
103 }
104 if (enabled) {
105 return 1 | 2;
106 }
107 }
108 },
109 IfStatement(node) {
110 if (isBlockStatement(node.consequent)) {
111 return 1 | 2;
112 }
113 }
114};
115nodes.ObjectProperty = nodes.ObjectTypeProperty = nodes.ObjectMethod = function (node, parent) {
116 if (parent.properties[0] === node) {
117 return 1;
118 }
119};
120nodes.ObjectTypeCallProperty = function (node, parent) {
121 var _parent$properties;
122 if (parent.callProperties[0] === node && !((_parent$properties = parent.properties) != null && _parent$properties.length)) {
123 return 1;
124 }
125};
126nodes.ObjectTypeIndexer = function (node, parent) {
127 var _parent$properties2, _parent$callPropertie;
128 if (parent.indexers[0] === node && !((_parent$properties2 = parent.properties) != null && _parent$properties2.length) && !((_parent$callPropertie = parent.callProperties) != null && _parent$callPropertie.length)) {
129 return 1;
130 }
131};
132nodes.ObjectTypeInternalSlot = function (node, parent) {
133 var _parent$properties3, _parent$callPropertie2, _parent$indexers;
134 if (parent.internalSlots[0] === node && !((_parent$properties3 = parent.properties) != null && _parent$properties3.length) && !((_parent$callPropertie2 = parent.callProperties) != null && _parent$callPropertie2.length) && !((_parent$indexers = parent.indexers) != null && _parent$indexers.length)) {
135 return 1;
136 }
137};
138[["Function", true], ["Class", true], ["Loop", true], ["LabeledStatement", true], ["SwitchStatement", true], ["TryStatement", true]].forEach(function ([type, amounts]) {
139 [type].concat(FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) {
140 const ret = amounts ? 1 | 2 : 0;
141 nodes[type] = () => ret;
142 });
143});
144
145//# sourceMappingURL=whitespace.js.map