index.js 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106
  1. import ts9 from 'typescript';
  2. // src/comments.ts
  3. function forEachToken(node, callback, sourceFile = node.getSourceFile()) {
  4. const queue = [];
  5. while (true) {
  6. if (ts9.isTokenKind(node.kind)) {
  7. callback(node);
  8. } else if (
  9. // eslint-disable-next-line deprecation/deprecation -- need for support of TS < 4.7
  10. node.kind !== ts9.SyntaxKind.JSDocComment
  11. ) {
  12. const children = node.getChildren(sourceFile);
  13. if (children.length === 1) {
  14. node = children[0];
  15. continue;
  16. }
  17. for (let i = children.length - 1; i >= 0; --i) {
  18. queue.push(children[i]);
  19. }
  20. }
  21. if (queue.length === 0) {
  22. break;
  23. }
  24. node = queue.pop();
  25. }
  26. }
  27. // src/comments.ts
  28. function canHaveTrailingTrivia(token) {
  29. switch (token.kind) {
  30. case ts9.SyntaxKind.CloseBraceToken:
  31. return token.parent.kind !== ts9.SyntaxKind.JsxExpression || !isJsxElementOrFragment(token.parent.parent);
  32. case ts9.SyntaxKind.GreaterThanToken:
  33. switch (token.parent.kind) {
  34. case ts9.SyntaxKind.JsxOpeningElement:
  35. return token.end !== token.parent.end;
  36. case ts9.SyntaxKind.JsxOpeningFragment:
  37. return false;
  38. // would be inside the fragment
  39. case ts9.SyntaxKind.JsxSelfClosingElement:
  40. return token.end !== token.parent.end || // if end is not equal, this is part of the type arguments list
  41. !isJsxElementOrFragment(token.parent.parent);
  42. // there's only trailing trivia if it's the end of the top element
  43. case ts9.SyntaxKind.JsxClosingElement:
  44. case ts9.SyntaxKind.JsxClosingFragment:
  45. return !isJsxElementOrFragment(token.parent.parent.parent);
  46. }
  47. }
  48. return true;
  49. }
  50. function isJsxElementOrFragment(node) {
  51. return node.kind === ts9.SyntaxKind.JsxElement || node.kind === ts9.SyntaxKind.JsxFragment;
  52. }
  53. function forEachComment(node, callback, sourceFile = node.getSourceFile()) {
  54. const fullText = sourceFile.text;
  55. const notJsx = sourceFile.languageVariant !== ts9.LanguageVariant.JSX;
  56. return forEachToken(
  57. node,
  58. (token) => {
  59. if (token.pos === token.end) {
  60. return;
  61. }
  62. if (token.kind !== ts9.SyntaxKind.JsxText) {
  63. ts9.forEachLeadingCommentRange(
  64. fullText,
  65. // skip shebang at position 0
  66. token.pos === 0 ? (ts9.getShebang(fullText) ?? "").length : token.pos,
  67. commentCallback
  68. );
  69. }
  70. if (notJsx || canHaveTrailingTrivia(token)) {
  71. return ts9.forEachTrailingCommentRange(
  72. fullText,
  73. token.end,
  74. commentCallback
  75. );
  76. }
  77. },
  78. sourceFile
  79. );
  80. function commentCallback(pos, end, kind) {
  81. callback(fullText, { end, kind, pos });
  82. }
  83. }
  84. function isCompilerOptionEnabled(options, option) {
  85. switch (option) {
  86. case "stripInternal":
  87. case "declarationMap":
  88. case "emitDeclarationOnly":
  89. return options[option] === true && isCompilerOptionEnabled(options, "declaration");
  90. case "declaration":
  91. return options.declaration || isCompilerOptionEnabled(options, "composite");
  92. case "incremental":
  93. return options.incremental === void 0 ? isCompilerOptionEnabled(options, "composite") : options.incremental;
  94. case "skipDefaultLibCheck":
  95. return options.skipDefaultLibCheck || isCompilerOptionEnabled(options, "skipLibCheck");
  96. case "suppressImplicitAnyIndexErrors":
  97. return options.suppressImplicitAnyIndexErrors === true && isCompilerOptionEnabled(options, "noImplicitAny");
  98. case "allowSyntheticDefaultImports":
  99. return options.allowSyntheticDefaultImports !== void 0 ? options.allowSyntheticDefaultImports : isCompilerOptionEnabled(options, "esModuleInterop") || options.module === ts9.ModuleKind.System;
  100. case "noUncheckedIndexedAccess":
  101. return options.noUncheckedIndexedAccess === true && isCompilerOptionEnabled(options, "strictNullChecks");
  102. case "allowJs":
  103. return options.allowJs === void 0 ? isCompilerOptionEnabled(options, "checkJs") : options.allowJs;
  104. case "noImplicitAny":
  105. case "noImplicitThis":
  106. case "strictNullChecks":
  107. case "strictFunctionTypes":
  108. case "strictPropertyInitialization":
  109. case "alwaysStrict":
  110. case "strictBindCallApply":
  111. return isStrictCompilerOptionEnabled(
  112. options,
  113. option
  114. );
  115. }
  116. return options[option] === true;
  117. }
  118. function isStrictCompilerOptionEnabled(options, option) {
  119. return (options.strict ? options[option] !== false : options[option] === true) && (option !== "strictPropertyInitialization" || isStrictCompilerOptionEnabled(options, "strictNullChecks"));
  120. }
  121. function isFlagSet(allFlags, flag) {
  122. return (allFlags & flag) !== 0;
  123. }
  124. function isFlagSetOnObject(obj, flag) {
  125. return isFlagSet(obj.flags, flag);
  126. }
  127. function isModifierFlagSet(node, flag) {
  128. return isFlagSet(ts9.getCombinedModifierFlags(node), flag);
  129. }
  130. var isNodeFlagSet = isFlagSetOnObject;
  131. function isObjectFlagSet(objectType, flag) {
  132. return isFlagSet(objectType.objectFlags, flag);
  133. }
  134. var isSymbolFlagSet = isFlagSetOnObject;
  135. function isTransientSymbolLinksFlagSet(links, flag) {
  136. return isFlagSet(links.checkFlags, flag);
  137. }
  138. var isTypeFlagSet = isFlagSetOnObject;
  139. // src/modifiers.ts
  140. function includesModifier(modifiers, ...kinds) {
  141. if (modifiers === void 0) {
  142. return false;
  143. }
  144. for (const modifier of modifiers) {
  145. if (kinds.includes(modifier.kind)) {
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. function isAssignmentKind(kind) {
  152. return kind >= ts9.SyntaxKind.FirstAssignment && kind <= ts9.SyntaxKind.LastAssignment;
  153. }
  154. function isNumericPropertyName(name) {
  155. return String(+name) === name;
  156. }
  157. function charSize(ch) {
  158. return ch >= 65536 ? 2 : 1;
  159. }
  160. function isValidPropertyAccess(text, languageVersion = ts9.ScriptTarget.Latest) {
  161. if (text.length === 0) {
  162. return false;
  163. }
  164. let ch = text.codePointAt(0);
  165. if (!ts9.isIdentifierStart(ch, languageVersion)) {
  166. return false;
  167. }
  168. for (let i = charSize(ch); i < text.length; i += charSize(ch)) {
  169. ch = text.codePointAt(i);
  170. if (!ts9.isIdentifierPart(ch, languageVersion)) {
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. // src/nodes/access.ts
  177. var AccessKind = /* @__PURE__ */ ((AccessKind2) => {
  178. AccessKind2[AccessKind2["None"] = 0] = "None";
  179. AccessKind2[AccessKind2["Read"] = 1] = "Read";
  180. AccessKind2[AccessKind2["Write"] = 2] = "Write";
  181. AccessKind2[AccessKind2["Delete"] = 4] = "Delete";
  182. AccessKind2[AccessKind2["ReadWrite"] = 3] = "ReadWrite";
  183. return AccessKind2;
  184. })(AccessKind || {});
  185. function getAccessKind(node) {
  186. const parent = node.parent;
  187. switch (parent.kind) {
  188. case ts9.SyntaxKind.DeleteExpression:
  189. return 4 /* Delete */;
  190. case ts9.SyntaxKind.PostfixUnaryExpression:
  191. return 3 /* ReadWrite */;
  192. case ts9.SyntaxKind.PrefixUnaryExpression:
  193. return parent.operator === ts9.SyntaxKind.PlusPlusToken || parent.operator === ts9.SyntaxKind.MinusMinusToken ? 3 /* ReadWrite */ : 1 /* Read */;
  194. case ts9.SyntaxKind.BinaryExpression:
  195. return parent.right === node ? 1 /* Read */ : !isAssignmentKind(parent.operatorToken.kind) ? 1 /* Read */ : parent.operatorToken.kind === ts9.SyntaxKind.EqualsToken ? 2 /* Write */ : 3 /* ReadWrite */;
  196. case ts9.SyntaxKind.ShorthandPropertyAssignment:
  197. return parent.objectAssignmentInitializer === node ? 1 /* Read */ : isInDestructuringAssignment(parent) ? 2 /* Write */ : 1 /* Read */;
  198. case ts9.SyntaxKind.PropertyAssignment:
  199. return parent.name === node ? 0 /* None */ : isInDestructuringAssignment(parent) ? 2 /* Write */ : 1 /* Read */;
  200. case ts9.SyntaxKind.ArrayLiteralExpression:
  201. case ts9.SyntaxKind.SpreadElement:
  202. case ts9.SyntaxKind.SpreadAssignment:
  203. return isInDestructuringAssignment(
  204. parent
  205. ) ? 2 /* Write */ : 1 /* Read */;
  206. case ts9.SyntaxKind.ParenthesizedExpression:
  207. case ts9.SyntaxKind.NonNullExpression:
  208. case ts9.SyntaxKind.TypeAssertionExpression:
  209. case ts9.SyntaxKind.AsExpression:
  210. return getAccessKind(parent);
  211. case ts9.SyntaxKind.ForOfStatement:
  212. case ts9.SyntaxKind.ForInStatement:
  213. return parent.initializer === node ? 2 /* Write */ : 1 /* Read */;
  214. case ts9.SyntaxKind.ExpressionWithTypeArguments:
  215. return parent.parent.token === ts9.SyntaxKind.ExtendsKeyword && parent.parent.parent.kind !== ts9.SyntaxKind.InterfaceDeclaration ? 1 /* Read */ : 0 /* None */;
  216. case ts9.SyntaxKind.ComputedPropertyName:
  217. case ts9.SyntaxKind.ExpressionStatement:
  218. case ts9.SyntaxKind.TypeOfExpression:
  219. case ts9.SyntaxKind.ElementAccessExpression:
  220. case ts9.SyntaxKind.ForStatement:
  221. case ts9.SyntaxKind.IfStatement:
  222. case ts9.SyntaxKind.DoStatement:
  223. case ts9.SyntaxKind.WhileStatement:
  224. case ts9.SyntaxKind.SwitchStatement:
  225. case ts9.SyntaxKind.WithStatement:
  226. case ts9.SyntaxKind.ThrowStatement:
  227. case ts9.SyntaxKind.CallExpression:
  228. case ts9.SyntaxKind.NewExpression:
  229. case ts9.SyntaxKind.TaggedTemplateExpression:
  230. case ts9.SyntaxKind.JsxExpression:
  231. case ts9.SyntaxKind.Decorator:
  232. case ts9.SyntaxKind.TemplateSpan:
  233. case ts9.SyntaxKind.JsxOpeningElement:
  234. case ts9.SyntaxKind.JsxSelfClosingElement:
  235. case ts9.SyntaxKind.JsxSpreadAttribute:
  236. case ts9.SyntaxKind.VoidExpression:
  237. case ts9.SyntaxKind.ReturnStatement:
  238. case ts9.SyntaxKind.AwaitExpression:
  239. case ts9.SyntaxKind.YieldExpression:
  240. case ts9.SyntaxKind.ConditionalExpression:
  241. case ts9.SyntaxKind.CaseClause:
  242. case ts9.SyntaxKind.JsxElement:
  243. return 1 /* Read */;
  244. case ts9.SyntaxKind.ArrowFunction:
  245. return parent.body === node ? 1 /* Read */ : 2 /* Write */;
  246. case ts9.SyntaxKind.PropertyDeclaration:
  247. case ts9.SyntaxKind.VariableDeclaration:
  248. case ts9.SyntaxKind.Parameter:
  249. case ts9.SyntaxKind.EnumMember:
  250. case ts9.SyntaxKind.BindingElement:
  251. case ts9.SyntaxKind.JsxAttribute:
  252. return parent.initializer === node ? 1 /* Read */ : 0 /* None */;
  253. case ts9.SyntaxKind.PropertyAccessExpression:
  254. return parent.expression === node ? 1 /* Read */ : 0 /* None */;
  255. case ts9.SyntaxKind.ExportAssignment:
  256. return parent.isExportEquals ? 1 /* Read */ : 0 /* None */;
  257. }
  258. return 0 /* None */;
  259. }
  260. function isInDestructuringAssignment(node) {
  261. switch (node.kind) {
  262. case ts9.SyntaxKind.ShorthandPropertyAssignment:
  263. if (node.objectAssignmentInitializer !== void 0) {
  264. return true;
  265. }
  266. // falls through
  267. case ts9.SyntaxKind.PropertyAssignment:
  268. case ts9.SyntaxKind.SpreadAssignment:
  269. node = node.parent;
  270. break;
  271. case ts9.SyntaxKind.SpreadElement:
  272. if (node.parent.kind !== ts9.SyntaxKind.ArrayLiteralExpression) {
  273. return false;
  274. }
  275. node = node.parent;
  276. }
  277. while (true) {
  278. switch (node.parent.kind) {
  279. case ts9.SyntaxKind.BinaryExpression:
  280. return node.parent.left === node && node.parent.operatorToken.kind === ts9.SyntaxKind.EqualsToken;
  281. case ts9.SyntaxKind.ForOfStatement:
  282. return node.parent.initializer === node;
  283. case ts9.SyntaxKind.ArrayLiteralExpression:
  284. case ts9.SyntaxKind.ObjectLiteralExpression:
  285. node = node.parent;
  286. break;
  287. case ts9.SyntaxKind.SpreadAssignment:
  288. case ts9.SyntaxKind.PropertyAssignment:
  289. node = node.parent.parent;
  290. break;
  291. case ts9.SyntaxKind.SpreadElement:
  292. if (node.parent.parent.kind !== ts9.SyntaxKind.ArrayLiteralExpression) {
  293. return false;
  294. }
  295. node = node.parent.parent;
  296. break;
  297. default:
  298. return false;
  299. }
  300. }
  301. }
  302. function isAbstractKeyword(node) {
  303. return node.kind === ts9.SyntaxKind.AbstractKeyword;
  304. }
  305. function isAccessorKeyword(node) {
  306. return node.kind === ts9.SyntaxKind.AccessorKeyword;
  307. }
  308. function isAnyKeyword(node) {
  309. return node.kind === ts9.SyntaxKind.AnyKeyword;
  310. }
  311. function isAssertKeyword(node) {
  312. return node.kind === ts9.SyntaxKind.AssertKeyword;
  313. }
  314. function isAssertsKeyword(node) {
  315. return node.kind === ts9.SyntaxKind.AssertsKeyword;
  316. }
  317. function isAsyncKeyword(node) {
  318. return node.kind === ts9.SyntaxKind.AsyncKeyword;
  319. }
  320. function isAwaitKeyword(node) {
  321. return node.kind === ts9.SyntaxKind.AwaitKeyword;
  322. }
  323. function isBigIntKeyword(node) {
  324. return node.kind === ts9.SyntaxKind.BigIntKeyword;
  325. }
  326. function isBooleanKeyword(node) {
  327. return node.kind === ts9.SyntaxKind.BooleanKeyword;
  328. }
  329. function isColonToken(node) {
  330. return node.kind === ts9.SyntaxKind.ColonToken;
  331. }
  332. function isConstKeyword(node) {
  333. return node.kind === ts9.SyntaxKind.ConstKeyword;
  334. }
  335. function isDeclareKeyword(node) {
  336. return node.kind === ts9.SyntaxKind.DeclareKeyword;
  337. }
  338. function isDefaultKeyword(node) {
  339. return node.kind === ts9.SyntaxKind.DefaultKeyword;
  340. }
  341. function isDotToken(node) {
  342. return node.kind === ts9.SyntaxKind.DotToken;
  343. }
  344. function isEndOfFileToken(node) {
  345. return node.kind === ts9.SyntaxKind.EndOfFileToken;
  346. }
  347. function isEqualsGreaterThanToken(node) {
  348. return node.kind === ts9.SyntaxKind.EqualsGreaterThanToken;
  349. }
  350. function isEqualsToken(node) {
  351. return node.kind === ts9.SyntaxKind.EqualsToken;
  352. }
  353. function isExclamationToken(node) {
  354. return node.kind === ts9.SyntaxKind.ExclamationToken;
  355. }
  356. function isExportKeyword(node) {
  357. return node.kind === ts9.SyntaxKind.ExportKeyword;
  358. }
  359. function isFalseKeyword(node) {
  360. return node.kind === ts9.SyntaxKind.FalseKeyword;
  361. }
  362. function isFalseLiteral(node) {
  363. return node.kind === ts9.SyntaxKind.FalseKeyword;
  364. }
  365. function isImportExpression(node) {
  366. return node.kind === ts9.SyntaxKind.ImportKeyword;
  367. }
  368. function isImportKeyword(node) {
  369. return node.kind === ts9.SyntaxKind.ImportKeyword;
  370. }
  371. function isInKeyword(node) {
  372. return node.kind === ts9.SyntaxKind.InKeyword;
  373. }
  374. function isInputFiles(node) {
  375. return node.kind === ts9.SyntaxKind.InputFiles;
  376. }
  377. function isJSDocText(node) {
  378. return node.kind === ts9.SyntaxKind.JSDocText;
  379. }
  380. function isJsonMinusNumericLiteral(node) {
  381. return node.kind === ts9.SyntaxKind.PrefixUnaryExpression;
  382. }
  383. function isNeverKeyword(node) {
  384. return node.kind === ts9.SyntaxKind.NeverKeyword;
  385. }
  386. function isNullKeyword(node) {
  387. return node.kind === ts9.SyntaxKind.NullKeyword;
  388. }
  389. function isNullLiteral(node) {
  390. return node.kind === ts9.SyntaxKind.NullKeyword;
  391. }
  392. function isNumberKeyword(node) {
  393. return node.kind === ts9.SyntaxKind.NumberKeyword;
  394. }
  395. function isObjectKeyword(node) {
  396. return node.kind === ts9.SyntaxKind.ObjectKeyword;
  397. }
  398. function isOutKeyword(node) {
  399. return node.kind === ts9.SyntaxKind.OutKeyword;
  400. }
  401. function isOverrideKeyword(node) {
  402. return node.kind === ts9.SyntaxKind.OverrideKeyword;
  403. }
  404. function isPrivateKeyword(node) {
  405. return node.kind === ts9.SyntaxKind.PrivateKeyword;
  406. }
  407. function isProtectedKeyword(node) {
  408. return node.kind === ts9.SyntaxKind.ProtectedKeyword;
  409. }
  410. function isPublicKeyword(node) {
  411. return node.kind === ts9.SyntaxKind.PublicKeyword;
  412. }
  413. function isQuestionDotToken(node) {
  414. return node.kind === ts9.SyntaxKind.QuestionDotToken;
  415. }
  416. function isQuestionToken(node) {
  417. return node.kind === ts9.SyntaxKind.QuestionToken;
  418. }
  419. function isReadonlyKeyword(node) {
  420. return node.kind === ts9.SyntaxKind.ReadonlyKeyword;
  421. }
  422. function isStaticKeyword(node) {
  423. return node.kind === ts9.SyntaxKind.StaticKeyword;
  424. }
  425. function isStringKeyword(node) {
  426. return node.kind === ts9.SyntaxKind.StringKeyword;
  427. }
  428. function isSuperExpression(node) {
  429. return node.kind === ts9.SyntaxKind.SuperKeyword;
  430. }
  431. function isSuperKeyword(node) {
  432. return node.kind === ts9.SyntaxKind.SuperKeyword;
  433. }
  434. function isSymbolKeyword(node) {
  435. return node.kind === ts9.SyntaxKind.SymbolKeyword;
  436. }
  437. function isSyntaxList(node) {
  438. return node.kind === ts9.SyntaxKind.SyntaxList;
  439. }
  440. function isThisExpression(node) {
  441. return node.kind === ts9.SyntaxKind.ThisKeyword;
  442. }
  443. function isThisKeyword(node) {
  444. return node.kind === ts9.SyntaxKind.ThisKeyword;
  445. }
  446. function isTrueKeyword(node) {
  447. return node.kind === ts9.SyntaxKind.TrueKeyword;
  448. }
  449. function isTrueLiteral(node) {
  450. return node.kind === ts9.SyntaxKind.TrueKeyword;
  451. }
  452. function isUndefinedKeyword(node) {
  453. return node.kind === ts9.SyntaxKind.UndefinedKeyword;
  454. }
  455. function isUnknownKeyword(node) {
  456. return node.kind === ts9.SyntaxKind.UnknownKeyword;
  457. }
  458. function isUnparsedPrologue(node) {
  459. return node.kind === ts9.SyntaxKind.UnparsedPrologue;
  460. }
  461. function isUnparsedSyntheticReference(node) {
  462. return node.kind === ts9.SyntaxKind.UnparsedSyntheticReference;
  463. }
  464. function isVoidKeyword(node) {
  465. return node.kind === ts9.SyntaxKind.VoidKeyword;
  466. }
  467. var [tsMajor, tsMinor] = ts9.versionMajorMinor.split(".").map((raw) => Number.parseInt(raw, 10));
  468. function isTsVersionAtLeast(major, minor = 0) {
  469. return tsMajor > major || tsMajor === major && tsMinor >= minor;
  470. }
  471. // src/nodes/typeGuards/union.ts
  472. function isAccessExpression(node) {
  473. return ts9.isPropertyAccessExpression(node) || ts9.isElementAccessExpression(node);
  474. }
  475. function isAccessibilityModifier(node) {
  476. return isPublicKeyword(node) || isPrivateKeyword(node) || isProtectedKeyword(node);
  477. }
  478. function isAccessorDeclaration(node) {
  479. return ts9.isGetAccessorDeclaration(node) || ts9.isSetAccessorDeclaration(node);
  480. }
  481. function isArrayBindingElement(node) {
  482. return ts9.isBindingElement(node) || ts9.isOmittedExpression(node);
  483. }
  484. function isArrayBindingOrAssignmentPattern(node) {
  485. return ts9.isArrayBindingPattern(node) || ts9.isArrayLiteralExpression(node);
  486. }
  487. function isAssignmentPattern(node) {
  488. return ts9.isObjectLiteralExpression(node) || ts9.isArrayLiteralExpression(node);
  489. }
  490. function isBindingOrAssignmentElementRestIndicator(node) {
  491. if (ts9.isSpreadElement(node) || ts9.isSpreadAssignment(node)) {
  492. return true;
  493. }
  494. if (isTsVersionAtLeast(4, 4)) {
  495. return ts9.isDotDotDotToken(node);
  496. }
  497. return false;
  498. }
  499. function isBindingOrAssignmentElementTarget(node) {
  500. return isBindingOrAssignmentPattern(node) || ts9.isIdentifier(node) || ts9.isPropertyAccessExpression(node) || ts9.isElementAccessExpression(node) || ts9.isOmittedExpression(node);
  501. }
  502. function isBindingOrAssignmentPattern(node) {
  503. return isObjectBindingOrAssignmentPattern(node) || isArrayBindingOrAssignmentPattern(node);
  504. }
  505. function isBindingPattern(node) {
  506. return ts9.isObjectBindingPattern(node) || ts9.isArrayBindingPattern(node);
  507. }
  508. function isBlockLike(node) {
  509. return ts9.isSourceFile(node) || ts9.isBlock(node) || ts9.isModuleBlock(node) || ts9.isCaseOrDefaultClause(node);
  510. }
  511. function isBooleanLiteral(node) {
  512. return isTrueLiteral(node) || isFalseLiteral(node);
  513. }
  514. function isClassLikeDeclaration(node) {
  515. return ts9.isClassDeclaration(node) || ts9.isClassExpression(node);
  516. }
  517. function isClassMemberModifier(node) {
  518. return isAccessibilityModifier(node) || isReadonlyKeyword(node) || isStaticKeyword(node) || isAccessorKeyword(node);
  519. }
  520. function isDeclarationName(node) {
  521. return ts9.isIdentifier(node) || ts9.isPrivateIdentifier(node) || ts9.isStringLiteralLike(node) || ts9.isNumericLiteral(node) || ts9.isComputedPropertyName(node) || ts9.isElementAccessExpression(node) || isBindingPattern(node) || isEntityNameExpression(node);
  522. }
  523. function isDeclarationWithTypeParameterChildren(node) {
  524. return isSignatureDeclaration(node) || // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  525. isClassLikeDeclaration(node) || ts9.isInterfaceDeclaration(node) || ts9.isTypeAliasDeclaration(node) || ts9.isJSDocTemplateTag(node);
  526. }
  527. function isDeclarationWithTypeParameters(node) {
  528. return isDeclarationWithTypeParameterChildren(node) || ts9.isJSDocTypedefTag(node) || ts9.isJSDocCallbackTag(node) || ts9.isJSDocSignature(node);
  529. }
  530. function isDestructuringPattern(node) {
  531. return isBindingPattern(node) || ts9.isObjectLiteralExpression(node) || ts9.isArrayLiteralExpression(node);
  532. }
  533. function isEntityNameExpression(node) {
  534. return ts9.isIdentifier(node) || isPropertyAccessEntityNameExpression(node);
  535. }
  536. function isEntityNameOrEntityNameExpression(node) {
  537. return ts9.isEntityName(node) || isEntityNameExpression(node);
  538. }
  539. function isForInOrOfStatement(node) {
  540. return ts9.isForInStatement(node) || ts9.isForOfStatement(node);
  541. }
  542. function isFunctionLikeDeclaration(node) {
  543. return ts9.isFunctionDeclaration(node) || ts9.isMethodDeclaration(node) || ts9.isGetAccessorDeclaration(node) || ts9.isSetAccessorDeclaration(node) || ts9.isConstructorDeclaration(node) || ts9.isFunctionExpression(node) || ts9.isArrowFunction(node);
  544. }
  545. function hasDecorators(node) {
  546. return ts9.isParameter(node) || ts9.isPropertyDeclaration(node) || ts9.isMethodDeclaration(node) || ts9.isGetAccessorDeclaration(node) || ts9.isSetAccessorDeclaration(node) || ts9.isClassExpression(node) || ts9.isClassDeclaration(node);
  547. }
  548. function hasExpressionInitializer(node) {
  549. return ts9.isVariableDeclaration(node) || ts9.isParameter(node) || ts9.isBindingElement(node) || ts9.isPropertyDeclaration(node) || ts9.isPropertyAssignment(node) || ts9.isEnumMember(node);
  550. }
  551. function hasInitializer(node) {
  552. return hasExpressionInitializer(node) || ts9.isForStatement(node) || ts9.isForInStatement(node) || ts9.isForOfStatement(node) || ts9.isJsxAttribute(node);
  553. }
  554. function hasJSDoc(node) {
  555. if (
  556. // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  557. isAccessorDeclaration(node) || ts9.isArrowFunction(node) || ts9.isBlock(node) || ts9.isBreakStatement(node) || ts9.isCallSignatureDeclaration(node) || ts9.isCaseClause(node) || // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  558. isClassLikeDeclaration(node) || ts9.isConstructorDeclaration(node) || ts9.isConstructorTypeNode(node) || ts9.isConstructSignatureDeclaration(node) || ts9.isContinueStatement(node) || ts9.isDebuggerStatement(node) || ts9.isDoStatement(node) || ts9.isEmptyStatement(node) || isEndOfFileToken(node) || ts9.isEnumDeclaration(node) || ts9.isEnumMember(node) || ts9.isExportAssignment(node) || ts9.isExportDeclaration(node) || ts9.isExportSpecifier(node) || ts9.isExpressionStatement(node) || ts9.isForInStatement(node) || ts9.isForOfStatement(node) || ts9.isForStatement(node) || ts9.isFunctionDeclaration(node) || ts9.isFunctionExpression(node) || ts9.isFunctionTypeNode(node) || ts9.isIfStatement(node) || ts9.isImportDeclaration(node) || ts9.isImportEqualsDeclaration(node) || ts9.isIndexSignatureDeclaration(node) || ts9.isInterfaceDeclaration(node) || ts9.isJSDocFunctionType(node) || ts9.isLabeledStatement(node) || ts9.isMethodDeclaration(node) || ts9.isMethodSignature(node) || ts9.isModuleDeclaration(node) || ts9.isNamedTupleMember(node) || ts9.isNamespaceExportDeclaration(node) || ts9.isParameter(node) || ts9.isParenthesizedExpression(node) || ts9.isPropertyAssignment(node) || ts9.isPropertyDeclaration(node) || ts9.isPropertySignature(node) || ts9.isReturnStatement(node) || ts9.isShorthandPropertyAssignment(node) || ts9.isSpreadAssignment(node) || ts9.isSwitchStatement(node) || ts9.isThrowStatement(node) || ts9.isTryStatement(node) || ts9.isTypeAliasDeclaration(node) || ts9.isVariableDeclaration(node) || ts9.isVariableStatement(node) || ts9.isWhileStatement(node) || ts9.isWithStatement(node)
  559. ) {
  560. return true;
  561. }
  562. if (isTsVersionAtLeast(4, 4) && ts9.isClassStaticBlockDeclaration(node)) {
  563. return true;
  564. }
  565. if (isTsVersionAtLeast(5, 0) && (ts9.isBinaryExpression(node) || ts9.isElementAccessExpression(node) || ts9.isIdentifier(node) || ts9.isJSDocSignature(node) || ts9.isObjectLiteralExpression(node) || ts9.isPropertyAccessExpression(node) || ts9.isTypeParameterDeclaration(node))) {
  566. return true;
  567. }
  568. return false;
  569. }
  570. function hasModifiers(node) {
  571. return ts9.isTypeParameterDeclaration(node) || ts9.isParameter(node) || ts9.isConstructorTypeNode(node) || ts9.isPropertySignature(node) || ts9.isPropertyDeclaration(node) || ts9.isMethodSignature(node) || ts9.isMethodDeclaration(node) || ts9.isConstructorDeclaration(node) || ts9.isGetAccessorDeclaration(node) || ts9.isSetAccessorDeclaration(node) || ts9.isIndexSignatureDeclaration(node) || ts9.isFunctionExpression(node) || ts9.isArrowFunction(node) || ts9.isClassExpression(node) || ts9.isVariableStatement(node) || ts9.isFunctionDeclaration(node) || ts9.isClassDeclaration(node) || ts9.isInterfaceDeclaration(node) || ts9.isTypeAliasDeclaration(node) || ts9.isEnumDeclaration(node) || ts9.isModuleDeclaration(node) || ts9.isImportEqualsDeclaration(node) || ts9.isImportDeclaration(node) || ts9.isExportAssignment(node) || ts9.isExportDeclaration(node);
  572. }
  573. function hasType(node) {
  574. return isSignatureDeclaration(node) || ts9.isVariableDeclaration(node) || ts9.isParameter(node) || ts9.isPropertySignature(node) || ts9.isPropertyDeclaration(node) || ts9.isTypePredicateNode(node) || ts9.isParenthesizedTypeNode(node) || ts9.isTypeOperatorNode(node) || ts9.isMappedTypeNode(node) || ts9.isAssertionExpression(node) || ts9.isTypeAliasDeclaration(node) || ts9.isJSDocTypeExpression(node) || ts9.isJSDocNonNullableType(node) || ts9.isJSDocNullableType(node) || ts9.isJSDocOptionalType(node) || ts9.isJSDocVariadicType(node);
  575. }
  576. function hasTypeArguments(node) {
  577. return ts9.isCallExpression(node) || ts9.isNewExpression(node) || ts9.isTaggedTemplateExpression(node) || ts9.isJsxOpeningElement(node) || ts9.isJsxSelfClosingElement(node);
  578. }
  579. function isJSDocComment(node) {
  580. if (isJSDocText(node)) {
  581. return true;
  582. }
  583. if (isTsVersionAtLeast(4, 4)) {
  584. return ts9.isJSDocLink(node) || ts9.isJSDocLinkCode(node) || ts9.isJSDocLinkPlain(node);
  585. }
  586. return false;
  587. }
  588. function isJSDocNamespaceBody(node) {
  589. return ts9.isIdentifier(node) || isJSDocNamespaceDeclaration(node);
  590. }
  591. function isJSDocTypeReferencingNode(node) {
  592. return ts9.isJSDocVariadicType(node) || ts9.isJSDocOptionalType(node) || ts9.isJSDocNullableType(node) || ts9.isJSDocNonNullableType(node);
  593. }
  594. function isJsonObjectExpression(node) {
  595. return ts9.isObjectLiteralExpression(node) || ts9.isArrayLiteralExpression(node) || isJsonMinusNumericLiteral(node) || ts9.isNumericLiteral(node) || ts9.isStringLiteral(node) || isBooleanLiteral(node) || isNullLiteral(node);
  596. }
  597. function isJsxAttributeLike(node) {
  598. return ts9.isJsxAttribute(node) || ts9.isJsxSpreadAttribute(node);
  599. }
  600. function isJsxAttributeValue(node) {
  601. return ts9.isStringLiteral(node) || ts9.isJsxExpression(node) || ts9.isJsxElement(node) || ts9.isJsxSelfClosingElement(node) || ts9.isJsxFragment(node);
  602. }
  603. function isJsxChild(node) {
  604. return ts9.isJsxText(node) || ts9.isJsxExpression(node) || ts9.isJsxElement(node) || ts9.isJsxSelfClosingElement(node) || ts9.isJsxFragment(node);
  605. }
  606. function isJsxTagNameExpression(node) {
  607. return ts9.isIdentifier(node) || isThisExpression(node) || isJsxTagNamePropertyAccess(node);
  608. }
  609. function isLiteralToken(node) {
  610. return ts9.isNumericLiteral(node) || ts9.isBigIntLiteral(node) || ts9.isStringLiteral(node) || ts9.isJsxText(node) || ts9.isRegularExpressionLiteral(node) || ts9.isNoSubstitutionTemplateLiteral(node);
  611. }
  612. function isModuleBody(node) {
  613. return isNamespaceBody(node) || isJSDocNamespaceBody(node);
  614. }
  615. function isModuleName(node) {
  616. return ts9.isIdentifier(node) || ts9.isStringLiteral(node);
  617. }
  618. function isModuleReference(node) {
  619. return ts9.isEntityName(node) || ts9.isExternalModuleReference(node);
  620. }
  621. function isNamedImportBindings(node) {
  622. return ts9.isNamespaceImport(node) || ts9.isNamedImports(node);
  623. }
  624. function isNamedImportsOrExports(node) {
  625. return ts9.isNamedImports(node) || ts9.isNamedExports(node);
  626. }
  627. function isNamespaceBody(node) {
  628. return ts9.isModuleBlock(node) || isNamespaceDeclaration(node);
  629. }
  630. function isObjectBindingOrAssignmentElement(node) {
  631. return ts9.isBindingElement(node) || ts9.isPropertyAssignment(node) || ts9.isShorthandPropertyAssignment(node) || ts9.isSpreadAssignment(node);
  632. }
  633. function isObjectBindingOrAssignmentPattern(node) {
  634. return ts9.isObjectBindingPattern(node) || ts9.isObjectLiteralExpression(node);
  635. }
  636. function isObjectTypeDeclaration(node) {
  637. return (
  638. // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  639. isClassLikeDeclaration(node) || ts9.isInterfaceDeclaration(node) || ts9.isTypeLiteralNode(node)
  640. );
  641. }
  642. function isParameterPropertyModifier(node) {
  643. return isAccessibilityModifier(node) || isReadonlyKeyword(node);
  644. }
  645. function isPropertyNameLiteral(node) {
  646. return ts9.isIdentifier(node) || ts9.isStringLiteralLike(node) || ts9.isNumericLiteral(node);
  647. }
  648. function isPseudoLiteralToken(node) {
  649. return ts9.isTemplateHead(node) || ts9.isTemplateMiddle(node) || ts9.isTemplateTail(node);
  650. }
  651. function isSignatureDeclaration(node) {
  652. return ts9.isCallSignatureDeclaration(node) || ts9.isConstructSignatureDeclaration(node) || ts9.isMethodSignature(node) || ts9.isIndexSignatureDeclaration(node) || ts9.isFunctionTypeNode(node) || ts9.isConstructorTypeNode(node) || ts9.isJSDocFunctionType(node) || ts9.isFunctionDeclaration(node) || ts9.isMethodDeclaration(node) || ts9.isConstructorDeclaration(node) || // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts <5
  653. isAccessorDeclaration(node) || ts9.isFunctionExpression(node) || ts9.isArrowFunction(node);
  654. }
  655. function isSuperProperty(node) {
  656. return isSuperPropertyAccessExpression(node) || isSuperElementAccessExpression(node);
  657. }
  658. function isTypeOnlyCompatibleAliasDeclaration(node) {
  659. if (ts9.isImportClause(node) || ts9.isImportEqualsDeclaration(node) || ts9.isNamespaceImport(node) || ts9.isImportOrExportSpecifier(node)) {
  660. return true;
  661. }
  662. if (isTsVersionAtLeast(5, 0) && (ts9.isExportDeclaration(node) || ts9.isNamespaceExport(node))) {
  663. return true;
  664. }
  665. return false;
  666. }
  667. function isTypeReferenceType(node) {
  668. return ts9.isTypeReferenceNode(node) || ts9.isExpressionWithTypeArguments(node);
  669. }
  670. function isUnionOrIntersectionTypeNode(node) {
  671. return ts9.isUnionTypeNode(node) || ts9.isIntersectionTypeNode(node);
  672. }
  673. function isUnparsedSourceText(node) {
  674. return ts9.isUnparsedPrepend(node) || ts9.isUnparsedTextLike(node);
  675. }
  676. function isVariableLikeDeclaration(node) {
  677. return ts9.isVariableDeclaration(node) || ts9.isParameter(node) || ts9.isBindingElement(node) || ts9.isPropertyDeclaration(node) || ts9.isPropertyAssignment(node) || ts9.isPropertySignature(node) || ts9.isJsxAttribute(node) || ts9.isShorthandPropertyAssignment(node) || ts9.isEnumMember(node) || ts9.isJSDocPropertyTag(node) || ts9.isJSDocParameterTag(node);
  678. }
  679. // src/nodes/typeGuards/compound.ts
  680. function isConstAssertionExpression(node) {
  681. return ts9.isTypeReferenceNode(node.type) && ts9.isIdentifier(node.type.typeName) && node.type.typeName.escapedText === "const";
  682. }
  683. function isIterationStatement(node) {
  684. switch (node.kind) {
  685. case ts9.SyntaxKind.DoStatement:
  686. case ts9.SyntaxKind.ForInStatement:
  687. case ts9.SyntaxKind.ForOfStatement:
  688. case ts9.SyntaxKind.ForStatement:
  689. case ts9.SyntaxKind.WhileStatement:
  690. return true;
  691. default:
  692. return false;
  693. }
  694. }
  695. function isJSDocNamespaceDeclaration(node) {
  696. return ts9.isModuleDeclaration(node) && ts9.isIdentifier(node.name) && (node.body === void 0 || isJSDocNamespaceBody(node.body));
  697. }
  698. function isJsxTagNamePropertyAccess(node) {
  699. return ts9.isPropertyAccessExpression(node) && // eslint-disable-next-line deprecation/deprecation -- Keep compatibility with ts < 5
  700. isJsxTagNameExpression(node.expression);
  701. }
  702. function isNamedDeclarationWithName(node) {
  703. return "name" in node && node.name !== void 0 && node.name !== null && isDeclarationName(node.name);
  704. }
  705. function isNamespaceDeclaration(node) {
  706. return ts9.isModuleDeclaration(node) && ts9.isIdentifier(node.name) && node.body !== void 0 && isNamespaceBody(node.body);
  707. }
  708. function isNumericOrStringLikeLiteral(node) {
  709. switch (node.kind) {
  710. case ts9.SyntaxKind.StringLiteral:
  711. case ts9.SyntaxKind.NumericLiteral:
  712. case ts9.SyntaxKind.NoSubstitutionTemplateLiteral:
  713. return true;
  714. default:
  715. return false;
  716. }
  717. }
  718. function isPropertyAccessEntityNameExpression(node) {
  719. return ts9.isPropertyAccessExpression(node) && ts9.isIdentifier(node.name) && isEntityNameExpression(node.expression);
  720. }
  721. function isSuperElementAccessExpression(node) {
  722. return ts9.isElementAccessExpression(node) && isSuperExpression(node.expression);
  723. }
  724. function isSuperPropertyAccessExpression(node) {
  725. return ts9.isPropertyAccessExpression(node) && isSuperExpression(node.expression);
  726. }
  727. function isFunctionScopeBoundary(node) {
  728. switch (node.kind) {
  729. case ts9.SyntaxKind.FunctionExpression:
  730. case ts9.SyntaxKind.ArrowFunction:
  731. case ts9.SyntaxKind.Constructor:
  732. case ts9.SyntaxKind.ModuleDeclaration:
  733. case ts9.SyntaxKind.ClassDeclaration:
  734. case ts9.SyntaxKind.ClassExpression:
  735. case ts9.SyntaxKind.EnumDeclaration:
  736. case ts9.SyntaxKind.MethodDeclaration:
  737. case ts9.SyntaxKind.FunctionDeclaration:
  738. case ts9.SyntaxKind.GetAccessor:
  739. case ts9.SyntaxKind.SetAccessor:
  740. case ts9.SyntaxKind.MethodSignature:
  741. case ts9.SyntaxKind.CallSignature:
  742. case ts9.SyntaxKind.ConstructSignature:
  743. case ts9.SyntaxKind.ConstructorType:
  744. case ts9.SyntaxKind.FunctionType:
  745. return true;
  746. case ts9.SyntaxKind.SourceFile:
  747. return ts9.isExternalModule(node);
  748. default:
  749. return false;
  750. }
  751. }
  752. function isIntrinsicAnyType(type) {
  753. return isTypeFlagSet(type, ts9.TypeFlags.Any);
  754. }
  755. function isIntrinsicBooleanType(type) {
  756. return isTypeFlagSet(type, ts9.TypeFlags.Boolean);
  757. }
  758. function isIntrinsicBigIntType(type) {
  759. return isTypeFlagSet(type, ts9.TypeFlags.BigInt);
  760. }
  761. function isIntrinsicErrorType(type) {
  762. return isIntrinsicType(type) && type.intrinsicName === "error";
  763. }
  764. function isIntrinsicESSymbolType(type) {
  765. return isTypeFlagSet(type, ts9.TypeFlags.ESSymbol);
  766. }
  767. var IntrinsicTypeFlags = ts9.TypeFlags.Intrinsic ?? ts9.TypeFlags.Any | ts9.TypeFlags.Unknown | ts9.TypeFlags.String | ts9.TypeFlags.Number | ts9.TypeFlags.BigInt | ts9.TypeFlags.Boolean | ts9.TypeFlags.BooleanLiteral | ts9.TypeFlags.ESSymbol | ts9.TypeFlags.Void | ts9.TypeFlags.Undefined | ts9.TypeFlags.Null | ts9.TypeFlags.Never | ts9.TypeFlags.NonPrimitive;
  768. function isIntrinsicType(type) {
  769. return isTypeFlagSet(type, IntrinsicTypeFlags);
  770. }
  771. function isIntrinsicNeverType(type) {
  772. return isTypeFlagSet(type, ts9.TypeFlags.Never);
  773. }
  774. function isIntrinsicNonPrimitiveType(type) {
  775. return isTypeFlagSet(type, ts9.TypeFlags.NonPrimitive);
  776. }
  777. function isIntrinsicNullType(type) {
  778. return isTypeFlagSet(type, ts9.TypeFlags.Null);
  779. }
  780. function isIntrinsicNumberType(type) {
  781. return isTypeFlagSet(type, ts9.TypeFlags.Number);
  782. }
  783. function isIntrinsicStringType(type) {
  784. return isTypeFlagSet(type, ts9.TypeFlags.String);
  785. }
  786. function isIntrinsicUndefinedType(type) {
  787. return isTypeFlagSet(type, ts9.TypeFlags.Undefined);
  788. }
  789. function isIntrinsicUnknownType(type) {
  790. return isTypeFlagSet(type, ts9.TypeFlags.Unknown);
  791. }
  792. function isIntrinsicVoidType(type) {
  793. return isTypeFlagSet(type, ts9.TypeFlags.Void);
  794. }
  795. function isConditionalType(type) {
  796. return isTypeFlagSet(type, ts9.TypeFlags.Conditional);
  797. }
  798. function isEnumType(type) {
  799. return isTypeFlagSet(type, ts9.TypeFlags.Enum);
  800. }
  801. function isFreshableType(type) {
  802. return isTypeFlagSet(type, ts9.TypeFlags.Freshable);
  803. }
  804. function isIndexType(type) {
  805. return isTypeFlagSet(type, ts9.TypeFlags.Index);
  806. }
  807. function isIndexedAccessType(type) {
  808. return isTypeFlagSet(type, ts9.TypeFlags.IndexedAccess);
  809. }
  810. function isInstantiableType(type) {
  811. return isTypeFlagSet(type, ts9.TypeFlags.Instantiable);
  812. }
  813. function isIntersectionType(type) {
  814. return isTypeFlagSet(type, ts9.TypeFlags.Intersection);
  815. }
  816. function isObjectType(type) {
  817. return isTypeFlagSet(type, ts9.TypeFlags.Object);
  818. }
  819. function isStringMappingType(type) {
  820. return isTypeFlagSet(type, ts9.TypeFlags.StringMapping);
  821. }
  822. function isSubstitutionType(type) {
  823. return isTypeFlagSet(type, ts9.TypeFlags.Substitution);
  824. }
  825. function isTypeParameter(type) {
  826. return isTypeFlagSet(type, ts9.TypeFlags.TypeParameter);
  827. }
  828. function isTypeVariable(type) {
  829. return isTypeFlagSet(type, ts9.TypeFlags.TypeVariable);
  830. }
  831. function isUnionType(type) {
  832. return isTypeFlagSet(type, ts9.TypeFlags.Union);
  833. }
  834. function isUnionOrIntersectionType(type) {
  835. return isTypeFlagSet(type, ts9.TypeFlags.UnionOrIntersection);
  836. }
  837. function isUniqueESSymbolType(type) {
  838. return isTypeFlagSet(type, ts9.TypeFlags.UniqueESSymbol);
  839. }
  840. // src/types/typeGuards/objects.ts
  841. function isEvolvingArrayType(type) {
  842. return isObjectType(type) && isObjectFlagSet(type, ts9.ObjectFlags.EvolvingArray);
  843. }
  844. function isTupleType(type) {
  845. return isObjectType(type) && isObjectFlagSet(type, ts9.ObjectFlags.Tuple);
  846. }
  847. function isTypeReference(type) {
  848. return isObjectType(type) && isObjectFlagSet(type, ts9.ObjectFlags.Reference);
  849. }
  850. // src/types/typeGuards/compound.ts
  851. function isFreshableIntrinsicType(type) {
  852. return isIntrinsicType(type) && isFreshableType(type);
  853. }
  854. function isTupleTypeReference(type) {
  855. return isTypeReference(type) && isTupleType(type.target);
  856. }
  857. function isBooleanLiteralType(type) {
  858. return isTypeFlagSet(type, ts9.TypeFlags.BooleanLiteral);
  859. }
  860. function isBigIntLiteralType(type) {
  861. return isTypeFlagSet(type, ts9.TypeFlags.BigIntLiteral);
  862. }
  863. function isFalseLiteralType(type) {
  864. return isBooleanLiteralType(type) && type.intrinsicName === "false";
  865. }
  866. function isLiteralType(type) {
  867. return isTypeFlagSet(type, ts9.TypeFlags.Literal);
  868. }
  869. function isNumberLiteralType(type) {
  870. return isTypeFlagSet(type, ts9.TypeFlags.NumberLiteral);
  871. }
  872. function isStringLiteralType(type) {
  873. return isTypeFlagSet(type, ts9.TypeFlags.StringLiteral);
  874. }
  875. function isTemplateLiteralType(type) {
  876. return isTypeFlagSet(type, ts9.TypeFlags.TemplateLiteral);
  877. }
  878. function isTrueLiteralType(type) {
  879. return isBooleanLiteralType(type) && type.intrinsicName === "true";
  880. }
  881. function isUnknownLiteralType(type) {
  882. return isTypeFlagSet(type, ts9.TypeFlags.Literal);
  883. }
  884. // src/types/getters.ts
  885. function getCallSignaturesOfType(type) {
  886. if (isUnionType(type)) {
  887. const signatures = [];
  888. for (const subType of type.types) {
  889. signatures.push(...getCallSignaturesOfType(subType));
  890. }
  891. return signatures;
  892. }
  893. if (isIntersectionType(type)) {
  894. let signatures;
  895. for (const subType of type.types) {
  896. const sig = getCallSignaturesOfType(subType);
  897. if (sig.length !== 0) {
  898. if (signatures !== void 0) {
  899. return [];
  900. }
  901. signatures = sig;
  902. }
  903. }
  904. return signatures === void 0 ? [] : signatures;
  905. }
  906. return type.getCallSignatures();
  907. }
  908. function getPropertyOfType(type, name) {
  909. if (!name.startsWith("__")) {
  910. return type.getProperty(name);
  911. }
  912. return type.getProperties().find((s) => s.escapedName === name);
  913. }
  914. function getWellKnownSymbolPropertyOfType(type, wellKnownSymbolName, typeChecker) {
  915. const prefix = "__@" + wellKnownSymbolName;
  916. for (const prop of type.getProperties()) {
  917. if (!prop.name.startsWith(prefix)) {
  918. continue;
  919. }
  920. const declaration = prop.valueDeclaration ?? prop.getDeclarations()[0];
  921. if (!isNamedDeclarationWithName(declaration) || declaration.name === void 0 || !ts9.isComputedPropertyName(declaration.name)) {
  922. continue;
  923. }
  924. const globalSymbol = typeChecker.getApparentType(
  925. typeChecker.getTypeAtLocation(declaration.name.expression)
  926. ).symbol;
  927. if (prop.escapedName === getPropertyNameOfWellKnownSymbol(
  928. typeChecker,
  929. globalSymbol,
  930. wellKnownSymbolName
  931. )) {
  932. return prop;
  933. }
  934. }
  935. return void 0;
  936. }
  937. function getPropertyNameOfWellKnownSymbol(typeChecker, symbolConstructor, symbolName) {
  938. const knownSymbol = symbolConstructor && typeChecker.getTypeOfSymbolAtLocation(
  939. symbolConstructor,
  940. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
  941. symbolConstructor.valueDeclaration
  942. ).getProperty(symbolName);
  943. const knownSymbolType = knownSymbol && typeChecker.getTypeOfSymbolAtLocation(
  944. knownSymbol,
  945. // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
  946. knownSymbol.valueDeclaration
  947. );
  948. if (knownSymbolType && isUniqueESSymbolType(knownSymbolType)) {
  949. return knownSymbolType.escapedName;
  950. }
  951. return "__@" + symbolName;
  952. }
  953. function isBindableObjectDefinePropertyCall(node) {
  954. return node.arguments.length === 3 && isEntityNameExpression(node.arguments[0]) && isNumericOrStringLikeLiteral(node.arguments[1]) && ts9.isPropertyAccessExpression(node.expression) && node.expression.name.escapedText === "defineProperty" && ts9.isIdentifier(node.expression.expression) && node.expression.expression.escapedText === "Object";
  955. }
  956. function isInConstContext(node, typeChecker) {
  957. var _a, _b;
  958. let current = node;
  959. while (true) {
  960. const parent = current.parent;
  961. outer: switch (parent.kind) {
  962. case ts9.SyntaxKind.TypeAssertionExpression:
  963. case ts9.SyntaxKind.AsExpression:
  964. return isConstAssertionExpression(parent);
  965. case ts9.SyntaxKind.PrefixUnaryExpression:
  966. if (current.kind !== ts9.SyntaxKind.NumericLiteral) {
  967. return false;
  968. }
  969. switch (parent.operator) {
  970. case ts9.SyntaxKind.PlusToken:
  971. case ts9.SyntaxKind.MinusToken:
  972. current = parent;
  973. break outer;
  974. default:
  975. return false;
  976. }
  977. case ts9.SyntaxKind.PropertyAssignment:
  978. if (parent.initializer !== current) {
  979. return false;
  980. }
  981. current = parent.parent;
  982. break;
  983. case ts9.SyntaxKind.ShorthandPropertyAssignment:
  984. current = parent.parent;
  985. break;
  986. case ts9.SyntaxKind.ParenthesizedExpression:
  987. case ts9.SyntaxKind.ArrayLiteralExpression:
  988. case ts9.SyntaxKind.ObjectLiteralExpression:
  989. case ts9.SyntaxKind.TemplateExpression:
  990. current = parent;
  991. break;
  992. case ts9.SyntaxKind.CallExpression:
  993. if (!ts9.isExpression(current)) {
  994. return false;
  995. }
  996. const functionSignature = typeChecker.getResolvedSignature(
  997. parent
  998. );
  999. if (functionSignature === void 0) {
  1000. return false;
  1001. }
  1002. const argumentIndex = parent.arguments.indexOf(
  1003. current
  1004. );
  1005. if (argumentIndex < 0) {
  1006. return false;
  1007. }
  1008. const parameterSymbol = functionSignature.getParameters()[argumentIndex];
  1009. if (parameterSymbol === void 0 || !("links" in parameterSymbol)) {
  1010. return false;
  1011. }
  1012. const parameterSymbolLinks = parameterSymbol.links;
  1013. const propertySymbol = (_b = (_a = parameterSymbolLinks.type) == null ? void 0 : _a.getProperties()) == null ? void 0 : _b[argumentIndex];
  1014. if (propertySymbol === void 0 || !("links" in propertySymbol)) {
  1015. return false;
  1016. }
  1017. return isTransientSymbolLinksFlagSet(
  1018. propertySymbol.links,
  1019. ts9.CheckFlags.Readonly
  1020. );
  1021. default:
  1022. return false;
  1023. }
  1024. }
  1025. }
  1026. // src/types/utilities.ts
  1027. function isFalsyType(type) {
  1028. if (isTypeFlagSet(
  1029. type,
  1030. ts9.TypeFlags.Undefined | ts9.TypeFlags.Null | ts9.TypeFlags.Void
  1031. )) {
  1032. return true;
  1033. }
  1034. if (typeIsLiteral(type)) {
  1035. if (typeof type.value === "object") {
  1036. return type.value.base10Value === "0";
  1037. } else {
  1038. return !type.value;
  1039. }
  1040. }
  1041. return isFalseLiteralType(type);
  1042. }
  1043. function intersectionTypeParts(type) {
  1044. return isIntersectionType(type) ? type.types : [type];
  1045. }
  1046. function typeParts(type) {
  1047. return isIntersectionType(type) || isUnionType(type) ? type.types : [type];
  1048. }
  1049. function isReadonlyPropertyIntersection(type, name, typeChecker) {
  1050. const typeParts2 = isIntersectionType(type) ? type.types : [type];
  1051. return typeParts2.some((subType) => {
  1052. const prop = getPropertyOfType(subType, name);
  1053. if (prop === void 0) {
  1054. return false;
  1055. }
  1056. if (prop.flags & ts9.SymbolFlags.Transient) {
  1057. if (/^(?:[1-9]\d*|0)$/.test(name) && isTupleTypeReference(subType)) {
  1058. return subType.target.readonly;
  1059. }
  1060. switch (isReadonlyPropertyFromMappedType(subType, name, typeChecker)) {
  1061. case true:
  1062. return true;
  1063. case false:
  1064. return false;
  1065. }
  1066. }
  1067. return !!// members of namespace import
  1068. (isSymbolFlagSet(prop, ts9.SymbolFlags.ValueModule) || // we unwrapped every mapped type, now we can check the actual declarations
  1069. symbolHasReadonlyDeclaration(prop, typeChecker));
  1070. });
  1071. }
  1072. function isReadonlyPropertyFromMappedType(type, name, typeChecker) {
  1073. if (!isObjectType(type) || !isObjectFlagSet(type, ts9.ObjectFlags.Mapped)) {
  1074. return;
  1075. }
  1076. const declaration = type.symbol.declarations[0];
  1077. if (declaration.readonlyToken !== void 0 && !/^__@[^@]+$/.test(name)) {
  1078. return declaration.readonlyToken.kind !== ts9.SyntaxKind.MinusToken;
  1079. }
  1080. const { modifiersType } = type;
  1081. return modifiersType && isPropertyReadonlyInType(modifiersType, name, typeChecker);
  1082. }
  1083. function isCallback(typeChecker, param, node) {
  1084. let type = typeChecker.getApparentType(
  1085. typeChecker.getTypeOfSymbolAtLocation(param, node)
  1086. );
  1087. if (param.valueDeclaration.dotDotDotToken) {
  1088. type = type.getNumberIndexType();
  1089. if (type === void 0) {
  1090. return false;
  1091. }
  1092. }
  1093. for (const subType of unionTypeParts(type)) {
  1094. if (subType.getCallSignatures().length !== 0) {
  1095. return true;
  1096. }
  1097. }
  1098. return false;
  1099. }
  1100. function isPropertyReadonlyInType(type, name, typeChecker) {
  1101. let seenProperty = false;
  1102. let seenReadonlySignature = false;
  1103. for (const subType of unionTypeParts(type)) {
  1104. if (getPropertyOfType(subType, name) === void 0) {
  1105. const index = (isNumericPropertyName(name) ? typeChecker.getIndexInfoOfType(subType, ts9.IndexKind.Number) : void 0) ?? typeChecker.getIndexInfoOfType(subType, ts9.IndexKind.String);
  1106. if (index == null ? void 0 : index.isReadonly) {
  1107. if (seenProperty) {
  1108. return true;
  1109. }
  1110. seenReadonlySignature = true;
  1111. }
  1112. } else if (seenReadonlySignature || isReadonlyPropertyIntersection(subType, name, typeChecker)) {
  1113. return true;
  1114. } else {
  1115. seenProperty = true;
  1116. }
  1117. }
  1118. return false;
  1119. }
  1120. function isReadonlyAssignmentDeclaration(node, typeChecker) {
  1121. if (!isBindableObjectDefinePropertyCall(node)) {
  1122. return false;
  1123. }
  1124. const descriptorType = typeChecker.getTypeAtLocation(node.arguments[2]);
  1125. if (descriptorType.getProperty("value") === void 0) {
  1126. return descriptorType.getProperty("set") === void 0;
  1127. }
  1128. const writableProp = descriptorType.getProperty("writable");
  1129. if (writableProp === void 0) {
  1130. return false;
  1131. }
  1132. const writableType = writableProp.valueDeclaration !== void 0 && ts9.isPropertyAssignment(writableProp.valueDeclaration) ? typeChecker.getTypeAtLocation(writableProp.valueDeclaration.initializer) : typeChecker.getTypeOfSymbolAtLocation(writableProp, node.arguments[2]);
  1133. return isFalseLiteralType(writableType);
  1134. }
  1135. function isThenableType(typeChecker, node, type = typeChecker.getTypeAtLocation(node)) {
  1136. for (const typePart of unionTypeParts(typeChecker.getApparentType(type))) {
  1137. const then = typePart.getProperty("then");
  1138. if (then === void 0) {
  1139. continue;
  1140. }
  1141. const thenType = typeChecker.getTypeOfSymbolAtLocation(then, node);
  1142. for (const subTypePart of unionTypeParts(thenType)) {
  1143. for (const signature of subTypePart.getCallSignatures()) {
  1144. if (signature.parameters.length !== 0 && isCallback(typeChecker, signature.parameters[0], node)) {
  1145. return true;
  1146. }
  1147. }
  1148. }
  1149. }
  1150. return false;
  1151. }
  1152. function symbolHasReadonlyDeclaration(symbol, typeChecker) {
  1153. var _a;
  1154. return !!((symbol.flags & ts9.SymbolFlags.Accessor) === ts9.SymbolFlags.GetAccessor || ((_a = symbol.declarations) == null ? void 0 : _a.some(
  1155. (node) => isModifierFlagSet(node, ts9.ModifierFlags.Readonly) || ts9.isVariableDeclaration(node) && isNodeFlagSet(node.parent, ts9.NodeFlags.Const) || ts9.isCallExpression(node) && isReadonlyAssignmentDeclaration(node, typeChecker) || ts9.isEnumMember(node) || (ts9.isPropertyAssignment(node) || ts9.isShorthandPropertyAssignment(node)) && isInConstContext(node, typeChecker)
  1156. )));
  1157. }
  1158. function unionTypeParts(type) {
  1159. return isUnionType(type) ? type.types : [type];
  1160. }
  1161. function typeIsLiteral(type) {
  1162. if (isTsVersionAtLeast(5, 0)) {
  1163. return type.isLiteral();
  1164. } else {
  1165. return isTypeFlagSet(
  1166. type,
  1167. ts9.TypeFlags.StringLiteral | ts9.TypeFlags.NumberLiteral | ts9.TypeFlags.BigIntLiteral
  1168. );
  1169. }
  1170. }
  1171. function isBlockScopeBoundary(node) {
  1172. switch (node.kind) {
  1173. case ts9.SyntaxKind.Block: {
  1174. const parent = node.parent;
  1175. return parent.kind !== ts9.SyntaxKind.CatchClause && // blocks inside SourceFile are block scope boundaries
  1176. (parent.kind === ts9.SyntaxKind.SourceFile || // blocks that are direct children of a function scope boundary are no scope boundary
  1177. // for example the FunctionBlock is part of the function scope of the containing function
  1178. !isFunctionScopeBoundary(parent)) ? 2 /* Block */ : 0 /* None */;
  1179. }
  1180. case ts9.SyntaxKind.ForStatement:
  1181. case ts9.SyntaxKind.ForInStatement:
  1182. case ts9.SyntaxKind.ForOfStatement:
  1183. case ts9.SyntaxKind.CaseBlock:
  1184. case ts9.SyntaxKind.CatchClause:
  1185. case ts9.SyntaxKind.WithStatement:
  1186. return 2 /* Block */;
  1187. default:
  1188. return 0 /* None */;
  1189. }
  1190. }
  1191. function identifierToKeywordKind(node) {
  1192. return "identifierToKeywordKind" in ts9 ? ts9.identifierToKeywordKind(node) : (
  1193. // eslint-disable-next-line deprecation/deprecation
  1194. node.originalKeywordKind
  1195. );
  1196. }
  1197. function canHaveDecorators(node) {
  1198. return "canHaveDecorators" in ts9 ? ts9.canHaveDecorators(node) : "decorators" in node;
  1199. }
  1200. function getDecorators(node) {
  1201. return "getDecorators" in ts9 ? ts9.getDecorators(node) : node.decorators;
  1202. }
  1203. // src/usage/declarations.ts
  1204. var DeclarationDomain = /* @__PURE__ */ ((DeclarationDomain2) => {
  1205. DeclarationDomain2[DeclarationDomain2["Import"] = 8] = "Import";
  1206. DeclarationDomain2[DeclarationDomain2["Namespace"] = 1] = "Namespace";
  1207. DeclarationDomain2[DeclarationDomain2["Type"] = 2] = "Type";
  1208. DeclarationDomain2[DeclarationDomain2["Value"] = 4] = "Value";
  1209. DeclarationDomain2[DeclarationDomain2["Any"] = 7] = "Any";
  1210. return DeclarationDomain2;
  1211. })(DeclarationDomain || {});
  1212. function getDeclarationDomain(node) {
  1213. switch (node.parent.kind) {
  1214. case ts9.SyntaxKind.TypeParameter:
  1215. case ts9.SyntaxKind.InterfaceDeclaration:
  1216. case ts9.SyntaxKind.TypeAliasDeclaration:
  1217. return 2 /* Type */;
  1218. case ts9.SyntaxKind.ClassDeclaration:
  1219. case ts9.SyntaxKind.ClassExpression:
  1220. return 2 /* Type */ | 4 /* Value */;
  1221. case ts9.SyntaxKind.EnumDeclaration:
  1222. return 7 /* Any */;
  1223. case ts9.SyntaxKind.NamespaceImport:
  1224. case ts9.SyntaxKind.ImportClause:
  1225. return 7 /* Any */ | 8 /* Import */;
  1226. // TODO handle type-only imports
  1227. case ts9.SyntaxKind.ImportEqualsDeclaration:
  1228. case ts9.SyntaxKind.ImportSpecifier:
  1229. return node.parent.name === node ? 7 /* Any */ | 8 /* Import */ : void 0;
  1230. case ts9.SyntaxKind.ModuleDeclaration:
  1231. return 1 /* Namespace */;
  1232. case ts9.SyntaxKind.Parameter:
  1233. if (node.parent.parent.kind === ts9.SyntaxKind.IndexSignature || identifierToKeywordKind(node) === ts9.SyntaxKind.ThisKeyword) {
  1234. return;
  1235. }
  1236. // falls through
  1237. case ts9.SyntaxKind.BindingElement:
  1238. case ts9.SyntaxKind.VariableDeclaration:
  1239. return node.parent.name === node ? 4 /* Value */ : void 0;
  1240. case ts9.SyntaxKind.FunctionDeclaration:
  1241. case ts9.SyntaxKind.FunctionExpression:
  1242. return 4 /* Value */;
  1243. }
  1244. }
  1245. function unwrapParentheses(node) {
  1246. while (node.kind === ts9.SyntaxKind.ParenthesizedExpression) {
  1247. node = node.expression;
  1248. }
  1249. return node;
  1250. }
  1251. function getPropertyName(propertyName) {
  1252. if (propertyName.kind === ts9.SyntaxKind.ComputedPropertyName) {
  1253. const expression = unwrapParentheses(propertyName.expression);
  1254. if (ts9.isPrefixUnaryExpression(expression)) {
  1255. let negate = false;
  1256. switch (expression.operator) {
  1257. case ts9.SyntaxKind.MinusToken:
  1258. negate = true;
  1259. // falls through
  1260. case ts9.SyntaxKind.PlusToken:
  1261. return ts9.isNumericLiteral(expression.operand) ? `${negate ? "-" : ""}${expression.operand.text}` : ts9.isBigIntLiteral(expression.operand) ? `${negate ? "-" : ""}${expression.operand.text.slice(0, -1)}` : void 0;
  1262. default:
  1263. return;
  1264. }
  1265. }
  1266. if (ts9.isBigIntLiteral(expression)) {
  1267. return expression.text.slice(0, -1);
  1268. }
  1269. if (isNumericOrStringLikeLiteral(expression)) {
  1270. return expression.text;
  1271. }
  1272. return;
  1273. }
  1274. return propertyName.kind === ts9.SyntaxKind.PrivateIdentifier ? void 0 : propertyName.text;
  1275. }
  1276. var UsageDomain = /* @__PURE__ */ ((UsageDomain2) => {
  1277. UsageDomain2[UsageDomain2["Namespace"] = 1] = "Namespace";
  1278. UsageDomain2[UsageDomain2["Type"] = 2] = "Type";
  1279. UsageDomain2[UsageDomain2["TypeQuery"] = 8] = "TypeQuery";
  1280. UsageDomain2[UsageDomain2["Value"] = 4] = "Value";
  1281. UsageDomain2[UsageDomain2["ValueOrNamespace"] = 5] = "ValueOrNamespace";
  1282. UsageDomain2[UsageDomain2["Any"] = 7] = "Any";
  1283. return UsageDomain2;
  1284. })(UsageDomain || {});
  1285. function getUsageDomain(node) {
  1286. const parent = node.parent;
  1287. switch (parent.kind) {
  1288. case ts9.SyntaxKind.TypeReference:
  1289. return identifierToKeywordKind(node) !== ts9.SyntaxKind.ConstKeyword ? 2 /* Type */ : void 0;
  1290. case ts9.SyntaxKind.ExpressionWithTypeArguments:
  1291. return parent.parent.token === ts9.SyntaxKind.ImplementsKeyword || parent.parent.parent.kind === ts9.SyntaxKind.InterfaceDeclaration ? 2 /* Type */ : 4 /* Value */;
  1292. case ts9.SyntaxKind.TypeQuery:
  1293. return 5 /* ValueOrNamespace */ | 8 /* TypeQuery */;
  1294. case ts9.SyntaxKind.QualifiedName:
  1295. if (parent.left === node) {
  1296. if (getEntityNameParent(parent).kind === ts9.SyntaxKind.TypeQuery) {
  1297. return 1 /* Namespace */ | 8 /* TypeQuery */;
  1298. }
  1299. return 1 /* Namespace */;
  1300. }
  1301. break;
  1302. case ts9.SyntaxKind.ExportSpecifier:
  1303. if (parent.propertyName === void 0 || parent.propertyName === node) {
  1304. return 7 /* Any */;
  1305. }
  1306. break;
  1307. case ts9.SyntaxKind.ExportAssignment:
  1308. return 7 /* Any */;
  1309. // Value
  1310. case ts9.SyntaxKind.BindingElement:
  1311. if (parent.initializer === node) {
  1312. return 5 /* ValueOrNamespace */;
  1313. }
  1314. break;
  1315. case ts9.SyntaxKind.Parameter:
  1316. case ts9.SyntaxKind.EnumMember:
  1317. case ts9.SyntaxKind.PropertyDeclaration:
  1318. case ts9.SyntaxKind.VariableDeclaration:
  1319. case ts9.SyntaxKind.PropertyAssignment:
  1320. case ts9.SyntaxKind.PropertyAccessExpression:
  1321. case ts9.SyntaxKind.ImportEqualsDeclaration:
  1322. if (parent.name !== node) {
  1323. return 5 /* ValueOrNamespace */;
  1324. }
  1325. break;
  1326. case ts9.SyntaxKind.JsxAttribute:
  1327. case ts9.SyntaxKind.FunctionDeclaration:
  1328. case ts9.SyntaxKind.FunctionExpression:
  1329. case ts9.SyntaxKind.NamespaceImport:
  1330. case ts9.SyntaxKind.ClassDeclaration:
  1331. case ts9.SyntaxKind.ClassExpression:
  1332. case ts9.SyntaxKind.ModuleDeclaration:
  1333. case ts9.SyntaxKind.MethodDeclaration:
  1334. case ts9.SyntaxKind.EnumDeclaration:
  1335. case ts9.SyntaxKind.GetAccessor:
  1336. case ts9.SyntaxKind.SetAccessor:
  1337. case ts9.SyntaxKind.LabeledStatement:
  1338. case ts9.SyntaxKind.BreakStatement:
  1339. case ts9.SyntaxKind.ContinueStatement:
  1340. case ts9.SyntaxKind.ImportClause:
  1341. case ts9.SyntaxKind.ImportSpecifier:
  1342. case ts9.SyntaxKind.TypePredicate:
  1343. // TODO this actually references a parameter
  1344. case ts9.SyntaxKind.MethodSignature:
  1345. case ts9.SyntaxKind.PropertySignature:
  1346. case ts9.SyntaxKind.NamespaceExportDeclaration:
  1347. case ts9.SyntaxKind.NamespaceExport:
  1348. case ts9.SyntaxKind.InterfaceDeclaration:
  1349. case ts9.SyntaxKind.TypeAliasDeclaration:
  1350. case ts9.SyntaxKind.TypeParameter:
  1351. case ts9.SyntaxKind.NamedTupleMember:
  1352. break;
  1353. default:
  1354. return 5 /* ValueOrNamespace */;
  1355. }
  1356. }
  1357. function getEntityNameParent(name) {
  1358. let parent = name.parent;
  1359. while (parent.kind === ts9.SyntaxKind.QualifiedName) {
  1360. parent = parent.parent;
  1361. }
  1362. return parent;
  1363. }
  1364. // src/usage/scopes.ts
  1365. var AbstractScope = class {
  1366. constructor(global) {
  1367. this.global = global;
  1368. this.#enumScopes = void 0;
  1369. this.namespaceScopes = void 0;
  1370. this.uses = [];
  1371. this.variables = /* @__PURE__ */ new Map();
  1372. }
  1373. #enumScopes;
  1374. addUse(use) {
  1375. this.uses.push(use);
  1376. }
  1377. // eslint-disable-next-line @typescript-eslint/no-empty-function
  1378. addUseToParent(_use) {
  1379. }
  1380. addVariable(identifier, name, selector, exported, domain) {
  1381. const variables = this.getDestinationScope(selector).getVariables();
  1382. const declaration = {
  1383. declaration: name,
  1384. domain,
  1385. exported
  1386. };
  1387. const variable = variables.get(identifier);
  1388. if (variable === void 0) {
  1389. variables.set(identifier, {
  1390. declarations: [declaration],
  1391. domain,
  1392. uses: []
  1393. });
  1394. } else {
  1395. variable.domain |= domain;
  1396. variable.declarations.push(declaration);
  1397. }
  1398. }
  1399. applyUse(use, variables = this.variables) {
  1400. const variable = variables.get(use.location.text);
  1401. if (variable === void 0 || (variable.domain & use.domain) === 0) {
  1402. return false;
  1403. }
  1404. variable.uses.push(use);
  1405. return true;
  1406. }
  1407. applyUses() {
  1408. for (const use of this.uses) {
  1409. if (!this.applyUse(use)) {
  1410. this.addUseToParent(use);
  1411. }
  1412. }
  1413. this.uses = [];
  1414. }
  1415. createOrReuseEnumScope(name, _exported) {
  1416. let scope;
  1417. if (this.#enumScopes === void 0) {
  1418. this.#enumScopes = /* @__PURE__ */ new Map();
  1419. } else {
  1420. scope = this.#enumScopes.get(name);
  1421. }
  1422. if (scope === void 0) {
  1423. scope = new EnumScope(this);
  1424. this.#enumScopes.set(name, scope);
  1425. }
  1426. return scope;
  1427. }
  1428. // only relevant for the root scope
  1429. createOrReuseNamespaceScope(name, _exported, ambient, hasExportStatement) {
  1430. let scope;
  1431. if (this.namespaceScopes === void 0) {
  1432. this.namespaceScopes = /* @__PURE__ */ new Map();
  1433. } else {
  1434. scope = this.namespaceScopes.get(name);
  1435. }
  1436. if (scope === void 0) {
  1437. scope = new NamespaceScope(ambient, hasExportStatement, this);
  1438. this.namespaceScopes.set(name, scope);
  1439. } else {
  1440. scope.refresh(ambient, hasExportStatement);
  1441. }
  1442. return scope;
  1443. }
  1444. end(cb) {
  1445. if (this.namespaceScopes !== void 0) {
  1446. this.namespaceScopes.forEach((value) => value.finish(cb));
  1447. }
  1448. this.namespaceScopes = this.#enumScopes = void 0;
  1449. this.applyUses();
  1450. this.variables.forEach((variable) => {
  1451. for (const declaration of variable.declarations) {
  1452. const result = {
  1453. declarations: [],
  1454. domain: declaration.domain,
  1455. exported: declaration.exported,
  1456. inGlobalScope: this.global,
  1457. uses: []
  1458. };
  1459. for (const other of variable.declarations) {
  1460. if (other.domain & declaration.domain) {
  1461. result.declarations.push(other.declaration);
  1462. }
  1463. }
  1464. for (const use of variable.uses) {
  1465. if (use.domain & declaration.domain) {
  1466. result.uses.push(use);
  1467. }
  1468. }
  1469. cb(result, declaration.declaration, this);
  1470. }
  1471. });
  1472. }
  1473. getFunctionScope() {
  1474. return this;
  1475. }
  1476. getVariables() {
  1477. return this.variables;
  1478. }
  1479. // eslint-disable-next-line @typescript-eslint/no-empty-function
  1480. markExported(_name) {
  1481. }
  1482. };
  1483. var NonRootScope = class extends AbstractScope {
  1484. constructor(parent, boundary) {
  1485. super(false);
  1486. this.parent = parent;
  1487. this.boundary = boundary;
  1488. }
  1489. addUseToParent(use) {
  1490. return this.parent.addUse(use, this);
  1491. }
  1492. getDestinationScope(selector) {
  1493. return this.boundary & selector ? this : this.parent.getDestinationScope(selector);
  1494. }
  1495. };
  1496. var EnumScope = class extends NonRootScope {
  1497. constructor(parent) {
  1498. super(parent, 1 /* Function */);
  1499. }
  1500. end() {
  1501. this.applyUses();
  1502. }
  1503. };
  1504. var RootScope = class extends AbstractScope {
  1505. #exportAll;
  1506. #exports = void 0;
  1507. #innerScope = new NonRootScope(this, 1 /* Function */);
  1508. constructor(exportAll, global) {
  1509. super(global);
  1510. this.#exportAll = exportAll;
  1511. }
  1512. addUse(use, origin) {
  1513. if (origin === this.#innerScope) {
  1514. return super.addUse(use);
  1515. }
  1516. return this.#innerScope.addUse(use);
  1517. }
  1518. addVariable(identifier, name, selector, exported, domain) {
  1519. if (domain & 8 /* Import */) {
  1520. return super.addVariable(identifier, name, selector, exported, domain);
  1521. }
  1522. return this.#innerScope.addVariable(
  1523. identifier,
  1524. name,
  1525. selector,
  1526. exported,
  1527. domain
  1528. );
  1529. }
  1530. end(cb) {
  1531. this.#innerScope.end((value, key) => {
  1532. value.exported ||= this.#exportAll || this.#exports !== void 0 && this.#exports.includes(key.text);
  1533. value.inGlobalScope = this.global;
  1534. return cb(value, key, this);
  1535. });
  1536. return super.end((value, key, scope) => {
  1537. value.exported ||= scope === this && this.#exports !== void 0 && this.#exports.includes(key.text);
  1538. return cb(value, key, scope);
  1539. });
  1540. }
  1541. getDestinationScope() {
  1542. return this;
  1543. }
  1544. markExported(id) {
  1545. if (this.#exports === void 0) {
  1546. this.#exports = [id.text];
  1547. } else {
  1548. this.#exports.push(id.text);
  1549. }
  1550. }
  1551. };
  1552. var NamespaceScope = class extends NonRootScope {
  1553. #ambient;
  1554. #exports = void 0;
  1555. #hasExport;
  1556. #innerScope = new NonRootScope(this, 1 /* Function */);
  1557. constructor(ambient, hasExport, parent) {
  1558. super(parent, 1 /* Function */);
  1559. this.#ambient = ambient;
  1560. this.#hasExport = hasExport;
  1561. }
  1562. addUse(use, source) {
  1563. if (source !== this.#innerScope) {
  1564. return this.#innerScope.addUse(use);
  1565. }
  1566. this.uses.push(use);
  1567. }
  1568. createOrReuseEnumScope(name, exported) {
  1569. if (!exported && (!this.#ambient || this.#hasExport)) {
  1570. return this.#innerScope.createOrReuseEnumScope(name, exported);
  1571. }
  1572. return super.createOrReuseEnumScope(name, exported);
  1573. }
  1574. createOrReuseNamespaceScope(name, exported, ambient, hasExportStatement) {
  1575. if (!exported && (!this.#ambient || this.#hasExport)) {
  1576. return this.#innerScope.createOrReuseNamespaceScope(
  1577. name,
  1578. exported,
  1579. ambient || this.#ambient,
  1580. hasExportStatement
  1581. );
  1582. }
  1583. return super.createOrReuseNamespaceScope(
  1584. name,
  1585. exported,
  1586. ambient || this.#ambient,
  1587. hasExportStatement
  1588. );
  1589. }
  1590. end(cb) {
  1591. this.#innerScope.end((variable, key, scope) => {
  1592. if (scope !== this.#innerScope || !variable.exported && (!this.#ambient || this.#exports !== void 0 && !this.#exports.has(key.text))) {
  1593. return cb(variable, key, scope);
  1594. }
  1595. const namespaceVar = this.variables.get(key.text);
  1596. if (namespaceVar === void 0) {
  1597. this.variables.set(key.text, {
  1598. declarations: variable.declarations.map(mapDeclaration),
  1599. domain: variable.domain,
  1600. uses: [...variable.uses]
  1601. });
  1602. } else {
  1603. outer: for (const declaration of variable.declarations) {
  1604. for (const existing of namespaceVar.declarations) {
  1605. if (existing.declaration === declaration) {
  1606. continue outer;
  1607. }
  1608. namespaceVar.declarations.push(mapDeclaration(declaration));
  1609. }
  1610. }
  1611. namespaceVar.domain |= variable.domain;
  1612. for (const use of variable.uses) {
  1613. if (namespaceVar.uses.includes(use)) {
  1614. continue;
  1615. }
  1616. namespaceVar.uses.push(use);
  1617. }
  1618. }
  1619. });
  1620. this.applyUses();
  1621. this.#innerScope = new NonRootScope(this, 1 /* Function */);
  1622. }
  1623. finish(cb) {
  1624. return super.end(cb);
  1625. }
  1626. getDestinationScope() {
  1627. return this.#innerScope;
  1628. }
  1629. markExported(name) {
  1630. if (this.#exports === void 0) {
  1631. this.#exports = /* @__PURE__ */ new Set();
  1632. }
  1633. this.#exports.add(name.text);
  1634. }
  1635. refresh(ambient, hasExport) {
  1636. this.#ambient = ambient;
  1637. this.#hasExport = hasExport;
  1638. }
  1639. };
  1640. function mapDeclaration(declaration) {
  1641. return {
  1642. declaration,
  1643. domain: getDeclarationDomain(declaration),
  1644. exported: true
  1645. };
  1646. }
  1647. var FunctionScope = class extends NonRootScope {
  1648. constructor(parent) {
  1649. super(parent, 1 /* Function */);
  1650. }
  1651. beginBody() {
  1652. this.applyUses();
  1653. }
  1654. };
  1655. var AbstractNamedExpressionScope = class extends NonRootScope {
  1656. #domain;
  1657. #name;
  1658. constructor(name, domain, parent) {
  1659. super(parent, 1 /* Function */);
  1660. this.#name = name;
  1661. this.#domain = domain;
  1662. }
  1663. addUse(use, source) {
  1664. if (source !== this.innerScope) {
  1665. return this.innerScope.addUse(use);
  1666. }
  1667. if (use.domain & this.#domain && use.location.text === this.#name.text) {
  1668. this.uses.push(use);
  1669. } else {
  1670. return this.parent.addUse(use, this);
  1671. }
  1672. }
  1673. end(cb) {
  1674. this.innerScope.end(cb);
  1675. return cb(
  1676. {
  1677. declarations: [this.#name],
  1678. domain: this.#domain,
  1679. exported: false,
  1680. inGlobalScope: false,
  1681. uses: this.uses
  1682. },
  1683. this.#name,
  1684. this
  1685. );
  1686. }
  1687. getDestinationScope() {
  1688. return this.innerScope;
  1689. }
  1690. getFunctionScope() {
  1691. return this.innerScope;
  1692. }
  1693. };
  1694. var FunctionExpressionScope = class extends AbstractNamedExpressionScope {
  1695. constructor(name, parent) {
  1696. super(name, 4 /* Value */, parent);
  1697. this.innerScope = new FunctionScope(this);
  1698. }
  1699. beginBody() {
  1700. return this.innerScope.beginBody();
  1701. }
  1702. };
  1703. var BlockScope = class extends NonRootScope {
  1704. #functionScope;
  1705. constructor(functionScope, parent) {
  1706. super(parent, 2 /* Block */);
  1707. this.#functionScope = functionScope;
  1708. }
  1709. getFunctionScope() {
  1710. return this.#functionScope;
  1711. }
  1712. };
  1713. var ClassExpressionScope = class extends AbstractNamedExpressionScope {
  1714. constructor(name, parent) {
  1715. super(name, 4 /* Value */ | 2 /* Type */, parent);
  1716. this.innerScope = new NonRootScope(this, 1 /* Function */);
  1717. }
  1718. };
  1719. var ConditionalTypeScope = class extends NonRootScope {
  1720. #state = 0 /* Initial */;
  1721. constructor(parent) {
  1722. super(parent, 8 /* ConditionalType */);
  1723. }
  1724. addUse(use) {
  1725. if (this.#state === 2 /* TrueType */) {
  1726. return void this.uses.push(use);
  1727. }
  1728. return this.parent.addUse(use, this);
  1729. }
  1730. updateState(newState) {
  1731. this.#state = newState;
  1732. }
  1733. };
  1734. // src/usage/UsageWalker.ts
  1735. var UsageWalker = class {
  1736. #result = /* @__PURE__ */ new Map();
  1737. #scope;
  1738. #handleBindingName(name, blockScoped, exported) {
  1739. if (name.kind === ts9.SyntaxKind.Identifier) {
  1740. return this.#scope.addVariable(
  1741. name.text,
  1742. name,
  1743. blockScoped ? 3 /* Block */ : 1 /* Function */,
  1744. exported,
  1745. 4 /* Value */
  1746. );
  1747. }
  1748. forEachDestructuringIdentifier(name, (declaration) => {
  1749. this.#scope.addVariable(
  1750. declaration.name.text,
  1751. declaration.name,
  1752. blockScoped ? 3 /* Block */ : 1 /* Function */,
  1753. exported,
  1754. 4 /* Value */
  1755. );
  1756. });
  1757. }
  1758. #handleConditionalType(node, cb, varCb) {
  1759. const savedScope = this.#scope;
  1760. const scope = this.#scope = new ConditionalTypeScope(savedScope);
  1761. cb(node.checkType);
  1762. scope.updateState(1 /* Extends */);
  1763. cb(node.extendsType);
  1764. scope.updateState(2 /* TrueType */);
  1765. cb(node.trueType);
  1766. scope.updateState(3 /* FalseType */);
  1767. cb(node.falseType);
  1768. scope.end(varCb);
  1769. this.#scope = savedScope;
  1770. }
  1771. #handleDeclaration(node, blockScoped, domain) {
  1772. if (node.name !== void 0) {
  1773. this.#scope.addVariable(
  1774. node.name.text,
  1775. node.name,
  1776. blockScoped ? 3 /* Block */ : 1 /* Function */,
  1777. includesModifier(
  1778. node.modifiers,
  1779. ts9.SyntaxKind.ExportKeyword
  1780. ),
  1781. domain
  1782. );
  1783. }
  1784. }
  1785. #handleFunctionLikeDeclaration(node, cb, varCb) {
  1786. var _a;
  1787. if (canHaveDecorators(node)) {
  1788. (_a = getDecorators(node)) == null ? void 0 : _a.forEach(cb);
  1789. }
  1790. const savedScope = this.#scope;
  1791. if (node.kind === ts9.SyntaxKind.FunctionDeclaration) {
  1792. this.#handleDeclaration(node, false, 4 /* Value */);
  1793. }
  1794. const scope = this.#scope = node.kind === ts9.SyntaxKind.FunctionExpression && node.name !== void 0 ? new FunctionExpressionScope(node.name, savedScope) : new FunctionScope(savedScope);
  1795. if (node.name !== void 0) {
  1796. cb(node.name);
  1797. }
  1798. if (node.typeParameters !== void 0) {
  1799. node.typeParameters.forEach(cb);
  1800. }
  1801. node.parameters.forEach(cb);
  1802. if (node.type !== void 0) {
  1803. cb(node.type);
  1804. }
  1805. if (node.body !== void 0) {
  1806. scope.beginBody();
  1807. cb(node.body);
  1808. }
  1809. scope.end(varCb);
  1810. this.#scope = savedScope;
  1811. }
  1812. #handleModule(node, next) {
  1813. if (node.flags & ts9.NodeFlags.GlobalAugmentation) {
  1814. return next(
  1815. node,
  1816. this.#scope.createOrReuseNamespaceScope("-global", false, true, false)
  1817. );
  1818. }
  1819. if (node.name.kind === ts9.SyntaxKind.Identifier) {
  1820. const exported = isNamespaceExported(node);
  1821. this.#scope.addVariable(
  1822. node.name.text,
  1823. node.name,
  1824. 1 /* Function */,
  1825. exported,
  1826. 1 /* Namespace */ | 4 /* Value */
  1827. );
  1828. const ambient = includesModifier(
  1829. node.modifiers,
  1830. ts9.SyntaxKind.DeclareKeyword
  1831. );
  1832. return next(
  1833. node,
  1834. this.#scope.createOrReuseNamespaceScope(
  1835. node.name.text,
  1836. exported,
  1837. ambient,
  1838. ambient && namespaceHasExportStatement(node)
  1839. )
  1840. );
  1841. }
  1842. return next(
  1843. node,
  1844. this.#scope.createOrReuseNamespaceScope(
  1845. `"${node.name.text}"`,
  1846. false,
  1847. true,
  1848. namespaceHasExportStatement(node)
  1849. )
  1850. );
  1851. }
  1852. #handleVariableDeclaration(declarationList) {
  1853. const blockScoped = isBlockScopedVariableDeclarationList(declarationList);
  1854. const exported = declarationList.parent.kind === ts9.SyntaxKind.VariableStatement && includesModifier(
  1855. declarationList.parent.modifiers,
  1856. ts9.SyntaxKind.ExportKeyword
  1857. );
  1858. for (const declaration of declarationList.declarations) {
  1859. this.#handleBindingName(declaration.name, blockScoped, exported);
  1860. }
  1861. }
  1862. getUsage(sourceFile) {
  1863. const variableCallback = (variable, key) => {
  1864. this.#result.set(key, variable);
  1865. };
  1866. const isModule = ts9.isExternalModule(sourceFile);
  1867. this.#scope = new RootScope(
  1868. sourceFile.isDeclarationFile && isModule && !containsExportStatement(sourceFile),
  1869. !isModule
  1870. );
  1871. const cb = (node) => {
  1872. if (isBlockScopeBoundary(node)) {
  1873. return continueWithScope(
  1874. node,
  1875. new BlockScope(this.#scope.getFunctionScope(), this.#scope),
  1876. handleBlockScope
  1877. );
  1878. }
  1879. switch (node.kind) {
  1880. case ts9.SyntaxKind.ClassExpression:
  1881. return continueWithScope(
  1882. node,
  1883. node.name !== void 0 ? new ClassExpressionScope(
  1884. node.name,
  1885. this.#scope
  1886. ) : new NonRootScope(this.#scope, 1 /* Function */)
  1887. );
  1888. case ts9.SyntaxKind.ClassDeclaration:
  1889. this.#handleDeclaration(
  1890. node,
  1891. true,
  1892. 4 /* Value */ | 2 /* Type */
  1893. );
  1894. return continueWithScope(
  1895. node,
  1896. new NonRootScope(this.#scope, 1 /* Function */)
  1897. );
  1898. case ts9.SyntaxKind.InterfaceDeclaration:
  1899. case ts9.SyntaxKind.TypeAliasDeclaration:
  1900. this.#handleDeclaration(
  1901. node,
  1902. true,
  1903. 2 /* Type */
  1904. );
  1905. return continueWithScope(
  1906. node,
  1907. new NonRootScope(this.#scope, 4 /* Type */)
  1908. );
  1909. case ts9.SyntaxKind.EnumDeclaration:
  1910. this.#handleDeclaration(
  1911. node,
  1912. true,
  1913. 7 /* Any */
  1914. );
  1915. return continueWithScope(
  1916. node,
  1917. this.#scope.createOrReuseEnumScope(
  1918. node.name.text,
  1919. includesModifier(
  1920. node.modifiers,
  1921. ts9.SyntaxKind.ExportKeyword
  1922. )
  1923. )
  1924. );
  1925. case ts9.SyntaxKind.ModuleDeclaration:
  1926. return this.#handleModule(
  1927. node,
  1928. continueWithScope
  1929. );
  1930. case ts9.SyntaxKind.MappedType:
  1931. return continueWithScope(
  1932. node,
  1933. new NonRootScope(this.#scope, 4 /* Type */)
  1934. );
  1935. case ts9.SyntaxKind.FunctionExpression:
  1936. case ts9.SyntaxKind.ArrowFunction:
  1937. case ts9.SyntaxKind.Constructor:
  1938. case ts9.SyntaxKind.MethodDeclaration:
  1939. case ts9.SyntaxKind.FunctionDeclaration:
  1940. case ts9.SyntaxKind.GetAccessor:
  1941. case ts9.SyntaxKind.SetAccessor:
  1942. case ts9.SyntaxKind.MethodSignature:
  1943. case ts9.SyntaxKind.CallSignature:
  1944. case ts9.SyntaxKind.ConstructSignature:
  1945. case ts9.SyntaxKind.ConstructorType:
  1946. case ts9.SyntaxKind.FunctionType:
  1947. return this.#handleFunctionLikeDeclaration(
  1948. node,
  1949. cb,
  1950. variableCallback
  1951. );
  1952. case ts9.SyntaxKind.ConditionalType:
  1953. return this.#handleConditionalType(
  1954. node,
  1955. cb,
  1956. variableCallback
  1957. );
  1958. // End of Scope specific handling
  1959. case ts9.SyntaxKind.VariableDeclarationList:
  1960. this.#handleVariableDeclaration(node);
  1961. break;
  1962. case ts9.SyntaxKind.Parameter:
  1963. if (node.parent.kind !== ts9.SyntaxKind.IndexSignature && (node.name.kind !== ts9.SyntaxKind.Identifier || identifierToKeywordKind(
  1964. node.name
  1965. ) !== ts9.SyntaxKind.ThisKeyword)) {
  1966. this.#handleBindingName(
  1967. node.name,
  1968. false,
  1969. false
  1970. );
  1971. }
  1972. break;
  1973. case ts9.SyntaxKind.EnumMember:
  1974. this.#scope.addVariable(
  1975. getPropertyName(node.name),
  1976. node.name,
  1977. 1 /* Function */,
  1978. true,
  1979. 4 /* Value */
  1980. );
  1981. break;
  1982. case ts9.SyntaxKind.ImportClause:
  1983. case ts9.SyntaxKind.ImportSpecifier:
  1984. case ts9.SyntaxKind.NamespaceImport:
  1985. case ts9.SyntaxKind.ImportEqualsDeclaration:
  1986. this.#handleDeclaration(
  1987. node,
  1988. false,
  1989. 7 /* Any */ | 8 /* Import */
  1990. );
  1991. break;
  1992. case ts9.SyntaxKind.TypeParameter:
  1993. this.#scope.addVariable(
  1994. node.name.text,
  1995. node.name,
  1996. node.parent.kind === ts9.SyntaxKind.InferType ? 8 /* InferType */ : 7 /* Type */,
  1997. false,
  1998. 2 /* Type */
  1999. );
  2000. break;
  2001. case ts9.SyntaxKind.ExportSpecifier:
  2002. if (node.propertyName !== void 0) {
  2003. return this.#scope.markExported(
  2004. node.propertyName,
  2005. node.name
  2006. );
  2007. }
  2008. return this.#scope.markExported(node.name);
  2009. case ts9.SyntaxKind.ExportAssignment:
  2010. if (node.expression.kind === ts9.SyntaxKind.Identifier) {
  2011. return this.#scope.markExported(
  2012. node.expression
  2013. );
  2014. }
  2015. break;
  2016. case ts9.SyntaxKind.Identifier: {
  2017. const domain = getUsageDomain(node);
  2018. if (domain !== void 0) {
  2019. this.#scope.addUse({ domain, location: node });
  2020. }
  2021. return;
  2022. }
  2023. }
  2024. return ts9.forEachChild(node, cb);
  2025. };
  2026. const continueWithScope = (node, scope, next = forEachChild) => {
  2027. const savedScope = this.#scope;
  2028. this.#scope = scope;
  2029. next(node);
  2030. this.#scope.end(variableCallback);
  2031. this.#scope = savedScope;
  2032. };
  2033. const handleBlockScope = (node) => {
  2034. if (node.kind === ts9.SyntaxKind.CatchClause && node.variableDeclaration !== void 0) {
  2035. this.#handleBindingName(
  2036. node.variableDeclaration.name,
  2037. true,
  2038. false
  2039. );
  2040. }
  2041. return ts9.forEachChild(node, cb);
  2042. };
  2043. ts9.forEachChild(sourceFile, cb);
  2044. this.#scope.end(variableCallback);
  2045. return this.#result;
  2046. function forEachChild(node) {
  2047. return ts9.forEachChild(node, cb);
  2048. }
  2049. }
  2050. };
  2051. function isNamespaceExported(node) {
  2052. return node.parent.kind === ts9.SyntaxKind.ModuleDeclaration || includesModifier(node.modifiers, ts9.SyntaxKind.ExportKeyword);
  2053. }
  2054. function namespaceHasExportStatement(ns) {
  2055. if (ns.body === void 0 || ns.body.kind !== ts9.SyntaxKind.ModuleBlock) {
  2056. return false;
  2057. }
  2058. return containsExportStatement(ns.body);
  2059. }
  2060. function containsExportStatement(block) {
  2061. for (const statement of block.statements) {
  2062. if (statement.kind === ts9.SyntaxKind.ExportDeclaration || statement.kind === ts9.SyntaxKind.ExportAssignment) {
  2063. return true;
  2064. }
  2065. }
  2066. return false;
  2067. }
  2068. function isBlockScopedVariableDeclarationList(declarationList) {
  2069. return (declarationList.flags & ts9.NodeFlags.BlockScoped) !== 0;
  2070. }
  2071. function forEachDestructuringIdentifier(pattern, fn) {
  2072. for (const element of pattern.elements) {
  2073. if (element.kind !== ts9.SyntaxKind.BindingElement) {
  2074. continue;
  2075. }
  2076. let result;
  2077. if (element.name.kind === ts9.SyntaxKind.Identifier) {
  2078. result = fn(element);
  2079. } else {
  2080. result = forEachDestructuringIdentifier(element.name, fn);
  2081. }
  2082. if (result) {
  2083. return result;
  2084. }
  2085. }
  2086. }
  2087. // src/usage/collectVariableUsage.ts
  2088. function collectVariableUsage(sourceFile) {
  2089. return new UsageWalker().getUsage(sourceFile);
  2090. }
  2091. export { AccessKind, DeclarationDomain, UsageDomain, collectVariableUsage, forEachComment, forEachToken, getAccessKind, getCallSignaturesOfType, getPropertyOfType, getWellKnownSymbolPropertyOfType, hasDecorators, hasExpressionInitializer, hasInitializer, hasJSDoc, hasModifiers, hasType, hasTypeArguments, includesModifier, intersectionTypeParts, isAbstractKeyword, isAccessExpression, isAccessibilityModifier, isAccessorDeclaration, isAccessorKeyword, isAnyKeyword, isArrayBindingElement, isArrayBindingOrAssignmentPattern, isAssertKeyword, isAssertsKeyword, isAssignmentKind, isAssignmentPattern, isAsyncKeyword, isAwaitKeyword, isBigIntKeyword, isBigIntLiteralType, isBindingOrAssignmentElementRestIndicator, isBindingOrAssignmentElementTarget, isBindingOrAssignmentPattern, isBindingPattern, isBlockLike, isBooleanKeyword, isBooleanLiteral, isBooleanLiteralType, isClassLikeDeclaration, isClassMemberModifier, isColonToken, isCompilerOptionEnabled, isConditionalType, isConstAssertionExpression, isConstKeyword, isDeclarationName, isDeclarationWithTypeParameterChildren, isDeclarationWithTypeParameters, isDeclareKeyword, isDefaultKeyword, isDestructuringPattern, isDotToken, isEndOfFileToken, isEntityNameExpression, isEntityNameOrEntityNameExpression, isEnumType, isEqualsGreaterThanToken, isEqualsToken, isEvolvingArrayType, isExclamationToken, isExportKeyword, isFalseKeyword, isFalseLiteral, isFalseLiteralType, isFalsyType, isForInOrOfStatement, isFreshableIntrinsicType, isFreshableType, isFunctionLikeDeclaration, isFunctionScopeBoundary, isImportExpression, isImportKeyword, isInKeyword, isIndexType, isIndexedAccessType, isInputFiles, isInstantiableType, isIntersectionType, isIntrinsicAnyType, isIntrinsicBigIntType, isIntrinsicBooleanType, isIntrinsicESSymbolType, isIntrinsicErrorType, isIntrinsicNeverType, isIntrinsicNonPrimitiveType, isIntrinsicNullType, isIntrinsicNumberType, isIntrinsicStringType, isIntrinsicType, isIntrinsicUndefinedType, isIntrinsicUnknownType, isIntrinsicVoidType, isIterationStatement, isJSDocComment, isJSDocNamespaceBody, isJSDocNamespaceDeclaration, isJSDocText, isJSDocTypeReferencingNode, isJsonMinusNumericLiteral, isJsonObjectExpression, isJsxAttributeLike, isJsxAttributeValue, isJsxChild, isJsxTagNameExpression, isJsxTagNamePropertyAccess, isLiteralToken, isLiteralType, isModifierFlagSet, isModuleBody, isModuleName, isModuleReference, isNamedDeclarationWithName, isNamedImportBindings, isNamedImportsOrExports, isNamespaceBody, isNamespaceDeclaration, isNeverKeyword, isNodeFlagSet, isNullKeyword, isNullLiteral, isNumberKeyword, isNumberLiteralType, isNumericOrStringLikeLiteral, isNumericPropertyName, isObjectBindingOrAssignmentElement, isObjectBindingOrAssignmentPattern, isObjectFlagSet, isObjectKeyword, isObjectType, isObjectTypeDeclaration, isOutKeyword, isOverrideKeyword, isParameterPropertyModifier, isPrivateKeyword, isPropertyAccessEntityNameExpression, isPropertyNameLiteral, isPropertyReadonlyInType, isProtectedKeyword, isPseudoLiteralToken, isPublicKeyword, isQuestionDotToken, isQuestionToken, isReadonlyKeyword, isSignatureDeclaration, isStaticKeyword, isStrictCompilerOptionEnabled, isStringKeyword, isStringLiteralType, isStringMappingType, isSubstitutionType, isSuperElementAccessExpression, isSuperExpression, isSuperKeyword, isSuperProperty, isSuperPropertyAccessExpression, isSymbolFlagSet, isSymbolKeyword, isSyntaxList, isTemplateLiteralType, isThenableType, isThisExpression, isThisKeyword, isTransientSymbolLinksFlagSet, isTrueKeyword, isTrueLiteral, isTrueLiteralType, isTupleType, isTupleTypeReference, isTypeFlagSet, isTypeOnlyCompatibleAliasDeclaration, isTypeParameter, isTypeReference, isTypeReferenceType, isTypeVariable, isUndefinedKeyword, isUnionOrIntersectionType, isUnionOrIntersectionTypeNode, isUnionType, isUniqueESSymbolType, isUnknownKeyword, isUnknownLiteralType, isUnparsedPrologue, isUnparsedSourceText, isUnparsedSyntheticReference, isValidPropertyAccess, isVariableLikeDeclaration, isVoidKeyword, symbolHasReadonlyDeclaration, typeIsLiteral, typeParts, unionTypeParts };