UNPKG

10.5 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 });
19var fs = require("fs");
20var resolve = require("resolve");
21var ts = require("typescript");
22/**
23 * Enforces the invariant that the input is an array.
24 */
25function arrayify(arg) {
26 if (Array.isArray(arg)) {
27 return arg;
28 }
29 else if (arg != undefined) {
30 return [arg];
31 }
32 else {
33 return [];
34 }
35}
36exports.arrayify = arrayify;
37/**
38 * @deprecated (no longer used)
39 * Enforces the invariant that the input is an object.
40 */
41function objectify(arg) {
42 if (typeof arg === "object" && arg != undefined) {
43 return arg;
44 }
45 else {
46 return {};
47 }
48}
49exports.objectify = objectify;
50function hasOwnProperty(arg, key) {
51 return Object.prototype.hasOwnProperty.call(arg, key);
52}
53exports.hasOwnProperty = hasOwnProperty;
54/**
55 * Replace hyphens in a rule name by upper-casing the letter after them.
56 * E.g. "foo-bar" -> "fooBar"
57 */
58function camelize(stringWithHyphens) {
59 return stringWithHyphens.replace(/-(.)/g, function (_, nextLetter) {
60 return nextLetter.toUpperCase();
61 });
62}
63exports.camelize = camelize;
64function isUpperCase(str) {
65 return str === str.toUpperCase();
66}
67exports.isUpperCase = isUpperCase;
68function isLowerCase(str) {
69 return str === str.toLowerCase();
70}
71exports.isLowerCase = isLowerCase;
72/**
73 * Removes leading indents from a template string without removing all leading whitespace
74 */
75function dedent(strings) {
76 var values = [];
77 for (var _i = 1; _i < arguments.length; _i++) {
78 values[_i - 1] = arguments[_i];
79 }
80 var fullString = strings.reduce(function (accumulator, str, i) { return "" + accumulator + values[i - 1] + str; });
81 // match all leading spaces/tabs at the start of each line
82 var match = fullString.match(/^[ \t]*(?=\S)/gm);
83 if (match === null) {
84 // e.g. if the string is empty or all whitespace.
85 return fullString;
86 }
87 // find the smallest indent, we don't want to remove all leading whitespace
88 var indent = Math.min.apply(Math, match.map(function (el) { return el.length; }));
89 var regexp = new RegExp("^[ \\t]{" + indent + "}", "gm");
90 fullString = indent > 0 ? fullString.replace(regexp, "") : fullString;
91 return fullString;
92}
93exports.dedent = dedent;
94/**
95 * Strip comments from file content.
96 */
97function stripComments(content) {
98 /**
99 * First capturing group matches double quoted string
100 * Second matches single quotes string
101 * Third matches block comments
102 * Fourth matches line comments
103 */
104 var regexp = /("(?:[^\\\"]*(?:\\.)?)*")|('(?:[^\\\']*(?:\\.)?)*')|(\/\*(?:\r?\n|.)*?\*\/)|(\/{2,}.*?(?:(?:\r?\n)|$))/g;
105 var result = content.replace(regexp, function (match, _m1, _m2, m3, m4) {
106 // Only one of m1, m2, m3, m4 matches
107 if (m3 !== undefined) {
108 // A block comment. Replace with nothing
109 return "";
110 }
111 else if (m4 !== undefined) {
112 // A line comment. If it ends in \r?\n then keep it.
113 var length = m4.length;
114 if (length > 2 && m4[length - 1] === "\n") {
115 return m4[length - 2] === "\r" ? "\r\n" : "\n";
116 }
117 else {
118 return "";
119 }
120 }
121 else {
122 // We match a string
123 return match;
124 }
125 });
126 return result;
127}
128exports.stripComments = stripComments;
129/**
130 * Escapes all special characters in RegExp pattern to avoid broken regular expressions and ensure proper matches
131 */
132function escapeRegExp(re) {
133 return re.replace(/[.+*?|^$[\]{}()\\]/g, "\\$&");
134}
135exports.escapeRegExp = escapeRegExp;
136function arraysAreEqual(a, b, eq) {
137 return (a === b ||
138 (a !== undefined &&
139 b !== undefined &&
140 a.length === b.length &&
141 a.every(function (x, idx) { return eq(x, b[idx]); })));
142}
143exports.arraysAreEqual = arraysAreEqual;
144/** Returns the first non-`undefined` result. */
145function find(inputs, getResult) {
146 for (var _i = 0, inputs_1 = inputs; _i < inputs_1.length; _i++) {
147 var element = inputs_1[_i];
148 var result = getResult(element);
149 if (result !== undefined) {
150 return result;
151 }
152 }
153 return undefined;
154}
155exports.find = find;
156/** Returns an array that is the concatenation of all output arrays. */
157function flatMap(inputs, getOutputs) {
158 var out = [];
159 for (var i = 0; i < inputs.length; i++) {
160 out.push.apply(out, getOutputs(inputs[i], i));
161 }
162 return out;
163}
164exports.flatMap = flatMap;
165/** Returns an array of all outputs that are not `undefined`. */
166function mapDefined(inputs, getOutput) {
167 var out = [];
168 for (var _i = 0, inputs_2 = inputs; _i < inputs_2.length; _i++) {
169 var input = inputs_2[_i];
170 var output = getOutput(input);
171 if (output !== undefined) {
172 out.push(output);
173 }
174 }
175 return out;
176}
177exports.mapDefined = mapDefined;
178function readBufferWithDetectedEncoding(buffer) {
179 switch (detectBufferEncoding(buffer)) {
180 case "utf8":
181 return buffer.toString();
182 case "utf8-bom":
183 return buffer.toString("utf-8", 2);
184 case "utf16le":
185 return buffer.toString("utf16le", 2);
186 case "utf16be":
187 // Round down to nearest multiple of 2.
188 var len = buffer.length & ~1; // tslint:disable-line no-bitwise
189 // Flip all byte pairs, then read as little-endian.
190 for (var i = 0; i < len; i += 2) {
191 var temp = buffer[i];
192 buffer[i] = buffer[i + 1];
193 buffer[i + 1] = temp;
194 }
195 return buffer.toString("utf16le", 2);
196 }
197}
198exports.readBufferWithDetectedEncoding = readBufferWithDetectedEncoding;
199function detectBufferEncoding(buffer, length) {
200 if (length === void 0) { length = buffer.length; }
201 if (length < 2) {
202 return "utf8";
203 }
204 switch (buffer[0]) {
205 case 0xef:
206 if (buffer[1] === 0xbb && length >= 3 && buffer[2] === 0xbf) {
207 return "utf8-bom";
208 }
209 break;
210 case 0xfe:
211 if (buffer[1] === 0xff) {
212 return "utf16be";
213 }
214 break;
215 case 0xff:
216 if (buffer[1] === 0xfe) {
217 return "utf16le";
218 }
219 }
220 return "utf8";
221}
222exports.detectBufferEncoding = detectBufferEncoding;
223// converts Windows normalized paths (with backwards slash `\`) to paths used by TypeScript (with forward slash `/`)
224function denormalizeWinPath(path) {
225 return path.replace(/\\/g, "/");
226}
227exports.denormalizeWinPath = denormalizeWinPath;
228function isPascalCased(name) {
229 return isUpperCase(name[0]) && !name.includes("_") && !name.includes("-");
230}
231exports.isPascalCased = isPascalCased;
232function isCamelCased(name) {
233 return isLowerCase(name[0]) && !name.includes("_") && !name.includes("-");
234}
235exports.isCamelCased = isCamelCased;
236function isSeparatorCased(name, disallowedSeparator) {
237 for (var i = 0; i < name.length; i++) {
238 var c = name.charAt(i);
239 if (c === disallowedSeparator || !isLowerCase(c)) {
240 return false;
241 }
242 }
243 return true;
244}
245function isKebabCased(name) {
246 return isSeparatorCased(name, "_");
247}
248exports.isKebabCased = isKebabCased;
249function isSnakeCased(name) {
250 return isSeparatorCased(name, "-");
251}
252exports.isSnakeCased = isSnakeCased;
253/**
254 * Tries to resolve a package by name, optionally relative to a file path. If the
255 * file path is under a symlink, it tries to resolve the package under both the real path and under
256 * the symlink path.
257 */
258function tryResolvePackage(packageName, relativeTo) {
259 var realRelativeToPath = relativeTo !== undefined ? fs.realpathSync(relativeTo) : undefined;
260 var resolvedPath = tryResolveSync(packageName, realRelativeToPath);
261 if (resolvedPath === undefined) {
262 resolvedPath = tryResolveSync(packageName, relativeTo);
263 }
264 return resolvedPath;
265}
266exports.tryResolvePackage = tryResolvePackage;
267/**
268 * Calls `resolve.sync` and if it fails, it returns `undefined`
269 */
270function tryResolveSync(packageName, relativeTo) {
271 try {
272 return resolve.sync(packageName, { basedir: relativeTo });
273 }
274 catch (_a) {
275 return undefined;
276 }
277}
278/**
279 * @deprecated Copied from tsutils 2.27.2. This will be removed once TSLint requires tsutils > 3.0.
280 */
281function isFunctionScopeBoundary(node) {
282 switch (node.kind) {
283 case ts.SyntaxKind.FunctionExpression:
284 case ts.SyntaxKind.ArrowFunction:
285 case ts.SyntaxKind.Constructor:
286 case ts.SyntaxKind.ModuleDeclaration:
287 case ts.SyntaxKind.ClassDeclaration:
288 case ts.SyntaxKind.ClassExpression:
289 case ts.SyntaxKind.EnumDeclaration:
290 case ts.SyntaxKind.MethodDeclaration:
291 case ts.SyntaxKind.FunctionDeclaration:
292 case ts.SyntaxKind.GetAccessor:
293 case ts.SyntaxKind.SetAccessor:
294 case ts.SyntaxKind.InterfaceDeclaration:
295 case ts.SyntaxKind.TypeAliasDeclaration:
296 case ts.SyntaxKind.MethodSignature:
297 case ts.SyntaxKind.CallSignature:
298 case ts.SyntaxKind.ConstructSignature:
299 case ts.SyntaxKind.ConstructorType:
300 case ts.SyntaxKind.FunctionType:
301 case ts.SyntaxKind.MappedType:
302 case ts.SyntaxKind.ConditionalType:
303 return true;
304 case ts.SyntaxKind.SourceFile:
305 // if SourceFile is no module, it contributes to the global scope and is therefore no scope boundary
306 // tslint:disable:no-angle-bracket-type-assertion Code copied as from tsutils as is.
307 return ts.isExternalModule(node);
308 // tslint:enable:no-angle-bracket-type-assertopn
309 default:
310 return false;
311 }
312}
313exports.isFunctionScopeBoundary = isFunctionScopeBoundary;