UI for Zipcoin Blue

utils.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 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. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. var path = require("path");
  20. var tsutils_1 = require("tsutils");
  21. var ts = require("typescript");
  22. function getSourceFile(fileName, source) {
  23. var normalizedName = path.normalize(fileName).replace(/\\/g, "/");
  24. return ts.createSourceFile(normalizedName, source, ts.ScriptTarget.ES5, /*setParentNodes*/ true);
  25. }
  26. exports.getSourceFile = getSourceFile;
  27. /** @deprecated See IDisabledInterval. */
  28. function doesIntersect(failure, disabledIntervals) {
  29. return disabledIntervals.some(function (interval) {
  30. var maxStart = Math.max(interval.startPosition, failure.getStartPosition().getPosition());
  31. var minEnd = Math.min(interval.endPosition, failure.getEndPosition().getPosition());
  32. return maxStart <= minEnd;
  33. });
  34. }
  35. exports.doesIntersect = doesIntersect;
  36. /**
  37. * @returns true if any modifier kinds passed along exist in the given modifiers array
  38. *
  39. * @deprecated use `hasModifier` from `tsutils`
  40. */
  41. function hasModifier(modifiers) {
  42. var modifierKinds = [];
  43. for (var _i = 1; _i < arguments.length; _i++) {
  44. modifierKinds[_i - 1] = arguments[_i];
  45. }
  46. if (modifiers === undefined || modifierKinds.length === 0) {
  47. return false;
  48. }
  49. return modifiers.some(function (m) { return modifierKinds.some(function (k) { return m.kind === k; }); });
  50. }
  51. exports.hasModifier = hasModifier;
  52. /**
  53. * Determines if the appropriate bit in the parent (VariableDeclarationList) is set,
  54. * which indicates this is a "let" or "const".
  55. *
  56. * @deprecated use `isBlockScopedVariableDeclarationList` from `tsutils`
  57. */
  58. function isBlockScopedVariable(node) {
  59. if (node.kind === ts.SyntaxKind.VariableDeclaration) {
  60. var parent = node.parent;
  61. return parent.kind === ts.SyntaxKind.CatchClause || tsutils_1.isBlockScopedVariableDeclarationList(parent);
  62. }
  63. else {
  64. return tsutils_1.isBlockScopedVariableDeclarationList(node.declarationList);
  65. }
  66. }
  67. exports.isBlockScopedVariable = isBlockScopedVariable;
  68. /** @deprecated use `isBlockScopedVariableDeclarationList` and `getDeclarationOfBindingElement` from `tsutils` */
  69. function isBlockScopedBindingElement(node) {
  70. var variableDeclaration = getBindingElementVariableDeclaration(node); // tslint:disable-line:deprecation
  71. // if no variable declaration, it must be a function param, which is block scoped
  72. return (variableDeclaration === null) || isBlockScopedVariable(variableDeclaration); // tslint:disable-line:deprecation
  73. }
  74. exports.isBlockScopedBindingElement = isBlockScopedBindingElement;
  75. /** @deprecated use `getDeclarationOfBindingElement` from `tsutils` */
  76. function getBindingElementVariableDeclaration(node) {
  77. var currentParent = node.parent;
  78. while (currentParent.kind !== ts.SyntaxKind.VariableDeclaration) {
  79. if (currentParent.parent === undefined) {
  80. return null; // function parameter, no variable declaration
  81. }
  82. else {
  83. currentParent = currentParent.parent;
  84. }
  85. }
  86. return currentParent;
  87. }
  88. exports.getBindingElementVariableDeclaration = getBindingElementVariableDeclaration;
  89. /**
  90. * Finds a child of a given node with a given kind.
  91. * Note: This uses `node.getChildren()`, which does extra parsing work to include tokens.
  92. *
  93. * @deprecated use `getChildOfKind` from `tsutils`
  94. */
  95. function childOfKind(node, kind) {
  96. return node.getChildren().find(function (child) { return child.kind === kind; });
  97. }
  98. exports.childOfKind = childOfKind;
  99. /**
  100. * @returns true if some ancestor of `node` satisfies `predicate`, including `node` itself.
  101. *
  102. * @deprecated no longer used, use a `while` loop instead
  103. */
  104. function someAncestor(node, predicate) {
  105. return predicate(node) || (node.parent !== undefined && someAncestor(node.parent, predicate)); // tslint:disable-line:deprecation
  106. }
  107. exports.someAncestor = someAncestor;
  108. function ancestorWhere(node, predicate) {
  109. var cur = node;
  110. do {
  111. if (predicate(cur)) {
  112. return cur;
  113. }
  114. cur = cur.parent;
  115. } while (cur !== undefined);
  116. return undefined;
  117. }
  118. exports.ancestorWhere = ancestorWhere;
  119. /** @deprecated use `isBinaryExpression(node) && isAssignmentKind(node.operatorToken.kind)` with functions from `tsutils` */
  120. function isAssignment(node) {
  121. if (node.kind === ts.SyntaxKind.BinaryExpression) {
  122. var binaryExpression = node;
  123. return binaryExpression.operatorToken.kind >= ts.SyntaxKind.FirstAssignment
  124. && binaryExpression.operatorToken.kind <= ts.SyntaxKind.LastAssignment;
  125. }
  126. else {
  127. return false;
  128. }
  129. }
  130. exports.isAssignment = isAssignment;
  131. /**
  132. * Bitwise check for node flags.
  133. *
  134. * @deprecated use `isNodeFlagSet` from `tsutils`
  135. */
  136. function isNodeFlagSet(node, flagToCheck) {
  137. // tslint:disable-next-line:no-bitwise
  138. return (node.flags & flagToCheck) !== 0;
  139. }
  140. exports.isNodeFlagSet = isNodeFlagSet;
  141. /**
  142. * Bitwise check for combined node flags.
  143. *
  144. * @deprecated no longer used
  145. */
  146. function isCombinedNodeFlagSet(node, flagToCheck) {
  147. // tslint:disable-next-line:no-bitwise
  148. return (ts.getCombinedNodeFlags(node) & flagToCheck) !== 0;
  149. }
  150. exports.isCombinedNodeFlagSet = isCombinedNodeFlagSet;
  151. /**
  152. * Bitwise check for combined modifier flags.
  153. *
  154. * @deprecated no longer used
  155. */
  156. function isCombinedModifierFlagSet(node, flagToCheck) {
  157. // tslint:disable-next-line:no-bitwise
  158. return (ts.getCombinedModifierFlags(node) & flagToCheck) !== 0;
  159. }
  160. exports.isCombinedModifierFlagSet = isCombinedModifierFlagSet;
  161. /**
  162. * Bitwise check for type flags.
  163. *
  164. * @deprecated use `isTypeFlagSet` from `tsutils`
  165. */
  166. function isTypeFlagSet(type, flagToCheck) {
  167. // tslint:disable-next-line:no-bitwise
  168. return (type.flags & flagToCheck) !== 0;
  169. }
  170. exports.isTypeFlagSet = isTypeFlagSet;
  171. /**
  172. * Bitwise check for symbol flags.
  173. *
  174. * @deprecated use `isSymbolFlagSet` from `tsutils`
  175. */
  176. function isSymbolFlagSet(symbol, flagToCheck) {
  177. // tslint:disable-next-line:no-bitwise
  178. return (symbol.flags & flagToCheck) !== 0;
  179. }
  180. exports.isSymbolFlagSet = isSymbolFlagSet;
  181. /**
  182. * Bitwise check for object flags.
  183. * Does not work with TypeScript 2.0.x
  184. *
  185. * @deprecated use `isObjectFlagSet` from `tsutils`
  186. */
  187. function isObjectFlagSet(objectType, flagToCheck) {
  188. // tslint:disable-next-line:no-bitwise
  189. return (objectType.objectFlags & flagToCheck) !== 0;
  190. }
  191. exports.isObjectFlagSet = isObjectFlagSet;
  192. /**
  193. * @returns true if decl is a nested module declaration, i.e. represents a segment of a dotted module path.
  194. *
  195. * @deprecated use `decl.parent!.kind === ts.SyntaxKind.ModuleDeclaration`
  196. */
  197. function isNestedModuleDeclaration(decl) {
  198. // in a declaration expression like 'module a.b.c' - 'a' is the top level module declaration node and 'b' and 'c'
  199. // are nested therefore we can depend that a node's position will only match with its name's position for nested
  200. // nodes
  201. return decl.name.pos === decl.pos;
  202. }
  203. exports.isNestedModuleDeclaration = isNestedModuleDeclaration;
  204. function unwrapParentheses(node) {
  205. while (node.kind === ts.SyntaxKind.ParenthesizedExpression) {
  206. node = node.expression;
  207. }
  208. return node;
  209. }
  210. exports.unwrapParentheses = unwrapParentheses;
  211. /** @deprecated use `isFunctionScopeBoundary` from `tsutils` */
  212. function isScopeBoundary(node) {
  213. return node.kind === ts.SyntaxKind.FunctionDeclaration
  214. || node.kind === ts.SyntaxKind.FunctionExpression
  215. || node.kind === ts.SyntaxKind.PropertyAssignment
  216. || node.kind === ts.SyntaxKind.ShorthandPropertyAssignment
  217. || node.kind === ts.SyntaxKind.MethodDeclaration
  218. || node.kind === ts.SyntaxKind.Constructor
  219. || node.kind === ts.SyntaxKind.ModuleDeclaration
  220. || node.kind === ts.SyntaxKind.ArrowFunction
  221. || node.kind === ts.SyntaxKind.ParenthesizedExpression
  222. || node.kind === ts.SyntaxKind.ClassDeclaration
  223. || node.kind === ts.SyntaxKind.ClassExpression
  224. || node.kind === ts.SyntaxKind.InterfaceDeclaration
  225. || node.kind === ts.SyntaxKind.GetAccessor
  226. || node.kind === ts.SyntaxKind.SetAccessor
  227. || node.kind === ts.SyntaxKind.SourceFile && ts.isExternalModule(node);
  228. }
  229. exports.isScopeBoundary = isScopeBoundary;
  230. /** @deprecated use `isBlockScopeBoundary` from `tsutils` */
  231. function isBlockScopeBoundary(node) {
  232. return isScopeBoundary(node) // tslint:disable-line:deprecation
  233. || node.kind === ts.SyntaxKind.Block
  234. || isLoop(node) // tslint:disable-line:deprecation
  235. || node.kind === ts.SyntaxKind.WithStatement
  236. || node.kind === ts.SyntaxKind.SwitchStatement
  237. || node.parent !== undefined
  238. && (node.parent.kind === ts.SyntaxKind.TryStatement
  239. || node.parent.kind === ts.SyntaxKind.IfStatement);
  240. }
  241. exports.isBlockScopeBoundary = isBlockScopeBoundary;
  242. /** @deprecated use `isIterationStatement` from `tsutils` or `typescript` */
  243. function isLoop(node) {
  244. return node.kind === ts.SyntaxKind.DoStatement
  245. || node.kind === ts.SyntaxKind.WhileStatement
  246. || node.kind === ts.SyntaxKind.ForStatement
  247. || node.kind === ts.SyntaxKind.ForInStatement
  248. || node.kind === ts.SyntaxKind.ForOfStatement;
  249. }
  250. exports.isLoop = isLoop;
  251. /**
  252. * @returns Whether node is a numeric expression.
  253. */
  254. function isNumeric(node) {
  255. while (tsutils_1.isPrefixUnaryExpression(node) &&
  256. (node.operator === ts.SyntaxKind.PlusToken || node.operator === ts.SyntaxKind.MinusToken)) {
  257. node = node.operand;
  258. }
  259. return node.kind === ts.SyntaxKind.NumericLiteral ||
  260. tsutils_1.isIdentifier(node) && (node.text === "NaN" || node.text === "Infinity");
  261. }
  262. exports.isNumeric = isNumeric;
  263. /**
  264. * Iterate over all tokens of `node`
  265. *
  266. * @description JsDoc comments are treated like regular comments and only visited if `skipTrivia` === false.
  267. *
  268. * @param node The node whose tokens should be visited
  269. * @param skipTrivia If set to false all trivia preceeding `node` or any of its children is included
  270. * @param cb Is called for every token of `node`. It gets the full text of the SourceFile and the position of the token within that text.
  271. * @param filter If provided, will be called for every Node and Token found. If it returns false `cb` will not be called for this subtree.
  272. *
  273. * @deprecated use `forEachToken` or `forEachTokenWithTrivia` from `tsutils`
  274. */
  275. function forEachToken(node, skipTrivia, cb, filter) {
  276. // this function will most likely be called with SourceFile anyways, so there is no need for an additional parameter
  277. var sourceFile = node.getSourceFile();
  278. var fullText = sourceFile.text;
  279. var iterateFn = filter === undefined ? iterateChildren : iterateWithFilter;
  280. var handleTrivia = skipTrivia ? undefined : createTriviaHandler(sourceFile, cb);
  281. iterateFn(node);
  282. // this function is used to save the if condition for the common case where no filter is provided
  283. function iterateWithFilter(child) {
  284. if (filter(child)) {
  285. return iterateChildren(child);
  286. }
  287. }
  288. function iterateChildren(child) {
  289. if (child.kind < ts.SyntaxKind.FirstNode ||
  290. // for backwards compatibility to typescript 2.0.10
  291. // JsxText was no Token, but a Node in that version
  292. child.kind === ts.SyntaxKind.JsxText) {
  293. // we found a token, tokens have no children, stop recursing here
  294. return callback(child);
  295. }
  296. /* Exclude everything contained in JsDoc, it will be handled with the other trivia anyway.
  297. * When we would handle JsDoc tokens like regular ones, we would scan some trivia multiple times.
  298. * Even worse, we would scan for trivia inside the JsDoc comment, which yields unexpected results.*/
  299. if (child.kind !== ts.SyntaxKind.JSDocComment) {
  300. // recurse into Node's children to find tokens
  301. return child.getChildren(sourceFile).forEach(iterateFn);
  302. }
  303. }
  304. function callback(token) {
  305. var tokenStart = token.getStart(sourceFile);
  306. if (!skipTrivia && tokenStart !== token.pos) {
  307. // we only have to handle trivia before each token, because there is nothing after EndOfFileToken
  308. handleTrivia(token.pos, tokenStart, token);
  309. }
  310. return cb(fullText, token.kind, { tokenStart: tokenStart, fullStart: token.pos, end: token.end }, token.parent);
  311. }
  312. }
  313. exports.forEachToken = forEachToken;
  314. function createTriviaHandler(sourceFile, cb) {
  315. var fullText = sourceFile.text;
  316. var scanner = ts.createScanner(sourceFile.languageVersion, false, sourceFile.languageVariant, fullText);
  317. /**
  318. * Scan the specified range to get all trivia tokens.
  319. * This includes trailing trivia of the last token and the leading trivia of the current token
  320. */
  321. function handleTrivia(start, end, token) {
  322. var parent = token.parent;
  323. // prevent false positives by not scanning inside JsxText
  324. if (!canHaveLeadingTrivia(token.kind, parent)) {
  325. return;
  326. }
  327. scanner.setTextPos(start);
  328. var position;
  329. // we only get here if start !== end, so we can scan at least one time
  330. do {
  331. var kind = scanner.scan();
  332. position = scanner.getTextPos();
  333. cb(fullText, kind, { tokenStart: scanner.getTokenPos(), end: position, fullStart: start }, parent);
  334. } while (position < end);
  335. }
  336. return handleTrivia;
  337. }
  338. /**
  339. * Iterate over all comments owned by `node` or its children
  340. *
  341. * @deprecated use `forEachComment` from `tsutils`
  342. */
  343. function forEachComment(node, cb) {
  344. /* Visit all tokens and skip trivia.
  345. Comment ranges between tokens are parsed without the need of a scanner.
  346. forEachToken also does intentionally not pay attention to the correct comment ownership of nodes as it always
  347. scans all trivia before each token, which could include trailing comments of the previous token.
  348. Comment onwership is done right in this function*/
  349. return forEachToken(node, true, function (fullText, tokenKind, pos, parent) {
  350. // don't search for comments inside JsxText
  351. if (canHaveLeadingTrivia(tokenKind, parent)) {
  352. // Comments before the first token (pos.fullStart === 0) are all considered leading comments, so no need for special treatment
  353. var comments = ts.getLeadingCommentRanges(fullText, pos.fullStart);
  354. if (comments !== undefined) {
  355. for (var _i = 0, comments_1 = comments; _i < comments_1.length; _i++) {
  356. var comment = comments_1[_i];
  357. cb(fullText, comment.kind, { fullStart: pos.fullStart, tokenStart: comment.pos, end: comment.end });
  358. }
  359. }
  360. }
  361. if (canHaveTrailingTrivia(tokenKind, parent)) {
  362. var comments = ts.getTrailingCommentRanges(fullText, pos.end);
  363. if (comments !== undefined) {
  364. for (var _a = 0, comments_2 = comments; _a < comments_2.length; _a++) {
  365. var comment = comments_2[_a];
  366. cb(fullText, comment.kind, { fullStart: pos.fullStart, tokenStart: comment.pos, end: comment.end });
  367. }
  368. }
  369. }
  370. });
  371. }
  372. exports.forEachComment = forEachComment;
  373. /** Exclude leading positions that would lead to scanning for trivia inside JsxText */
  374. function canHaveLeadingTrivia(tokenKind, parent) {
  375. switch (tokenKind) {
  376. case ts.SyntaxKind.JsxText:
  377. return false; // there is no trivia before JsxText
  378. case ts.SyntaxKind.OpenBraceToken:
  379. // before a JsxExpression inside a JsxElement's body can only be other JsxChild, but no trivia
  380. return parent.kind !== ts.SyntaxKind.JsxExpression || parent.parent.kind !== ts.SyntaxKind.JsxElement;
  381. case ts.SyntaxKind.LessThanToken:
  382. switch (parent.kind) {
  383. case ts.SyntaxKind.JsxClosingElement:
  384. return false; // would be inside the element body
  385. case ts.SyntaxKind.JsxOpeningElement:
  386. case ts.SyntaxKind.JsxSelfClosingElement:
  387. // there can only be leading trivia if we are at the end of the top level element
  388. return parent.parent.parent.kind !== ts.SyntaxKind.JsxElement;
  389. default:
  390. return true;
  391. }
  392. default:
  393. return true;
  394. }
  395. }
  396. /** Exclude trailing positions that would lead to scanning for trivia inside JsxText */
  397. function canHaveTrailingTrivia(tokenKind, parent) {
  398. switch (tokenKind) {
  399. case ts.SyntaxKind.JsxText:
  400. // there is no trivia after JsxText
  401. return false;
  402. case ts.SyntaxKind.CloseBraceToken:
  403. // after a JsxExpression inside a JsxElement's body can only be other JsxChild, but no trivia
  404. return parent.kind !== ts.SyntaxKind.JsxExpression || parent.parent.kind !== ts.SyntaxKind.JsxElement;
  405. case ts.SyntaxKind.GreaterThanToken:
  406. switch (parent.kind) {
  407. case ts.SyntaxKind.JsxOpeningElement:
  408. return false; // would be inside the element
  409. case ts.SyntaxKind.JsxClosingElement:
  410. case ts.SyntaxKind.JsxSelfClosingElement:
  411. // there can only be trailing trivia if we are at the end of the top level element
  412. return parent.parent.parent.kind !== ts.SyntaxKind.JsxElement;
  413. default:
  414. return true;
  415. }
  416. default:
  417. return true;
  418. }
  419. }
  420. /**
  421. * Checks if there are any comments between `position` and the next non-trivia token
  422. *
  423. * @param text The text to scan
  424. * @param position The position inside `text` where to start scanning. Make sure that this is a valid start position.
  425. * This value is typically obtained from `node.getFullStart()` or `node.getEnd()`
  426. */
  427. function hasCommentAfterPosition(text, position) {
  428. return ts.getTrailingCommentRanges(text, position) !== undefined ||
  429. ts.getLeadingCommentRanges(text, position) !== undefined;
  430. }
  431. exports.hasCommentAfterPosition = hasCommentAfterPosition;
  432. function getEqualsKind(node) {
  433. switch (node.kind) {
  434. case ts.SyntaxKind.EqualsEqualsToken:
  435. return { isPositive: true, isStrict: false };
  436. case ts.SyntaxKind.EqualsEqualsEqualsToken:
  437. return { isPositive: true, isStrict: true };
  438. case ts.SyntaxKind.ExclamationEqualsToken:
  439. return { isPositive: false, isStrict: false };
  440. case ts.SyntaxKind.ExclamationEqualsEqualsToken:
  441. return { isPositive: false, isStrict: true };
  442. default:
  443. return undefined;
  444. }
  445. }
  446. exports.getEqualsKind = getEqualsKind;
  447. function isStrictNullChecksEnabled(options) {
  448. return options.strictNullChecks === true ||
  449. (options.strict === true && options.strictNullChecks !== false);
  450. }
  451. exports.isStrictNullChecksEnabled = isStrictNullChecksEnabled;
  452. function isNegativeNumberLiteral(node) {
  453. return tsutils_1.isPrefixUnaryExpression(node) &&
  454. node.operator === ts.SyntaxKind.MinusToken &&
  455. node.operand.kind === ts.SyntaxKind.NumericLiteral;
  456. }
  457. exports.isNegativeNumberLiteral = isNegativeNumberLiteral;
  458. /** Wrapper for compatibility with typescript@<2.3.1 */
  459. function isWhiteSpace(ch) {
  460. // tslint:disable-next-line
  461. return (ts.isWhiteSpaceLike || ts.isWhiteSpace)(ch);
  462. }
  463. exports.isWhiteSpace = isWhiteSpace;