1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.isFunctionScopeBoundary = exports.newLineWithIndentation = exports.getIndentation = exports.tryResolvePackage = exports.isSnakeCased = exports.isKebabCased = exports.isCamelCased = exports.isPascalCased = exports.denormalizeWinPath = exports.trimSingleQuotes = exports.detectBufferEncoding = exports.readBufferWithDetectedEncoding = exports.mapDefined = exports.flatMap = exports.find = exports.arraysAreEqual = exports.escapeRegExp = exports.stripComments = exports.dedent = exports.isLowerCase = exports.isUpperCase = exports.camelize = exports.hasOwnProperty = exports.objectify = exports.arrayify = void 0;
|
20 | var fs = require("fs");
|
21 | var resolve = require("resolve");
|
22 | var ts = require("typescript");
|
23 |
|
24 |
|
25 |
|
26 | function arrayify(arg) {
|
27 | if (Array.isArray(arg)) {
|
28 | return arg;
|
29 | }
|
30 | else if (arg != undefined) {
|
31 | return [arg];
|
32 | }
|
33 | else {
|
34 | return [];
|
35 | }
|
36 | }
|
37 | exports.arrayify = arrayify;
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function objectify(arg) {
|
43 | if (typeof arg === "object" && arg != undefined) {
|
44 | return arg;
|
45 | }
|
46 | else {
|
47 | return {};
|
48 | }
|
49 | }
|
50 | exports.objectify = objectify;
|
51 | function hasOwnProperty(arg, key) {
|
52 | return Object.prototype.hasOwnProperty.call(arg, key);
|
53 | }
|
54 | exports.hasOwnProperty = hasOwnProperty;
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | function camelize(stringWithHyphens) {
|
60 | return stringWithHyphens.replace(/-(.)/g, function (_, nextLetter) {
|
61 | return nextLetter.toUpperCase();
|
62 | });
|
63 | }
|
64 | exports.camelize = camelize;
|
65 | function isUpperCase(str) {
|
66 | return str === str.toUpperCase();
|
67 | }
|
68 | exports.isUpperCase = isUpperCase;
|
69 | function isLowerCase(str) {
|
70 | return str === str.toLowerCase();
|
71 | }
|
72 | exports.isLowerCase = isLowerCase;
|
73 |
|
74 |
|
75 |
|
76 | function dedent(strings) {
|
77 | var values = [];
|
78 | for (var _i = 1; _i < arguments.length; _i++) {
|
79 | values[_i - 1] = arguments[_i];
|
80 | }
|
81 | var fullString = strings.reduce(function (accumulator, str, i) { return "" + accumulator + values[i - 1] + str; });
|
82 |
|
83 | var match = fullString.match(/^[ \t]*(?=\S)/gm);
|
84 | if (match === null) {
|
85 |
|
86 | return fullString;
|
87 | }
|
88 |
|
89 | var indent = Math.min.apply(Math, match.map(function (el) { return el.length; }));
|
90 | var regexp = new RegExp("^[ \\t]{" + indent + "}", "gm");
|
91 | fullString = indent > 0 ? fullString.replace(regexp, "") : fullString;
|
92 | return fullString;
|
93 | }
|
94 | exports.dedent = dedent;
|
95 |
|
96 |
|
97 |
|
98 | function stripComments(content) {
|
99 | |
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | var regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
|
106 | var result = content.replace(regexp, function (match, _m1, _m2, m3, m4) {
|
107 |
|
108 | if (m3 !== undefined) {
|
109 |
|
110 | return "";
|
111 | }
|
112 | else if (m4 !== undefined) {
|
113 |
|
114 | var length = m4.length;
|
115 | if (length > 2 && m4[length - 1] === "\n") {
|
116 | return m4[length - 2] === "\r" ? "\r\n" : "\n";
|
117 | }
|
118 | else {
|
119 | return "";
|
120 | }
|
121 | }
|
122 | else {
|
123 |
|
124 | return match;
|
125 | }
|
126 | });
|
127 | return result;
|
128 | }
|
129 | exports.stripComments = stripComments;
|
130 |
|
131 |
|
132 |
|
133 | function escapeRegExp(re) {
|
134 | return re.replace(/[.+*?|^$[\]{}()\\]/g, "\\$&");
|
135 | }
|
136 | exports.escapeRegExp = escapeRegExp;
|
137 | function arraysAreEqual(a, b, eq) {
|
138 | return (a === b ||
|
139 | (a !== undefined &&
|
140 | b !== undefined &&
|
141 | a.length === b.length &&
|
142 | a.every(function (x, idx) { return eq(x, b[idx]); })));
|
143 | }
|
144 | exports.arraysAreEqual = arraysAreEqual;
|
145 |
|
146 | function find(inputs, getResult) {
|
147 | for (var _i = 0, inputs_1 = inputs; _i < inputs_1.length; _i++) {
|
148 | var element = inputs_1[_i];
|
149 | var result = getResult(element);
|
150 | if (result !== undefined) {
|
151 | return result;
|
152 | }
|
153 | }
|
154 | return undefined;
|
155 | }
|
156 | exports.find = find;
|
157 |
|
158 | function flatMap(inputs, getOutputs) {
|
159 | var out = [];
|
160 | for (var i = 0; i < inputs.length; i++) {
|
161 | out.push.apply(out, getOutputs(inputs[i], i));
|
162 | }
|
163 | return out;
|
164 | }
|
165 | exports.flatMap = flatMap;
|
166 |
|
167 | function mapDefined(inputs, getOutput) {
|
168 | var out = [];
|
169 | for (var _i = 0, inputs_2 = inputs; _i < inputs_2.length; _i++) {
|
170 | var input = inputs_2[_i];
|
171 | var output = getOutput(input);
|
172 | if (output !== undefined) {
|
173 | out.push(output);
|
174 | }
|
175 | }
|
176 | return out;
|
177 | }
|
178 | exports.mapDefined = mapDefined;
|
179 | function readBufferWithDetectedEncoding(buffer) {
|
180 | switch (detectBufferEncoding(buffer)) {
|
181 | case "utf8":
|
182 | return buffer.toString();
|
183 | case "utf8-bom":
|
184 | return buffer.toString("utf-8", 2);
|
185 | case "utf16le":
|
186 | return buffer.toString("utf16le", 2);
|
187 | case "utf16be":
|
188 |
|
189 | var len = buffer.length & ~1;
|
190 |
|
191 | for (var i = 0; i < len; i += 2) {
|
192 | var temp = buffer[i];
|
193 | buffer[i] = buffer[i + 1];
|
194 | buffer[i + 1] = temp;
|
195 | }
|
196 | return buffer.toString("utf16le", 2);
|
197 | }
|
198 | }
|
199 | exports.readBufferWithDetectedEncoding = readBufferWithDetectedEncoding;
|
200 | function detectBufferEncoding(buffer, length) {
|
201 | if (length === void 0) { length = buffer.length; }
|
202 | if (length < 2) {
|
203 | return "utf8";
|
204 | }
|
205 | switch (buffer[0]) {
|
206 | case 0xef:
|
207 | if (buffer[1] === 0xbb && length >= 3 && buffer[2] === 0xbf) {
|
208 | return "utf8-bom";
|
209 | }
|
210 | break;
|
211 | case 0xfe:
|
212 | if (buffer[1] === 0xff) {
|
213 | return "utf16be";
|
214 | }
|
215 | break;
|
216 | case 0xff:
|
217 | if (buffer[1] === 0xfe) {
|
218 | return "utf16le";
|
219 | }
|
220 | }
|
221 | return "utf8";
|
222 | }
|
223 | exports.detectBufferEncoding = detectBufferEncoding;
|
224 | function trimSingleQuotes(str) {
|
225 | return str.replace(/^'|'$/g, "");
|
226 | }
|
227 | exports.trimSingleQuotes = trimSingleQuotes;
|
228 |
|
229 | function denormalizeWinPath(path) {
|
230 | return path.replace(/\\/g, "/");
|
231 | }
|
232 | exports.denormalizeWinPath = denormalizeWinPath;
|
233 | function isPascalCased(name) {
|
234 | return isUpperCase(name[0]) && !name.includes("_") && !name.includes("-");
|
235 | }
|
236 | exports.isPascalCased = isPascalCased;
|
237 | function isCamelCased(name) {
|
238 | return isLowerCase(name[0]) && !name.includes("_") && !name.includes("-");
|
239 | }
|
240 | exports.isCamelCased = isCamelCased;
|
241 | function isSeparatorCased(name, disallowedSeparator) {
|
242 | for (var i = 0; i < name.length; i++) {
|
243 | var c = name.charAt(i);
|
244 | if (c === disallowedSeparator || !isLowerCase(c)) {
|
245 | return false;
|
246 | }
|
247 | }
|
248 | return true;
|
249 | }
|
250 | function isKebabCased(name) {
|
251 | return isSeparatorCased(name, "_");
|
252 | }
|
253 | exports.isKebabCased = isKebabCased;
|
254 | function isSnakeCased(name) {
|
255 | return isSeparatorCased(name, "-");
|
256 | }
|
257 | exports.isSnakeCased = isSnakeCased;
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | function tryResolvePackage(packageName, relativeTo) {
|
264 | var realRelativeToPath = relativeTo !== undefined ? fs.realpathSync(relativeTo) : undefined;
|
265 | var resolvedPath = tryResolveSync(packageName, realRelativeToPath);
|
266 | if (resolvedPath === undefined) {
|
267 | resolvedPath = tryResolveSync(packageName, relativeTo);
|
268 | }
|
269 | return resolvedPath;
|
270 | }
|
271 | exports.tryResolvePackage = tryResolvePackage;
|
272 |
|
273 |
|
274 |
|
275 | function tryResolveSync(packageName, relativeTo) {
|
276 | try {
|
277 | return resolve.sync(packageName, { basedir: relativeTo });
|
278 | }
|
279 | catch (_a) {
|
280 | return undefined;
|
281 | }
|
282 | }
|
283 |
|
284 |
|
285 |
|
286 | function getIndentation(node, sourceFile) {
|
287 | var text = sourceFile.text.substr(node.pos, node.getStart() - node.pos);
|
288 | var matches = text.match(/([ \t]*)$/);
|
289 | return matches !== null ? matches[1] : "";
|
290 | }
|
291 | exports.getIndentation = getIndentation;
|
292 |
|
293 |
|
294 |
|
295 | function newLineWithIndentation(node, sourceFile, linesCount) {
|
296 | if (linesCount === void 0) { linesCount = 1; }
|
297 | var maybeCarriageReturn = sourceFile.text[sourceFile.getLineEndOfPosition(node.pos) - 1] === "\r" ? "\r" : "";
|
298 | var indentation = getIndentation(node, sourceFile);
|
299 | return "" + (maybeCarriageReturn + "\n").repeat(linesCount) + indentation;
|
300 | }
|
301 | exports.newLineWithIndentation = newLineWithIndentation;
|
302 |
|
303 |
|
304 |
|
305 | function isFunctionScopeBoundary(node) {
|
306 | switch (node.kind) {
|
307 | case ts.SyntaxKind.FunctionExpression:
|
308 | case ts.SyntaxKind.ArrowFunction:
|
309 | case ts.SyntaxKind.Constructor:
|
310 | case ts.SyntaxKind.ModuleDeclaration:
|
311 | case ts.SyntaxKind.ClassDeclaration:
|
312 | case ts.SyntaxKind.ClassExpression:
|
313 | case ts.SyntaxKind.EnumDeclaration:
|
314 | case ts.SyntaxKind.MethodDeclaration:
|
315 | case ts.SyntaxKind.FunctionDeclaration:
|
316 | case ts.SyntaxKind.GetAccessor:
|
317 | case ts.SyntaxKind.SetAccessor:
|
318 | case ts.SyntaxKind.InterfaceDeclaration:
|
319 | case ts.SyntaxKind.TypeAliasDeclaration:
|
320 | case ts.SyntaxKind.MethodSignature:
|
321 | case ts.SyntaxKind.CallSignature:
|
322 | case ts.SyntaxKind.ConstructSignature:
|
323 | case ts.SyntaxKind.ConstructorType:
|
324 | case ts.SyntaxKind.FunctionType:
|
325 | case ts.SyntaxKind.MappedType:
|
326 | case ts.SyntaxKind.ConditionalType:
|
327 | return true;
|
328 | case ts.SyntaxKind.SourceFile:
|
329 |
|
330 |
|
331 | return ts.isExternalModule(node);
|
332 |
|
333 | default:
|
334 | return false;
|
335 | }
|
336 | }
|
337 | exports.isFunctionScopeBoundary = isFunctionScopeBoundary;
|