UNPKG

12.1 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright 2018 Palantir Technologies, Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18Object.defineProperty(exports, "__esModule", { value: true });
19exports.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;
20var fs = require("fs");
21var resolve = require("resolve");
22var ts = require("typescript");
23/**
24 * Enforces the invariant that the input is an array.
25 */
26function 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}
37exports.arrayify = arrayify;
38/**
39 * @deprecated (no longer used)
40 * Enforces the invariant that the input is an object.
41 */
42function objectify(arg) {
43 if (typeof arg === "object" && arg != undefined) {
44 return arg;
45 }
46 else {
47 return {};
48 }
49}
50exports.objectify = objectify;
51function hasOwnProperty(arg, key) {
52 return Object.prototype.hasOwnProperty.call(arg, key);
53}
54exports.hasOwnProperty = hasOwnProperty;
55/**
56 * Replace hyphens in a rule name by upper-casing the letter after them.
57 * E.g. "foo-bar" -> "fooBar"
58 */
59function camelize(stringWithHyphens) {
60 return stringWithHyphens.replace(/-(.)/g, function (_, nextLetter) {
61 return nextLetter.toUpperCase();
62 });
63}
64exports.camelize = camelize;
65function isUpperCase(str) {
66 return str === str.toUpperCase();
67}
68exports.isUpperCase = isUpperCase;
69function isLowerCase(str) {
70 return str === str.toLowerCase();
71}
72exports.isLowerCase = isLowerCase;
73/**
74 * Removes leading indents from a template string without removing all leading whitespace
75 */
76function 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 // match all leading spaces/tabs at the start of each line
83 var match = fullString.match(/^[ \t]*(?=\S)/gm);
84 if (match === null) {
85 // e.g. if the string is empty or all whitespace.
86 return fullString;
87 }
88 // find the smallest indent, we don't want to remove all leading whitespace
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}
94exports.dedent = dedent;
95/**
96 * Strip comments from file content.
97 */
98function stripComments(content) {
99 /**
100 * First capturing group matches double quoted string
101 * Second matches single quotes string
102 * Third matches block comments
103 * Fourth matches line comments
104 */
105 var regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
106 var result = content.replace(regexp, function (match, _m1, _m2, m3, m4) {
107 // Only one of m1, m2, m3, m4 matches
108 if (m3 !== undefined) {
109 // A block comment. Replace with nothing
110 return "";
111 }
112 else if (m4 !== undefined) {
113 // A line comment. If it ends in \r?\n then keep it.
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 // We match a string
124 return match;
125 }
126 });
127 return result;
128}
129exports.stripComments = stripComments;
130/**
131 * Escapes all special characters in RegExp pattern to avoid broken regular expressions and ensure proper matches
132 */
133function escapeRegExp(re) {
134 return re.replace(/[.+*?|^$[\]{}()\\]/g, "\\$&");
135}
136exports.escapeRegExp = escapeRegExp;
137function 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}
144exports.arraysAreEqual = arraysAreEqual;
145/** Returns the first non-`undefined` result. */
146function 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}
156exports.find = find;
157/** Returns an array that is the concatenation of all output arrays. */
158function 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}
165exports.flatMap = flatMap;
166/** Returns an array of all outputs that are not `undefined`. */
167function 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}
178exports.mapDefined = mapDefined;
179function 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 // Round down to nearest multiple of 2.
189 var len = buffer.length & ~1; // tslint:disable-line no-bitwise
190 // Flip all byte pairs, then read as little-endian.
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}
199exports.readBufferWithDetectedEncoding = readBufferWithDetectedEncoding;
200function 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}
223exports.detectBufferEncoding = detectBufferEncoding;
224function trimSingleQuotes(str) {
225 return str.replace(/^'|'$/g, "");
226}
227exports.trimSingleQuotes = trimSingleQuotes;
228// converts Windows normalized paths (with backwards slash `\`) to paths used by TypeScript (with forward slash `/`)
229function denormalizeWinPath(path) {
230 return path.replace(/\\/g, "/");
231}
232exports.denormalizeWinPath = denormalizeWinPath;
233function isPascalCased(name) {
234 return isUpperCase(name[0]) && !name.includes("_") && !name.includes("-");
235}
236exports.isPascalCased = isPascalCased;
237function isCamelCased(name) {
238 return isLowerCase(name[0]) && !name.includes("_") && !name.includes("-");
239}
240exports.isCamelCased = isCamelCased;
241function 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}
250function isKebabCased(name) {
251 return isSeparatorCased(name, "_");
252}
253exports.isKebabCased = isKebabCased;
254function isSnakeCased(name) {
255 return isSeparatorCased(name, "-");
256}
257exports.isSnakeCased = isSnakeCased;
258/**
259 * Tries to resolve a package by name, optionally relative to a file path. If the
260 * file path is under a symlink, it tries to resolve the package under both the real path and under
261 * the symlink path.
262 */
263function 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}
271exports.tryResolvePackage = tryResolvePackage;
272/**
273 * Calls `resolve.sync` and if it fails, it returns `undefined`
274 */
275function tryResolveSync(packageName, relativeTo) {
276 try {
277 return resolve.sync(packageName, { basedir: relativeTo });
278 }
279 catch (_a) {
280 return undefined;
281 }
282}
283/**
284 * Gets the full indentation of the provided node
285 */
286function 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}
291exports.getIndentation = getIndentation;
292/**
293 * Creates x new lines with a proper indentation at the last one based on the provided node
294 */
295function 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}
301exports.newLineWithIndentation = newLineWithIndentation;
302/**
303 * @deprecated Copied from tsutils 2.27.2. This will be removed once TSLint requires tsutils > 3.0.
304 */
305function 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 // if SourceFile is no module, it contributes to the global scope and is therefore no scope boundary
330 // tslint:disable:no-angle-bracket-type-assertion Code copied as from tsutils as is.
331 return ts.isExternalModule(node);
332 // tslint:enable:no-angle-bracket-type-assertopn
333 default:
334 return false;
335 }
336}
337exports.isFunctionScopeBoundary = isFunctionScopeBoundary;