index.js 79 KB

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