index.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213
  1. // src/comments.ts
  2. import ts2 from "typescript";
  3. // src/tokens.ts
  4. import ts from "typescript";
  5. function forEachToken(node, callback, sourceFile = node.getSourceFile()) {
  6. const queue = [];
  7. while (true) {
  8. if (ts.isTokenKind(node.kind)) {
  9. callback(node);
  10. } else if (
  11. // eslint-disable-next-line deprecation/deprecation -- need for support of TS < 4.7
  12. node.kind !== ts.SyntaxKind.JSDocComment
  13. ) {
  14. const children = node.getChildren(sourceFile);
  15. if (children.length === 1) {
  16. node = children[0];
  17. continue;
  18. }
  19. for (let i = children.length - 1; i >= 0; --i)
  20. queue.push(children[i]);
  21. }
  22. if (queue.length === 0)
  23. break;
  24. node = queue.pop();
  25. }
  26. }
  27. // src/comments.ts
  28. function canHaveTrailingTrivia(token) {
  29. switch (token.kind) {
  30. case ts2.SyntaxKind.CloseBraceToken:
  31. return token.parent.kind !== ts2.SyntaxKind.JsxExpression || !isJsxElementOrFragment(token.parent.parent);
  32. case ts2.SyntaxKind.GreaterThanToken:
  33. switch (token.parent.kind) {
  34. case ts2.SyntaxKind.JsxOpeningElement:
  35. return token.end !== token.parent.end;
  36. case ts2.SyntaxKind.JsxOpeningFragment:
  37. return false;
  38. case ts2.SyntaxKind.JsxSelfClosingElement:
  39. return token.end !== token.parent.end || // if end is not equal, this is part of the type arguments list
  40. !isJsxElementOrFragment(token.parent.parent);
  41. case ts2.SyntaxKind.JsxClosingElement:
  42. case ts2.SyntaxKind.JsxClosingFragment:
  43. return !isJsxElementOrFragment(token.parent.parent.parent);
  44. }
  45. }
  46. return true;
  47. }
  48. function isJsxElementOrFragment(node) {
  49. return node.kind === ts2.SyntaxKind.JsxElement || node.kind === ts2.SyntaxKind.JsxFragment;
  50. }
  51. function forEachComment(node, callback, sourceFile = node.getSourceFile()) {
  52. const fullText = sourceFile.text;
  53. const notJsx = sourceFile.languageVariant !== ts2.LanguageVariant.JSX;
  54. return forEachToken(
  55. node,
  56. (token) => {
  57. if (token.pos === token.end)
  58. return;
  59. if (token.kind !== ts2.SyntaxKind.JsxText)
  60. ts2.forEachLeadingCommentRange(
  61. fullText,
  62. // skip shebang at position 0
  63. token.pos === 0 ? (ts2.getShebang(fullText) ?? "").length : token.pos,
  64. commentCallback
  65. );
  66. if (notJsx || canHaveTrailingTrivia(token))
  67. return ts2.forEachTrailingCommentRange(
  68. fullText,
  69. token.end,
  70. commentCallback
  71. );
  72. },
  73. sourceFile
  74. );
  75. function commentCallback(pos, end, kind) {
  76. callback(fullText, { pos, end, kind });
  77. }
  78. }
  79. // src/compilerOptions.ts
  80. import ts3 from "typescript";
  81. function isCompilerOptionEnabled(options, option) {
  82. switch (option) {
  83. case "stripInternal":
  84. case "declarationMap":
  85. case "emitDeclarationOnly":
  86. return options[option] === true && isCompilerOptionEnabled(options, "declaration");
  87. case "declaration":
  88. return (
  89. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  90. options.declaration || isCompilerOptionEnabled(options, "composite")
  91. );
  92. case "incremental":
  93. return options.incremental === void 0 ? isCompilerOptionEnabled(options, "composite") : options.incremental;
  94. case "skipDefaultLibCheck":
  95. return (
  96. // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
  97. options.skipDefaultLibCheck || isCompilerOptionEnabled(options, "skipLibCheck")
  98. );
  99. case "suppressImplicitAnyIndexErrors":
  100. return options.suppressImplicitAnyIndexErrors === true && isCompilerOptionEnabled(options, "noImplicitAny");
  101. case "allowSyntheticDefaultImports":
  102. return options.allowSyntheticDefaultImports !== void 0 ? options.allowSyntheticDefaultImports : isCompilerOptionEnabled(options, "esModuleInterop") || options.module === ts3.ModuleKind.System;
  103. case "noUncheckedIndexedAccess":
  104. return options.noUncheckedIndexedAccess === true && isCompilerOptionEnabled(options, "strictNullChecks");
  105. case "allowJs":
  106. return options.allowJs === void 0 ? isCompilerOptionEnabled(options, "checkJs") : options.allowJs;
  107. case "noImplicitAny":
  108. case "noImplicitThis":
  109. case "strictNullChecks":
  110. case "strictFunctionTypes":
  111. case "strictPropertyInitialization":
  112. case "alwaysStrict":
  113. case "strictBindCallApply":
  114. return isStrictCompilerOptionEnabled(
  115. options,
  116. option
  117. );
  118. }
  119. return options[option] === true;
  120. }
  121. function isStrictCompilerOptionEnabled(options, option) {
  122. return (options.strict ? options[option] !== false : options[option] === true) && (option !== "strictPropertyInitialization" || isStrictCompilerOptionEnabled(options, "strictNullChecks"));
  123. }
  124. // src/flags.ts
  125. import ts4 from "typescript";
  126. function isFlagSet(allFlags, flag) {
  127. return (allFlags & flag) !== 0;
  128. }
  129. function isFlagSetOnObject(obj, flag) {
  130. return isFlagSet(obj.flags, flag);
  131. }
  132. function isModifierFlagSet(node, flag) {
  133. return isFlagSet(ts4.getCombinedModifierFlags(node), flag);
  134. }
  135. var isNodeFlagSet = isFlagSetOnObject;
  136. function isObjectFlagSet(objectType, flag) {
  137. return isFlagSet(objectType.objectFlags, flag);
  138. }
  139. var isSymbolFlagSet = isFlagSetOnObject;
  140. var isTypeFlagSet = isFlagSetOnObject;
  141. // src/modifiers.ts
  142. function includesModifier(modifiers, ...kinds) {
  143. if (modifiers === void 0)
  144. return false;
  145. for (const modifier of modifiers)
  146. if (kinds.includes(modifier.kind))
  147. return true;
  148. return false;
  149. }
  150. // src/nodes/typeGuards/compound.ts
  151. import ts8 from "typescript";
  152. // src/nodes/typeGuards/single.ts
  153. import ts5 from "typescript";
  154. function isAbstractKeyword(node) {
  155. return node.kind === ts5.SyntaxKind.AbstractKeyword;
  156. }
  157. function isAccessorKeyword(node) {
  158. return node.kind === ts5.SyntaxKind.AccessorKeyword;
  159. }
  160. function isAnyKeyword(node) {
  161. return node.kind === ts5.SyntaxKind.AnyKeyword;
  162. }
  163. function isAssertKeyword(node) {
  164. return node.kind === ts5.SyntaxKind.AssertKeyword;
  165. }
  166. function isAssertsKeyword(node) {
  167. return node.kind === ts5.SyntaxKind.AssertsKeyword;
  168. }
  169. function isAsyncKeyword(node) {
  170. return node.kind === ts5.SyntaxKind.AsyncKeyword;
  171. }
  172. function isAwaitKeyword(node) {
  173. return node.kind === ts5.SyntaxKind.AwaitKeyword;
  174. }
  175. function isBigIntKeyword(node) {
  176. return node.kind === ts5.SyntaxKind.BigIntKeyword;
  177. }
  178. function isBooleanKeyword(node) {
  179. return node.kind === ts5.SyntaxKind.BooleanKeyword;
  180. }
  181. function isColonToken(node) {
  182. return node.kind === ts5.SyntaxKind.ColonToken;
  183. }
  184. function isConstKeyword(node) {
  185. return node.kind === ts5.SyntaxKind.ConstKeyword;
  186. }
  187. function isDeclareKeyword(node) {
  188. return node.kind === ts5.SyntaxKind.DeclareKeyword;
  189. }
  190. function isDefaultKeyword(node) {
  191. return node.kind === ts5.SyntaxKind.DefaultKeyword;
  192. }
  193. function isDotToken(node) {
  194. return node.kind === ts5.SyntaxKind.DotToken;
  195. }
  196. function isEndOfFileToken(node) {
  197. return node.kind === ts5.SyntaxKind.EndOfFileToken;
  198. }
  199. function isEqualsGreaterThanToken(node) {
  200. return node.kind === ts5.SyntaxKind.EqualsGreaterThanToken;
  201. }
  202. function isEqualsToken(node) {
  203. return node.kind === ts5.SyntaxKind.EqualsToken;
  204. }
  205. function isExclamationToken(node) {
  206. return node.kind === ts5.SyntaxKind.ExclamationToken;
  207. }
  208. function isExportKeyword(node) {
  209. return node.kind === ts5.SyntaxKind.ExportKeyword;
  210. }
  211. function isFalseKeyword(node) {
  212. return node.kind === ts5.SyntaxKind.FalseKeyword;
  213. }
  214. function isFalseLiteral(node) {
  215. return node.kind === ts5.SyntaxKind.FalseKeyword;
  216. }
  217. function isImportExpression(node) {
  218. return node.kind === ts5.SyntaxKind.ImportKeyword;
  219. }
  220. function isImportKeyword(node) {
  221. return node.kind === ts5.SyntaxKind.ImportKeyword;
  222. }
  223. function isInKeyword(node) {
  224. return node.kind === ts5.SyntaxKind.InKeyword;
  225. }
  226. function isInputFiles(node) {
  227. return node.kind === ts5.SyntaxKind.InputFiles;
  228. }
  229. function isJSDocText(node) {
  230. return node.kind === ts5.SyntaxKind.JSDocText;
  231. }
  232. function isJsonMinusNumericLiteral(node) {
  233. return node.kind === ts5.SyntaxKind.PrefixUnaryExpression;
  234. }
  235. function isNeverKeyword(node) {
  236. return node.kind === ts5.SyntaxKind.NeverKeyword;
  237. }
  238. function isNullKeyword(node) {
  239. return node.kind === ts5.SyntaxKind.NullKeyword;
  240. }
  241. function isNullLiteral(node) {
  242. return node.kind === ts5.SyntaxKind.NullKeyword;
  243. }
  244. function isNumberKeyword(node) {
  245. return node.kind === ts5.SyntaxKind.NumberKeyword;
  246. }
  247. function isObjectKeyword(node) {
  248. return node.kind === ts5.SyntaxKind.ObjectKeyword;
  249. }
  250. function isOutKeyword(node) {
  251. return node.kind === ts5.SyntaxKind.OutKeyword;
  252. }
  253. function isOverrideKeyword(node) {
  254. return node.kind === ts5.SyntaxKind.OverrideKeyword;
  255. }
  256. function isPrivateKeyword(node) {
  257. return node.kind === ts5.SyntaxKind.PrivateKeyword;
  258. }
  259. function isProtectedKeyword(node) {
  260. return node.kind === ts5.SyntaxKind.ProtectedKeyword;
  261. }
  262. function isPublicKeyword(node) {
  263. return node.kind === ts5.SyntaxKind.PublicKeyword;
  264. }
  265. function isQuestionDotToken(node) {
  266. return node.kind === ts5.SyntaxKind.QuestionDotToken;
  267. }
  268. function isQuestionToken(node) {
  269. return node.kind === ts5.SyntaxKind.QuestionToken;
  270. }
  271. function isReadonlyKeyword(node) {
  272. return node.kind === ts5.SyntaxKind.ReadonlyKeyword;
  273. }
  274. function isStaticKeyword(node) {
  275. return node.kind === ts5.SyntaxKind.StaticKeyword;
  276. }
  277. function isStringKeyword(node) {
  278. return node.kind === ts5.SyntaxKind.StringKeyword;
  279. }
  280. function isSuperExpression(node) {
  281. return node.kind === ts5.SyntaxKind.SuperKeyword;
  282. }
  283. function isSuperKeyword(node) {
  284. return node.kind === ts5.SyntaxKind.SuperKeyword;
  285. }
  286. function isSymbolKeyword(node) {
  287. return node.kind === ts5.SyntaxKind.SymbolKeyword;
  288. }
  289. function isSyntaxList(node) {
  290. return node.kind === ts5.SyntaxKind.SyntaxList;
  291. }
  292. function isThisExpression(node) {
  293. return node.kind === ts5.SyntaxKind.ThisKeyword;
  294. }
  295. function isThisKeyword(node) {
  296. return node.kind === ts5.SyntaxKind.ThisKeyword;
  297. }
  298. function isTrueKeyword(node) {
  299. return node.kind === ts5.SyntaxKind.TrueKeyword;
  300. }
  301. function isTrueLiteral(node) {
  302. return node.kind === ts5.SyntaxKind.TrueKeyword;
  303. }
  304. function isUndefinedKeyword(node) {
  305. return node.kind === ts5.SyntaxKind.UndefinedKeyword;
  306. }
  307. function isUnknownKeyword(node) {
  308. return node.kind === ts5.SyntaxKind.UnknownKeyword;
  309. }
  310. function isUnparsedPrologue(node) {
  311. return node.kind === ts5.SyntaxKind.UnparsedPrologue;
  312. }
  313. function isUnparsedSyntheticReference(node) {
  314. return node.kind === ts5.SyntaxKind.UnparsedSyntheticReference;
  315. }
  316. function isVoidKeyword(node) {
  317. return node.kind === ts5.SyntaxKind.VoidKeyword;
  318. }
  319. // src/nodes/typeGuards/union.ts
  320. import ts7 from "typescript";
  321. // src/utils.ts
  322. import ts6 from "typescript";
  323. var [tsMajor, tsMinor] = ts6.versionMajorMinor.split(".").map((raw) => Number.parseInt(raw, 10));
  324. function isTsVersionAtLeast(major, minor = 0) {
  325. return tsMajor > major || tsMajor === major && tsMinor >= minor;
  326. }
  327. // src/nodes/typeGuards/union.ts
  328. function isAccessExpression(node) {
  329. return ts7.isPropertyAccessExpression(node) || ts7.isElementAccessExpression(node);
  330. }
  331. function isAccessibilityModifier(node) {
  332. return isPublicKeyword(node) || isPrivateKeyword(node) || isProtectedKeyword(node);
  333. }
  334. function isAccessorDeclaration(node) {
  335. return ts7.isGetAccessorDeclaration(node) || ts7.isSetAccessorDeclaration(node);
  336. }
  337. function isArrayBindingElement(node) {
  338. return ts7.isBindingElement(node) || ts7.isOmittedExpression(node);
  339. }
  340. function isArrayBindingOrAssignmentPattern(node) {
  341. return ts7.isArrayBindingPattern(node) || ts7.isArrayLiteralExpression(node);
  342. }
  343. function isAssignmentPattern(node) {
  344. return ts7.isObjectLiteralExpression(node) || ts7.isArrayLiteralExpression(node);
  345. }
  346. function isBindingOrAssignmentElementRestIndicator(node) {
  347. if (ts7.isSpreadElement(node) || ts7.isSpreadAssignment(node)) {
  348. return true;
  349. }
  350. if (isTsVersionAtLeast(4, 4)) {
  351. return ts7.isDotDotDotToken(node);
  352. }
  353. return false;
  354. }
  355. function isBindingOrAssignmentElementTarget(node) {
  356. return isBindingOrAssignmentPattern(node) || ts7.isIdentifier(node) || ts7.isPropertyAccessExpression(node) || ts7.isElementAccessExpression(node) || ts7.isOmittedExpression(node);
  357. }
  358. function isBindingOrAssignmentPattern(node) {
  359. return isObjectBindingOrAssignmentPattern(node) || isArrayBindingOrAssignmentPattern(node);
  360. }
  361. function isBindingPattern(node) {
  362. return ts7.isObjectBindingPattern(node) || ts7.isArrayBindingPattern(node);
  363. }
  364. function isBlockLike(node) {
  365. return ts7.isSourceFile(node) || ts7.isBlock(node) || ts7.isModuleBlock(node) || ts7.isCaseOrDefaultClause(node);
  366. }
  367. function isBooleanLiteral(node) {
  368. return isTrueLiteral(node) || isFalseLiteral(node);
  369. }
  370. function isClassLikeDeclaration(node) {
  371. return ts7.isClassDeclaration(node) || ts7.isClassExpression(node);
  372. }
  373. function isClassMemberModifier(node) {
  374. return isAccessibilityModifier(node) || isReadonlyKeyword(node) || isStaticKeyword(node) || isAccessorKeyword(node);
  375. }
  376. function isDeclarationName(node) {
  377. return ts7.isIdentifier(node) || ts7.isPrivateIdentifier(node) || ts7.isStringLiteralLike(node) || ts7.isNumericLiteral(node) || ts7.isComputedPropertyName(node) || ts7.isElementAccessExpression(node) || isBindingPattern(node) || isEntityNameExpression(node);
  378. }
  379. function isDeclarationWithTypeParameterChildren(node) {
  380. return isSignatureDeclaration(node) || // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  381. isClassLikeDeclaration(node) || ts7.isInterfaceDeclaration(node) || ts7.isTypeAliasDeclaration(node) || ts7.isJSDocTemplateTag(node);
  382. }
  383. function isDeclarationWithTypeParameters(node) {
  384. return isDeclarationWithTypeParameterChildren(node) || ts7.isJSDocTypedefTag(node) || ts7.isJSDocCallbackTag(node) || ts7.isJSDocSignature(node);
  385. }
  386. function isDestructuringPattern(node) {
  387. return isBindingPattern(node) || ts7.isObjectLiteralExpression(node) || ts7.isArrayLiteralExpression(node);
  388. }
  389. function isEntityNameExpression(node) {
  390. return ts7.isIdentifier(node) || isPropertyAccessEntityNameExpression(node);
  391. }
  392. function isEntityNameOrEntityNameExpression(node) {
  393. return ts7.isEntityName(node) || isEntityNameExpression(node);
  394. }
  395. function isForInOrOfStatement(node) {
  396. return ts7.isForInStatement(node) || ts7.isForOfStatement(node);
  397. }
  398. function isFunctionLikeDeclaration(node) {
  399. return ts7.isFunctionDeclaration(node) || ts7.isMethodDeclaration(node) || ts7.isGetAccessorDeclaration(node) || ts7.isSetAccessorDeclaration(node) || ts7.isConstructorDeclaration(node) || ts7.isFunctionExpression(node) || ts7.isArrowFunction(node);
  400. }
  401. function hasDecorators(node) {
  402. return ts7.isParameter(node) || ts7.isPropertyDeclaration(node) || ts7.isMethodDeclaration(node) || ts7.isGetAccessorDeclaration(node) || ts7.isSetAccessorDeclaration(node) || ts7.isClassExpression(node) || ts7.isClassDeclaration(node);
  403. }
  404. function hasExpressionInitializer(node) {
  405. return ts7.isVariableDeclaration(node) || ts7.isParameter(node) || ts7.isBindingElement(node) || ts7.isPropertyDeclaration(node) || ts7.isPropertyAssignment(node) || ts7.isEnumMember(node);
  406. }
  407. function hasInitializer(node) {
  408. return hasExpressionInitializer(node) || ts7.isForStatement(node) || ts7.isForInStatement(node) || ts7.isForOfStatement(node) || ts7.isJsxAttribute(node);
  409. }
  410. function hasJSDoc(node) {
  411. if (
  412. // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  413. isAccessorDeclaration(node) || ts7.isArrowFunction(node) || ts7.isBlock(node) || ts7.isBreakStatement(node) || ts7.isCallSignatureDeclaration(node) || ts7.isCaseClause(node) || // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  414. isClassLikeDeclaration(node) || ts7.isConstructorDeclaration(node) || ts7.isConstructorTypeNode(node) || ts7.isConstructSignatureDeclaration(node) || ts7.isContinueStatement(node) || ts7.isDebuggerStatement(node) || ts7.isDoStatement(node) || ts7.isEmptyStatement(node) || isEndOfFileToken(node) || ts7.isEnumDeclaration(node) || ts7.isEnumMember(node) || ts7.isExportAssignment(node) || ts7.isExportDeclaration(node) || ts7.isExportSpecifier(node) || ts7.isExpressionStatement(node) || ts7.isForInStatement(node) || ts7.isForOfStatement(node) || ts7.isForStatement(node) || ts7.isFunctionDeclaration(node) || ts7.isFunctionExpression(node) || ts7.isFunctionTypeNode(node) || ts7.isIfStatement(node) || ts7.isImportDeclaration(node) || ts7.isImportEqualsDeclaration(node) || ts7.isIndexSignatureDeclaration(node) || ts7.isInterfaceDeclaration(node) || ts7.isJSDocFunctionType(node) || ts7.isLabeledStatement(node) || ts7.isMethodDeclaration(node) || ts7.isMethodSignature(node) || ts7.isModuleDeclaration(node) || ts7.isNamedTupleMember(node) || ts7.isNamespaceExportDeclaration(node) || ts7.isParameter(node) || ts7.isParenthesizedExpression(node) || ts7.isPropertyAssignment(node) || ts7.isPropertyDeclaration(node) || ts7.isPropertySignature(node) || ts7.isReturnStatement(node) || ts7.isShorthandPropertyAssignment(node) || ts7.isSpreadAssignment(node) || ts7.isSwitchStatement(node) || ts7.isThrowStatement(node) || ts7.isTryStatement(node) || ts7.isTypeAliasDeclaration(node) || ts7.isVariableDeclaration(node) || ts7.isVariableStatement(node) || ts7.isWhileStatement(node) || ts7.isWithStatement(node)
  415. ) {
  416. return true;
  417. }
  418. if (isTsVersionAtLeast(4, 4) && ts7.isClassStaticBlockDeclaration(node)) {
  419. return true;
  420. }
  421. if (isTsVersionAtLeast(5, 0) && (ts7.isBinaryExpression(node) || ts7.isElementAccessExpression(node) || ts7.isIdentifier(node) || ts7.isJSDocSignature(node) || ts7.isObjectLiteralExpression(node) || ts7.isPropertyAccessExpression(node) || ts7.isTypeParameterDeclaration(node))) {
  422. return true;
  423. }
  424. return false;
  425. }
  426. function hasModifiers(node) {
  427. return ts7.isTypeParameterDeclaration(node) || ts7.isParameter(node) || ts7.isConstructorTypeNode(node) || ts7.isPropertySignature(node) || ts7.isPropertyDeclaration(node) || ts7.isMethodSignature(node) || ts7.isMethodDeclaration(node) || ts7.isConstructorDeclaration(node) || ts7.isGetAccessorDeclaration(node) || ts7.isSetAccessorDeclaration(node) || ts7.isIndexSignatureDeclaration(node) || ts7.isFunctionExpression(node) || ts7.isArrowFunction(node) || ts7.isClassExpression(node) || ts7.isVariableStatement(node) || ts7.isFunctionDeclaration(node) || ts7.isClassDeclaration(node) || ts7.isInterfaceDeclaration(node) || ts7.isTypeAliasDeclaration(node) || ts7.isEnumDeclaration(node) || ts7.isModuleDeclaration(node) || ts7.isImportEqualsDeclaration(node) || ts7.isImportDeclaration(node) || ts7.isExportAssignment(node) || ts7.isExportDeclaration(node);
  428. }
  429. function hasType(node) {
  430. return isSignatureDeclaration(node) || ts7.isVariableDeclaration(node) || ts7.isParameter(node) || ts7.isPropertySignature(node) || ts7.isPropertyDeclaration(node) || ts7.isTypePredicateNode(node) || ts7.isParenthesizedTypeNode(node) || ts7.isTypeOperatorNode(node) || ts7.isMappedTypeNode(node) || ts7.isAssertionExpression(node) || ts7.isTypeAliasDeclaration(node) || ts7.isJSDocTypeExpression(node) || ts7.isJSDocNonNullableType(node) || ts7.isJSDocNullableType(node) || ts7.isJSDocOptionalType(node) || ts7.isJSDocVariadicType(node);
  431. }
  432. function hasTypeArguments(node) {
  433. return ts7.isCallExpression(node) || ts7.isNewExpression(node) || ts7.isTaggedTemplateExpression(node) || ts7.isJsxOpeningElement(node) || ts7.isJsxSelfClosingElement(node);
  434. }
  435. function isJSDocComment(node) {
  436. if (isJSDocText(node)) {
  437. return true;
  438. }
  439. if (isTsVersionAtLeast(4, 4)) {
  440. return ts7.isJSDocLink(node) || ts7.isJSDocLinkCode(node) || ts7.isJSDocLinkPlain(node);
  441. }
  442. return false;
  443. }
  444. function isJSDocNamespaceBody(node) {
  445. return ts7.isIdentifier(node) || isJSDocNamespaceDeclaration(node);
  446. }
  447. function isJSDocTypeReferencingNode(node) {
  448. return ts7.isJSDocVariadicType(node) || ts7.isJSDocOptionalType(node) || ts7.isJSDocNullableType(node) || ts7.isJSDocNonNullableType(node);
  449. }
  450. function isJsonObjectExpression(node) {
  451. return ts7.isObjectLiteralExpression(node) || ts7.isArrayLiteralExpression(node) || isJsonMinusNumericLiteral(node) || ts7.isNumericLiteral(node) || ts7.isStringLiteral(node) || isBooleanLiteral(node) || isNullLiteral(node);
  452. }
  453. function isJsxAttributeLike(node) {
  454. return ts7.isJsxAttribute(node) || ts7.isJsxSpreadAttribute(node);
  455. }
  456. function isJsxAttributeValue(node) {
  457. return ts7.isStringLiteral(node) || ts7.isJsxExpression(node) || ts7.isJsxElement(node) || ts7.isJsxSelfClosingElement(node) || ts7.isJsxFragment(node);
  458. }
  459. function isJsxChild(node) {
  460. return ts7.isJsxText(node) || ts7.isJsxExpression(node) || ts7.isJsxElement(node) || ts7.isJsxSelfClosingElement(node) || ts7.isJsxFragment(node);
  461. }
  462. function isJsxTagNameExpression(node) {
  463. return ts7.isIdentifier(node) || isThisExpression(node) || isJsxTagNamePropertyAccess(node);
  464. }
  465. function isLiteralToken(node) {
  466. return ts7.isNumericLiteral(node) || ts7.isBigIntLiteral(node) || ts7.isStringLiteral(node) || ts7.isJsxText(node) || ts7.isRegularExpressionLiteral(node) || ts7.isNoSubstitutionTemplateLiteral(node);
  467. }
  468. function isModuleBody(node) {
  469. return isNamespaceBody(node) || isJSDocNamespaceBody(node);
  470. }
  471. function isModuleName(node) {
  472. return ts7.isIdentifier(node) || ts7.isStringLiteral(node);
  473. }
  474. function isModuleReference(node) {
  475. return ts7.isEntityName(node) || ts7.isExternalModuleReference(node);
  476. }
  477. function isNamedImportBindings(node) {
  478. return ts7.isNamespaceImport(node) || ts7.isNamedImports(node);
  479. }
  480. function isNamedImportsOrExports(node) {
  481. return ts7.isNamedImports(node) || ts7.isNamedExports(node);
  482. }
  483. function isNamespaceBody(node) {
  484. return ts7.isModuleBlock(node) || isNamespaceDeclaration(node);
  485. }
  486. function isObjectBindingOrAssignmentElement(node) {
  487. return ts7.isBindingElement(node) || ts7.isPropertyAssignment(node) || ts7.isShorthandPropertyAssignment(node) || ts7.isSpreadAssignment(node);
  488. }
  489. function isObjectBindingOrAssignmentPattern(node) {
  490. return ts7.isObjectBindingPattern(node) || ts7.isObjectLiteralExpression(node);
  491. }
  492. function isObjectTypeDeclaration(node) {
  493. return (
  494. // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  495. isClassLikeDeclaration(node) || ts7.isInterfaceDeclaration(node) || ts7.isTypeLiteralNode(node)
  496. );
  497. }
  498. function isParameterPropertyModifier(node) {
  499. return isAccessibilityModifier(node) || isReadonlyKeyword(node);
  500. }
  501. function isPropertyNameLiteral(node) {
  502. return ts7.isIdentifier(node) || ts7.isStringLiteralLike(node) || ts7.isNumericLiteral(node);
  503. }
  504. function isPseudoLiteralToken(node) {
  505. return ts7.isTemplateHead(node) || ts7.isTemplateMiddle(node) || ts7.isTemplateTail(node);
  506. }
  507. function isSignatureDeclaration(node) {
  508. return ts7.isCallSignatureDeclaration(node) || ts7.isConstructSignatureDeclaration(node) || ts7.isMethodSignature(node) || ts7.isIndexSignatureDeclaration(node) || ts7.isFunctionTypeNode(node) || ts7.isConstructorTypeNode(node) || ts7.isJSDocFunctionType(node) || ts7.isFunctionDeclaration(node) || ts7.isMethodDeclaration(node) || ts7.isConstructorDeclaration(node) || // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  509. isAccessorDeclaration(node) || ts7.isFunctionExpression(node) || ts7.isArrowFunction(node);
  510. }
  511. function isSuperProperty(node) {
  512. return isSuperPropertyAccessExpression(node) || isSuperElementAccessExpression(node);
  513. }
  514. function isTypeOnlyCompatibleAliasDeclaration(node) {
  515. if (ts7.isImportClause(node) || ts7.isImportEqualsDeclaration(node) || ts7.isNamespaceImport(node) || ts7.isImportOrExportSpecifier(node)) {
  516. return true;
  517. }
  518. if (isTsVersionAtLeast(5, 0) && (ts7.isExportDeclaration(node) || ts7.isNamespaceExport(node))) {
  519. return true;
  520. }
  521. return false;
  522. }
  523. function isTypeReferenceType(node) {
  524. return ts7.isTypeReferenceNode(node) || ts7.isExpressionWithTypeArguments(node);
  525. }
  526. function isUnionOrIntersectionTypeNode(node) {
  527. return ts7.isUnionTypeNode(node) || ts7.isIntersectionTypeNode(node);
  528. }
  529. function isUnparsedSourceText(node) {
  530. return ts7.isUnparsedPrepend(node) || ts7.isUnparsedTextLike(node);
  531. }
  532. function isVariableLikeDeclaration(node) {
  533. return ts7.isVariableDeclaration(node) || ts7.isParameter(node) || ts7.isBindingElement(node) || ts7.isPropertyDeclaration(node) || ts7.isPropertyAssignment(node) || ts7.isPropertySignature(node) || ts7.isJsxAttribute(node) || ts7.isShorthandPropertyAssignment(node) || ts7.isEnumMember(node) || ts7.isJSDocPropertyTag(node) || ts7.isJSDocParameterTag(node);
  534. }
  535. // src/nodes/typeGuards/compound.ts
  536. function isConstAssertionExpression(node) {
  537. return ts8.isTypeReferenceNode(node.type) && ts8.isIdentifier(node.type.typeName) && node.type.typeName.escapedText === "const";
  538. }
  539. function isIterationStatement(node) {
  540. switch (node.kind) {
  541. case ts8.SyntaxKind.DoStatement:
  542. case ts8.SyntaxKind.ForInStatement:
  543. case ts8.SyntaxKind.ForOfStatement:
  544. case ts8.SyntaxKind.ForStatement:
  545. case ts8.SyntaxKind.WhileStatement:
  546. return true;
  547. default:
  548. return false;
  549. }
  550. }
  551. function isJSDocNamespaceDeclaration(node) {
  552. return ts8.isModuleDeclaration(node) && ts8.isIdentifier(node.name) && (node.body === void 0 || isJSDocNamespaceBody(node.body));
  553. }
  554. function isJsxTagNamePropertyAccess(node) {
  555. return ts8.isPropertyAccessExpression(node) && // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts < 5
  556. isJsxTagNameExpression(node.expression);
  557. }
  558. function isNamedDeclarationWithName(node) {
  559. return "name" in node && node.name !== void 0 && node.name !== null && isDeclarationName(node.name);
  560. }
  561. function isNamespaceDeclaration(node) {
  562. return ts8.isModuleDeclaration(node) && ts8.isIdentifier(node.name) && node.body !== void 0 && isNamespaceBody(node.body);
  563. }
  564. function isNumericOrStringLikeLiteral(node) {
  565. switch (node.kind) {
  566. case ts8.SyntaxKind.StringLiteral:
  567. case ts8.SyntaxKind.NumericLiteral:
  568. case ts8.SyntaxKind.NoSubstitutionTemplateLiteral:
  569. return true;
  570. default:
  571. return false;
  572. }
  573. }
  574. function isPropertyAccessEntityNameExpression(node) {
  575. return ts8.isPropertyAccessExpression(node) && ts8.isIdentifier(node.name) && isEntityNameExpression(node.expression);
  576. }
  577. function isSuperElementAccessExpression(node) {
  578. return ts8.isElementAccessExpression(node) && isSuperExpression(node.expression);
  579. }
  580. function isSuperPropertyAccessExpression(node) {
  581. return ts8.isPropertyAccessExpression(node) && isSuperExpression(node.expression);
  582. }
  583. // src/scopes.ts
  584. import ts9 from "typescript";
  585. function isFunctionScopeBoundary(node) {
  586. switch (node.kind) {
  587. case ts9.SyntaxKind.FunctionExpression:
  588. case ts9.SyntaxKind.ArrowFunction:
  589. case ts9.SyntaxKind.Constructor:
  590. case ts9.SyntaxKind.ModuleDeclaration:
  591. case ts9.SyntaxKind.ClassDeclaration:
  592. case ts9.SyntaxKind.ClassExpression:
  593. case ts9.SyntaxKind.EnumDeclaration:
  594. case ts9.SyntaxKind.MethodDeclaration:
  595. case ts9.SyntaxKind.FunctionDeclaration:
  596. case ts9.SyntaxKind.GetAccessor:
  597. case ts9.SyntaxKind.SetAccessor:
  598. case ts9.SyntaxKind.MethodSignature:
  599. case ts9.SyntaxKind.CallSignature:
  600. case ts9.SyntaxKind.ConstructSignature:
  601. case ts9.SyntaxKind.ConstructorType:
  602. case ts9.SyntaxKind.FunctionType:
  603. return true;
  604. case ts9.SyntaxKind.SourceFile:
  605. return ts9.isExternalModule(node);
  606. default:
  607. return false;
  608. }
  609. }
  610. // src/syntax.ts
  611. import ts10 from "typescript";
  612. function isAssignmentKind(kind) {
  613. return kind >= ts10.SyntaxKind.FirstAssignment && kind <= ts10.SyntaxKind.LastAssignment;
  614. }
  615. function isNumericPropertyName(name) {
  616. return String(+name) === name;
  617. }
  618. function charSize(ch) {
  619. return ch >= 65536 ? 2 : 1;
  620. }
  621. function isValidPropertyAccess(text, languageVersion = ts10.ScriptTarget.Latest) {
  622. if (text.length === 0)
  623. return false;
  624. let ch = text.codePointAt(0);
  625. if (!ts10.isIdentifierStart(ch, languageVersion))
  626. return false;
  627. for (let i = charSize(ch); i < text.length; i += charSize(ch)) {
  628. ch = text.codePointAt(i);
  629. if (!ts10.isIdentifierPart(ch, languageVersion))
  630. return false;
  631. }
  632. return true;
  633. }
  634. // src/types/getters.ts
  635. import ts15 from "typescript";
  636. // src/types/typeGuards/intrinsic.ts
  637. import ts11 from "typescript";
  638. function isIntrinsicAnyType(type) {
  639. return isTypeFlagSet(type, ts11.TypeFlags.Any);
  640. }
  641. function isIntrinsicBooleanType(type) {
  642. return isTypeFlagSet(type, ts11.TypeFlags.Boolean);
  643. }
  644. function isIntrinsicBigIntType(type) {
  645. return isTypeFlagSet(type, ts11.TypeFlags.BigInt);
  646. }
  647. function isIntrinsicErrorType(type) {
  648. return isIntrinsicType(type) && type.intrinsicName === "error";
  649. }
  650. function isIntrinsicESSymbolType(type) {
  651. return isTypeFlagSet(type, ts11.TypeFlags.ESSymbol);
  652. }
  653. var IntrinsicTypeFlags = ts11.TypeFlags.Intrinsic ?? ts11.TypeFlags.Any | ts11.TypeFlags.Unknown | ts11.TypeFlags.String | ts11.TypeFlags.Number | ts11.TypeFlags.BigInt | ts11.TypeFlags.Boolean | ts11.TypeFlags.BooleanLiteral | ts11.TypeFlags.ESSymbol | ts11.TypeFlags.Void | ts11.TypeFlags.Undefined | ts11.TypeFlags.Null | ts11.TypeFlags.Never | ts11.TypeFlags.NonPrimitive;
  654. function isIntrinsicType(type) {
  655. return isTypeFlagSet(type, IntrinsicTypeFlags);
  656. }
  657. function isIntrinsicNeverType(type) {
  658. return isTypeFlagSet(type, ts11.TypeFlags.Never);
  659. }
  660. function isIntrinsicNonPrimitiveType(type) {
  661. return isTypeFlagSet(type, ts11.TypeFlags.NonPrimitive);
  662. }
  663. function isIntrinsicNullType(type) {
  664. return isTypeFlagSet(type, ts11.TypeFlags.Null);
  665. }
  666. function isIntrinsicNumberType(type) {
  667. return isTypeFlagSet(type, ts11.TypeFlags.Number);
  668. }
  669. function isIntrinsicStringType(type) {
  670. return isTypeFlagSet(type, ts11.TypeFlags.String);
  671. }
  672. function isIntrinsicUndefinedType(type) {
  673. return isTypeFlagSet(type, ts11.TypeFlags.Undefined);
  674. }
  675. function isIntrinsicUnknownType(type) {
  676. return isTypeFlagSet(type, ts11.TypeFlags.Unknown);
  677. }
  678. function isIntrinsicVoidType(type) {
  679. return isTypeFlagSet(type, ts11.TypeFlags.Void);
  680. }
  681. // src/types/typeGuards/objects.ts
  682. import ts13 from "typescript";
  683. // src/types/typeGuards/single.ts
  684. import ts12 from "typescript";
  685. function isConditionalType(type) {
  686. return isTypeFlagSet(type, ts12.TypeFlags.Conditional);
  687. }
  688. function isEnumType(type) {
  689. return isTypeFlagSet(type, ts12.TypeFlags.Enum);
  690. }
  691. function isFreshableType(type) {
  692. return isTypeFlagSet(type, ts12.TypeFlags.Freshable);
  693. }
  694. function isIndexType(type) {
  695. return isTypeFlagSet(type, ts12.TypeFlags.Index);
  696. }
  697. function isIndexedAccessType(type) {
  698. return isTypeFlagSet(type, ts12.TypeFlags.IndexedAccess);
  699. }
  700. function isInstantiableType(type) {
  701. return isTypeFlagSet(type, ts12.TypeFlags.Instantiable);
  702. }
  703. function isIntersectionType(type) {
  704. return isTypeFlagSet(type, ts12.TypeFlags.Intersection);
  705. }
  706. function isObjectType(type) {
  707. return isTypeFlagSet(type, ts12.TypeFlags.Object);
  708. }
  709. function isStringMappingType(type) {
  710. return isTypeFlagSet(type, ts12.TypeFlags.StringMapping);
  711. }
  712. function isSubstitutionType(type) {
  713. return isTypeFlagSet(type, ts12.TypeFlags.Substitution);
  714. }
  715. function isTypeParameter(type) {
  716. return isTypeFlagSet(type, ts12.TypeFlags.TypeParameter);
  717. }
  718. function isTypeVariable(type) {
  719. return isTypeFlagSet(type, ts12.TypeFlags.TypeVariable);
  720. }
  721. function isUnionType(type) {
  722. return isTypeFlagSet(type, ts12.TypeFlags.Union);
  723. }
  724. function isUnionOrIntersectionType(type) {
  725. return isTypeFlagSet(type, ts12.TypeFlags.UnionOrIntersection);
  726. }
  727. function isUniqueESSymbolType(type) {
  728. return isTypeFlagSet(type, ts12.TypeFlags.UniqueESSymbol);
  729. }
  730. // src/types/typeGuards/objects.ts
  731. function isEvolvingArrayType(type) {
  732. return isObjectType(type) && isObjectFlagSet(type, ts13.ObjectFlags.EvolvingArray);
  733. }
  734. function isTupleType(type) {
  735. return isObjectType(type) && isObjectFlagSet(type, ts13.ObjectFlags.Tuple);
  736. }
  737. function isTypeReference(type) {
  738. return isObjectType(type) && isObjectFlagSet(type, ts13.ObjectFlags.Reference);
  739. }
  740. // src/types/typeGuards/compound.ts
  741. function isFreshableIntrinsicType(type) {
  742. return isIntrinsicType(type) && isFreshableType(type);
  743. }
  744. function isTupleTypeReference(type) {
  745. return isTypeReference(type) && isTupleType(type.target);
  746. }
  747. // src/types/typeGuards/literal.ts
  748. import ts14 from "typescript";
  749. function isBooleanLiteralType(type) {
  750. return isTypeFlagSet(type, ts14.TypeFlags.BooleanLiteral);
  751. }
  752. function isBigIntLiteralType(type) {
  753. return isTypeFlagSet(type, ts14.TypeFlags.BigIntLiteral);
  754. }
  755. function isFalseLiteralType(type) {
  756. return isBooleanLiteralType(type) && type.intrinsicName === "false";
  757. }
  758. function isLiteralType(type) {
  759. return isTypeFlagSet(type, ts14.TypeFlags.Literal);
  760. }
  761. function isNumberLiteralType(type) {
  762. return isTypeFlagSet(type, ts14.TypeFlags.NumberLiteral);
  763. }
  764. function isStringLiteralType(type) {
  765. return isTypeFlagSet(type, ts14.TypeFlags.StringLiteral);
  766. }
  767. function isTemplateLiteralType(type) {
  768. return isTypeFlagSet(type, ts14.TypeFlags.TemplateLiteral);
  769. }
  770. function isTrueLiteralType(type) {
  771. return isBooleanLiteralType(type) && type.intrinsicName === "true";
  772. }
  773. function isUnknownLiteralType(type) {
  774. return isTypeFlagSet(type, ts14.TypeFlags.Literal);
  775. }
  776. // src/types/getters.ts
  777. function getCallSignaturesOfType(type) {
  778. if (isUnionType(type)) {
  779. const signatures = [];
  780. for (const subType of type.types) {
  781. signatures.push(...getCallSignaturesOfType(subType));
  782. }
  783. return signatures;
  784. }
  785. if (isIntersectionType(type)) {
  786. let signatures;
  787. for (const subType of type.types) {
  788. const sig = getCallSignaturesOfType(subType);
  789. if (sig.length !== 0) {
  790. if (signatures !== void 0)
  791. return [];
  792. signatures = sig;
  793. }
  794. }
  795. return signatures === void 0 ? [] : signatures;
  796. }
  797. return type.getCallSignatures();
  798. }
  799. function getPropertyOfType(type, name) {
  800. if (!name.startsWith("__"))
  801. return type.getProperty(name);
  802. return type.getProperties().find((s) => s.escapedName === name);
  803. }
  804. function getWellKnownSymbolPropertyOfType(type, wellKnownSymbolName, typeChecker) {
  805. const prefix = "__@" + wellKnownSymbolName;
  806. for (const prop of type.getProperties()) {
  807. if (!prop.name.startsWith(prefix)) {
  808. continue;
  809. }
  810. const declaration = prop.valueDeclaration ?? prop.getDeclarations()[0];
  811. if (!isNamedDeclarationWithName(declaration) || declaration.name === void 0 || !ts15.isComputedPropertyName(declaration.name)) {
  812. continue;
  813. }
  814. const globalSymbol = typeChecker.getApparentType(
  815. typeChecker.getTypeAtLocation(declaration.name.expression)
  816. ).symbol;
  817. if (prop.escapedName === getPropertyNameOfWellKnownSymbol(
  818. typeChecker,
  819. globalSymbol,
  820. wellKnownSymbolName
  821. )) {
  822. return prop;
  823. }
  824. }
  825. return void 0;
  826. }
  827. function getPropertyNameOfWellKnownSymbol(typeChecker, symbolConstructor, symbolName) {
  828. const knownSymbol = symbolConstructor && typeChecker.getTypeOfSymbolAtLocation(
  829. symbolConstructor,
  830. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
  831. symbolConstructor.valueDeclaration
  832. ).getProperty(symbolName);
  833. const knownSymbolType = knownSymbol && typeChecker.getTypeOfSymbolAtLocation(
  834. knownSymbol,
  835. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
  836. knownSymbol.valueDeclaration
  837. );
  838. if (knownSymbolType && isUniqueESSymbolType(knownSymbolType))
  839. return knownSymbolType.escapedName;
  840. return "__@" + symbolName;
  841. }
  842. // src/types/utilities.ts
  843. import ts17 from "typescript";
  844. // src/nodes/utilities.ts
  845. import ts16 from "typescript";
  846. function isBindableObjectDefinePropertyCall(node) {
  847. return node.arguments.length === 3 && isEntityNameExpression(node.arguments[0]) && isNumericOrStringLikeLiteral(node.arguments[1]) && ts16.isPropertyAccessExpression(node.expression) && node.expression.name.escapedText === "defineProperty" && ts16.isIdentifier(node.expression.expression) && node.expression.expression.escapedText === "Object";
  848. }
  849. function isInConstContext(node) {
  850. let current = node;
  851. while (true) {
  852. const parent = current.parent;
  853. outer:
  854. switch (parent.kind) {
  855. case ts16.SyntaxKind.TypeAssertionExpression:
  856. case ts16.SyntaxKind.AsExpression:
  857. return isConstAssertionExpression(parent);
  858. case ts16.SyntaxKind.PrefixUnaryExpression:
  859. if (current.kind !== ts16.SyntaxKind.NumericLiteral)
  860. return false;
  861. switch (parent.operator) {
  862. case ts16.SyntaxKind.PlusToken:
  863. case ts16.SyntaxKind.MinusToken:
  864. current = parent;
  865. break outer;
  866. default:
  867. return false;
  868. }
  869. case ts16.SyntaxKind.PropertyAssignment:
  870. if (parent.initializer !== current)
  871. return false;
  872. current = parent.parent;
  873. break;
  874. case ts16.SyntaxKind.ShorthandPropertyAssignment:
  875. current = parent.parent;
  876. break;
  877. case ts16.SyntaxKind.ParenthesizedExpression:
  878. case ts16.SyntaxKind.ArrayLiteralExpression:
  879. case ts16.SyntaxKind.ObjectLiteralExpression:
  880. case ts16.SyntaxKind.TemplateExpression:
  881. current = parent;
  882. break;
  883. default:
  884. return false;
  885. }
  886. }
  887. }
  888. // src/types/utilities.ts
  889. function isFalsyType(type) {
  890. if (isTypeFlagSet(
  891. type,
  892. ts17.TypeFlags.Undefined | ts17.TypeFlags.Null | ts17.TypeFlags.Void
  893. ))
  894. return true;
  895. if (type.isLiteral())
  896. return !type.value;
  897. return isFalseLiteralType(type);
  898. }
  899. function intersectionTypeParts(type) {
  900. return isIntersectionType(type) ? type.types : [type];
  901. }
  902. function isReadonlyPropertyIntersection(type, name, typeChecker) {
  903. const typeParts = isIntersectionType(type) ? type.types : [type];
  904. return typeParts.some((subType) => {
  905. const prop = getPropertyOfType(subType, name);
  906. if (prop === void 0)
  907. return false;
  908. if (prop.flags & ts17.SymbolFlags.Transient) {
  909. if (/^(?:[1-9]\d*|0)$/.test(name) && isTupleTypeReference(subType))
  910. return subType.target.readonly;
  911. switch (isReadonlyPropertyFromMappedType(subType, name, typeChecker)) {
  912. case true:
  913. return true;
  914. case false:
  915. return false;
  916. default:
  917. }
  918. }
  919. return !!// members of namespace import
  920. (isSymbolFlagSet(prop, ts17.SymbolFlags.ValueModule) || // we unwrapped every mapped type, now we can check the actual declarations
  921. symbolHasReadonlyDeclaration(prop, typeChecker));
  922. });
  923. }
  924. function isReadonlyPropertyFromMappedType(type, name, typeChecker) {
  925. if (!isObjectType(type) || !isObjectFlagSet(type, ts17.ObjectFlags.Mapped))
  926. return;
  927. const declaration = type.symbol.declarations[0];
  928. if (declaration.readonlyToken !== void 0 && !/^__@[^@]+$/.test(name))
  929. return declaration.readonlyToken.kind !== ts17.SyntaxKind.MinusToken;
  930. const { modifiersType } = type;
  931. return modifiersType && isPropertyReadonlyInType(modifiersType, name, typeChecker);
  932. }
  933. function isCallback(typeChecker, param, node) {
  934. let type = typeChecker.getApparentType(
  935. typeChecker.getTypeOfSymbolAtLocation(param, node)
  936. );
  937. if (param.valueDeclaration.dotDotDotToken) {
  938. type = type.getNumberIndexType();
  939. if (type === void 0)
  940. return false;
  941. }
  942. for (const subType of unionTypeParts(type)) {
  943. if (subType.getCallSignatures().length !== 0)
  944. return true;
  945. }
  946. return false;
  947. }
  948. function isPropertyReadonlyInType(type, name, typeChecker) {
  949. let seenProperty = false;
  950. let seenReadonlySignature = false;
  951. for (const subType of unionTypeParts(type)) {
  952. if (getPropertyOfType(subType, name) === void 0) {
  953. const index = (isNumericPropertyName(name) ? typeChecker.getIndexInfoOfType(subType, ts17.IndexKind.Number) : void 0) ?? typeChecker.getIndexInfoOfType(subType, ts17.IndexKind.String);
  954. if (index?.isReadonly) {
  955. if (seenProperty)
  956. return true;
  957. seenReadonlySignature = true;
  958. }
  959. } else if (seenReadonlySignature || isReadonlyPropertyIntersection(subType, name, typeChecker)) {
  960. return true;
  961. } else {
  962. seenProperty = true;
  963. }
  964. }
  965. return false;
  966. }
  967. function isReadonlyAssignmentDeclaration(node, typeChecker) {
  968. if (!isBindableObjectDefinePropertyCall(node))
  969. return false;
  970. const descriptorType = typeChecker.getTypeAtLocation(node.arguments[2]);
  971. if (descriptorType.getProperty("value") === void 0)
  972. return descriptorType.getProperty("set") === void 0;
  973. const writableProp = descriptorType.getProperty("writable");
  974. if (writableProp === void 0)
  975. return false;
  976. const writableType = writableProp.valueDeclaration !== void 0 && ts17.isPropertyAssignment(writableProp.valueDeclaration) ? typeChecker.getTypeAtLocation(writableProp.valueDeclaration.initializer) : typeChecker.getTypeOfSymbolAtLocation(writableProp, node.arguments[2]);
  977. return isFalseLiteralType(writableType);
  978. }
  979. function isThenableType(typeChecker, node, type = typeChecker.getTypeAtLocation(node)) {
  980. for (const typePart of unionTypeParts(typeChecker.getApparentType(type))) {
  981. const then = typePart.getProperty("then");
  982. if (then === void 0)
  983. continue;
  984. const thenType = typeChecker.getTypeOfSymbolAtLocation(then, node);
  985. for (const subTypePart of unionTypeParts(thenType))
  986. for (const signature of subTypePart.getCallSignatures())
  987. if (signature.parameters.length !== 0 && isCallback(typeChecker, signature.parameters[0], node))
  988. return true;
  989. }
  990. return false;
  991. }
  992. function symbolHasReadonlyDeclaration(symbol, typeChecker) {
  993. return !!((symbol.flags & ts17.SymbolFlags.Accessor) === ts17.SymbolFlags.GetAccessor || symbol.declarations?.some(
  994. (node) => isModifierFlagSet(node, ts17.ModifierFlags.Readonly) || ts17.isVariableDeclaration(node) && isNodeFlagSet(node.parent, ts17.NodeFlags.Const) || ts17.isCallExpression(node) && isReadonlyAssignmentDeclaration(node, typeChecker) || ts17.isEnumMember(node) || (ts17.isPropertyAssignment(node) || ts17.isShorthandPropertyAssignment(node)) && isInConstContext(node.parent)
  995. ));
  996. }
  997. function unionTypeParts(type) {
  998. return isUnionType(type) ? type.types : [type];
  999. }
  1000. export {
  1001. forEachComment,
  1002. forEachToken,
  1003. getCallSignaturesOfType,
  1004. getPropertyOfType,
  1005. getWellKnownSymbolPropertyOfType,
  1006. hasDecorators,
  1007. hasExpressionInitializer,
  1008. hasInitializer,
  1009. hasJSDoc,
  1010. hasModifiers,
  1011. hasType,
  1012. hasTypeArguments,
  1013. includesModifier,
  1014. intersectionTypeParts,
  1015. isAbstractKeyword,
  1016. isAccessExpression,
  1017. isAccessibilityModifier,
  1018. isAccessorDeclaration,
  1019. isAccessorKeyword,
  1020. isAnyKeyword,
  1021. isArrayBindingElement,
  1022. isArrayBindingOrAssignmentPattern,
  1023. isAssertKeyword,
  1024. isAssertsKeyword,
  1025. isAssignmentKind,
  1026. isAssignmentPattern,
  1027. isAsyncKeyword,
  1028. isAwaitKeyword,
  1029. isBigIntKeyword,
  1030. isBigIntLiteralType,
  1031. isBindingOrAssignmentElementRestIndicator,
  1032. isBindingOrAssignmentElementTarget,
  1033. isBindingOrAssignmentPattern,
  1034. isBindingPattern,
  1035. isBlockLike,
  1036. isBooleanKeyword,
  1037. isBooleanLiteral,
  1038. isBooleanLiteralType,
  1039. isClassLikeDeclaration,
  1040. isClassMemberModifier,
  1041. isColonToken,
  1042. isCompilerOptionEnabled,
  1043. isConditionalType,
  1044. isConstAssertionExpression,
  1045. isConstKeyword,
  1046. isDeclarationName,
  1047. isDeclarationWithTypeParameterChildren,
  1048. isDeclarationWithTypeParameters,
  1049. isDeclareKeyword,
  1050. isDefaultKeyword,
  1051. isDestructuringPattern,
  1052. isDotToken,
  1053. isEndOfFileToken,
  1054. isEntityNameExpression,
  1055. isEntityNameOrEntityNameExpression,
  1056. isEnumType,
  1057. isEqualsGreaterThanToken,
  1058. isEqualsToken,
  1059. isEvolvingArrayType,
  1060. isExclamationToken,
  1061. isExportKeyword,
  1062. isFalseKeyword,
  1063. isFalseLiteral,
  1064. isFalseLiteralType,
  1065. isFalsyType,
  1066. isForInOrOfStatement,
  1067. isFreshableIntrinsicType,
  1068. isFreshableType,
  1069. isFunctionLikeDeclaration,
  1070. isFunctionScopeBoundary,
  1071. isImportExpression,
  1072. isImportKeyword,
  1073. isInKeyword,
  1074. isIndexType,
  1075. isIndexedAccessType,
  1076. isInputFiles,
  1077. isInstantiableType,
  1078. isIntersectionType,
  1079. isIntrinsicAnyType,
  1080. isIntrinsicBigIntType,
  1081. isIntrinsicBooleanType,
  1082. isIntrinsicESSymbolType,
  1083. isIntrinsicErrorType,
  1084. isIntrinsicNeverType,
  1085. isIntrinsicNonPrimitiveType,
  1086. isIntrinsicNullType,
  1087. isIntrinsicNumberType,
  1088. isIntrinsicStringType,
  1089. isIntrinsicType,
  1090. isIntrinsicUndefinedType,
  1091. isIntrinsicUnknownType,
  1092. isIntrinsicVoidType,
  1093. isIterationStatement,
  1094. isJSDocComment,
  1095. isJSDocNamespaceBody,
  1096. isJSDocNamespaceDeclaration,
  1097. isJSDocText,
  1098. isJSDocTypeReferencingNode,
  1099. isJsonMinusNumericLiteral,
  1100. isJsonObjectExpression,
  1101. isJsxAttributeLike,
  1102. isJsxAttributeValue,
  1103. isJsxChild,
  1104. isJsxTagNameExpression,
  1105. isJsxTagNamePropertyAccess,
  1106. isLiteralToken,
  1107. isLiteralType,
  1108. isModifierFlagSet,
  1109. isModuleBody,
  1110. isModuleName,
  1111. isModuleReference,
  1112. isNamedDeclarationWithName,
  1113. isNamedImportBindings,
  1114. isNamedImportsOrExports,
  1115. isNamespaceBody,
  1116. isNamespaceDeclaration,
  1117. isNeverKeyword,
  1118. isNodeFlagSet,
  1119. isNullKeyword,
  1120. isNullLiteral,
  1121. isNumberKeyword,
  1122. isNumberLiteralType,
  1123. isNumericOrStringLikeLiteral,
  1124. isNumericPropertyName,
  1125. isObjectBindingOrAssignmentElement,
  1126. isObjectBindingOrAssignmentPattern,
  1127. isObjectFlagSet,
  1128. isObjectKeyword,
  1129. isObjectType,
  1130. isObjectTypeDeclaration,
  1131. isOutKeyword,
  1132. isOverrideKeyword,
  1133. isParameterPropertyModifier,
  1134. isPrivateKeyword,
  1135. isPropertyAccessEntityNameExpression,
  1136. isPropertyNameLiteral,
  1137. isPropertyReadonlyInType,
  1138. isProtectedKeyword,
  1139. isPseudoLiteralToken,
  1140. isPublicKeyword,
  1141. isQuestionDotToken,
  1142. isQuestionToken,
  1143. isReadonlyKeyword,
  1144. isSignatureDeclaration,
  1145. isStaticKeyword,
  1146. isStrictCompilerOptionEnabled,
  1147. isStringKeyword,
  1148. isStringLiteralType,
  1149. isStringMappingType,
  1150. isSubstitutionType,
  1151. isSuperElementAccessExpression,
  1152. isSuperExpression,
  1153. isSuperKeyword,
  1154. isSuperProperty,
  1155. isSuperPropertyAccessExpression,
  1156. isSymbolFlagSet,
  1157. isSymbolKeyword,
  1158. isSyntaxList,
  1159. isTemplateLiteralType,
  1160. isThenableType,
  1161. isThisExpression,
  1162. isThisKeyword,
  1163. isTrueKeyword,
  1164. isTrueLiteral,
  1165. isTrueLiteralType,
  1166. isTupleType,
  1167. isTupleTypeReference,
  1168. isTypeFlagSet,
  1169. isTypeOnlyCompatibleAliasDeclaration,
  1170. isTypeParameter,
  1171. isTypeReference,
  1172. isTypeReferenceType,
  1173. isTypeVariable,
  1174. isUndefinedKeyword,
  1175. isUnionOrIntersectionType,
  1176. isUnionOrIntersectionTypeNode,
  1177. isUnionType,
  1178. isUniqueESSymbolType,
  1179. isUnknownKeyword,
  1180. isUnknownLiteralType,
  1181. isUnparsedPrologue,
  1182. isUnparsedSourceText,
  1183. isUnparsedSyntheticReference,
  1184. isValidPropertyAccess,
  1185. isVariableLikeDeclaration,
  1186. isVoidKeyword,
  1187. symbolHasReadonlyDeclaration,
  1188. unionTypeParts
  1189. };