@iaforged/context-code 2.1.5 → 2.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (409) hide show
  1. package/dist/src/Task.js +1 -1
  2. package/dist/src/commands/add-dir/add-dir.js +1 -1
  3. package/dist/src/commands/add-dir/validation.js +1 -1
  4. package/dist/src/constants/oauth.js +1 -1
  5. package/dist/src/context/mailbox.js +1 -1
  6. package/dist/src/context/voice.js +1 -1
  7. package/dist/src/hooks/useTerminalSize.js +1 -1
  8. package/dist/src/ink/Ansi.js +1 -1
  9. package/dist/src/ink/clearTerminal.js +1 -1
  10. package/dist/src/ink/colorize.js +1 -1
  11. package/dist/src/ink/components/App.js +1 -1
  12. package/dist/src/ink/components/Button.js +1 -1
  13. package/dist/src/ink/components/ClockContext.js +1 -1
  14. package/dist/src/ink/components/CursorDeclarationContext.js +1 -1
  15. package/dist/src/ink/components/Link.js +1 -1
  16. package/dist/src/ink/components/StdinContext.js +1 -1
  17. package/dist/src/ink/components/TerminalFocusContext.js +1 -1
  18. package/dist/src/ink/dom.js +1 -1
  19. package/dist/src/ink/events/keyboard-event.js +1 -1
  20. package/dist/src/ink/hit-test.js +1 -1
  21. package/dist/src/ink/hooks/use-animation-frame.js +1 -1
  22. package/dist/src/ink/hooks/use-app.js +1 -1
  23. package/dist/src/ink/hooks/use-input.js +1 -1
  24. package/dist/src/ink/hooks/use-interval.js +1 -1
  25. package/dist/src/ink/hooks/use-selection.js +1 -1
  26. package/dist/src/ink/hooks/use-tab-status.js +1 -1
  27. package/dist/src/ink/hooks/use-terminal-focus.js +1 -1
  28. package/dist/src/ink/hooks/use-terminal-title.js +1 -1
  29. package/dist/src/ink/hooks/use-terminal-viewport.js +1 -1
  30. package/dist/src/ink/ink.js +1 -1
  31. package/dist/src/ink/layout/yoga.js +1 -1
  32. package/dist/src/ink/line-width-cache.js +1 -1
  33. package/dist/src/ink/log-update.js +1 -1
  34. package/dist/src/ink/measure-text.js +1 -1
  35. package/dist/src/ink/output.js +1 -1
  36. package/dist/src/ink/parse-keypress.js +1 -1
  37. package/dist/src/ink/reconciler.js +1 -1
  38. package/dist/src/ink/render-border.js +1 -1
  39. package/dist/src/ink/render-node-to-output.js +1 -1
  40. package/dist/src/ink/render-to-screen.js +1 -1
  41. package/dist/src/ink/renderer.js +1 -1
  42. package/dist/src/ink/root.js +1 -1
  43. package/dist/src/ink/screen.js +1 -1
  44. package/dist/src/ink/searchHighlight.js +1 -1
  45. package/dist/src/ink/selection.js +1 -1
  46. package/dist/src/ink/squash-text-nodes.js +1 -1
  47. package/dist/src/ink/stringWidth.js +1 -1
  48. package/dist/src/ink/tabstops.js +1 -1
  49. package/dist/src/ink/terminal.js +1 -1
  50. package/dist/src/ink/termio/osc.js +1 -1
  51. package/dist/src/ink/termio/parser.js +1 -1
  52. package/dist/src/ink/termio/tokenize.js +1 -1
  53. package/dist/src/ink/useTerminalNotification.js +1 -1
  54. package/dist/src/ink/warn.js +1 -1
  55. package/dist/src/ink/widest-line.js +1 -1
  56. package/dist/src/ink/wrap-text.js +1 -1
  57. package/dist/src/ink/wrapAnsi.js +1 -1
  58. package/dist/src/native-ts/yoga-layout/index.js +1 -1
  59. package/dist/src/schemas/hooks.js +1 -1
  60. package/dist/src/services/SessionMemory/sessionMemoryUtils.js +1 -1
  61. package/dist/src/services/api/client.js +1 -1
  62. package/dist/src/services/api/dumpPrompts.js +1 -1
  63. package/dist/src/services/api/errorUtils.js +1 -1
  64. package/dist/src/services/api/promptCacheBreakDetection.js +1 -1
  65. package/dist/src/services/api/withRetry.js +1 -1
  66. package/dist/src/services/autoDream/consolidationLock.js +1 -1
  67. package/dist/src/services/mcp/elicitationHandler.js +1 -1
  68. package/dist/src/services/mcp/mcpStringUtils.js +1 -1
  69. package/dist/src/services/mcp/oauthPort.js +1 -1
  70. package/dist/src/services/mcp/vscodeSdkMcp.js +1 -1
  71. package/dist/src/services/oauth/client.js +1 -1
  72. package/dist/src/services/oauth/getOauthProfile.js +1 -1
  73. package/dist/src/services/objetivo/types.js +1 -1
  74. package/dist/src/services/rateLimitMocking.js +1 -1
  75. package/dist/src/services/remoteManagedSettings/syncCacheState.js +1 -1
  76. package/dist/src/skills/bundledSkills.js +1 -1
  77. package/dist/src/tasks/DreamTask/DreamTask.js +1 -1
  78. package/dist/src/tools/AgentTool/agentMemory.js +1 -1
  79. package/dist/src/tools/AgentTool/forkSubagent.js +1 -1
  80. package/dist/src/tools/BashTool/BashToolResultMessage.js +1 -1
  81. package/dist/src/tools/BashTool/UI.js +1 -1
  82. package/dist/src/tools/BashTool/sedEditParser.js +1 -1
  83. package/dist/src/tools/BashTool/utils.js +1 -1
  84. package/dist/src/tools/FileReadTool/imageProcessor.js +1 -1
  85. package/dist/src/tools/FileReadTool/prompt.js +1 -1
  86. package/dist/src/tools/ListMcpResourcesTool/ListMcpResourcesTool.js +1 -1
  87. package/dist/src/tools/ListMcpResourcesTool/UI.js +1 -1
  88. package/dist/src/tools/MCPTool/MCPTool.js +1 -1
  89. package/dist/src/tools/MCPTool/UI.js +1 -1
  90. package/dist/src/tools/McpAuthTool/McpAuthTool.js +1 -1
  91. package/dist/src/tools/NotebookEditTool/prompt.js +1 -1
  92. package/dist/src/tools/PowerShellTool/PowerShellTool.js +1 -1
  93. package/dist/src/tools/PowerShellTool/UI.js +1 -1
  94. package/dist/src/tools/PowerShellTool/gitSafety.js +1 -1
  95. package/dist/src/tools/PowerShellTool/modeValidation.js +1 -1
  96. package/dist/src/tools/PowerShellTool/pathValidation.js +1 -1
  97. package/dist/src/tools/PowerShellTool/powershellPermissions.js +1 -1
  98. package/dist/src/tools/PowerShellTool/powershellSecurity.js +1 -1
  99. package/dist/src/tools/PowerShellTool/prompt.js +1 -1
  100. package/dist/src/tools/PowerShellTool/readOnlyValidation.js +1 -1
  101. package/dist/src/tools/REPLTool/constants.js +1 -1
  102. package/dist/src/tools/REPLTool/primitiveTools.js +1 -1
  103. package/dist/src/tools/ReadMcpResourceTool/ReadMcpResourceTool.js +1 -1
  104. package/dist/src/tools/ReadMcpResourceTool/UI.js +1 -1
  105. package/dist/src/tools/ScheduleCronTool/prompt.js +1 -1
  106. package/dist/src/tools/SkillTool/prompt.js +1 -1
  107. package/dist/src/tools/TodoWriteTool/TodoWriteTool.js +1 -1
  108. package/dist/src/tools/ToolSearchTool/prompt.js +1 -1
  109. package/dist/src/tools/WebSearchTool/prompt.js +1 -1
  110. package/dist/src/tools/shared/gitOperationTracking.js +1 -1
  111. package/dist/src/types/permissions.js +1 -1
  112. package/dist/src/utils/Cursor.js +1 -1
  113. package/dist/src/utils/QueryGuard.js +1 -1
  114. package/dist/src/utils/Shell.js +1 -1
  115. package/dist/src/utils/ShellCommand.js +1 -1
  116. package/dist/src/utils/activityManager.js +1 -1
  117. package/dist/src/utils/advisor.js +1 -1
  118. package/dist/src/utils/appleTerminalBackup.js +1 -1
  119. package/dist/src/utils/argumentSubstitution.js +1 -1
  120. package/dist/src/utils/authFileDescriptor.js +1 -1
  121. package/dist/src/utils/autoUpdater.js +1 -1
  122. package/dist/src/utils/background/remote/preconditions.js +1 -1
  123. package/dist/src/utils/background/remote/remoteSession.js +1 -1
  124. package/dist/src/utils/bash/ShellSnapshot.js +1 -1
  125. package/dist/src/utils/bash/ast.js +1 -1
  126. package/dist/src/utils/bash/bashParser.js +1 -1
  127. package/dist/src/utils/bash/bashPipeCommand.js +1 -1
  128. package/dist/src/utils/bash/parser.js +1 -1
  129. package/dist/src/utils/bash/shellQuote.js +1 -1
  130. package/dist/src/utils/bash/shellQuoting.js +1 -1
  131. package/dist/src/utils/billing.js +1 -1
  132. package/dist/src/utils/caCerts.js +1 -1
  133. package/dist/src/utils/claudeInChrome/common.js +1 -1
  134. package/dist/src/utils/claudeInChrome/setupPortable.js +1 -1
  135. package/dist/src/utils/claudemd.js +1 -1
  136. package/dist/src/utils/collapseBackgroundBashNotifications.js +1 -1
  137. package/dist/src/utils/collapseReadSearch.js +1 -1
  138. package/dist/src/utils/completionCache.js +1 -1
  139. package/dist/src/utils/computerUse/common.js +1 -1
  140. package/dist/src/utils/concurrentSessions.js +1 -1
  141. package/dist/src/utils/context.js +1 -1
  142. package/dist/src/utils/cron.js +1 -1
  143. package/dist/src/utils/cronTasks.js +1 -1
  144. package/dist/src/utils/cwd.js +1 -1
  145. package/dist/src/utils/debug.js +1 -1
  146. package/dist/src/utils/debugFilter.js +1 -1
  147. package/dist/src/utils/detectRepository.js +1 -1
  148. package/dist/src/utils/diagLogs.js +1 -1
  149. package/dist/src/utils/diff.js +1 -1
  150. package/dist/src/utils/directMemberMessage.js +1 -1
  151. package/dist/src/utils/doctorDiagnostic.js +1 -1
  152. package/dist/src/utils/dxt/helpers.js +1 -1
  153. package/dist/src/utils/dxt/zip.js +1 -1
  154. package/dist/src/utils/earlyInput.js +1 -1
  155. package/dist/src/utils/editor.js +1 -1
  156. package/dist/src/utils/effort.js +1 -1
  157. package/dist/src/utils/embeddedTools.js +1 -1
  158. package/dist/src/utils/envDynamic.js +1 -1
  159. package/dist/src/utils/envUtils.js +1 -1
  160. package/dist/src/utils/execFileNoThrowPortable.js +1 -1
  161. package/dist/src/utils/execSyncWrapper.js +1 -1
  162. package/dist/src/utils/exportRenderer.js +1 -1
  163. package/dist/src/utils/extraUsage.js +1 -1
  164. package/dist/src/utils/fastMode.js +1 -1
  165. package/dist/src/utils/fileOperationAnalytics.js +1 -1
  166. package/dist/src/utils/fileRead.js +1 -1
  167. package/dist/src/utils/findExecutable.js +1 -1
  168. package/dist/src/utils/format.js +1 -1
  169. package/dist/src/utils/frontmatterParser.js +1 -1
  170. package/dist/src/utils/fsOperations.js +1 -1
  171. package/dist/src/utils/fullscreen.js +1 -1
  172. package/dist/src/utils/genericProcessUtils.js +1 -1
  173. package/dist/src/utils/getWorktreePaths.js +1 -1
  174. package/dist/src/utils/git/gitConfigParser.js +1 -1
  175. package/dist/src/utils/git/gitFilesystem.js +1 -1
  176. package/dist/src/utils/git/gitignore.js +1 -1
  177. package/dist/src/utils/gitDiff.js +1 -1
  178. package/dist/src/utils/gitSettings.js +1 -1
  179. package/dist/src/utils/glob.js +1 -1
  180. package/dist/src/utils/gracefulShutdown.js +1 -1
  181. package/dist/src/utils/groupToolUses.js +1 -1
  182. package/dist/src/utils/handlePromptSubmit.js +1 -1
  183. package/dist/src/utils/hash.js +1 -1
  184. package/dist/src/utils/hooks/fileChangedWatcher.js +1 -1
  185. package/dist/src/utils/hooks/hooksSettings.js +1 -1
  186. package/dist/src/utils/hooks/registerSkillHooks.js +1 -1
  187. package/dist/src/utils/hooks/sessionHooks.js +1 -1
  188. package/dist/src/utils/http.js +1 -1
  189. package/dist/src/utils/hyperlink.js +1 -1
  190. package/dist/src/utils/ide.js +1 -1
  191. package/dist/src/utils/idePathConversion.js +1 -1
  192. package/dist/src/utils/imagePaste.js +1 -1
  193. package/dist/src/utils/imageResizer.js +1 -1
  194. package/dist/src/utils/imageStore.js +1 -1
  195. package/dist/src/utils/inProcessTeammateHelpers.js +1 -1
  196. package/dist/src/utils/ink.js +1 -1
  197. package/dist/src/utils/jetbrains.js +1 -1
  198. package/dist/src/utils/json.js +1 -1
  199. package/dist/src/utils/listSessionsImpl.js +1 -1
  200. package/dist/src/utils/localInstaller.js +1 -1
  201. package/dist/src/utils/lockfile.js +1 -1
  202. package/dist/src/utils/logoV2Utils.js +1 -1
  203. package/dist/src/utils/markdown.js +1 -1
  204. package/dist/src/utils/mcp/dateTimeParser.js +1 -1
  205. package/dist/src/utils/mcpOutputStorage.js +1 -1
  206. package/dist/src/utils/mcpValidation.js +1 -1
  207. package/dist/src/utils/memoize.js +1 -1
  208. package/dist/src/utils/memory/types.js +1 -1
  209. package/dist/src/utils/memoryFileDetection.js +1 -1
  210. package/dist/src/utils/messageQueueManager.js +1 -1
  211. package/dist/src/utils/messages/mappers.js +1 -1
  212. package/dist/src/utils/messages/systemInit.js +1 -1
  213. package/dist/src/utils/model/antModels.js +1 -1
  214. package/dist/src/utils/model/check1mAccess.js +1 -1
  215. package/dist/src/utils/model/contextWindowUpgradeCheck.js +1 -1
  216. package/dist/src/utils/model/model.js +1 -1
  217. package/dist/src/utils/model/modelAllowlist.js +1 -1
  218. package/dist/src/utils/model/modelCapabilities.js +1 -1
  219. package/dist/src/utils/model/modelOptions.js +1 -1
  220. package/dist/src/utils/model/modelStrings.js +1 -1
  221. package/dist/src/utils/model/providerModels.js +1 -1
  222. package/dist/src/utils/model/providerProfiles.js +1 -1
  223. package/dist/src/utils/model/providerProfilesDb.js +1 -1
  224. package/dist/src/utils/model/providerSwitch.js +1 -1
  225. package/dist/src/utils/model/providers.js +1 -1
  226. package/dist/src/utils/modelCost.js +1 -1
  227. package/dist/src/utils/modifiers.js +1 -1
  228. package/dist/src/utils/mtls.js +1 -1
  229. package/dist/src/utils/nativeInstaller/download.js +1 -1
  230. package/dist/src/utils/nativeInstaller/installer.js +1 -1
  231. package/dist/src/utils/nativeInstaller/packageManagers.js +1 -1
  232. package/dist/src/utils/nativeInstaller/pidLock.js +1 -1
  233. package/dist/src/utils/notebook.js +1 -1
  234. package/dist/src/utils/pasteStore.js +1 -1
  235. package/dist/src/utils/path.js +1 -1
  236. package/dist/src/utils/permissions/PermissionMode.js +1 -1
  237. package/dist/src/utils/permissions/PermissionPromptToolResultSchema.js +1 -1
  238. package/dist/src/utils/permissions/PermissionUpdate.js +1 -1
  239. package/dist/src/utils/permissions/PermissionUpdateSchema.js +1 -1
  240. package/dist/src/utils/permissions/autoModeState.js +1 -1
  241. package/dist/src/utils/permissions/bypassPermissionsKillswitch.js +1 -1
  242. package/dist/src/utils/permissions/filesystem.js +1 -1
  243. package/dist/src/utils/permissions/getNextPermissionMode.js +1 -1
  244. package/dist/src/utils/permissions/pathValidation.js +1 -1
  245. package/dist/src/utils/permissions/permissionRuleParser.js +1 -1
  246. package/dist/src/utils/permissions/permissionsDb.js +1 -1
  247. package/dist/src/utils/permissions/permissionsLoader.js +1 -1
  248. package/dist/src/utils/permissions/shellRuleMatching.js +1 -1
  249. package/dist/src/utils/planModeV2.js +1 -1
  250. package/dist/src/utils/plans.js +1 -1
  251. package/dist/src/utils/platform.js +1 -1
  252. package/dist/src/utils/plugins/addDirPluginSettings.js +1 -1
  253. package/dist/src/utils/plugins/cacheUtils.js +1 -1
  254. package/dist/src/utils/plugins/dependencyResolver.js +1 -1
  255. package/dist/src/utils/plugins/fetchTelemetry.js +1 -1
  256. package/dist/src/utils/plugins/gitAvailability.js +1 -1
  257. package/dist/src/utils/plugins/hintRecommendation.js +1 -1
  258. package/dist/src/utils/plugins/installedPluginsManager.js +1 -1
  259. package/dist/src/utils/plugins/loadPluginAgents.js +1 -1
  260. package/dist/src/utils/plugins/loadPluginCommands.js +1 -1
  261. package/dist/src/utils/plugins/loadPluginHooks.js +1 -1
  262. package/dist/src/utils/plugins/loadPluginOutputStyles.js +1 -1
  263. package/dist/src/utils/plugins/lspPluginIntegration.js +1 -1
  264. package/dist/src/utils/plugins/lspRecommendation.js +1 -1
  265. package/dist/src/utils/plugins/managedPlugins.js +1 -1
  266. package/dist/src/utils/plugins/marketplaceHelpers.js +1 -1
  267. package/dist/src/utils/plugins/marketplaceManager.js +1 -1
  268. package/dist/src/utils/plugins/mcpPluginIntegration.js +1 -1
  269. package/dist/src/utils/plugins/mcpbHandler.js +1 -1
  270. package/dist/src/utils/plugins/officialMarketplaceGcs.js +1 -1
  271. package/dist/src/utils/plugins/officialMarketplaceStartupCheck.js +1 -1
  272. package/dist/src/utils/plugins/orphanedPluginFilter.js +1 -1
  273. package/dist/src/utils/plugins/performStartupChecks.js +1 -1
  274. package/dist/src/utils/plugins/pluginAutoupdate.js +1 -1
  275. package/dist/src/utils/plugins/pluginBlocklist.js +1 -1
  276. package/dist/src/utils/plugins/pluginDirectories.js +1 -1
  277. package/dist/src/utils/plugins/pluginFlagging.js +1 -1
  278. package/dist/src/utils/plugins/pluginInstallationHelpers.js +1 -1
  279. package/dist/src/utils/plugins/pluginLoader.js +1 -1
  280. package/dist/src/utils/plugins/pluginOptionsStorage.js +1 -1
  281. package/dist/src/utils/plugins/pluginPolicy.js +1 -1
  282. package/dist/src/utils/plugins/pluginStartupCheck.js +1 -1
  283. package/dist/src/utils/plugins/pluginVersioning.js +1 -1
  284. package/dist/src/utils/plugins/reconciler.js +1 -1
  285. package/dist/src/utils/plugins/refresh.js +1 -1
  286. package/dist/src/utils/plugins/schemas.js +1 -1
  287. package/dist/src/utils/plugins/walkPluginMarkdown.js +1 -1
  288. package/dist/src/utils/plugins/zipCache.js +1 -1
  289. package/dist/src/utils/powershell/parser.js +1 -1
  290. package/dist/src/utils/processUserInput/processBashCommand.js +1 -1
  291. package/dist/src/utils/processUserInput/processSlashCommand.js +1 -1
  292. package/dist/src/utils/processUserInput/processTextPrompt.js +1 -1
  293. package/dist/src/utils/processUserInput/processUserInput.js +1 -1
  294. package/dist/src/utils/profilerBase.js +1 -1
  295. package/dist/src/utils/promptCategory.js +1 -1
  296. package/dist/src/utils/promptEditor.js +1 -1
  297. package/dist/src/utils/promptShellExecution.js +1 -1
  298. package/dist/src/utils/proxy.js +1 -1
  299. package/dist/src/utils/queryHelpers.js +1 -1
  300. package/dist/src/utils/queryProfiler.js +1 -1
  301. package/dist/src/utils/queueProcessor.js +1 -1
  302. package/dist/src/utils/readFileInRange.js +1 -1
  303. package/dist/src/utils/releaseNotes.js +1 -1
  304. package/dist/src/utils/renderOptions.js +1 -1
  305. package/dist/src/utils/ripgrep.js +1 -1
  306. package/dist/src/utils/sandbox/sandbox-adapter.js +1 -1
  307. package/dist/src/utils/sdkEventQueue.js +1 -1
  308. package/dist/src/utils/secureStorage/index.js +1 -1
  309. package/dist/src/utils/secureStorage/macOsKeychainHelpers.js +1 -1
  310. package/dist/src/utils/secureStorage/macOsKeychainStorage.js +1 -1
  311. package/dist/src/utils/secureStorage/plainTextStorage.js +1 -1
  312. package/dist/src/utils/secureStorage/sqliteStorage.js +1 -1
  313. package/dist/src/utils/sessionEnvironment.js +1 -1
  314. package/dist/src/utils/sessionIngressAuth.js +1 -1
  315. package/dist/src/utils/sessionRestore.js +1 -1
  316. package/dist/src/utils/sessionStart.js +1 -1
  317. package/dist/src/utils/sessionTitle.js +1 -1
  318. package/dist/src/utils/settings/managedPath.js +1 -1
  319. package/dist/src/utils/settings/mdm/rawRead.js +1 -1
  320. package/dist/src/utils/settings/mdm/settings.js +1 -1
  321. package/dist/src/utils/settings/permissionValidation.js +1 -1
  322. package/dist/src/utils/settings/pluginOnlyPolicy.js +1 -1
  323. package/dist/src/utils/settings/schemaOutput.js +1 -1
  324. package/dist/src/utils/settings/settings.js +1 -1
  325. package/dist/src/utils/settings/types.js +1 -1
  326. package/dist/src/utils/settings/validateEditTool.js +1 -1
  327. package/dist/src/utils/settings/validation.js +1 -1
  328. package/dist/src/utils/shell/bashProvider.js +1 -1
  329. package/dist/src/utils/shell/powershellDetection.js +1 -1
  330. package/dist/src/utils/shell/powershellProvider.js +1 -1
  331. package/dist/src/utils/shell/readOnlyCommandValidation.js +1 -1
  332. package/dist/src/utils/shell/resolveDefaultShell.js +1 -1
  333. package/dist/src/utils/shell/shellToolUtils.js +1 -1
  334. package/dist/src/utils/shell/specPrefix.js +1 -1
  335. package/dist/src/utils/shellConfig.js +1 -1
  336. package/dist/src/utils/sideQuestion.js +1 -1
  337. package/dist/src/utils/skills/skillChangeDetector.js +1 -1
  338. package/dist/src/utils/slashCommandParsing.js +1 -1
  339. package/dist/src/utils/sliceAnsi.js +1 -1
  340. package/dist/src/utils/slowOperations.js +1 -1
  341. package/dist/src/utils/standaloneAgent.js +1 -1
  342. package/dist/src/utils/startupProfiler.js +1 -1
  343. package/dist/src/utils/staticRender.js +1 -1
  344. package/dist/src/utils/status.js +1 -1
  345. package/dist/src/utils/statusNoticeDefinitions.js +1 -1
  346. package/dist/src/utils/suggestions/commandSuggestions.js +1 -1
  347. package/dist/src/utils/suggestions/directoryCompletion.js +1 -1
  348. package/dist/src/utils/suggestions/shellHistoryCompletion.js +1 -1
  349. package/dist/src/utils/suggestions/skillUsageTracking.js +1 -1
  350. package/dist/src/utils/suggestions/slackChannelSuggestions.js +1 -1
  351. package/dist/src/utils/swarm/backends/detection.js +1 -1
  352. package/dist/src/utils/swarm/permissionSync.js +1 -1
  353. package/dist/src/utils/swarm/reconnection.js +1 -1
  354. package/dist/src/utils/swarm/spawnUtils.js +91 -1
  355. package/dist/src/utils/swarm/teammateInit.js +1 -1
  356. package/dist/src/utils/systemDirectories.js +1 -1
  357. package/dist/src/utils/systemPrompt.js +1 -1
  358. package/dist/src/utils/systemTheme.js +1 -1
  359. package/dist/src/utils/task/TaskOutput.js +1 -1
  360. package/dist/src/utils/task/diskOutput.js +1 -1
  361. package/dist/src/utils/tasks.js +1 -1
  362. package/dist/src/utils/teamDiscovery.js +1 -1
  363. package/dist/src/utils/teamMemoryOps.js +1 -1
  364. package/dist/src/utils/teammateMailbox.js +1 -1
  365. package/dist/src/utils/telemetry/betaSessionTracing.js +1 -1
  366. package/dist/src/utils/telemetry/bigqueryExporter.js +1 -1
  367. package/dist/src/utils/telemetry/events.js +1 -1
  368. package/dist/src/utils/telemetry/instrumentation.js +1 -1
  369. package/dist/src/utils/telemetry/logger.js +1 -1
  370. package/dist/src/utils/telemetry/perfettoTracing.js +1 -1
  371. package/dist/src/utils/telemetry/pluginTelemetry.js +1 -1
  372. package/dist/src/utils/telemetry/sessionTracing.js +1 -1
  373. package/dist/src/utils/telemetryAttributes.js +1 -1
  374. package/dist/src/utils/teleport/api.js +1 -1
  375. package/dist/src/utils/teleport/environments.js +1 -1
  376. package/dist/src/utils/teleport/gitBundle.js +1 -1
  377. package/dist/src/utils/teleport.js +1 -1
  378. package/dist/src/utils/tempfile.js +1 -1
  379. package/dist/src/utils/terminal.js +1 -1
  380. package/dist/src/utils/terminalPanel.js +1 -1
  381. package/dist/src/utils/textHighlighting.js +1 -1
  382. package/dist/src/utils/theme.js +1 -1
  383. package/dist/src/utils/themes/bootstrap.js +1 -1
  384. package/dist/src/utils/themes/loader.js +1 -1
  385. package/dist/src/utils/thinking.js +1 -1
  386. package/dist/src/utils/tmuxSocket.js +1 -1
  387. package/dist/src/utils/tokens.js +1 -1
  388. package/dist/src/utils/toolPool.js +1 -1
  389. package/dist/src/utils/toolResultStorage.js +1 -1
  390. package/dist/src/utils/transcriptSearch.js +1 -1
  391. package/dist/src/utils/truncate.js +1 -1
  392. package/dist/src/utils/ultraplan/keyword.js +1 -1
  393. package/dist/src/utils/unaryLogging.js +1 -1
  394. package/dist/src/utils/undercover.js +1 -1
  395. package/dist/src/utils/user.js +1 -1
  396. package/dist/src/utils/userPromptKeywords.js +1 -1
  397. package/dist/src/utils/which.js +1 -1
  398. package/dist/src/utils/windowsPaths.js +1 -1
  399. package/dist/src/utils/worktree.js +1 -1
  400. package/dist/src/utils/zodToJsonSchema.js +1 -1
  401. package/dist/src/vim/operators.js +1 -1
  402. package/dist/src/vim/textObjects.js +1 -1
  403. package/dist/src/vim/transitions.js +1 -1
  404. package/dist/src/voice/voiceModeEnabled.js +1 -1
  405. package/dist/src/webapp/auth.js +1 -1
  406. package/dist/src/webapp/tunnel.js +1 -1
  407. package/dist/src/whatsapp/bridge.js +1 -1
  408. package/dist/src/whatsapp/mirror.js +1 -1
  409. package/package.json +1 -1
@@ -1 +1 @@
1
- const e={parse:function(e,n){const t=(r=e,{src:r,len:r.length,i:0,b:0,heredocs:[],byteTable:null}),a=function(e){let n=0;for(let t=0;t<e.length;t++){const a=e.charCodeAt(t);a<128?n++:a<2048?n+=2:a>=55296&&a<=56319?(n+=4,t++):n+=3}return n}(e),s={L:t,src:e,srcBytes:a,isAscii:a===e.length,nodeCount:0,deadline:performance.now()+(n??50),aborted:!1,inBacktick:0,stopToken:null};var r;try{const e=function(e){const n=[];skipBlanks(e.L);for(;;){const n=saveLex(e.L);if("NEWLINE"!==nextToken(e.L,"cmd").type){restoreLex(e.L,n);break}skipBlanks(e.L)}const t=e.L.b;for(;e.L.i<e.L.len;){const t=saveLex(e.L),a=nextToken(e.L,"cmd");if("EOF"===a.type)break;if("NEWLINE"===a.type)continue;if("COMMENT"===a.type){n.push(leaf(e,"comment",a));continue}restoreLex(e.L,t);const s=parseStatements(e,null);for(const e of s)n.push(e);if(0===s.length){const t=nextToken(e.L,"cmd");if("EOF"===t.type)break;if("OP"===t.type&&";;"===t.value&&n.length>0)continue;n.push(mk(e,"ERROR",t.start,t.end,[]))}}const a=n.length>0?e.srcBytes:t;return mk(e,"program",t,a,n)}(s);return s.aborted?null:e}catch{return null}}},n=Promise.resolve();export function ensureParserInitialized(){return n}export function getParserModule(){return e}const t=new Set(["?","$","@","*","#","-","!","_"]),a=new Set(["export","declare","typeset","readonly","local"]);export const SHELL_KEYWORDS=new Set(["if","then","elif","else","fi","while","until","for","in","do","done","case","esac","function","select"]);function advance(e){const n=e.src.charCodeAt(e.i);e.i++,n<128?e.b++:n<2048?e.b+=2:n>=55296&&n<=56319?(e.b+=4,e.i++):e.b+=3}function peek(e,n=0){return e.i+n<e.len?e.src[e.i+n]:""}function byteAt(e,n){if(e.byteTable)return e.byteTable[n];const t=new Uint32Array(e.len+1);let a=0,s=0;for(;s<e.len;){t[s]=a;const n=e.src.charCodeAt(s);n<128?(a++,s++):n<2048?(a+=2,s++):n>=55296&&n<=56319?(t[s+1]=a+2,a+=4,s+=2):(a+=3,s++)}return t[e.len]=a,e.byteTable=t,t[n]}function isWordChar(e){return e>="a"&&e<="z"||e>="A"&&e<="Z"||e>="0"&&e<="9"||"_"===e||"/"===e||"."===e||"-"===e||"+"===e||":"===e||"@"===e||"%"===e||","===e||"~"===e||"^"===e||"?"===e||"*"===e||"!"===e||"="===e||"["===e||"]"===e}function isIdentStart(e){return e>="a"&&e<="z"||e>="A"&&e<="Z"||"_"===e}function isIdentChar(e){return isIdentStart(e)||e>="0"&&e<="9"}function isDigit(e){return e>="0"&&e<="9"}function isHexDigit(e){return isDigit(e)||e>="a"&&e<="f"||e>="A"&&e<="F"}function isBaseDigit(e){return isIdentChar(e)||"@"===e}function isHeredocDelimChar(e){return""!==e&&" "!==e&&"\t"!==e&&"\n"!==e&&"<"!==e&&">"!==e&&"|"!==e&&"&"!==e&&";"!==e&&"("!==e&&")"!==e&&"'"!==e&&'"'!==e&&"`"!==e&&"\\"!==e}function skipBlanks(e){for(;e.i<e.len;){const n=e.src[e.i];if(" "===n||"\t"===n||"\r"===n)advance(e);else{if("\\"!==n)break;{const n=e.src[e.i+1];if("\n"===n||"\r"===n&&"\n"===e.src[e.i+2])advance(e),advance(e),"\r"===n&&advance(e);else{if(" "!==n&&"\t"!==n)break;advance(e),advance(e)}}}}}function nextToken(e,n="arg"){skipBlanks(e);const t=e.b;if(e.i>=e.len)return{type:"EOF",value:"",start:t,end:t};const a=e.src[e.i],s=peek(e,1),r=peek(e,2);if("\n"===a)return advance(e),{type:"NEWLINE",value:"\n",start:t,end:e.b};if("#"===a){const n=e.i;for(;e.i<e.len&&"\n"!==e.src[e.i];)advance(e);return{type:"COMMENT",value:e.src.slice(n,e.i),start:t,end:e.b}}if("&"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:"&&",start:t,end:e.b};if("|"===a&&"|"===s)return advance(e),advance(e),{type:"OP",value:"||",start:t,end:e.b};if("|"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:"|&",start:t,end:e.b};if(";"===a&&";"===s&&"&"===r)return advance(e),advance(e),advance(e),{type:"OP",value:";;&",start:t,end:e.b};if(";"===a&&";"===s)return advance(e),advance(e),{type:"OP",value:";;",start:t,end:e.b};if(";"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:";&",start:t,end:e.b};if(">"===a&&">"===s)return advance(e),advance(e),{type:"OP",value:">>",start:t,end:e.b};if(">"===a&&"&"===s&&"-"===r)return advance(e),advance(e),advance(e),{type:"OP",value:">&-",start:t,end:e.b};if(">"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:">&",start:t,end:e.b};if(">"===a&&"|"===s)return advance(e),advance(e),{type:"OP",value:">|",start:t,end:e.b};if("&"===a&&">"===s&&">"===r)return advance(e),advance(e),advance(e),{type:"OP",value:"&>>",start:t,end:e.b};if("&"===a&&">"===s)return advance(e),advance(e),{type:"OP",value:"&>",start:t,end:e.b};if("<"===a&&"<"===s&&"<"===r)return advance(e),advance(e),advance(e),{type:"OP",value:"<<<",start:t,end:e.b};if("<"===a&&"<"===s&&"-"===r)return advance(e),advance(e),advance(e),{type:"OP",value:"<<-",start:t,end:e.b};if("<"===a&&"<"===s)return advance(e),advance(e),{type:"OP",value:"<<",start:t,end:e.b};if("<"===a&&"&"===s&&"-"===r)return advance(e),advance(e),advance(e),{type:"OP",value:"<&-",start:t,end:e.b};if("<"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:"<&",start:t,end:e.b};if("<"===a&&"("===s)return advance(e),advance(e),{type:"LT_PAREN",value:"<(",start:t,end:e.b};if(">"===a&&"("===s)return advance(e),advance(e),{type:"GT_PAREN",value:">(",start:t,end:e.b};if("("===a&&"("===s)return advance(e),advance(e),{type:"OP",value:"((",start:t,end:e.b};if(")"===a&&")"===s)return advance(e),advance(e),{type:"OP",value:"))",start:t,end:e.b};if("|"===a||"&"===a||";"===a||">"===a||"<"===a)return advance(e),{type:"OP",value:a,start:t,end:e.b};if("("===a||")"===a)return advance(e),{type:"OP",value:a,start:t,end:e.b};if("cmd"===n){if("["===a&&"["===s)return advance(e),advance(e),{type:"OP",value:"[[",start:t,end:e.b};if("["===a)return advance(e),{type:"OP",value:"[",start:t,end:e.b};if("{"===a&&(" "===s||"\t"===s||"\n"===s))return advance(e),{type:"OP",value:"{",start:t,end:e.b};if("}"===a)return advance(e),{type:"OP",value:"}",start:t,end:e.b};if("!"===a&&(" "===s||"\t"===s))return advance(e),{type:"OP",value:"!",start:t,end:e.b}}if('"'===a)return advance(e),{type:"DQUOTE",value:'"',start:t,end:e.b};if("'"===a){const n=e.i;for(advance(e);e.i<e.len&&"'"!==e.src[e.i];)advance(e);return e.i<e.len&&advance(e),{type:"SQUOTE",value:e.src.slice(n,e.i),start:t,end:e.b}}if("$"===a){if("("===s&&"("===r)return advance(e),advance(e),advance(e),{type:"DOLLAR_DPAREN",value:"$((",start:t,end:e.b};if("("===s)return advance(e),advance(e),{type:"DOLLAR_PAREN",value:"$(",start:t,end:e.b};if("{"===s)return advance(e),advance(e),{type:"DOLLAR_BRACE",value:"${",start:t,end:e.b};if("'"===s){const n=e.i;for(advance(e),advance(e);e.i<e.len&&"'"!==e.src[e.i];)"\\"===e.src[e.i]&&e.i+1<e.len&&advance(e),advance(e);return e.i<e.len&&advance(e),{type:"ANSI_C",value:e.src.slice(n,e.i),start:t,end:e.b}}return advance(e),{type:"DOLLAR",value:"$",start:t,end:e.b}}if("`"===a)return advance(e),{type:"BACKTICK",value:"`",start:t,end:e.b};if(isDigit(a)){let n=e.i;for(;n<e.len&&isDigit(e.src[n]);)n++;const a=n<e.len?e.src[n]:"";if(">"===a||"<"===a){const a=e.i;for(;e.i<n;)advance(e);return{type:"WORD",value:e.src.slice(a,e.i),start:t,end:e.b}}}if(function(e){return isWordChar(e)||"\\"===e}(a)||"{"===a||"}"===a){const n=e.i;for(;e.i<e.len;){const n=e.src[e.i];if("\\"!==n){if(!isWordChar(n)&&"{"!==n&&"}"!==n)break;advance(e)}else{if(e.i+1>=e.len)break;if("\n"===e.src[e.i+1]){advance(e),advance(e);continue}advance(e),advance(e)}}if(e.i>n){const a=e.src.slice(n,e.i);return/^-?\d+$/.test(a)?{type:"NUMBER",value:a,start:t,end:e.b}:{type:"WORD",value:a,start:t,end:e.b}}}return advance(e),{type:"WORD",value:a,start:t,end:e.b}}function mk(e,n,t,a,s){return function(e){if(e.nodeCount++,e.nodeCount>5e4)throw e.aborted=!0,new Error("budget");if(!(127&e.nodeCount)&&performance.now()>e.deadline)throw e.aborted=!0,new Error("timeout")}(e),{type:n,text:sliceBytes(e,t,a),startIndex:t,endIndex:a,children:s}}function sliceBytes(e,n,t){if(e.isAscii)return e.src.slice(n,t);const a=e.L;a.byteTable||byteAt(a,0);const s=a.byteTable;let r=0,i=e.src.length;for(;r<i;){const e=r+i>>>1;s[e]<n?r=e+1:i=e}const L=r;for(r=L,i=e.src.length;r<i;){const e=r+i>>>1;s[e]<t?r=e+1:i=e}return e.src.slice(L,r)}function leaf(e,n,t){return mk(e,n,t.start,t.end,[])}function saveLex(e){return 65536*e.b+e.i}function restoreLex(e,n){e.i=65535&n,e.b=n>>>16}function parseStatements(e,n){const t=[];for(;;){skipBlanks(e.L);const a=saveLex(e.L),s=nextToken(e.L,"cmd");if("EOF"===s.type){restoreLex(e.L,a);break}if("NEWLINE"===s.type){e.L.heredocs.length>0&&scanHeredocBodies(e);continue}if("COMMENT"===s.type){t.push(leaf(e,"comment",s));continue}if(n&&"OP"===s.type&&s.value===n){restoreLex(e.L,a);break}if("OP"===s.type&&(")"===s.value||"}"===s.value||";;"===s.value||";&"===s.value||";;&"===s.value||"))"===s.value||"]]"===s.value||"]"===s.value)){restoreLex(e.L,a);break}if("BACKTICK"===s.type&&e.inBacktick>0){restoreLex(e.L,a);break}if("WORD"===s.type&&("then"===s.value||"elif"===s.value||"else"===s.value||"fi"===s.value||"do"===s.value||"done"===s.value||"esac"===s.value)){restoreLex(e.L,a);break}restoreLex(e.L,a);const r=parseAndOr(e);if(!r)break;t.push(r),skipBlanks(e.L);const i=saveLex(e.L),L=nextToken(e.L,"cmd");if("OP"!==L.type||";"!==L.value&&"&"!==L.value){if("NEWLINE"===L.type){e.L.heredocs.length>0&&scanHeredocBodies(e);continue}restoreLex(e.L,i)}else{const n=saveLex(e.L),a=nextToken(e.L,"cmd");if(restoreLex(e.L,n),t.push(leaf(e,L.value,L)),"EOF"===a.type||"OP"===a.type&&(")"===a.value||"}"===a.value||";;"===a.value||";&"===a.value||";;&"===a.value)||"WORD"===a.type&&("then"===a.value||"elif"===a.value||"else"===a.value||"fi"===a.value||"do"===a.value||"done"===a.value||"esac"===a.value))continue}}return t}function parseAndOr(e){let n=parsePipeline(e);if(!n)return null;for(;;){const t=saveLex(e.L),a=nextToken(e.L,"cmd");if("OP"!==a.type||"&&"!==a.value&&"||"!==a.value){restoreLex(e.L,t);break}{const t=leaf(e,a.value,a);skipNewlines(e);const s=parsePipeline(e);if(!s){n=mk(e,"list",n.startIndex,t.endIndex,[n,t]);break}if("redirected_statement"===s.type&&s.children.length>=2){const a=s.children[0],r=s.children.slice(1),i=mk(e,"list",n.startIndex,a.endIndex,[n,t,a]),L=r[r.length-1];n=mk(e,"redirected_statement",i.startIndex,L.endIndex,[i,...r])}else n=mk(e,"list",n.startIndex,s.endIndex,[n,t,s])}}return n}function skipNewlines(e){for(;;){const n=saveLex(e.L);if("NEWLINE"!==nextToken(e.L,"cmd").type){restoreLex(e.L,n);break}}}function parsePipeline(e){let n=parseCommand(e);if(!n)return null;const t=[n];for(;;){const a=saveLex(e.L),s=nextToken(e.L,"cmd");if("OP"!==s.type||"|"!==s.value&&"|&"!==s.value){restoreLex(e.L,a);break}{const a=leaf(e,s.value,s);skipNewlines(e);const r=parseCommand(e);if(!r){t.push(a);break}if("redirected_statement"===r.type&&r.children.length>=2&&t.length>=1){const s=r.children[0],i=r.children.slice(1),L=[...t,a,s],c=mk(e,"pipeline",L[0].startIndex,s.endIndex,L),o=i[i.length-1],d=mk(e,"redirected_statement",c.startIndex,o.endIndex,[c,...i]);t.length=0,t.push(d),n=d;continue}t.push(a,r)}}if(1===t.length)return t[0];const a=t[t.length-1];return mk(e,"pipeline",t[0].startIndex,a.endIndex,t)}function parseCommand(e){skipBlanks(e.L);const n=saveLex(e.L),t=nextToken(e.L,"cmd");if("EOF"===t.type)return restoreLex(e.L,n),null;if("OP"===t.type&&"!"===t.value){const a=leaf(e,"!",t),s=parseCommand(e);if(!s)return restoreLex(e.L,n),null;if("redirected_statement"===s.type&&s.children.length>=2){const n=s.children[0],t=s.children.slice(1),r=mk(e,"negated_command",a.startIndex,n.endIndex,[a,n]),i=t[t.length-1];return mk(e,"redirected_statement",r.startIndex,i.endIndex,[r,...t])}return mk(e,"negated_command",a.startIndex,s.endIndex,[a,s])}if("OP"===t.type&&"("===t.value){const n=leaf(e,"(",t),a=parseStatements(e,")"),s=nextToken(e.L,"cmd"),r="OP"===s.type&&")"===s.value?leaf(e,")",s):mk(e,")",n.endIndex,n.endIndex,[]);return maybeRedirect(e,mk(e,"subshell",n.startIndex,r.endIndex,[n,...a,r]))}if("OP"===t.type&&"(("===t.value){const n=leaf(e,"((",t),a=parseArithCommaList(e,"))","var"),s=nextToken(e.L,"cmd"),r="))"===s.value?leaf(e,"))",s):mk(e,"))",n.endIndex,n.endIndex,[]);return mk(e,"compound_statement",n.startIndex,r.endIndex,[n,...a,r])}if("OP"===t.type&&"{"===t.value){const n=leaf(e,"{",t),a=parseStatements(e,"}"),s=nextToken(e.L,"cmd"),r="OP"===s.type&&"}"===s.value?leaf(e,"}",s):mk(e,"}",n.endIndex,n.endIndex,[]);return maybeRedirect(e,mk(e,"compound_statement",n.startIndex,r.endIndex,[n,...a,r]))}if("OP"===t.type&&("["===t.value||"[["===t.value)){const n=leaf(e,t.value,t),a="["===t.value?"]":"]]",s=saveLex(e.L);let r=parseTestExpr(e,a);if(skipBlanks(e.L),"["===t.value&&"]"!==peek(e.L)){restoreLex(e.L,s);const n=e.stopToken;e.stopToken="]";const t=parseCommand(e);e.stopToken=n,t&&"redirected_statement"===t.type?r=t:(restoreLex(e.L,s),r=parseTestExpr(e,a)),skipBlanks(e.L)}const i=nextToken(e.L,"arg");let L;L=i.value===a?leaf(e,a,i):mk(e,a,n.endIndex,n.endIndex,[]);const c=r?[n,r,L]:[n,L];return mk(e,"test_command",n.startIndex,L.endIndex,c)}if("WORD"===t.type){if("if"===t.value)return maybeRedirect(e,function(e,n){const t=leaf(e,"if",n),a=[t],s=parseStatements(e,null);a.push(...s),consumeKeyword(e,"then",a);const r=parseStatements(e,null);a.push(...r);for(;;){const n=saveLex(e.L),t=nextToken(e.L,"cmd");if("WORD"===t.type&&"elif"===t.value){const n=leaf(e,"elif",t),s=[n,...parseStatements(e,null)];consumeKeyword(e,"then",s);const r=parseStatements(e,null);s.push(...r);const i=s[s.length-1];a.push(mk(e,"elif_clause",n.startIndex,i.endIndex,s))}else{if("WORD"!==t.type||"else"!==t.value){restoreLex(e.L,n);break}{const n=leaf(e,"else",t),s=parseStatements(e,null),r=s.length>0?s[s.length-1]:n;a.push(mk(e,"else_clause",n.startIndex,r.endIndex,[n,...s]))}}}consumeKeyword(e,"fi",a);const i=a[a.length-1];return mk(e,"if_statement",t.startIndex,i.endIndex,a)}(e,t),!0);if("while"===t.value||"until"===t.value)return maybeRedirect(e,function(e,n){const t=leaf(e,n.value,n),a=[t],s=parseStatements(e,null);a.push(...s);const r=parseDoGroup(e);r&&a.push(r);const i=a[a.length-1];return mk(e,"while_statement",t.startIndex,i.endIndex,a)}(e,t),!0);if("for"===t.value)return maybeRedirect(e,parseFor(e,t),!0);if("select"===t.value)return maybeRedirect(e,parseFor(e,t),!0);if("case"===t.value)return maybeRedirect(e,function(e,n){const t=leaf(e,"case",n),a=[t];skipBlanks(e.L);const s=parseWord(e,"arg");s&&a.push(s);skipBlanks(e.L),consumeKeyword(e,"in",a),skipNewlines(e);for(;;){skipBlanks(e.L),skipNewlines(e);const n=saveLex(e.L),t=nextToken(e.L,"arg");if("WORD"===t.type&&"esac"===t.value){a.push(leaf(e,"esac",t));break}if("EOF"===t.type)break;restoreLex(e.L,n);const s=parseCaseItem(e);if(!s)break;a.push(s)}const r=a[a.length-1];return mk(e,"case_statement",t.startIndex,r.endIndex,a)}(e,t),!0);if("function"===t.value)return function(e,n){const t=leaf(e,"function",n);skipBlanks(e.L);const a=nextToken(e.L,"arg"),s=mk(e,"word",a.start,a.end,[]),r=[t,s];if(skipBlanks(e.L),"("===peek(e.L)&&")"===peek(e.L,1)){const n=nextToken(e.L,"cmd"),t=nextToken(e.L,"cmd");r.push(leaf(e,"(",n)),r.push(leaf(e,")",t))}skipBlanks(e.L),skipNewlines(e);const i=parseCommand(e);i&&("redirected_statement"===i.type&&i.children.length>=2&&"compound_statement"===i.children[0].type?r.push(...i.children):r.push(i));const L=r[r.length-1];return mk(e,"function_definition",t.startIndex,L.endIndex,r)}(e,t);if(a.has(t.value))return maybeRedirect(e,function(e,n){const t=leaf(e,n.value,n),a=[t];for(;;){skipBlanks(e.L);const n=peek(e.L);if(""===n||"\n"===n||";"===n||"&"===n||"|"===n||")"===n||"<"===n||">"===n)break;const t=tryParseAssignment(e);if(t){a.push(t);continue}if('"'===n||"'"===n||"$"===n){const n=parseWord(e,"arg");if(n){a.push(n);continue}break}const s=saveLex(e.L),r=nextToken(e.L,"arg");if("WORD"!==r.type&&"NUMBER"!==r.type){restoreLex(e.L,s);break}r.value.startsWith("-")?a.push(leaf(e,"word",r)):isIdentStart(r.value[0]??"")?a.push(mk(e,"variable_name",r.start,r.end,[])):a.push(leaf(e,"word",r))}const s=a[a.length-1];return mk(e,"declaration_command",t.startIndex,s.endIndex,a)}(e,t));if("unset"===t.value||"unsetenv"===t.value)return maybeRedirect(e,function(e,n){const t=leaf(e,"unset",n),a=[t];for(;;){skipBlanks(e.L);const n=peek(e.L);if(""===n||"\n"===n||";"===n||"&"===n||"|"===n||")"===n||"<"===n||">"===n)break;const t=parseWord(e,"arg");if(!t)break;"word"===t.type?t.text.startsWith("-")?a.push(t):a.push(mk(e,"variable_name",t.startIndex,t.endIndex,[])):a.push(t)}const s=a[a.length-1];return mk(e,"unset_command",t.startIndex,s.endIndex,a)}(e,t))}return restoreLex(e.L,n),function(e){const n=e.L.b,t=[],a=[];for(;;){skipBlanks(e.L);const n=tryParseAssignment(e);if(n){t.push(n);continue}const s=tryParseRedirect(e);if(!s)break;a.push(s)}skipBlanks(e.L);const s=saveLex(e.L),r=nextToken(e.L,"cmd");if("EOF"===r.type||"NEWLINE"===r.type||"COMMENT"===r.type||"OP"===r.type&&"{"!==r.value&&"["!==r.value&&"[["!==r.value||"WORD"===r.type&&SHELL_KEYWORDS.has(r.value)&&"in"!==r.value){if(restoreLex(e.L,s),1===t.length&&0===a.length)return t[0];if(a.length>0&&0===t.length){const n=a[a.length-1];return mk(e,"redirected_statement",a[0].startIndex,n.endIndex,a)}if(t.length>1&&0===a.length){const n=t[t.length-1];return mk(e,"variable_assignments",t[0].startIndex,n.endIndex,t)}if(t.length>0||a.length>0){const s=[...t,...a];return mk(e,"command",n,s[s.length-1].endIndex,s)}return null}restoreLex(e.L,s);const i=saveLex(e.L),L=parseWord(e,"cmd");if(L&&"word"===L.type&&(skipBlanks(e.L),"("===peek(e.L)&&")"===peek(e.L,1))){const n=nextToken(e.L,"cmd"),t=nextToken(e.L,"cmd"),a=leaf(e,"(",n),s=leaf(e,")",t);skipBlanks(e.L),skipNewlines(e);const r=parseCommand(e);if(r){let n=[r];"redirected_statement"===r.type&&r.children.length>=2&&"compound_statement"===r.children[0].type&&(n=r.children);const t=n[n.length-1];return mk(e,"function_definition",L.startIndex,t.endIndex,[L,a,s,...n])}}restoreLex(e.L,i);const c=parseWord(e,"cmd");if(!c)return 1===t.length?t[0]:null;const o=mk(e,"command_name",c.startIndex,c.endIndex,[c]),d=[],l=[];let p=null;for(;;){skipBlanks(e.L);const n=tryParseRedirect(e,!0);if(n){"heredoc_redirect"===n.type?p=n:"herestring_redirect"===n.type?d.push(n):l.push(n);continue}if(l.length>0)break;if("]"===e.stopToken&&"]"===peek(e.L))break;const t=saveLex(e.L),a=nextToken(e.L,"arg");if("EOF"===a.type||"NEWLINE"===a.type||"COMMENT"===a.type||"OP"===a.type&&("|"===a.value||"|&"===a.value||"&&"===a.value||"||"===a.value||";"===a.value||";;"===a.value||";&"===a.value||";;&"===a.value||"&"===a.value||")"===a.value||"}"===a.value||"))"===a.value)){restoreLex(e.L,t);break}restoreLex(e.L,t);const s=parseWord(e,"arg");if(!s){if("("===peek(e.L)){const n=leaf(e,"(",nextToken(e.L,"cmd")),t=parseStatements(e,")"),a=nextToken(e.L,"cmd"),s="OP"===a.type&&")"===a.value?leaf(e,")",a):mk(e,")",n.endIndex,n.endIndex,[]);d.push(mk(e,"subshell",n.startIndex,s.endIndex,[n,...t,s]));continue}break}"word"!==s.type||"="!==s.text?"word"!==s.type&&"concatenation"!==s.type||"("!==peek(e.L)||e.L.b!==s.endIndex?d.push(s):d.push(mk(e,"ERROR",s.startIndex,s.endIndex,[s])):d.push(mk(e,"ERROR",s.startIndex,s.endIndex,[s]))}const u=[...t,...a,o,...d],k=u.length>0?u[u.length-1].endIndex:o.endIndex,f=u[0].startIndex,v=mk(e,"command",f,k,u);if(p){scanHeredocBodies(e);const n=e.L.heredocs.shift();if(n&&p.children.length>=2){const t=mk(e,"heredoc_body",n.bodyStart,n.bodyEnd,n.quoted?[]:function(e,n,t){const a=saveLex(e.L);!function(e,n){e.L.byteTable||byteAt(e.L,0);const t=e.L.byteTable;let a=0,s=e.src.length;for(;a<s;){const e=a+s>>>1;t[e]<n?a=e+1:s=e}e.L.i=a,e.L.b=n}(e,n);const s=[];let r=e.L.b,i=!1;for(;e.L.b<t;){const n=peek(e.L);if("\\"===n){const n=peek(e.L,1);if("$"===n||"`"===n||"\\"===n){advance(e.L),advance(e.L);continue}advance(e.L);continue}if("$"===n||"`"===n){const n=e.L.b,t=parseDollarLike(e);!t||"simple_expansion"!==t.type&&"expansion"!==t.type&&"command_substitution"!==t.type&&"arithmetic_expansion"!==t.type||(i&&n>r&&s.push(mk(e,"heredoc_content",r,n,[])),s.push(t),r=e.L.b,i=!0);continue}advance(e.L)}i&&s.push(mk(e,"heredoc_content",r,t,[]));return restoreLex(e.L,a),s}(e,n.bodyStart,n.bodyEnd)),a=mk(e,"heredoc_end",n.endStart,n.endEnd,[]);p.children.push(t,a),p.endIndex=n.endEnd,p.text=sliceBytes(e,p.startIndex,n.endEnd)}const t=[...a,p,...l];return mk(e,"redirected_statement",a.length>0?Math.min(v.startIndex,a[0].startIndex):v.startIndex,p.endIndex,[v,...t])}if(l.length>0){const n=l[l.length-1];return mk(e,"redirected_statement",v.startIndex,n.endIndex,[v,...l])}return v}(e)}function maybeRedirect(e,n,t=!1){const a=[];for(;;){skipBlanks(e.L);const n=saveLex(e.L),s=tryParseRedirect(e);if(!s)break;if("herestring_redirect"===s.type&&!t){restoreLex(e.L,n);break}a.push(s)}if(0===a.length)return n;const s=a[a.length-1];return mk(e,"redirected_statement",n.startIndex,s.endIndex,[n,...a])}function tryParseAssignment(e){const n=saveLex(e.L);skipBlanks(e.L);const a=e.L.b;if(!isIdentStart(peek(e.L)))return restoreLex(e.L,n),null;for(;isIdentChar(peek(e.L));)advance(e.L);const s=e.L.b;let r=s;if("["===peek(e.L)){advance(e.L);let n=1;for(;e.L.i<e.L.len&&n>0;){const t=peek(e.L);"["===t?n++:"]"===t&&n--,advance(e.L)}r=e.L.b}const i=peek(e.L),L=peek(e.L,1);let c;if("="===i&&"="!==L)c="=";else{if("+"!==i||"="!==L)return restoreLex(e.L,n),null;c="+="}const o=mk(e,"variable_name",a,s,[]);let d=o;if(r>s){const n=mk(e,"[",s,s+1,[]),i=function(e,n,a){const s=sliceBytes(e,n,a);if(/^\d+$/.test(s))return mk(e,"number",n,a,[]);if(/^\$([a-zA-Z_]\w*)$/.exec(s)){const t=mk(e,"$",n,n+1,[]),s=mk(e,"variable_name",n+1,a,[]);return mk(e,"simple_expansion",n,a,[t,s])}if(2===s.length&&"$"===s[0]&&t.has(s[1])){const t=mk(e,"$",n,n+1,[]),s=mk(e,"special_variable_name",n+1,a,[]);return mk(e,"simple_expansion",n,a,[t,s])}return mk(e,"word",n,a,[])}(e,s+1,r-1),L=mk(e,"]",r-1,r,[]);d=mk(e,"subscript",a,r,[o,n,i,L])}const l=e.L.b;advance(e.L),"+="===c&&advance(e.L);const p=e.L.b,u=mk(e,c,l,p,[]);let k=null;if("("===peek(e.L)){const n=leaf(e,"(",nextToken(e.L,"cmd")),t=[n];for(;skipBlanks(e.L),")"!==peek(e.L);){const n=parseWord(e,"arg");if(!n)break;t.push(n)}const a=nextToken(e.L,"cmd"),s=")"===a.value?leaf(e,")",a):mk(e,")",n.endIndex,n.endIndex,[]);t.push(s),k=mk(e,"array",n.startIndex,s.endIndex,t)}else{const n=peek(e.L);n&&" "!==n&&"\t"!==n&&"\n"!==n&&";"!==n&&"&"!==n&&"|"!==n&&")"!==n&&"}"!==n&&(k=parseWord(e,"arg"))}const f=k?[d,u,k]:[d,u];return mk(e,"variable_assignment",a,k?k.endIndex:p,f)}function isRedirectLiteralStart(e){const n=peek(e.L);if(""===n||"\n"===n)return!1;if("|"===n||"&"===n||";"===n||"("===n||")"===n)return!1;if("<"===n||">"===n)return"("===peek(e.L,1);if(isDigit(n)){let n=e.L.i;for(;n<e.L.len&&isDigit(e.L.src[n]);)n++;const t=n<e.L.len?e.L.src[n]:"";if(">"===t||"<"===t)return!1}return"}"!==n&&("]"!==e.stopToken||"]"!==n)}function tryParseRedirect(e,n=!1){const t=saveLex(e.L);skipBlanks(e.L);let a=null;if(isDigit(peek(e.L))){const n=e.L.b;let t=e.L.i;for(;t<e.L.len&&isDigit(e.L.src[t]);)t++;const s=t<e.L.len?e.L.src[t]:"";if(">"===s||"<"===s){for(;e.L.i<t;)advance(e.L);a=mk(e,"file_descriptor",n,e.L.b,[])}}const s=nextToken(e.L,"arg");if("OP"!==s.type)return restoreLex(e.L,t),null;const r=s.value;if("<<<"===r){const n=leaf(e,"<<<",s);skipBlanks(e.L);const t=parseWord(e,"arg"),r=t?t.endIndex:n.endIndex,i=t?[n,t]:[n];return mk(e,"herestring_redirect",a?a.startIndex:n.startIndex,r,a?[a,...i]:i)}if("<<"===r||"<<-"===r){const n=leaf(e,r,s);skipBlanks(e.L);const t=e.L.b;let i=!1,L="";const c=peek(e.L);if("'"===c||'"'===c){for(i=!0,advance(e.L);e.L.i<e.L.len&&peek(e.L)!==c;)L+=peek(e.L),advance(e.L);e.L.i<e.L.len&&advance(e.L)}else if("\\"===c)for(i=!0,advance(e.L),e.L.i<e.L.len&&"\n"!==peek(e.L)&&(L+=peek(e.L),advance(e.L));e.L.i<e.L.len&&isIdentChar(peek(e.L));)L+=peek(e.L),advance(e.L);else for(;e.L.i<e.L.len&&isHeredocDelimChar(peek(e.L));)L+=peek(e.L),advance(e.L);const o=mk(e,"heredoc_start",t,e.L.b,[]);e.L.heredocs.push({delim:L,stripTabs:"<<-"===r,quoted:i,bodyStart:0,bodyEnd:0,endStart:0,endEnd:0});const d=a?[a,n,o]:[n,o],l=a?a.startIndex:n.startIndex;for(;;){skipBlanks(e.L);const n=peek(e.L);if("\n"===n||""===n||e.L.i>=e.L.len)break;if(">"===n||"<"===n||isDigit(n)){const n=saveLex(e.L),t=tryParseRedirect(e);if(t&&"file_redirect"===t.type){d.push(t);continue}restoreLex(e.L,n)}if("|"===n&&"|"!==peek(e.L,1)){advance(e.L),skipBlanks(e.L);const n=[];for(;;){const t=parseCommand(e);if(!t)break;if(n.push(t),skipBlanks(e.L),"|"===peek(e.L)&&"|"!==peek(e.L,1)){const t=e.L.b;advance(e.L),n.push(mk(e,"|",t,e.L.b,[])),skipBlanks(e.L);continue}break}if(n.length>0){const t=n[n.length-1];d.push(mk(e,"pipeline",n[0].startIndex,t.endIndex,n))}continue}if("&"===n&&"&"===peek(e.L,1)||"|"===n&&"|"===peek(e.L,1)){advance(e.L),advance(e.L),skipBlanks(e.L);const n=parseCommand(e);n&&d.push(n);continue}if("&"===n||";"===n||"("===n||")"===n){const n=e.L.b;for(;e.L.i<e.L.len&&"\n"!==peek(e.L);)advance(e.L);d.push(mk(e,"ERROR",n,e.L.b,[]));break}const t=parseWord(e,"arg");if(t){d.push(t);continue}const a=e.L.b;for(;e.L.i<e.L.len&&"\n"!==peek(e.L);)advance(e.L);e.L.b>a&&d.push(mk(e,"ERROR",a,e.L.b,[]));break}return mk(e,"heredoc_redirect",l,e.L.b,d)}if("<&-"===r||">&-"===r){const n=leaf(e,r,s),t=[];a&&t.push(a),t.push(n),skipBlanks(e.L);const i=saveLex(e.L),L=isRedirectLiteralStart(e)?parseWord(e,"arg"):null;L?t.push(L):restoreLex(e.L,i);return mk(e,"file_redirect",a?a.startIndex:n.startIndex,L?L.endIndex:n.endIndex,t)}if(">"===r||">>"===r||">&"===r||">|"===r||"&>"===r||"&>>"===r||"<"===r||"<&"===r){const t=leaf(e,r,s),i=[];a&&i.push(a),i.push(t);let L=t.endIndex,c=0;for(;skipBlanks(e.L),isRedirectLiteralStart(e)&&(n||!(c>=1));){const n=peek(e.L),t=peek(e.L,1);let a=null;if(a="<"!==n&&">"!==n||"("!==t?parseWord(e,"arg"):parseProcessSub(e),!a)break;i.push(a),L=a.endIndex,c++}return mk(e,"file_redirect",a?a.startIndex:t.startIndex,L,i)}return restoreLex(e.L,t),null}function parseProcessSub(e){const n=peek(e.L);if("<"!==n&&">"!==n||"("!==peek(e.L,1))return null;const t=e.L.b;advance(e.L),advance(e.L);const a=mk(e,n+"(",t,e.L.b,[]),s=parseStatements(e,")");let r;if(skipBlanks(e.L),")"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,")",n,e.L.b,[])}else r=mk(e,")",e.L.b,e.L.b,[]);return mk(e,"process_substitution",t,r.endIndex,[a,...s,r])}function scanHeredocBodies(e){for(;e.L.i<e.L.len&&"\n"!==e.L.src[e.L.i];)advance(e.L);e.L.i<e.L.len&&advance(e.L);for(const n of e.L.heredocs){n.bodyStart=e.L.b;const t=n.delim.length;for(;e.L.i<e.L.len;){const a=e.L.i,s=e.L.b;let r=a;if(n.stripTabs)for(;r<e.L.len&&"\t"===e.L.src[r];)r++;if(e.L.src.startsWith(n.delim,r)&&(r+t>=e.L.len||"\n"===e.L.src[r+t]||"\r"===e.L.src[r+t])){for(n.bodyEnd=s;e.L.i<r;)advance(e.L);n.endStart=e.L.b;for(let n=0;n<t;n++)advance(e.L);return n.endEnd=e.L.b,void(e.L.i<e.L.len&&"\n"===e.L.src[e.L.i]&&advance(e.L))}for(;e.L.i<e.L.len&&"\n"!==e.L.src[e.L.i];)advance(e.L);e.L.i<e.L.len&&advance(e.L)}n.bodyEnd=e.L.b,n.endStart=e.L.b,n.endEnd=e.L.b}}function parseWord(e,n){skipBlanks(e.L);const t=[];for(;e.L.i<e.L.len;){const n=peek(e.L);if(" "===n||"\t"===n||"\n"===n||"\r"===n||""===n||"|"===n||"&"===n||";"===n||"("===n||")"===n)break;if("<"===n||">"===n){if("("===peek(e.L,1)){const n=parseProcessSub(e);n&&t.push(n);continue}break}if('"'===n){t.push(parseDoubleQuoted(e));continue}if("'"===n){const n=nextToken(e.L,"arg");t.push(leaf(e,"raw_string",n));continue}if("$"===n){const n=peek(e.L,1);if("'"===n){const n=nextToken(e.L,"arg");t.push(leaf(e,"ansi_c_string",n));continue}if('"'===n){const n={type:"DOLLAR",value:"$",start:e.L.b,end:e.L.b+1};advance(e.L),t.push(leaf(e,"$",n)),t.push(parseDoubleQuoted(e));continue}if("`"===n){advance(e.L);continue}const a=parseDollarLike(e);a&&t.push(a);continue}if("`"===n){if(e.inBacktick>0)break;const n=parseBacktick(e);n&&t.push(n);continue}if("{"===n){const n=tryParseBraceExpr(e);if(n){t.push(n);continue}const a=peek(e.L,1);if(";"===a||"|"===a||"&"===a||"\n"===a||""===a||")"===a||" "===a||"\t"===a){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]));continue}const s=tryParseBraceLikeCat(e);if(s){for(const e of s)t.push(e);continue}}if("}"===n){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]));continue}if("["===n||"]"===n){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]));continue}const a=parseBareWord(e);if(!a)break;if("word"===a.type&&/^-?(0x)?[0-9]+#$/.test(a.text)&&"$"===peek(e.L)&&("{"===peek(e.L,1)||"("===peek(e.L,1))){const n=parseDollarLike(e);if(n){t.push(mk(e,"number",a.startIndex,n.endIndex,[n]));continue}}t.push(a)}if(0===t.length)return null;if(1===t.length)return t[0];const a=t[0],s=t[t.length-1];return mk(e,"concatenation",a.startIndex,s.endIndex,t)}function parseBareWord(e){const n=e.L.b,t=e.L.i;for(;e.L.i<e.L.len;){const n=peek(e.L);if("\\"===n){if(e.L.i+1>=e.L.len)break;const n=e.L.src[e.L.i+1];if("\n"===n||"\r"===n&&"\n"===e.L.src[e.L.i+2])break;advance(e.L),advance(e.L);continue}if(" "===n||"\t"===n||"\n"===n||"\r"===n||""===n||"|"===n||"&"===n||";"===n||"("===n||")"===n||"<"===n||">"===n||'"'===n||"'"===n||"$"===n||"`"===n||"{"===n||"}"===n||"["===n||"]"===n)break;advance(e.L)}if(e.L.b===n)return null;const a=e.src.slice(t,e.L.i);return mk(e,/^-?\d+$/.test(a)?"number":"word",n,e.L.b,[])}function tryParseBraceExpr(e){const n=saveLex(e.L);if("{"!==peek(e.L))return null;const t=e.L.b;advance(e.L);const a=e.L.b,s=e.L.b;for(;isDigit(peek(e.L))||isIdentStart(peek(e.L));)advance(e.L);const r=e.L.b;if(r===s||"."!==peek(e.L)||"."!==peek(e.L,1))return restoreLex(e.L,n),null;const i=e.L.b;advance(e.L),advance(e.L);const L=e.L.b,c=e.L.b;for(;isDigit(peek(e.L))||isIdentStart(peek(e.L));)advance(e.L);const o=e.L.b;if(o===c||"}"!==peek(e.L))return restoreLex(e.L,n),null;const d=e.L.b;advance(e.L);const l=e.L.b,p=sliceBytes(e,s,r),u=sliceBytes(e,c,o),k=/^\d+$/.test(p),f=/^\d+$/.test(u);if(k!==f)return restoreLex(e.L,n),null;if(!k&&(1!==p.length||1!==u.length))return restoreLex(e.L,n),null;const v=k?"number":"word",b=f?"number":"word";return mk(e,"brace_expression",t,l,[mk(e,"{",t,a,[]),mk(e,v,s,r,[]),mk(e,"..",i,L,[]),mk(e,b,c,o,[]),mk(e,"}",d,l,[])])}function tryParseBraceLikeCat(e){if("{"!==peek(e.L))return null;const n=e.L.b;advance(e.L);const t=[mk(e,"word",n,e.L.b,[])];for(;e.L.i<e.L.len;){const n=peek(e.L);if("}"===n||"\n"===n||";"===n||"|"===n||"&"===n||" "===n||"\t"===n||"<"===n||">"===n||"("===n||")"===n)break;if("["===n||"]"===n){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]));continue}const a=e.L.b;for(;e.L.i<e.L.len;){const n=peek(e.L);if("}"===n||"\n"===n||";"===n||"|"===n||"&"===n||" "===n||"\t"===n||"<"===n||">"===n||"("===n||")"===n||"["===n||"]"===n)break;advance(e.L)}const s=e.L.b;if(!(s>a))break;{const n=sliceBytes(e,a,s),r=/^-?\d+$/.test(n)?"number":"word";t.push(mk(e,r,a,s,[]))}}if("}"===peek(e.L)){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]))}return t}function parseDoubleQuoted(e){const n=e.L.b;advance(e.L);const a=e.L.b,s=[mk(e,'"',n,a,[])];let r=e.L.b,i=e.L.i;const flushContent=()=>{if(e.L.b>r){const n=e.src.slice(i,e.L.i);/^[ \t]+$/.test(n)||s.push(mk(e,"string_content",r,e.L.b,[]))}};for(;e.L.i<e.L.len;){const n=peek(e.L);if('"'===n)break;if("\\"===n&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if("\n"!==n){if("$"===n){const n=peek(e.L,1);if("("===n||"{"===n||isIdentStart(n)||t.has(n)||isDigit(n)){flushContent();const n=parseDollarLike(e);n&&s.push(n),r=e.L.b,i=e.L.i;continue}if('"'!==n&&""!==n){flushContent();const n=e.L.b;advance(e.L),s.push(mk(e,"$",n,e.L.b,[])),r=e.L.b,i=e.L.i;continue}}if("`"===n){flushContent();const n=parseBacktick(e);n&&s.push(n),r=e.L.b,i=e.L.i;continue}advance(e.L)}else flushContent(),advance(e.L),r=e.L.b,i=e.L.i}let L;if(flushContent(),'"'===peek(e.L)){const n=e.L.b;advance(e.L),L=mk(e,'"',n,e.L.b,[])}else L=mk(e,'"',e.L.b,e.L.b,[]);return s.push(L),mk(e,"string",n,L.endIndex,s)}function parseDollarLike(e){const n=peek(e.L,1),a=e.L.b;if("("===n&&"("===peek(e.L,2)){advance(e.L),advance(e.L),advance(e.L);const n=mk(e,"$((",a,e.L.b,[]),t=parseArithCommaList(e,"))","var");let s;if(skipBlanks(e.L),")"===peek(e.L)&&")"===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L),s=mk(e,"))",n,e.L.b,[])}else s=mk(e,"))",e.L.b,e.L.b,[]);return mk(e,"arithmetic_expansion",a,s.endIndex,[n,...t,s])}if("["===n){advance(e.L),advance(e.L);const n=mk(e,"$[",a,e.L.b,[]),t=parseArithCommaList(e,"]","var");let s;if(skipBlanks(e.L),"]"===peek(e.L)){const n=e.L.b;advance(e.L),s=mk(e,"]",n,e.L.b,[])}else s=mk(e,"]",e.L.b,e.L.b,[]);return mk(e,"arithmetic_expansion",a,s.endIndex,[n,...t,s])}if("("===n){advance(e.L),advance(e.L);const n=mk(e,"$(",a,e.L.b,[]);let t,s=parseStatements(e,")");if(skipBlanks(e.L),")"===peek(e.L)){const n=e.L.b;advance(e.L),t=mk(e,")",n,e.L.b,[])}else t=mk(e,")",e.L.b,e.L.b,[]);return 1===s.length&&"redirected_statement"===s[0].type&&1===s[0].children.length&&"file_redirect"===s[0].children[0].type&&(s=s[0].children),mk(e,"command_substitution",a,t.endIndex,[n,...s,t])}if("{"===n){advance(e.L),advance(e.L);const n=mk(e,"${",a,e.L.b,[]),s=function(e){const n=[];skipBlanks(e.L);{const t=peek(e.L),a=peek(e.L,1);if("#"===t&&"!"===a&&"}"===peek(e.L,2))return advance(e.L),advance(e.L),n;if("!"===t&&"#"===a){let t=2;if("#"===peek(e.L,t)&&t++," "===peek(e.L,t)&&t++,"}"===peek(e.L,t)){for(;t-- >0;)advance(e.L);return n}}}if("#"===peek(e.L)){const t=e.L.b;advance(e.L),n.push(mk(e,"#",t,e.L.b,[]))}const a=peek(e.L);if(("!"===a||"="===a||"~"===a)&&(isIdentStart(peek(e.L,1))||isDigit(peek(e.L,1)))){const t=e.L.b;advance(e.L),n.push(mk(e,a,t,e.L.b,[]))}if(skipBlanks(e.L),isIdentStart(peek(e.L))){const t=e.L.b;for(;isIdentChar(peek(e.L));)advance(e.L);n.push(mk(e,"variable_name",t,e.L.b,[]))}else if(isDigit(peek(e.L))){const t=e.L.b;for(;isDigit(peek(e.L));)advance(e.L);n.push(mk(e,"variable_name",t,e.L.b,[]))}else if(t.has(peek(e.L))){const t=e.L.b;advance(e.L),n.push(mk(e,"special_variable_name",t,e.L.b,[]))}if("["===peek(e.L)){const t=n[n.length-1],a=e.L.b;advance(e.L);const s=mk(e,"[",a,e.L.b,[]),r=function(e){skipBlanks(e.L);const n=peek(e.L);if(("@"===n||"*"===n)&&"]"===peek(e.L,1)){const n=e.L.b;return advance(e.L),mk(e,"word",n,e.L.b,[])}if("("===n&&"("===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L);const t=mk(e,"((",n,e.L.b,[]),a=parseArithExpr(e,"))","var");let s;if(skipBlanks(e.L),")"===peek(e.L)&&")"===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L),s=mk(e,"))",n,e.L.b,[])}else s=mk(e,"))",e.L.b,e.L.b,[]);const r=a?[t,a,s]:[t,s];return mk(e,"compound_statement",t.startIndex,s.endIndex,r)}return parseArithExpr(e,"]","word")}(e);skipBlanks(e.L);const i=e.L.b;"]"===peek(e.L)&&advance(e.L);const L=mk(e,"]",i,e.L.b,[]);if(t){const a=r?[t,s,r,L]:[t,s,L];n[n.length-1]=mk(e,"subscript",t.startIndex,e.L.b,a)}}skipBlanks(e.L);const s=peek(e.L);if(("*"===s||"@"===s)&&"}"===peek(e.L,1)){const t=e.L.b;return advance(e.L),n.push(mk(e,s,t,e.L.b,[])),n}if("@"===s&&isIdentStart(peek(e.L,1))){const t=e.L.b;for(advance(e.L),n.push(mk(e,"@",t,e.L.b,[]));isIdentChar(peek(e.L));)advance(e.L);return n}const r=peek(e.L);if(":"===r){const t=peek(e.L,1);if("\n"===t||"}"===t){for(advance(e.L);"\n"===peek(e.L);)advance(e.L);return n}if("-"!==t&&"="!==t&&"?"!==t&&"+"!==t){advance(e.L),skipBlanks(e.L);let t;if("-"===peek(e.L)&&isDigit(peek(e.L,1))){const n=e.L.b;for(advance(e.L);isDigit(peek(e.L));)advance(e.L);t=mk(e,"number",n,e.L.b,[])}else t=parseArithExpr(e,":}","var");if(t&&n.push(t),skipBlanks(e.L),":"===peek(e.L)){advance(e.L),skipBlanks(e.L);let t;if("-"===peek(e.L)&&isDigit(peek(e.L,1))){const n=e.L.b;for(advance(e.L);isDigit(peek(e.L));)advance(e.L);t=mk(e,"number",n,e.L.b,[])}else t=parseArithExpr(e,"}","var");t&&n.push(t)}return n}}if(":"===r||"#"===r||"%"===r||"/"===r||"^"===r||","===r||"-"===r||"="===r||"?"===r||"+"===r){const t=e.L.b,a=peek(e.L,1);let s=r;":"!==r||"-"!==a&&"="!==a&&"?"!==a&&"+"!==a?"#"!==r&&"%"!==r&&"/"!==r&&"^"!==r&&","!==r||a!==r?advance(e.L):(advance(e.L),advance(e.L),s=r+r):(advance(e.L),advance(e.L),s=r+a),n.push(mk(e,s,t,e.L.b,[]));const i="#"===s||"##"===s||"%"===s||"%%"===s||"/"===s||"//"===s||"^"===s||"^^"===s||","===s||",,"===s;if("/"===s||"//"===s){const t=peek(e.L);if("#"===t||"%"===t){const a=e.L.b;advance(e.L),n.push(mk(e,t,a,e.L.b,[]))}if('"'===peek(e.L)){n.push(parseDoubleQuoted(e));const t=parseExpansionRest(e,"regex",!0);t&&n.push(t)}else{const t=parseExpansionRest(e,"regex",!0);t&&n.push(t)}if("/"===peek(e.L)){const t=e.L.b;advance(e.L),n.push(mk(e,"/",t,e.L.b,[]));const a=parseExpansionRest(e,"replword",!1);a&&("concatenation"===a.type&&2===a.children.length&&"command_substitution"===a.children[0].type?(n.push(a.children[0]),n.push(a.children[1])):n.push(a))}}else if("#"===s||"##"===s||"%"===s||"%%"===s)for(const t of function(e){const n=[];let t=e.L.b;const flushRegex=()=>{e.L.b>t&&n.push(mk(e,"regex",t,e.L.b,[]))};for(;e.L.i<e.L.len;){const a=peek(e.L);if("}"===a||"\n"===a)break;if("\\"===a&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if('"'!==a){if("'"===a){flushRegex();const a=e.L.b;for(advance(e.L);e.L.i<e.L.len&&"'"!==peek(e.L);)advance(e.L);"'"===peek(e.L)&&advance(e.L),n.push(mk(e,"raw_string",a,e.L.b,[])),t=e.L.b;continue}if("$"===a){const n=peek(e.L,1);if("{"===n){let n=1;for(advance(e.L),advance(e.L);e.L.i<e.L.len&&n>0;){const t=peek(e.L);"{"===t?n++:"}"===t&&n--,advance(e.L)}continue}if("("===n){let n=1;for(advance(e.L),advance(e.L);e.L.i<e.L.len&&n>0;){const t=peek(e.L);"("===t?n++:")"===t&&n--,advance(e.L)}continue}}advance(e.L)}else flushRegex(),n.push(parseDoubleQuoted(e)),t=e.L.b}flushRegex();for(;"\n"===peek(e.L);)advance(e.L);return n}(e))n.push(t);else{const t=parseExpansionRest(e,i?"regex":"word",!1);t&&n.push(t)}}return n}(e);let r;if("}"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,"}",n,e.L.b,[])}else r=mk(e,"}",e.L.b,e.L.b,[]);return mk(e,"expansion",a,r.endIndex,[n,...s,r])}advance(e.L);const s=mk(e,"$",a,e.L.b,[]),r=peek(e.L);if("_"===r&&!isIdentChar(peek(e.L,1))){const n=e.L.b;advance(e.L);const t=mk(e,"special_variable_name",n,e.L.b,[]);return mk(e,"simple_expansion",a,e.L.b,[s,t])}if(isIdentStart(r)){const n=e.L.b;for(;isIdentChar(peek(e.L));)advance(e.L);const t=mk(e,"variable_name",n,e.L.b,[]);return mk(e,"simple_expansion",a,e.L.b,[s,t])}if(isDigit(r)){const n=e.L.b;advance(e.L);const t=mk(e,"variable_name",n,e.L.b,[]);return mk(e,"simple_expansion",a,e.L.b,[s,t])}if(t.has(r)){const n=e.L.b;advance(e.L);const t=mk(e,"special_variable_name",n,e.L.b,[]);return mk(e,"simple_expansion",a,e.L.b,[s,t])}return s}function parseExpansionRest(e,n,a){const s=e.L.b;if("word"===n&&"("===peek(e.L)){advance(e.L);const n=[mk(e,"(",s,e.L.b,[])];for(;e.L.i<e.L.len;){skipBlanks(e.L);const t=peek(e.L);if(")"===t||"}"===t||"\n"===t||""===t)break;const a=e.L.b;for(;e.L.i<e.L.len;){const n=peek(e.L);if(")"===n||"}"===n||" "===n||"\t"===n||"\n"===n||""===n)break;advance(e.L)}if(!(e.L.b>a))break;n.push(mk(e,"word",a,e.L.b,[]))}if(")"===peek(e.L)){const t=e.L.b;advance(e.L),n.push(mk(e,")",t,e.L.b,[]))}for(;"\n"===peek(e.L);)advance(e.L);return mk(e,"array",s,e.L.b,n)}if("regex"===n){let n=0;for(;e.L.i<e.L.len;){const t=peek(e.L);if("\n"===t)break;if(0===n){if("}"===t)break;if(a&&"/"===t)break}if("\\"===t&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if('"'!==t&&"'"!==t){if("$"===t){const n=peek(e.L,1);if("{"===n){let n=0;for(advance(e.L),advance(e.L),n++;e.L.i<e.L.len&&n>0;){const t=peek(e.L);"{"===t?n++:"}"===t&&n--,advance(e.L)}continue}if("("===n){let n=0;for(advance(e.L),advance(e.L),n++;e.L.i<e.L.len&&n>0;){const t=peek(e.L);"("===t?n++:")"===t&&n--,advance(e.L)}continue}}"{"===t?n++:"}"===t&&n>0&&n--,advance(e.L)}else{for(advance(e.L);e.L.i<e.L.len&&peek(e.L)!==t;)"\\"===peek(e.L)&&e.L.i+1<e.L.len&&advance(e.L),advance(e.L);peek(e.L)===t&&advance(e.L)}}const t=e.L.b;for(;"\n"===peek(e.L);)advance(e.L);return t===s?null:mk(e,"regex",s,t,[])}const r=[];let i=e.L.b,L=0;const flushSeg=()=>{e.L.b>i&&r.push(mk(e,"word",i,e.L.b,[]))};for(;e.L.i<e.L.len;){const n=peek(e.L);if("\n"===n)break;if(0===L){if("}"===n)break;if(a&&"/"===n)break}if("\\"===n&&e.L.i+1<e.L.len){advance(e.L),advance(e.L);continue}const s=peek(e.L,1);if("$"===n){if("{"===s||"("===s||"["===s){flushSeg();const n=parseDollarLike(e);n&&r.push(n),i=e.L.b;continue}if("'"===s){flushSeg();const n=e.L.b;for(advance(e.L),advance(e.L);e.L.i<e.L.len&&"'"!==peek(e.L);)"\\"===peek(e.L)&&e.L.i+1<e.L.len&&advance(e.L),advance(e.L);"'"===peek(e.L)&&advance(e.L),r.push(mk(e,"ansi_c_string",n,e.L.b,[])),i=e.L.b;continue}if(isIdentStart(s)||isDigit(s)||t.has(s)){flushSeg();const n=parseDollarLike(e);n&&r.push(n),i=e.L.b;continue}}if('"'!==n){if("'"===n){flushSeg();const n=e.L.b;for(advance(e.L);e.L.i<e.L.len&&"'"!==peek(e.L);)advance(e.L);"'"===peek(e.L)&&advance(e.L),r.push(mk(e,"raw_string",n,e.L.b,[])),i=e.L.b;continue}if(("<"===n||">"===n)&&"("===s){flushSeg();const n=parseProcessSub(e);n&&r.push(n),i=e.L.b;continue}if("`"===n){flushSeg();const n=parseBacktick(e);n&&r.push(n),i=e.L.b;continue}"{"===n?L++:"}"===n&&L>0&&L--,advance(e.L)}else flushSeg(),r.push(parseDoubleQuoted(e)),i=e.L.b}for(flushSeg();"\n"===peek(e.L);)advance(e.L);if(r.length>1&&"word"===r[0].type&&/^[ \t]+$/.test(r[0].text)&&r.shift(),0===r.length)return null;if(1===r.length)return r[0];const c=r[r.length-1];return mk(e,"concatenation",r[0].startIndex,c.endIndex,r)}function parseBacktick(e){const n=e.L.b;advance(e.L);const t=mk(e,"`",n,e.L.b,[]);e.inBacktick++;const a=[];for(;skipBlanks(e.L),"`"!==peek(e.L)&&""!==peek(e.L);){const n=saveLex(e.L),t=nextToken(e.L,"cmd");if("EOF"===t.type||"BACKTICK"===t.type){restoreLex(e.L,n);break}if("NEWLINE"===t.type)continue;restoreLex(e.L,n);const s=parseAndOr(e);if(!s)break;if(a.push(s),skipBlanks(e.L),"`"===peek(e.L))break;const r=saveLex(e.L),i=nextToken(e.L,"cmd");"OP"!==i.type||";"!==i.value&&"&"!==i.value?"NEWLINE"!==i.type&&restoreLex(e.L,r):a.push(leaf(e,i.value,i))}let s;if(e.inBacktick--,"`"===peek(e.L)){const n=e.L.b;advance(e.L),s=mk(e,"`",n,e.L.b,[])}else s=mk(e,"`",e.L.b,e.L.b,[]);return 0===a.length?null:mk(e,"command_substitution",n,s.endIndex,[t,...a,s])}function parseFor(e,n){const t=leaf(e,n.value,n);if(skipBlanks(e.L),"for"===n.value&&"("===peek(e.L)&&"("===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L);const a=[t,mk(e,"((",n,e.L.b,[])];for(let n=0;n<3;n++){skipBlanks(e.L);const t=parseArithCommaList(e,n<2?";":"))","assign");if(a.push(...t),n<2&&";"===peek(e.L)){const n=e.L.b;advance(e.L),a.push(mk(e,";",n,e.L.b,[]))}}if(skipBlanks(e.L),")"===peek(e.L)&&")"===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L),a.push(mk(e,"))",n,e.L.b,[]))}const s=saveLex(e.L),r=nextToken(e.L,"cmd");"OP"===r.type&&";"===r.value?a.push(leaf(e,";",r)):"NEWLINE"!==r.type&&restoreLex(e.L,s);const i=parseDoGroup(e);if(i)a.push(i);else if(skipNewlines(e),skipBlanks(e.L),"{"===peek(e.L)){const n=e.L.b;advance(e.L);const t=mk(e,"{",n,e.L.b,[]),s=parseStatements(e,"}");let r;if("}"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,"}",n,e.L.b,[])}else r=mk(e,"}",e.L.b,e.L.b,[]);a.push(mk(e,"compound_statement",t.startIndex,r.endIndex,[t,...s,r]))}const L=a[a.length-1];return mk(e,"c_style_for_statement",t.startIndex,L.endIndex,a)}const a=[t],s=nextToken(e.L,"arg");a.push(mk(e,"variable_name",s.start,s.end,[])),skipBlanks(e.L);const r=saveLex(e.L),i=nextToken(e.L,"arg");if("WORD"===i.type&&"in"===i.value)for(a.push(leaf(e,"in",i));;){skipBlanks(e.L);const n=peek(e.L);if(";"===n||"\n"===n||""===n)break;const t=parseWord(e);if(!t)break;a.push(t)}else restoreLex(e.L,r);const L=saveLex(e.L),c=nextToken(e.L,"cmd");"OP"===c.type&&";"===c.value?a.push(leaf(e,";",c)):"NEWLINE"!==c.type&&restoreLex(e.L,L);const o=parseDoGroup(e);o&&a.push(o);const d=a[a.length-1];return mk(e,"for_statement",t.startIndex,d.endIndex,a)}function parseDoGroup(e){skipNewlines(e);const n=saveLex(e.L),t=nextToken(e.L,"cmd");if("WORD"!==t.type||"do"!==t.value)return restoreLex(e.L,n),null;const a=leaf(e,"do",t),s=[a,...parseStatements(e,null)];consumeKeyword(e,"done",s);const r=s[s.length-1];return mk(e,"do_group",a.startIndex,r.endIndex,s)}function parseCaseItem(e){skipBlanks(e.L);const n=e.L.b,t=[];if("("===peek(e.L)){const n=e.L.b;advance(e.L),t.push(mk(e,"(",n,e.L.b,[]))}let a=!0;for(;;){skipBlanks(e.L);const n=peek(e.L);if(")"===n||""===n)break;const s=parseCasePattern(e);if(0===s.length)break;if(!a&&s.length>1){const n=s.map(n=>"extglob_pattern"===n.type?mk(e,"word",n.startIndex,n.endIndex,[]):n),a=n[0],r=n[n.length-1];t.push(mk(e,"concatenation",a.startIndex,r.endIndex,n))}else t.push(...s);if(a=!1,skipBlanks(e.L),"\\"===peek(e.L)&&"\n"===peek(e.L,1)&&(advance(e.L),advance(e.L),skipBlanks(e.L)),"|"!==peek(e.L))break;{const n=e.L.b;advance(e.L),t.push(mk(e,"|",n,e.L.b,[])),"\\"===peek(e.L)&&"\n"===peek(e.L,1)&&(advance(e.L),advance(e.L))}}if(")"===peek(e.L)){const n=e.L.b;advance(e.L),t.push(mk(e,")",n,e.L.b,[]))}const s=parseStatements(e,null);t.push(...s);const r=saveLex(e.L),i=nextToken(e.L,"cmd");if("OP"!==i.type||";;"!==i.value&&";&"!==i.value&&";;&"!==i.value?restoreLex(e.L,r):t.push(leaf(e,i.value,i)),0===t.length)return null;if(0===s.length)for(let n=0;n<t.length;n++){const a=t[n];if("extglob_pattern"!==a.type)continue;const s=sliceBytes(e,a.startIndex,a.endIndex);/^[-+?*@!][a-zA-Z]/.test(s)&&!/[*?(]/.test(s)&&(t[n]=mk(e,"word",a.startIndex,a.endIndex,[]))}const L=t[t.length-1];return mk(e,"case_item",n,L.endIndex,t)}function parseCasePattern(e){skipBlanks(e.L);const n=saveLex(e.L),t=e.L.b,a=e.L.i;let s=0,r=!1,i=!1,L=!1;for(;e.L.i<e.L.len;){const n=peek(e.L);if("\\"===n&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if('"'!==n&&"'"!==n)if("("!==n)if(s>0){if(")"===n){s--,advance(e.L);continue}if("\n"===n)break;advance(e.L)}else{if(")"===n||"|"===n||" "===n||"\t"===n||"\n"===n)break;"$"===n&&(r=!0),"["===n&&(i=!0),advance(e.L)}else s++,advance(e.L);else{for(L=!0,advance(e.L);e.L.i<e.L.len&&peek(e.L)!==n;)"\\"===peek(e.L)&&e.L.i+1<e.L.len&&advance(e.L),advance(e.L);peek(e.L)===n&&advance(e.L)}}if(e.L.b===t)return[];const c=e.src.slice(a,e.L.i),o=/[*?+@!]\(/.test(c);if(L&&!o)return restoreLex(e.L,n),function(e){const n=[];let t=e.L.b,a=e.L.i;const flushSeg=()=>{if(e.L.i>a){const s=e.src.slice(a,e.L.i),r=/[*?]/.test(s)?"extglob_pattern":"word";n.push(mk(e,r,t,e.L.b,[]))}};for(;e.L.i<e.L.len;){const s=peek(e.L);if("\\"===s&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if('"'!==s){if("'"===s){flushSeg();const s=nextToken(e.L,"arg");n.push(leaf(e,"raw_string",s)),t=e.L.b,a=e.L.i;continue}if(")"===s||"|"===s||" "===s||"\t"===s||"\n"===s)break;advance(e.L)}else flushSeg(),n.push(parseDoubleQuoted(e)),t=e.L.b,a=e.L.i}return flushSeg(),n}(e);if(!o&&(r||i)){restoreLex(e.L,n);const t=parseWord(e);return t?[t]:[]}return[mk(e,o||/[*?]/.test(c)||/^[-+?*@!][a-zA-Z]/.test(c)?"extglob_pattern":"word",t,e.L.b,[])]}function consumeKeyword(e,n,t){skipNewlines(e);const a=saveLex(e.L),s=nextToken(e.L,"cmd");"WORD"===s.type&&s.value===n?t.push(leaf(e,n,s)):restoreLex(e.L,a)}function parseTestExpr(e,n){return parseTestOr(e,n)}function parseTestOr(e,n){let t=parseTestAnd(e,n);if(!t)return null;for(;;){skipBlanks(e.L);const a=saveLex(e.L);if("|"!==peek(e.L)||"|"!==peek(e.L,1))break;{const s=e.L.b;advance(e.L),advance(e.L);const r=mk(e,"||",s,e.L.b,[]),i=parseTestAnd(e,n);if(!i){restoreLex(e.L,a);break}t=mk(e,"binary_expression",t.startIndex,i.endIndex,[t,r,i])}}return t}function parseTestAnd(e,n){let t=parseTestUnary(e,n);if(!t)return null;for(;skipBlanks(e.L),"&"===peek(e.L)&&"&"===peek(e.L,1);){const a=e.L.b;advance(e.L),advance(e.L);const s=mk(e,"&&",a,e.L.b,[]),r=parseTestUnary(e,n);if(!r)break;t=mk(e,"binary_expression",t.startIndex,r.endIndex,[t,s,r])}return t}function parseTestUnary(e,n){skipBlanks(e.L);if("("===peek(e.L)){const t=e.L.b;advance(e.L);const a=mk(e,"(",t,e.L.b,[]),s=parseTestOr(e,n);let r;if(skipBlanks(e.L),")"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,")",n,e.L.b,[])}else r=mk(e,")",e.L.b,e.L.b,[]);const i=s?[a,s,r]:[a,r];return mk(e,"parenthesized_expression",a.startIndex,r.endIndex,i)}return function(e,n){skipBlanks(e.L);const a=parseTestNegatablePrimary(e,n);if(!a)return null;skipBlanks(e.L);const s=peek(e.L),r=peek(e.L,1);let i=null;const L=e.L.b;if("="===s&&"="===r)advance(e.L),advance(e.L),i=mk(e,"==",L,e.L.b,[]);else if("!"===s&&"="===r)advance(e.L),advance(e.L),i=mk(e,"!=",L,e.L.b,[]);else if("="===s&&"~"===r)advance(e.L),advance(e.L),i=mk(e,"=~",L,e.L.b,[]);else if("="===s&&"="!==r)advance(e.L),i=mk(e,"=",L,e.L.b,[]);else if("<"===s&&"<"!==r)advance(e.L),i=mk(e,"<",L,e.L.b,[]);else if(">"===s&&">"!==r)advance(e.L),i=mk(e,">",L,e.L.b,[]);else if("-"===s&&isIdentStart(r)){for(advance(e.L);isIdentChar(peek(e.L));)advance(e.L);i=mk(e,"test_operator",L,e.L.b,[])}if(!i)return a;if(skipBlanks(e.L),"]]"===n){const n=i.type;if("=~"===n){skipBlanks(e.L);const n=peek(e.L);let t=null;if('"'===n||"'"===n){const a=saveLex(e.L),s='"'===n?parseDoubleQuoted(e):leaf(e,"raw_string",nextToken(e.L,"arg"));let r=e.L.i;for(;r<e.L.len&&(" "===e.src[r]||"\t"===e.src[r]);)r++;const i=e.src[r]??"",L=e.src[r+1]??"";"]"===i&&"]"===L||"&"===i&&"&"===L||"|"===i&&"|"===L||"\n"===i||""===i?t=s:restoreLex(e.L,a)}return t||(t=parseTestRegexRhs(e)),t?mk(e,"binary_expression",a.startIndex,t.endIndex,[a,i,t]):a}if("="===n){const n=parseTestRegexRhs(e);return n?mk(e,"binary_expression",a.startIndex,n.endIndex,[a,i,n]):a}if("=="===n||"!="===n){const n=function(e){skipBlanks(e.L);const n=[];let a=e.L.b,s=e.L.i,r=0;const flushSeg=()=>{if(e.L.i>s){const t=e.src.slice(s,e.L.i),r=/^\d+$/.test(t)?"number":"extglob_pattern";n.push(mk(e,r,a,e.L.b,[]))}};for(;e.L.i<e.L.len;){const i=peek(e.L);if("\\"===i&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else{if("\n"===i)break;if(0===r){if("]"===i&&"]"===peek(e.L,1))break;if(" "===i||"\t"===i){let n=e.L.i;for(;n<e.L.len&&(" "===e.L.src[n]||"\t"===e.L.src[n]);)n++;const t=e.L.src[n]??"",a=e.L.src[n+1]??"";if("]"===t&&"]"===a||"&"===t&&"&"===a||"|"===t&&"|"===a)break;advance(e.L);continue}}if("$"===i){const r=peek(e.L,1);if("("===r||"{"===r||isIdentStart(r)||t.has(r)){flushSeg();const t=parseDollarLike(e);t&&n.push(t),a=e.L.b,s=e.L.i;continue}}if('"'!==i){if("'"===i){flushSeg();const t=nextToken(e.L,"arg");n.push(leaf(e,"raw_string",t)),a=e.L.b,s=e.L.i;continue}"("===i?r++:")"===i&&r>0&&r--,advance(e.L)}else flushSeg(),n.push(parseDoubleQuoted(e)),a=e.L.b,s=e.L.i}}return flushSeg(),n}(e);if(0===n.length)return a;const s=n[n.length-1];return mk(e,"binary_expression",a.startIndex,s.endIndex,[a,i,...n])}}const c=parseTestPrimary(e,n);return c?mk(e,"binary_expression",a.startIndex,c.endIndex,[a,i,c]):a}(e,n)}function parseTestNegatablePrimary(e,n){skipBlanks(e.L);const t=peek(e.L);if("!"===t){const t=e.L.b;advance(e.L);const a=mk(e,"!",t,e.L.b,[]),s=parseTestNegatablePrimary(e,n);return s?mk(e,"unary_expression",a.startIndex,s.endIndex,[a,s]):a}if("-"===t&&isIdentStart(peek(e.L,1))){const t=e.L.b;for(advance(e.L);isIdentChar(peek(e.L));)advance(e.L);const a=mk(e,"test_operator",t,e.L.b,[]);skipBlanks(e.L);const s=parseTestPrimary(e,n);return s?mk(e,"unary_expression",a.startIndex,s.endIndex,[a,s]):a}return parseTestPrimary(e,n)}function parseTestRegexRhs(e){skipBlanks(e.L);const n=e.L.b;let t=0,a=0;for(;e.L.i<e.L.len;){const n=peek(e.L);if("\\"===n&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else{if("\n"===n)break;if(0===t&&0===a){if("]"===n&&"]"===peek(e.L,1))break;if(" "===n||"\t"===n){let n=e.L.i;for(;n<e.L.len&&(" "===e.L.src[n]||"\t"===e.L.src[n]);)n++;const t=e.L.src[n]??"",a=e.L.src[n+1]??"";if("]"===t&&"]"===a||"&"===t&&"&"===a||"|"===t&&"|"===a)break;advance(e.L);continue}}"("===n?t++:")"===n&&t>0?t--:"["===n?a++:"]"===n&&a>0&&a--,advance(e.L)}}return e.L.b===n?null:mk(e,"regex",n,e.L.b,[])}function parseTestPrimary(e,n){return skipBlanks(e.L),"]"===n&&"]"===peek(e.L)||"]]"===n&&"]"===peek(e.L)&&"]"===peek(e.L,1)?null:parseWord(e)}const s={"=":2,"+=":2,"-=":2,"*=":2,"/=":2,"%=":2,"<<=":2,">>=":2,"&=":2,"^=":2,"|=":2,"||":4,"&&":5,"|":6,"^":7,"&":8,"==":9,"!=":9,"<":10,">":10,"<=":10,">=":10,"<<":11,">>":11,"+":12,"-":12,"*":13,"/":13,"%":13,"**":14},r=new Set(["=","+=","-=","*=","/=","%=","<<=",">>=","&=","^=","|=","**"]);function parseArithExpr(e,n,t="var"){return parseArithTernary(e,n,t)}function parseArithCommaList(e,n,t="var"){const a=[];for(;;){const s=parseArithTernary(e,n,t);if(s&&a.push(s),skipBlanks(e.L),","!==peek(e.L)||isArithStop(e,n))break;advance(e.L)}return a}function parseArithTernary(e,n,t){const a=parseArithBinary(e,n,0,t);if(!a)return null;if(skipBlanks(e.L),"?"===peek(e.L)){const s=e.L.b;advance(e.L);const r=mk(e,"?",s,e.L.b,[]),i=parseArithBinary(e,":",0,t);let L;if(skipBlanks(e.L),":"===peek(e.L)){const n=e.L.b;advance(e.L),L=mk(e,":",n,e.L.b,[])}else L=mk(e,":",e.L.b,e.L.b,[]);const c=parseArithTernary(e,n,t),o=c??L,d=[a,r];return i&&d.push(i),d.push(L),c&&d.push(c),mk(e,"ternary_expression",a.startIndex,o.endIndex,d)}return a}function scanArithOp(e){const n=peek(e.L),t=peek(e.L,1),a=peek(e.L,2);return"<"===n&&"<"===t&&"="===a?["<<=",3]:">"===n&&">"===t&&"="===a?[">>=",3]:"*"===n&&"*"===t?["**",2]:"<"===n&&"<"===t?["<<",2]:">"===n&&">"===t?[">>",2]:"="===n&&"="===t?["==",2]:"!"===n&&"="===t?["!=",2]:"<"===n&&"="===t?["<=",2]:">"===n&&"="===t?[">=",2]:"&"===n&&"&"===t?["&&",2]:"|"===n&&"|"===t?["||",2]:"+"===n&&"="===t?["+=",2]:"-"===n&&"="===t?["-=",2]:"*"===n&&"="===t?["*=",2]:"/"===n&&"="===t?["/=",2]:"%"===n&&"="===t?["%=",2]:"&"===n&&"="===t?["&=",2]:"^"===n&&"="===t?["^=",2]:"|"===n&&"="===t?["|=",2]:"+"===n&&"+"!==t?["+",1]:"-"===n&&"-"!==t?["-",1]:"*"===n?["*",1]:"/"===n?["/",1]:"%"===n?["%",1]:"<"===n?["<",1]:">"===n?[">",1]:"&"===n?["&",1]:"|"===n?["|",1]:"^"===n?["^",1]:"="===n?["=",1]:null}function parseArithBinary(e,n,t,a){let i=parseArithUnary(e,n,a);if(!i)return null;for(;skipBlanks(e.L),!isArithStop(e,n)&&","!==peek(e.L);){const L=scanArithOp(e);if(!L)break;const[c,o]=L,d=s[c];if(void 0===d||d<t)break;const l=e.L.b;for(let n=0;n<o;n++)advance(e.L);const p=mk(e,c,l,e.L.b,[]),u=parseArithBinary(e,n,r.has(c)?d:d+1,a);if(!u)break;i=mk(e,"binary_expression",i.startIndex,u.endIndex,[i,p,u])}return i}function parseArithUnary(e,n,t){if(skipBlanks(e.L),isArithStop(e,n))return null;const a=peek(e.L),s=peek(e.L,1);if("+"===a&&"+"===s||"-"===a&&"-"===s){const r=e.L.b;advance(e.L),advance(e.L);const i=mk(e,a+s,r,e.L.b,[]),L=parseArithUnary(e,n,t);return L?mk(e,"unary_expression",i.startIndex,L.endIndex,[i,L]):i}if("-"===a||"+"===a||"!"===a||"~"===a){if("var"!==t&&"-"===a&&isDigit(s)){const n=e.L.b;for(advance(e.L);isDigit(peek(e.L));)advance(e.L);return mk(e,"number",n,e.L.b,[])}const r=e.L.b;advance(e.L);const i=mk(e,a,r,e.L.b,[]),L=parseArithUnary(e,n,t);return L?mk(e,"unary_expression",i.startIndex,L.endIndex,[i,L]):i}return function(e,n,t){const a=function(e,n,t){if(skipBlanks(e.L),isArithStop(e,n))return null;const a=peek(e.L);if("("===a){const n=e.L.b;advance(e.L);const a=mk(e,"(",n,e.L.b,[]),s=parseArithCommaList(e,")",t);let r;if(skipBlanks(e.L),")"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,")",n,e.L.b,[])}else r=mk(e,")",e.L.b,e.L.b,[]);return mk(e,"parenthesized_expression",a.startIndex,r.endIndex,[a,...s,r])}if('"'===a)return parseDoubleQuoted(e);if("$"===a)return parseDollarLike(e);if(isDigit(a)){const n=e.L.b;for(;isDigit(peek(e.L));)advance(e.L);if(e.L.b-n!==1||"0"!==a||"x"!==peek(e.L)&&"X"!==peek(e.L)){if("#"===peek(e.L))for(advance(e.L);isBaseDigit(peek(e.L));)advance(e.L)}else for(advance(e.L);isHexDigit(peek(e.L));)advance(e.L);return mk(e,"number",n,e.L.b,[])}if(isIdentStart(a)){const a=e.L.b;for(;isIdentChar(peek(e.L));)advance(e.L);const s=peek(e.L);if("assign"===t){skipBlanks(e.L);const s=peek(e.L),r=peek(e.L,1);if("="===s&&"="!==r){const s=mk(e,"variable_name",a,e.L.b,[]),r=e.L.b;advance(e.L);const i=mk(e,"=",r,e.L.b,[]),L=parseArithTernary(e,n,t);return mk(e,"variable_assignment",a,L?L.endIndex:i.endIndex,L?[s,i,L]:[s,i])}}if("["===s){const n=mk(e,"variable_name",a,e.L.b,[]),t=e.L.b;advance(e.L);const s=mk(e,"[",t,e.L.b,[]),r=parseArithTernary(e,"]","var")??parseDollarLike(e);let i;if(skipBlanks(e.L),"]"===peek(e.L)){const n=e.L.b;advance(e.L),i=mk(e,"]",n,e.L.b,[])}else i=mk(e,"]",e.L.b,e.L.b,[]);const L=r?[n,s,r,i]:[n,s,i];return mk(e,"subscript",a,i.endIndex,L)}return mk(e,"var"===t?"variable_name":"word",a,e.L.b,[])}return null}(e,n,t);if(!a)return null;const s=peek(e.L),r=peek(e.L,1);if("+"===s&&"+"===r||"-"===s&&"-"===r){const n=e.L.b;advance(e.L),advance(e.L);const t=mk(e,s+r,n,e.L.b,[]);return mk(e,"postfix_expression",a.startIndex,t.endIndex,[a,t])}return a}(e,n,t)}function isArithStop(e,n){const t=peek(e.L);return"))"===n?")"===t&&")"===peek(e.L,1):")"===n?")"===t:";"===n?";"===t:":"===n?":"===t:"]"===n?"]"===t:"}"===n?"}"===t:":}"===n?":"===t||"}"===t:""===t||"\n"===t}
1
+ const e={parse:function(e,n){const t={src:r=e,len:r.length,i:0,b:0,heredocs:[],byteTable:null},a=function(e){let n=0;for(let t=0;t<e.length;t++){const a=e.charCodeAt(t);a<128?n++:a<2048?n+=2:a>=55296&&a<=56319?(n+=4,t++):n+=3}return n}(e),s={L:t,src:e,srcBytes:a,isAscii:a===e.length,nodeCount:0,deadline:performance.now()+(n??50),aborted:!1,inBacktick:0,stopToken:null};var r;try{const e=function(e){const n=[];for(skipBlanks(e.L);;){const n=saveLex(e.L);if("NEWLINE"!==nextToken(e.L,"cmd").type){restoreLex(e.L,n);break}skipBlanks(e.L)}const t=e.L.b;for(;e.L.i<e.L.len;){const t=saveLex(e.L),a=nextToken(e.L,"cmd");if("EOF"===a.type)break;if("NEWLINE"===a.type)continue;if("COMMENT"===a.type){n.push(leaf(e,"comment",a));continue}restoreLex(e.L,t);const s=parseStatements(e,null);for(const e of s)n.push(e);if(0===s.length){const t=nextToken(e.L,"cmd");if("EOF"===t.type)break;if("OP"===t.type&&";;"===t.value&&n.length>0)continue;n.push(mk(e,"ERROR",t.start,t.end,[]))}}return mk(e,"program",t,n.length>0?e.srcBytes:t,n)}(s);return s.aborted?null:e}catch{return null}}},n=Promise.resolve();export function ensureParserInitialized(){return n}export function getParserModule(){return e}const t=new Set(["?","$","@","*","#","-","!","_"]),a=new Set(["export","declare","typeset","readonly","local"]);export const SHELL_KEYWORDS=new Set(["if","then","elif","else","fi","while","until","for","in","do","done","case","esac","function","select"]);function advance(e){const n=e.src.charCodeAt(e.i);e.i++,n<128?e.b++:n<2048?e.b+=2:n>=55296&&n<=56319?(e.b+=4,e.i++):e.b+=3}function peek(e,n=0){return e.i+n<e.len?e.src[e.i+n]:""}function byteAt(e,n){if(e.byteTable)return e.byteTable[n];const t=new Uint32Array(e.len+1);let a=0,s=0;for(;s<e.len;){t[s]=a;const n=e.src.charCodeAt(s);n<128?(a++,s++):n<2048?(a+=2,s++):n>=55296&&n<=56319?(t[s+1]=a+2,a+=4,s+=2):(a+=3,s++)}return t[e.len]=a,e.byteTable=t,t[n]}function isWordChar(e){return e>="a"&&e<="z"||e>="A"&&e<="Z"||e>="0"&&e<="9"||"_"===e||"/"===e||"."===e||"-"===e||"+"===e||":"===e||"@"===e||"%"===e||","===e||"~"===e||"^"===e||"?"===e||"*"===e||"!"===e||"="===e||"["===e||"]"===e}function isIdentStart(e){return e>="a"&&e<="z"||e>="A"&&e<="Z"||"_"===e}function isIdentChar(e){return isIdentStart(e)||e>="0"&&e<="9"}function isDigit(e){return e>="0"&&e<="9"}function isHexDigit(e){return isDigit(e)||e>="a"&&e<="f"||e>="A"&&e<="F"}function isBaseDigit(e){return isIdentChar(e)||"@"===e}function isHeredocDelimChar(e){return""!==e&&" "!==e&&"\t"!==e&&"\n"!==e&&"<"!==e&&">"!==e&&"|"!==e&&"&"!==e&&";"!==e&&"("!==e&&")"!==e&&"'"!==e&&'"'!==e&&"`"!==e&&"\\"!==e}function skipBlanks(e){for(;e.i<e.len;){const n=e.src[e.i];if(" "===n||"\t"===n||"\r"===n)advance(e);else{if("\\"!==n)break;{const n=e.src[e.i+1];if("\n"===n||"\r"===n&&"\n"===e.src[e.i+2])advance(e),advance(e),"\r"===n&&advance(e);else{if(" "!==n&&"\t"!==n)break;advance(e),advance(e)}}}}}function nextToken(e,n="arg"){skipBlanks(e);const t=e.b;if(e.i>=e.len)return{type:"EOF",value:"",start:t,end:t};const a=e.src[e.i],s=peek(e,1),r=peek(e,2);if("\n"===a)return advance(e),{type:"NEWLINE",value:"\n",start:t,end:e.b};if("#"===a){const n=e.i;for(;e.i<e.len&&"\n"!==e.src[e.i];)advance(e);return{type:"COMMENT",value:e.src.slice(n,e.i),start:t,end:e.b}}if("&"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:"&&",start:t,end:e.b};if("|"===a&&"|"===s)return advance(e),advance(e),{type:"OP",value:"||",start:t,end:e.b};if("|"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:"|&",start:t,end:e.b};if(";"===a&&";"===s&&"&"===r)return advance(e),advance(e),advance(e),{type:"OP",value:";;&",start:t,end:e.b};if(";"===a&&";"===s)return advance(e),advance(e),{type:"OP",value:";;",start:t,end:e.b};if(";"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:";&",start:t,end:e.b};if(">"===a&&">"===s)return advance(e),advance(e),{type:"OP",value:">>",start:t,end:e.b};if(">"===a&&"&"===s&&"-"===r)return advance(e),advance(e),advance(e),{type:"OP",value:">&-",start:t,end:e.b};if(">"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:">&",start:t,end:e.b};if(">"===a&&"|"===s)return advance(e),advance(e),{type:"OP",value:">|",start:t,end:e.b};if("&"===a&&">"===s&&">"===r)return advance(e),advance(e),advance(e),{type:"OP",value:"&>>",start:t,end:e.b};if("&"===a&&">"===s)return advance(e),advance(e),{type:"OP",value:"&>",start:t,end:e.b};if("<"===a&&"<"===s&&"<"===r)return advance(e),advance(e),advance(e),{type:"OP",value:"<<<",start:t,end:e.b};if("<"===a&&"<"===s&&"-"===r)return advance(e),advance(e),advance(e),{type:"OP",value:"<<-",start:t,end:e.b};if("<"===a&&"<"===s)return advance(e),advance(e),{type:"OP",value:"<<",start:t,end:e.b};if("<"===a&&"&"===s&&"-"===r)return advance(e),advance(e),advance(e),{type:"OP",value:"<&-",start:t,end:e.b};if("<"===a&&"&"===s)return advance(e),advance(e),{type:"OP",value:"<&",start:t,end:e.b};if("<"===a&&"("===s)return advance(e),advance(e),{type:"LT_PAREN",value:"<(",start:t,end:e.b};if(">"===a&&"("===s)return advance(e),advance(e),{type:"GT_PAREN",value:">(",start:t,end:e.b};if("("===a&&"("===s)return advance(e),advance(e),{type:"OP",value:"((",start:t,end:e.b};if(")"===a&&")"===s)return advance(e),advance(e),{type:"OP",value:"))",start:t,end:e.b};if("|"===a||"&"===a||";"===a||">"===a||"<"===a)return advance(e),{type:"OP",value:a,start:t,end:e.b};if("("===a||")"===a)return advance(e),{type:"OP",value:a,start:t,end:e.b};if("cmd"===n){if("["===a&&"["===s)return advance(e),advance(e),{type:"OP",value:"[[",start:t,end:e.b};if("["===a)return advance(e),{type:"OP",value:"[",start:t,end:e.b};if("{"===a&&(" "===s||"\t"===s||"\n"===s))return advance(e),{type:"OP",value:"{",start:t,end:e.b};if("}"===a)return advance(e),{type:"OP",value:"}",start:t,end:e.b};if("!"===a&&(" "===s||"\t"===s))return advance(e),{type:"OP",value:"!",start:t,end:e.b}}if('"'===a)return advance(e),{type:"DQUOTE",value:'"',start:t,end:e.b};if("'"===a){const n=e.i;for(advance(e);e.i<e.len&&"'"!==e.src[e.i];)advance(e);return e.i<e.len&&advance(e),{type:"SQUOTE",value:e.src.slice(n,e.i),start:t,end:e.b}}if("$"===a){if("("===s&&"("===r)return advance(e),advance(e),advance(e),{type:"DOLLAR_DPAREN",value:"$((",start:t,end:e.b};if("("===s)return advance(e),advance(e),{type:"DOLLAR_PAREN",value:"$(",start:t,end:e.b};if("{"===s)return advance(e),advance(e),{type:"DOLLAR_BRACE",value:"${",start:t,end:e.b};if("'"===s){const n=e.i;for(advance(e),advance(e);e.i<e.len&&"'"!==e.src[e.i];)"\\"===e.src[e.i]&&e.i+1<e.len&&advance(e),advance(e);return e.i<e.len&&advance(e),{type:"ANSI_C",value:e.src.slice(n,e.i),start:t,end:e.b}}return advance(e),{type:"DOLLAR",value:"$",start:t,end:e.b}}if("`"===a)return advance(e),{type:"BACKTICK",value:"`",start:t,end:e.b};if(isDigit(a)){let n=e.i;for(;n<e.len&&isDigit(e.src[n]);)n++;const a=n<e.len?e.src[n]:"";if(">"===a||"<"===a){const a=e.i;for(;e.i<n;)advance(e);return{type:"WORD",value:e.src.slice(a,e.i),start:t,end:e.b}}}if(function(e){return isWordChar(e)||"\\"===e}(a)||"{"===a||"}"===a){const n=e.i;for(;e.i<e.len;){const n=e.src[e.i];if("\\"!==n){if(!isWordChar(n)&&"{"!==n&&"}"!==n)break;advance(e)}else{if(e.i+1>=e.len)break;if("\n"===e.src[e.i+1]){advance(e),advance(e);continue}advance(e),advance(e)}}if(e.i>n){const a=e.src.slice(n,e.i);return/^-?\d+$/.test(a)?{type:"NUMBER",value:a,start:t,end:e.b}:{type:"WORD",value:a,start:t,end:e.b}}}return advance(e),{type:"WORD",value:a,start:t,end:e.b}}function mk(e,n,t,a,s){return function(e){if(e.nodeCount++,e.nodeCount>5e4)throw e.aborted=!0,new Error("budget");if(!(127&e.nodeCount)&&performance.now()>e.deadline)throw e.aborted=!0,new Error("timeout")}(e),{type:n,text:sliceBytes(e,t,a),startIndex:t,endIndex:a,children:s}}function sliceBytes(e,n,t){if(e.isAscii)return e.src.slice(n,t);const a=e.L;a.byteTable||byteAt(a,0);const s=a.byteTable;let r=0,i=e.src.length;for(;r<i;){const e=r+i>>>1;s[e]<n?r=e+1:i=e}const L=r;for(r=L,i=e.src.length;r<i;){const e=r+i>>>1;s[e]<t?r=e+1:i=e}return e.src.slice(L,r)}function leaf(e,n,t){return mk(e,n,t.start,t.end,[])}function saveLex(e){return 65536*e.b+e.i}function restoreLex(e,n){e.i=65535&n,e.b=n>>>16}function parseStatements(e,n){const t=[];for(;;){skipBlanks(e.L);const a=saveLex(e.L),s=nextToken(e.L,"cmd");if("EOF"===s.type){restoreLex(e.L,a);break}if("NEWLINE"===s.type){e.L.heredocs.length>0&&scanHeredocBodies(e);continue}if("COMMENT"===s.type){t.push(leaf(e,"comment",s));continue}if(n&&"OP"===s.type&&s.value===n){restoreLex(e.L,a);break}if("OP"===s.type&&(")"===s.value||"}"===s.value||";;"===s.value||";&"===s.value||";;&"===s.value||"))"===s.value||"]]"===s.value||"]"===s.value)){restoreLex(e.L,a);break}if("BACKTICK"===s.type&&e.inBacktick>0){restoreLex(e.L,a);break}if("WORD"===s.type&&("then"===s.value||"elif"===s.value||"else"===s.value||"fi"===s.value||"do"===s.value||"done"===s.value||"esac"===s.value)){restoreLex(e.L,a);break}restoreLex(e.L,a);const r=parseAndOr(e);if(!r)break;t.push(r),skipBlanks(e.L);const i=saveLex(e.L),L=nextToken(e.L,"cmd");if("OP"!==L.type||";"!==L.value&&"&"!==L.value){if("NEWLINE"===L.type){e.L.heredocs.length>0&&scanHeredocBodies(e);continue}restoreLex(e.L,i)}else{const n=saveLex(e.L),a=nextToken(e.L,"cmd");if(restoreLex(e.L,n),t.push(leaf(e,L.value,L)),"EOF"===a.type||"OP"===a.type&&(")"===a.value||"}"===a.value||";;"===a.value||";&"===a.value||";;&"===a.value)||"WORD"===a.type&&("then"===a.value||"elif"===a.value||"else"===a.value||"fi"===a.value||"do"===a.value||"done"===a.value||"esac"===a.value))continue}}return t}function parseAndOr(e){let n=parsePipeline(e);if(!n)return null;for(;;){const t=saveLex(e.L),a=nextToken(e.L,"cmd");if("OP"!==a.type||"&&"!==a.value&&"||"!==a.value){restoreLex(e.L,t);break}{const t=leaf(e,a.value,a);skipNewlines(e);const s=parsePipeline(e);if(!s){n=mk(e,"list",n.startIndex,t.endIndex,[n,t]);break}if("redirected_statement"===s.type&&s.children.length>=2){const a=s.children[0],r=s.children.slice(1),i=mk(e,"list",n.startIndex,a.endIndex,[n,t,a]),L=r[r.length-1];n=mk(e,"redirected_statement",i.startIndex,L.endIndex,[i,...r])}else n=mk(e,"list",n.startIndex,s.endIndex,[n,t,s])}}return n}function skipNewlines(e){for(;;){const n=saveLex(e.L);if("NEWLINE"!==nextToken(e.L,"cmd").type){restoreLex(e.L,n);break}}}function parsePipeline(e){let n=parseCommand(e);if(!n)return null;const t=[n];for(;;){const a=saveLex(e.L),s=nextToken(e.L,"cmd");if("OP"!==s.type||"|"!==s.value&&"|&"!==s.value){restoreLex(e.L,a);break}{const a=leaf(e,s.value,s);skipNewlines(e);const r=parseCommand(e);if(!r){t.push(a);break}if("redirected_statement"===r.type&&r.children.length>=2&&t.length>=1){const s=r.children[0],i=r.children.slice(1),L=[...t,a,s],c=mk(e,"pipeline",L[0].startIndex,s.endIndex,L),o=i[i.length-1],d=mk(e,"redirected_statement",c.startIndex,o.endIndex,[c,...i]);t.length=0,t.push(d),n=d;continue}t.push(a,r)}}if(1===t.length)return t[0];const a=t[t.length-1];return mk(e,"pipeline",t[0].startIndex,a.endIndex,t)}function parseCommand(e){skipBlanks(e.L);const n=saveLex(e.L),t=nextToken(e.L,"cmd");if("EOF"===t.type)return restoreLex(e.L,n),null;if("OP"===t.type&&"!"===t.value){const a=leaf(e,"!",t),s=parseCommand(e);if(!s)return restoreLex(e.L,n),null;if("redirected_statement"===s.type&&s.children.length>=2){const n=s.children[0],t=s.children.slice(1),r=mk(e,"negated_command",a.startIndex,n.endIndex,[a,n]),i=t[t.length-1];return mk(e,"redirected_statement",r.startIndex,i.endIndex,[r,...t])}return mk(e,"negated_command",a.startIndex,s.endIndex,[a,s])}if("OP"===t.type&&"("===t.value){const n=leaf(e,"(",t),a=parseStatements(e,")"),s=nextToken(e.L,"cmd"),r="OP"===s.type&&")"===s.value?leaf(e,")",s):mk(e,")",n.endIndex,n.endIndex,[]);return maybeRedirect(e,mk(e,"subshell",n.startIndex,r.endIndex,[n,...a,r]))}if("OP"===t.type&&"(("===t.value){const n=leaf(e,"((",t),a=parseArithCommaList(e,"))","var"),s=nextToken(e.L,"cmd"),r="))"===s.value?leaf(e,"))",s):mk(e,"))",n.endIndex,n.endIndex,[]);return mk(e,"compound_statement",n.startIndex,r.endIndex,[n,...a,r])}if("OP"===t.type&&"{"===t.value){const n=leaf(e,"{",t),a=parseStatements(e,"}"),s=nextToken(e.L,"cmd"),r="OP"===s.type&&"}"===s.value?leaf(e,"}",s):mk(e,"}",n.endIndex,n.endIndex,[]);return maybeRedirect(e,mk(e,"compound_statement",n.startIndex,r.endIndex,[n,...a,r]))}if("OP"===t.type&&("["===t.value||"[["===t.value)){const n=leaf(e,t.value,t),a="["===t.value?"]":"]]",s=saveLex(e.L);let r=parseTestExpr(e,a);if(skipBlanks(e.L),"["===t.value&&"]"!==peek(e.L)){restoreLex(e.L,s);const n=e.stopToken;e.stopToken="]";const t=parseCommand(e);e.stopToken=n,t&&"redirected_statement"===t.type?r=t:(restoreLex(e.L,s),r=parseTestExpr(e,a)),skipBlanks(e.L)}const i=nextToken(e.L,"arg");let L;L=i.value===a?leaf(e,a,i):mk(e,a,n.endIndex,n.endIndex,[]);const c=r?[n,r,L]:[n,L];return mk(e,"test_command",n.startIndex,L.endIndex,c)}if("WORD"===t.type){if("if"===t.value)return maybeRedirect(e,function(e,n){const t=leaf(e,"if",n),a=[t],s=parseStatements(e,null);a.push(...s),consumeKeyword(e,"then",a);const r=parseStatements(e,null);for(a.push(...r);;){const n=saveLex(e.L),t=nextToken(e.L,"cmd");if("WORD"===t.type&&"elif"===t.value){const n=leaf(e,"elif",t),s=[n,...parseStatements(e,null)];consumeKeyword(e,"then",s);const r=parseStatements(e,null);s.push(...r);const i=s[s.length-1];a.push(mk(e,"elif_clause",n.startIndex,i.endIndex,s))}else{if("WORD"!==t.type||"else"!==t.value){restoreLex(e.L,n);break}{const n=leaf(e,"else",t),s=parseStatements(e,null),r=s.length>0?s[s.length-1]:n;a.push(mk(e,"else_clause",n.startIndex,r.endIndex,[n,...s]))}}}consumeKeyword(e,"fi",a);const i=a[a.length-1];return mk(e,"if_statement",t.startIndex,i.endIndex,a)}(e,t),!0);if("while"===t.value||"until"===t.value)return maybeRedirect(e,function(e,n){const t=leaf(e,n.value,n),a=[t],s=parseStatements(e,null);a.push(...s);const r=parseDoGroup(e);r&&a.push(r);const i=a[a.length-1];return mk(e,"while_statement",t.startIndex,i.endIndex,a)}(e,t),!0);if("for"===t.value)return maybeRedirect(e,parseFor(e,t),!0);if("select"===t.value)return maybeRedirect(e,parseFor(e,t),!0);if("case"===t.value)return maybeRedirect(e,function(e,n){const t=leaf(e,"case",n),a=[t];skipBlanks(e.L);const s=parseWord(e,"arg");for(s&&a.push(s),skipBlanks(e.L),consumeKeyword(e,"in",a),skipNewlines(e);;){skipBlanks(e.L),skipNewlines(e);const n=saveLex(e.L),t=nextToken(e.L,"arg");if("WORD"===t.type&&"esac"===t.value){a.push(leaf(e,"esac",t));break}if("EOF"===t.type)break;restoreLex(e.L,n);const s=parseCaseItem(e);if(!s)break;a.push(s)}const r=a[a.length-1];return mk(e,"case_statement",t.startIndex,r.endIndex,a)}(e,t),!0);if("function"===t.value)return function(e,n){const t=leaf(e,"function",n);skipBlanks(e.L);const a=nextToken(e.L,"arg"),s=[t,mk(e,"word",a.start,a.end,[])];if(skipBlanks(e.L),"("===peek(e.L)&&")"===peek(e.L,1)){const n=nextToken(e.L,"cmd"),t=nextToken(e.L,"cmd");s.push(leaf(e,"(",n)),s.push(leaf(e,")",t))}skipBlanks(e.L),skipNewlines(e);const r=parseCommand(e);r&&("redirected_statement"===r.type&&r.children.length>=2&&"compound_statement"===r.children[0].type?s.push(...r.children):s.push(r));const i=s[s.length-1];return mk(e,"function_definition",t.startIndex,i.endIndex,s)}(e,t);if(a.has(t.value))return maybeRedirect(e,function(e,n){const t=leaf(e,n.value,n),a=[t];for(;;){skipBlanks(e.L);const n=peek(e.L);if(""===n||"\n"===n||";"===n||"&"===n||"|"===n||")"===n||"<"===n||">"===n)break;const t=tryParseAssignment(e);if(t){a.push(t);continue}if('"'===n||"'"===n||"$"===n){const n=parseWord(e,"arg");if(n){a.push(n);continue}break}const s=saveLex(e.L),r=nextToken(e.L,"arg");if("WORD"!==r.type&&"NUMBER"!==r.type){restoreLex(e.L,s);break}r.value.startsWith("-")?a.push(leaf(e,"word",r)):isIdentStart(r.value[0]??"")?a.push(mk(e,"variable_name",r.start,r.end,[])):a.push(leaf(e,"word",r))}const s=a[a.length-1];return mk(e,"declaration_command",t.startIndex,s.endIndex,a)}(e,t));if("unset"===t.value||"unsetenv"===t.value)return maybeRedirect(e,function(e,n){const t=leaf(e,"unset",n),a=[t];for(;;){skipBlanks(e.L);const n=peek(e.L);if(""===n||"\n"===n||";"===n||"&"===n||"|"===n||")"===n||"<"===n||">"===n)break;const t=parseWord(e,"arg");if(!t)break;"word"===t.type?t.text.startsWith("-")?a.push(t):a.push(mk(e,"variable_name",t.startIndex,t.endIndex,[])):a.push(t)}const s=a[a.length-1];return mk(e,"unset_command",t.startIndex,s.endIndex,a)}(e,t))}return restoreLex(e.L,n),function(e){const n=e.L.b,t=[],a=[];for(;;){skipBlanks(e.L);const n=tryParseAssignment(e);if(n){t.push(n);continue}const s=tryParseRedirect(e);if(!s)break;a.push(s)}skipBlanks(e.L);const s=saveLex(e.L),r=nextToken(e.L,"cmd");if("EOF"===r.type||"NEWLINE"===r.type||"COMMENT"===r.type||"OP"===r.type&&"{"!==r.value&&"["!==r.value&&"[["!==r.value||"WORD"===r.type&&SHELL_KEYWORDS.has(r.value)&&"in"!==r.value){if(restoreLex(e.L,s),1===t.length&&0===a.length)return t[0];if(a.length>0&&0===t.length){const n=a[a.length-1];return mk(e,"redirected_statement",a[0].startIndex,n.endIndex,a)}if(t.length>1&&0===a.length){const n=t[t.length-1];return mk(e,"variable_assignments",t[0].startIndex,n.endIndex,t)}if(t.length>0||a.length>0){const s=[...t,...a];return mk(e,"command",n,s[s.length-1].endIndex,s)}return null}restoreLex(e.L,s);const i=saveLex(e.L),L=parseWord(e,"cmd");if(L&&"word"===L.type&&(skipBlanks(e.L),"("===peek(e.L)&&")"===peek(e.L,1))){const n=nextToken(e.L,"cmd"),t=nextToken(e.L,"cmd"),a=leaf(e,"(",n),s=leaf(e,")",t);skipBlanks(e.L),skipNewlines(e);const r=parseCommand(e);if(r){let n=[r];"redirected_statement"===r.type&&r.children.length>=2&&"compound_statement"===r.children[0].type&&(n=r.children);const t=n[n.length-1];return mk(e,"function_definition",L.startIndex,t.endIndex,[L,a,s,...n])}}restoreLex(e.L,i);const c=parseWord(e,"cmd");if(!c)return 1===t.length?t[0]:null;const o=mk(e,"command_name",c.startIndex,c.endIndex,[c]),d=[],l=[];let p=null;for(;;){skipBlanks(e.L);const n=tryParseRedirect(e,!0);if(n){"heredoc_redirect"===n.type?p=n:"herestring_redirect"===n.type?d.push(n):l.push(n);continue}if(l.length>0)break;if("]"===e.stopToken&&"]"===peek(e.L))break;const t=saveLex(e.L),a=nextToken(e.L,"arg");if("EOF"===a.type||"NEWLINE"===a.type||"COMMENT"===a.type||"OP"===a.type&&("|"===a.value||"|&"===a.value||"&&"===a.value||"||"===a.value||";"===a.value||";;"===a.value||";&"===a.value||";;&"===a.value||"&"===a.value||")"===a.value||"}"===a.value||"))"===a.value)){restoreLex(e.L,t);break}restoreLex(e.L,t);const s=parseWord(e,"arg");if(!s){if("("===peek(e.L)){const n=leaf(e,"(",nextToken(e.L,"cmd")),t=parseStatements(e,")"),a=nextToken(e.L,"cmd"),s="OP"===a.type&&")"===a.value?leaf(e,")",a):mk(e,")",n.endIndex,n.endIndex,[]);d.push(mk(e,"subshell",n.startIndex,s.endIndex,[n,...t,s]));continue}break}("word"!==s.type||"="!==s.text)&&("word"!==s.type&&"concatenation"!==s.type||"("!==peek(e.L)||e.L.b!==s.endIndex)?d.push(s):d.push(mk(e,"ERROR",s.startIndex,s.endIndex,[s]))}const u=[...t,...a,o,...d],k=u.length>0?u[u.length-1].endIndex:o.endIndex,f=mk(e,"command",u[0].startIndex,k,u);if(p){scanHeredocBodies(e);const n=e.L.heredocs.shift();if(n&&p.children.length>=2){const t=mk(e,"heredoc_body",n.bodyStart,n.bodyEnd,n.quoted?[]:function(e,n,t){const a=saveLex(e.L);!function(e,n){e.L.byteTable||byteAt(e.L,0);const t=e.L.byteTable;let a=0,s=e.src.length;for(;a<s;){const e=a+s>>>1;t[e]<n?a=e+1:s=e}e.L.i=a,e.L.b=n}(e,n);const s=[];let r=e.L.b,i=!1;for(;e.L.b<t;){const n=peek(e.L);if("\\"===n){const n=peek(e.L,1);if("$"===n||"`"===n||"\\"===n){advance(e.L),advance(e.L);continue}advance(e.L);continue}if("$"===n||"`"===n){const n=e.L.b,t=parseDollarLike(e);!t||"simple_expansion"!==t.type&&"expansion"!==t.type&&"command_substitution"!==t.type&&"arithmetic_expansion"!==t.type||(i&&n>r&&s.push(mk(e,"heredoc_content",r,n,[])),s.push(t),r=e.L.b,i=!0);continue}advance(e.L)}return i&&s.push(mk(e,"heredoc_content",r,t,[])),restoreLex(e.L,a),s}(e,n.bodyStart,n.bodyEnd)),a=mk(e,"heredoc_end",n.endStart,n.endEnd,[]);p.children.push(t,a),p.endIndex=n.endEnd,p.text=sliceBytes(e,p.startIndex,n.endEnd)}const t=[...a,p,...l];return mk(e,"redirected_statement",a.length>0?Math.min(f.startIndex,a[0].startIndex):f.startIndex,p.endIndex,[f,...t])}if(l.length>0){const n=l[l.length-1];return mk(e,"redirected_statement",f.startIndex,n.endIndex,[f,...l])}return f}(e)}function maybeRedirect(e,n,t=!1){const a=[];for(;;){skipBlanks(e.L);const n=saveLex(e.L),s=tryParseRedirect(e);if(!s)break;if("herestring_redirect"===s.type&&!t){restoreLex(e.L,n);break}a.push(s)}if(0===a.length)return n;const s=a[a.length-1];return mk(e,"redirected_statement",n.startIndex,s.endIndex,[n,...a])}function tryParseAssignment(e){const n=saveLex(e.L);skipBlanks(e.L);const a=e.L.b;if(!isIdentStart(peek(e.L)))return restoreLex(e.L,n),null;for(;isIdentChar(peek(e.L));)advance(e.L);const s=e.L.b;let r=s;if("["===peek(e.L)){advance(e.L);let n=1;for(;e.L.i<e.L.len&&n>0;){const t=peek(e.L);"["===t?n++:"]"===t&&n--,advance(e.L)}r=e.L.b}const i=peek(e.L),L=peek(e.L,1);let c;if("="===i&&"="!==L)c="=";else{if("+"!==i||"="!==L)return restoreLex(e.L,n),null;c="+="}const o=mk(e,"variable_name",a,s,[]);let d=o;if(r>s){const n=mk(e,"[",s,s+1,[]),i=function(e,n,a){const s=sliceBytes(e,n,a);if(/^\d+$/.test(s))return mk(e,"number",n,a,[]);if(/^\$([a-zA-Z_]\w*)$/.exec(s)){const t=mk(e,"$",n,n+1,[]),s=mk(e,"variable_name",n+1,a,[]);return mk(e,"simple_expansion",n,a,[t,s])}if(2===s.length&&"$"===s[0]&&t.has(s[1])){const t=mk(e,"$",n,n+1,[]),s=mk(e,"special_variable_name",n+1,a,[]);return mk(e,"simple_expansion",n,a,[t,s])}return mk(e,"word",n,a,[])}(e,s+1,r-1),L=mk(e,"]",r-1,r,[]);d=mk(e,"subscript",a,r,[o,n,i,L])}const l=e.L.b;advance(e.L),"+="===c&&advance(e.L);const p=e.L.b,u=mk(e,c,l,p,[]);let k=null;if("("===peek(e.L)){const n=leaf(e,"(",nextToken(e.L,"cmd")),t=[n];for(;skipBlanks(e.L),")"!==peek(e.L);){const n=parseWord(e,"arg");if(!n)break;t.push(n)}const a=nextToken(e.L,"cmd"),s=")"===a.value?leaf(e,")",a):mk(e,")",n.endIndex,n.endIndex,[]);t.push(s),k=mk(e,"array",n.startIndex,s.endIndex,t)}else{const n=peek(e.L);n&&" "!==n&&"\t"!==n&&"\n"!==n&&";"!==n&&"&"!==n&&"|"!==n&&")"!==n&&"}"!==n&&(k=parseWord(e,"arg"))}const f=k?[d,u,k]:[d,u];return mk(e,"variable_assignment",a,k?k.endIndex:p,f)}function isRedirectLiteralStart(e){const n=peek(e.L);if(""===n||"\n"===n)return!1;if("|"===n||"&"===n||";"===n||"("===n||")"===n)return!1;if("<"===n||">"===n)return"("===peek(e.L,1);if(isDigit(n)){let n=e.L.i;for(;n<e.L.len&&isDigit(e.L.src[n]);)n++;const t=n<e.L.len?e.L.src[n]:"";if(">"===t||"<"===t)return!1}return"}"!==n&&("]"!==e.stopToken||"]"!==n)}function tryParseRedirect(e,n=!1){const t=saveLex(e.L);skipBlanks(e.L);let a=null;if(isDigit(peek(e.L))){const n=e.L.b;let t=e.L.i;for(;t<e.L.len&&isDigit(e.L.src[t]);)t++;const s=t<e.L.len?e.L.src[t]:"";if(">"===s||"<"===s){for(;e.L.i<t;)advance(e.L);a=mk(e,"file_descriptor",n,e.L.b,[])}}const s=nextToken(e.L,"arg");if("OP"!==s.type)return restoreLex(e.L,t),null;const r=s.value;if("<<<"===r){const n=leaf(e,"<<<",s);skipBlanks(e.L);const t=parseWord(e,"arg"),r=t?t.endIndex:n.endIndex,i=t?[n,t]:[n];return mk(e,"herestring_redirect",a?a.startIndex:n.startIndex,r,a?[a,...i]:i)}if("<<"===r||"<<-"===r){const n=leaf(e,r,s);skipBlanks(e.L);const t=e.L.b;let i=!1,L="";const c=peek(e.L);if("'"===c||'"'===c){for(i=!0,advance(e.L);e.L.i<e.L.len&&peek(e.L)!==c;)L+=peek(e.L),advance(e.L);e.L.i<e.L.len&&advance(e.L)}else if("\\"===c)for(i=!0,advance(e.L),e.L.i<e.L.len&&"\n"!==peek(e.L)&&(L+=peek(e.L),advance(e.L));e.L.i<e.L.len&&isIdentChar(peek(e.L));)L+=peek(e.L),advance(e.L);else for(;e.L.i<e.L.len&&isHeredocDelimChar(peek(e.L));)L+=peek(e.L),advance(e.L);const o=mk(e,"heredoc_start",t,e.L.b,[]);e.L.heredocs.push({delim:L,stripTabs:"<<-"===r,quoted:i,bodyStart:0,bodyEnd:0,endStart:0,endEnd:0});const d=a?[a,n,o]:[n,o],l=a?a.startIndex:n.startIndex;for(;;){skipBlanks(e.L);const n=peek(e.L);if("\n"===n||""===n||e.L.i>=e.L.len)break;if(">"===n||"<"===n||isDigit(n)){const n=saveLex(e.L),t=tryParseRedirect(e);if(t&&"file_redirect"===t.type){d.push(t);continue}restoreLex(e.L,n)}if("|"===n&&"|"!==peek(e.L,1)){advance(e.L),skipBlanks(e.L);const n=[];for(;;){const t=parseCommand(e);if(!t)break;if(n.push(t),skipBlanks(e.L),"|"===peek(e.L)&&"|"!==peek(e.L,1)){const t=e.L.b;advance(e.L),n.push(mk(e,"|",t,e.L.b,[])),skipBlanks(e.L);continue}break}if(n.length>0){const t=n[n.length-1];d.push(mk(e,"pipeline",n[0].startIndex,t.endIndex,n))}continue}if("&"===n&&"&"===peek(e.L,1)||"|"===n&&"|"===peek(e.L,1)){advance(e.L),advance(e.L),skipBlanks(e.L);const n=parseCommand(e);n&&d.push(n);continue}if("&"===n||";"===n||"("===n||")"===n){const n=e.L.b;for(;e.L.i<e.L.len&&"\n"!==peek(e.L);)advance(e.L);d.push(mk(e,"ERROR",n,e.L.b,[]));break}const t=parseWord(e,"arg");if(t){d.push(t);continue}const a=e.L.b;for(;e.L.i<e.L.len&&"\n"!==peek(e.L);)advance(e.L);e.L.b>a&&d.push(mk(e,"ERROR",a,e.L.b,[]));break}return mk(e,"heredoc_redirect",l,e.L.b,d)}if("<&-"===r||">&-"===r){const n=leaf(e,r,s),t=[];a&&t.push(a),t.push(n),skipBlanks(e.L);const i=saveLex(e.L),L=isRedirectLiteralStart(e)?parseWord(e,"arg"):null;return L?t.push(L):restoreLex(e.L,i),mk(e,"file_redirect",a?a.startIndex:n.startIndex,L?L.endIndex:n.endIndex,t)}if(">"===r||">>"===r||">&"===r||">|"===r||"&>"===r||"&>>"===r||"<"===r||"<&"===r){const t=leaf(e,r,s),i=[];a&&i.push(a),i.push(t);let L=t.endIndex,c=0;for(;skipBlanks(e.L),isRedirectLiteralStart(e)&&(n||!(c>=1));){const n=peek(e.L),t=peek(e.L,1);let a=null;if(a="<"!==n&&">"!==n||"("!==t?parseWord(e,"arg"):parseProcessSub(e),!a)break;i.push(a),L=a.endIndex,c++}return mk(e,"file_redirect",a?a.startIndex:t.startIndex,L,i)}return restoreLex(e.L,t),null}function parseProcessSub(e){const n=peek(e.L);if("<"!==n&&">"!==n||"("!==peek(e.L,1))return null;const t=e.L.b;advance(e.L),advance(e.L);const a=mk(e,n+"(",t,e.L.b,[]),s=parseStatements(e,")");let r;if(skipBlanks(e.L),")"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,")",n,e.L.b,[])}else r=mk(e,")",e.L.b,e.L.b,[]);return mk(e,"process_substitution",t,r.endIndex,[a,...s,r])}function scanHeredocBodies(e){for(;e.L.i<e.L.len&&"\n"!==e.L.src[e.L.i];)advance(e.L);e.L.i<e.L.len&&advance(e.L);for(const n of e.L.heredocs){n.bodyStart=e.L.b;const t=n.delim.length;for(;e.L.i<e.L.len;){const a=e.L.i,s=e.L.b;let r=a;if(n.stripTabs)for(;r<e.L.len&&"\t"===e.L.src[r];)r++;if(e.L.src.startsWith(n.delim,r)&&(r+t>=e.L.len||"\n"===e.L.src[r+t]||"\r"===e.L.src[r+t])){for(n.bodyEnd=s;e.L.i<r;)advance(e.L);n.endStart=e.L.b;for(let n=0;n<t;n++)advance(e.L);return n.endEnd=e.L.b,void(e.L.i<e.L.len&&"\n"===e.L.src[e.L.i]&&advance(e.L))}for(;e.L.i<e.L.len&&"\n"!==e.L.src[e.L.i];)advance(e.L);e.L.i<e.L.len&&advance(e.L)}n.bodyEnd=e.L.b,n.endStart=e.L.b,n.endEnd=e.L.b}}function parseWord(e,n){skipBlanks(e.L);const t=[];for(;e.L.i<e.L.len;){const n=peek(e.L);if(" "===n||"\t"===n||"\n"===n||"\r"===n||""===n||"|"===n||"&"===n||";"===n||"("===n||")"===n)break;if("<"===n||">"===n){if("("===peek(e.L,1)){const n=parseProcessSub(e);n&&t.push(n);continue}break}if('"'===n){t.push(parseDoubleQuoted(e));continue}if("'"===n){const n=nextToken(e.L,"arg");t.push(leaf(e,"raw_string",n));continue}if("$"===n){const n=peek(e.L,1);if("'"===n){const n=nextToken(e.L,"arg");t.push(leaf(e,"ansi_c_string",n));continue}if('"'===n){const n={type:"DOLLAR",value:"$",start:e.L.b,end:e.L.b+1};advance(e.L),t.push(leaf(e,"$",n)),t.push(parseDoubleQuoted(e));continue}if("`"===n){advance(e.L);continue}const a=parseDollarLike(e);a&&t.push(a);continue}if("`"===n){if(e.inBacktick>0)break;const n=parseBacktick(e);n&&t.push(n);continue}if("{"===n){const n=tryParseBraceExpr(e);if(n){t.push(n);continue}const a=peek(e.L,1);if(";"===a||"|"===a||"&"===a||"\n"===a||""===a||")"===a||" "===a||"\t"===a){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]));continue}const s=tryParseBraceLikeCat(e);if(s){for(const e of s)t.push(e);continue}}if("}"===n){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]));continue}if("["===n||"]"===n){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]));continue}const a=parseBareWord(e);if(!a)break;if("word"===a.type&&/^-?(0x)?[0-9]+#$/.test(a.text)&&"$"===peek(e.L)&&("{"===peek(e.L,1)||"("===peek(e.L,1))){const n=parseDollarLike(e);if(n){t.push(mk(e,"number",a.startIndex,n.endIndex,[n]));continue}}t.push(a)}if(0===t.length)return null;if(1===t.length)return t[0];const a=t[0],s=t[t.length-1];return mk(e,"concatenation",a.startIndex,s.endIndex,t)}function parseBareWord(e){const n=e.L.b,t=e.L.i;for(;e.L.i<e.L.len;){const n=peek(e.L);if("\\"===n){if(e.L.i+1>=e.L.len)break;const n=e.L.src[e.L.i+1];if("\n"===n||"\r"===n&&"\n"===e.L.src[e.L.i+2])break;advance(e.L),advance(e.L);continue}if(" "===n||"\t"===n||"\n"===n||"\r"===n||""===n||"|"===n||"&"===n||";"===n||"("===n||")"===n||"<"===n||">"===n||'"'===n||"'"===n||"$"===n||"`"===n||"{"===n||"}"===n||"["===n||"]"===n)break;advance(e.L)}if(e.L.b===n)return null;const a=e.src.slice(t,e.L.i);return mk(e,/^-?\d+$/.test(a)?"number":"word",n,e.L.b,[])}function tryParseBraceExpr(e){const n=saveLex(e.L);if("{"!==peek(e.L))return null;const t=e.L.b;advance(e.L);const a=e.L.b,s=e.L.b;for(;isDigit(peek(e.L))||isIdentStart(peek(e.L));)advance(e.L);const r=e.L.b;if(r===s||"."!==peek(e.L)||"."!==peek(e.L,1))return restoreLex(e.L,n),null;const i=e.L.b;advance(e.L),advance(e.L);const L=e.L.b,c=e.L.b;for(;isDigit(peek(e.L))||isIdentStart(peek(e.L));)advance(e.L);const o=e.L.b;if(o===c||"}"!==peek(e.L))return restoreLex(e.L,n),null;const d=e.L.b;advance(e.L);const l=e.L.b,p=sliceBytes(e,s,r),u=sliceBytes(e,c,o),k=/^\d+$/.test(p),f=/^\d+$/.test(u);if(k!==f)return restoreLex(e.L,n),null;if(!k&&(1!==p.length||1!==u.length))return restoreLex(e.L,n),null;const v=k?"number":"word",b=f?"number":"word";return mk(e,"brace_expression",t,l,[mk(e,"{",t,a,[]),mk(e,v,s,r,[]),mk(e,"..",i,L,[]),mk(e,b,c,o,[]),mk(e,"}",d,l,[])])}function tryParseBraceLikeCat(e){if("{"!==peek(e.L))return null;const n=e.L.b;advance(e.L);const t=[mk(e,"word",n,e.L.b,[])];for(;e.L.i<e.L.len;){const n=peek(e.L);if("}"===n||"\n"===n||";"===n||"|"===n||"&"===n||" "===n||"\t"===n||"<"===n||">"===n||"("===n||")"===n)break;if("["===n||"]"===n){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]));continue}const a=e.L.b;for(;e.L.i<e.L.len;){const n=peek(e.L);if("}"===n||"\n"===n||";"===n||"|"===n||"&"===n||" "===n||"\t"===n||"<"===n||">"===n||"("===n||")"===n||"["===n||"]"===n)break;advance(e.L)}const s=e.L.b;if(!(s>a))break;{const n=sliceBytes(e,a,s),r=/^-?\d+$/.test(n)?"number":"word";t.push(mk(e,r,a,s,[]))}}if("}"===peek(e.L)){const n=e.L.b;advance(e.L),t.push(mk(e,"word",n,e.L.b,[]))}return t}function parseDoubleQuoted(e){const n=e.L.b;advance(e.L);const a=e.L.b,s=[mk(e,'"',n,a,[])];let r=e.L.b,i=e.L.i;const flushContent=()=>{if(e.L.b>r){const n=e.src.slice(i,e.L.i);/^[ \t]+$/.test(n)||s.push(mk(e,"string_content",r,e.L.b,[]))}};for(;e.L.i<e.L.len;){const n=peek(e.L);if('"'===n)break;if("\\"===n&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if("\n"!==n){if("$"===n){const n=peek(e.L,1);if("("===n||"{"===n||isIdentStart(n)||t.has(n)||isDigit(n)){flushContent();const n=parseDollarLike(e);n&&s.push(n),r=e.L.b,i=e.L.i;continue}if('"'!==n&&""!==n){flushContent();const n=e.L.b;advance(e.L),s.push(mk(e,"$",n,e.L.b,[])),r=e.L.b,i=e.L.i;continue}}if("`"===n){flushContent();const n=parseBacktick(e);n&&s.push(n),r=e.L.b,i=e.L.i;continue}advance(e.L)}else flushContent(),advance(e.L),r=e.L.b,i=e.L.i}let L;if(flushContent(),'"'===peek(e.L)){const n=e.L.b;advance(e.L),L=mk(e,'"',n,e.L.b,[])}else L=mk(e,'"',e.L.b,e.L.b,[]);return s.push(L),mk(e,"string",n,L.endIndex,s)}function parseDollarLike(e){const n=peek(e.L,1),a=e.L.b;if("("===n&&"("===peek(e.L,2)){advance(e.L),advance(e.L),advance(e.L);const n=mk(e,"$((",a,e.L.b,[]),t=parseArithCommaList(e,"))","var");let s;if(skipBlanks(e.L),")"===peek(e.L)&&")"===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L),s=mk(e,"))",n,e.L.b,[])}else s=mk(e,"))",e.L.b,e.L.b,[]);return mk(e,"arithmetic_expansion",a,s.endIndex,[n,...t,s])}if("["===n){advance(e.L),advance(e.L);const n=mk(e,"$[",a,e.L.b,[]),t=parseArithCommaList(e,"]","var");let s;if(skipBlanks(e.L),"]"===peek(e.L)){const n=e.L.b;advance(e.L),s=mk(e,"]",n,e.L.b,[])}else s=mk(e,"]",e.L.b,e.L.b,[]);return mk(e,"arithmetic_expansion",a,s.endIndex,[n,...t,s])}if("("===n){advance(e.L),advance(e.L);const n=mk(e,"$(",a,e.L.b,[]);let t,s=parseStatements(e,")");if(skipBlanks(e.L),")"===peek(e.L)){const n=e.L.b;advance(e.L),t=mk(e,")",n,e.L.b,[])}else t=mk(e,")",e.L.b,e.L.b,[]);return 1===s.length&&"redirected_statement"===s[0].type&&1===s[0].children.length&&"file_redirect"===s[0].children[0].type&&(s=s[0].children),mk(e,"command_substitution",a,t.endIndex,[n,...s,t])}if("{"===n){advance(e.L),advance(e.L);const n=mk(e,"${",a,e.L.b,[]),s=function(e){const n=[];skipBlanks(e.L);{const t=peek(e.L),a=peek(e.L,1);if("#"===t&&"!"===a&&"}"===peek(e.L,2))return advance(e.L),advance(e.L),n;if("!"===t&&"#"===a){let t=2;if("#"===peek(e.L,t)&&t++," "===peek(e.L,t)&&t++,"}"===peek(e.L,t)){for(;t-- >0;)advance(e.L);return n}}}if("#"===peek(e.L)){const t=e.L.b;advance(e.L),n.push(mk(e,"#",t,e.L.b,[]))}const a=peek(e.L);if(("!"===a||"="===a||"~"===a)&&(isIdentStart(peek(e.L,1))||isDigit(peek(e.L,1)))){const t=e.L.b;advance(e.L),n.push(mk(e,a,t,e.L.b,[]))}if(skipBlanks(e.L),isIdentStart(peek(e.L))){const t=e.L.b;for(;isIdentChar(peek(e.L));)advance(e.L);n.push(mk(e,"variable_name",t,e.L.b,[]))}else if(isDigit(peek(e.L))){const t=e.L.b;for(;isDigit(peek(e.L));)advance(e.L);n.push(mk(e,"variable_name",t,e.L.b,[]))}else if(t.has(peek(e.L))){const t=e.L.b;advance(e.L),n.push(mk(e,"special_variable_name",t,e.L.b,[]))}if("["===peek(e.L)){const t=n[n.length-1],a=e.L.b;advance(e.L);const s=mk(e,"[",a,e.L.b,[]),r=function(e){skipBlanks(e.L);const n=peek(e.L);if(("@"===n||"*"===n)&&"]"===peek(e.L,1)){const n=e.L.b;return advance(e.L),mk(e,"word",n,e.L.b,[])}if("("===n&&"("===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L);const t=mk(e,"((",n,e.L.b,[]),a=parseArithExpr(e,"))","var");let s;if(skipBlanks(e.L),")"===peek(e.L)&&")"===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L),s=mk(e,"))",n,e.L.b,[])}else s=mk(e,"))",e.L.b,e.L.b,[]);const r=a?[t,a,s]:[t,s];return mk(e,"compound_statement",t.startIndex,s.endIndex,r)}return parseArithExpr(e,"]","word")}(e);skipBlanks(e.L);const i=e.L.b;"]"===peek(e.L)&&advance(e.L);const L=mk(e,"]",i,e.L.b,[]);if(t){const a=r?[t,s,r,L]:[t,s,L];n[n.length-1]=mk(e,"subscript",t.startIndex,e.L.b,a)}}skipBlanks(e.L);const s=peek(e.L);if(("*"===s||"@"===s)&&"}"===peek(e.L,1)){const t=e.L.b;return advance(e.L),n.push(mk(e,s,t,e.L.b,[])),n}if("@"===s&&isIdentStart(peek(e.L,1))){const t=e.L.b;for(advance(e.L),n.push(mk(e,"@",t,e.L.b,[]));isIdentChar(peek(e.L));)advance(e.L);return n}const r=peek(e.L);if(":"===r){const t=peek(e.L,1);if("\n"===t||"}"===t){for(advance(e.L);"\n"===peek(e.L);)advance(e.L);return n}if("-"!==t&&"="!==t&&"?"!==t&&"+"!==t){let t;if(advance(e.L),skipBlanks(e.L),"-"===peek(e.L)&&isDigit(peek(e.L,1))){const n=e.L.b;for(advance(e.L);isDigit(peek(e.L));)advance(e.L);t=mk(e,"number",n,e.L.b,[])}else t=parseArithExpr(e,":}","var");if(t&&n.push(t),skipBlanks(e.L),":"===peek(e.L)){let t;if(advance(e.L),skipBlanks(e.L),"-"===peek(e.L)&&isDigit(peek(e.L,1))){const n=e.L.b;for(advance(e.L);isDigit(peek(e.L));)advance(e.L);t=mk(e,"number",n,e.L.b,[])}else t=parseArithExpr(e,"}","var");t&&n.push(t)}return n}}if(":"===r||"#"===r||"%"===r||"/"===r||"^"===r||","===r||"-"===r||"="===r||"?"===r||"+"===r){const t=e.L.b,a=peek(e.L,1);let s=r;":"!==r||"-"!==a&&"="!==a&&"?"!==a&&"+"!==a?"#"!==r&&"%"!==r&&"/"!==r&&"^"!==r&&","!==r||a!==r?advance(e.L):(advance(e.L),advance(e.L),s=r+r):(advance(e.L),advance(e.L),s=r+a),n.push(mk(e,s,t,e.L.b,[]));const i="#"===s||"##"===s||"%"===s||"%%"===s||"/"===s||"//"===s||"^"===s||"^^"===s||","===s||",,"===s;if("/"===s||"//"===s){const t=peek(e.L);if("#"===t||"%"===t){const a=e.L.b;advance(e.L),n.push(mk(e,t,a,e.L.b,[]))}if('"'===peek(e.L)){n.push(parseDoubleQuoted(e));const t=parseExpansionRest(e,"regex",!0);t&&n.push(t)}else{const t=parseExpansionRest(e,"regex",!0);t&&n.push(t)}if("/"===peek(e.L)){const t=e.L.b;advance(e.L),n.push(mk(e,"/",t,e.L.b,[]));const a=parseExpansionRest(e,"replword",!1);a&&("concatenation"===a.type&&2===a.children.length&&"command_substitution"===a.children[0].type?(n.push(a.children[0]),n.push(a.children[1])):n.push(a))}}else if("#"===s||"##"===s||"%"===s||"%%"===s)for(const t of function(e){const n=[];let t=e.L.b;const flushRegex=()=>{e.L.b>t&&n.push(mk(e,"regex",t,e.L.b,[]))};for(;e.L.i<e.L.len;){const a=peek(e.L);if("}"===a||"\n"===a)break;if("\\"===a&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if('"'!==a){if("'"===a){flushRegex();const a=e.L.b;for(advance(e.L);e.L.i<e.L.len&&"'"!==peek(e.L);)advance(e.L);"'"===peek(e.L)&&advance(e.L),n.push(mk(e,"raw_string",a,e.L.b,[])),t=e.L.b;continue}if("$"===a){const n=peek(e.L,1);if("{"===n){let n=1;for(advance(e.L),advance(e.L);e.L.i<e.L.len&&n>0;){const t=peek(e.L);"{"===t?n++:"}"===t&&n--,advance(e.L)}continue}if("("===n){let n=1;for(advance(e.L),advance(e.L);e.L.i<e.L.len&&n>0;){const t=peek(e.L);"("===t?n++:")"===t&&n--,advance(e.L)}continue}}advance(e.L)}else flushRegex(),n.push(parseDoubleQuoted(e)),t=e.L.b}for(flushRegex();"\n"===peek(e.L);)advance(e.L);return n}(e))n.push(t);else{const t=parseExpansionRest(e,i?"regex":"word",!1);t&&n.push(t)}}return n}(e);let r;if("}"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,"}",n,e.L.b,[])}else r=mk(e,"}",e.L.b,e.L.b,[]);return mk(e,"expansion",a,r.endIndex,[n,...s,r])}advance(e.L);const s=mk(e,"$",a,e.L.b,[]),r=peek(e.L);if("_"===r&&!isIdentChar(peek(e.L,1))){const n=e.L.b;advance(e.L);const t=mk(e,"special_variable_name",n,e.L.b,[]);return mk(e,"simple_expansion",a,e.L.b,[s,t])}if(isIdentStart(r)){const n=e.L.b;for(;isIdentChar(peek(e.L));)advance(e.L);const t=mk(e,"variable_name",n,e.L.b,[]);return mk(e,"simple_expansion",a,e.L.b,[s,t])}if(isDigit(r)){const n=e.L.b;advance(e.L);const t=mk(e,"variable_name",n,e.L.b,[]);return mk(e,"simple_expansion",a,e.L.b,[s,t])}if(t.has(r)){const n=e.L.b;advance(e.L);const t=mk(e,"special_variable_name",n,e.L.b,[]);return mk(e,"simple_expansion",a,e.L.b,[s,t])}return s}function parseExpansionRest(e,n,a){const s=e.L.b;if("word"===n&&"("===peek(e.L)){advance(e.L);const n=[mk(e,"(",s,e.L.b,[])];for(;e.L.i<e.L.len;){skipBlanks(e.L);const t=peek(e.L);if(")"===t||"}"===t||"\n"===t||""===t)break;const a=e.L.b;for(;e.L.i<e.L.len;){const n=peek(e.L);if(")"===n||"}"===n||" "===n||"\t"===n||"\n"===n||""===n)break;advance(e.L)}if(!(e.L.b>a))break;n.push(mk(e,"word",a,e.L.b,[]))}if(")"===peek(e.L)){const t=e.L.b;advance(e.L),n.push(mk(e,")",t,e.L.b,[]))}for(;"\n"===peek(e.L);)advance(e.L);return mk(e,"array",s,e.L.b,n)}if("regex"===n){let n=0;for(;e.L.i<e.L.len;){const t=peek(e.L);if("\n"===t)break;if(0===n){if("}"===t)break;if(a&&"/"===t)break}if("\\"===t&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if('"'!==t&&"'"!==t){if("$"===t){const n=peek(e.L,1);if("{"===n){let n=0;for(advance(e.L),advance(e.L),n++;e.L.i<e.L.len&&n>0;){const t=peek(e.L);"{"===t?n++:"}"===t&&n--,advance(e.L)}continue}if("("===n){let n=0;for(advance(e.L),advance(e.L),n++;e.L.i<e.L.len&&n>0;){const t=peek(e.L);"("===t?n++:")"===t&&n--,advance(e.L)}continue}}"{"===t?n++:"}"===t&&n>0&&n--,advance(e.L)}else{for(advance(e.L);e.L.i<e.L.len&&peek(e.L)!==t;)"\\"===peek(e.L)&&e.L.i+1<e.L.len&&advance(e.L),advance(e.L);peek(e.L)===t&&advance(e.L)}}const t=e.L.b;for(;"\n"===peek(e.L);)advance(e.L);return t===s?null:mk(e,"regex",s,t,[])}const r=[];let i=e.L.b,L=0;const flushSeg=()=>{e.L.b>i&&r.push(mk(e,"word",i,e.L.b,[]))};for(;e.L.i<e.L.len;){const n=peek(e.L);if("\n"===n)break;if(0===L){if("}"===n)break;if(a&&"/"===n)break}if("\\"===n&&e.L.i+1<e.L.len){advance(e.L),advance(e.L);continue}const s=peek(e.L,1);if("$"===n){if("{"===s||"("===s||"["===s){flushSeg();const n=parseDollarLike(e);n&&r.push(n),i=e.L.b;continue}if("'"===s){flushSeg();const n=e.L.b;for(advance(e.L),advance(e.L);e.L.i<e.L.len&&"'"!==peek(e.L);)"\\"===peek(e.L)&&e.L.i+1<e.L.len&&advance(e.L),advance(e.L);"'"===peek(e.L)&&advance(e.L),r.push(mk(e,"ansi_c_string",n,e.L.b,[])),i=e.L.b;continue}if(isIdentStart(s)||isDigit(s)||t.has(s)){flushSeg();const n=parseDollarLike(e);n&&r.push(n),i=e.L.b;continue}}if('"'!==n){if("'"===n){flushSeg();const n=e.L.b;for(advance(e.L);e.L.i<e.L.len&&"'"!==peek(e.L);)advance(e.L);"'"===peek(e.L)&&advance(e.L),r.push(mk(e,"raw_string",n,e.L.b,[])),i=e.L.b;continue}if(("<"===n||">"===n)&&"("===s){flushSeg();const n=parseProcessSub(e);n&&r.push(n),i=e.L.b;continue}if("`"===n){flushSeg();const n=parseBacktick(e);n&&r.push(n),i=e.L.b;continue}"{"===n?L++:"}"===n&&L>0&&L--,advance(e.L)}else flushSeg(),r.push(parseDoubleQuoted(e)),i=e.L.b}for(flushSeg();"\n"===peek(e.L);)advance(e.L);if(r.length>1&&"word"===r[0].type&&/^[ \t]+$/.test(r[0].text)&&r.shift(),0===r.length)return null;if(1===r.length)return r[0];const c=r[r.length-1];return mk(e,"concatenation",r[0].startIndex,c.endIndex,r)}function parseBacktick(e){const n=e.L.b;advance(e.L);const t=mk(e,"`",n,e.L.b,[]);e.inBacktick++;const a=[];for(;skipBlanks(e.L),"`"!==peek(e.L)&&""!==peek(e.L);){const n=saveLex(e.L),t=nextToken(e.L,"cmd");if("EOF"===t.type||"BACKTICK"===t.type){restoreLex(e.L,n);break}if("NEWLINE"===t.type)continue;restoreLex(e.L,n);const s=parseAndOr(e);if(!s)break;if(a.push(s),skipBlanks(e.L),"`"===peek(e.L))break;const r=saveLex(e.L),i=nextToken(e.L,"cmd");"OP"!==i.type||";"!==i.value&&"&"!==i.value?"NEWLINE"!==i.type&&restoreLex(e.L,r):a.push(leaf(e,i.value,i))}let s;if(e.inBacktick--,"`"===peek(e.L)){const n=e.L.b;advance(e.L),s=mk(e,"`",n,e.L.b,[])}else s=mk(e,"`",e.L.b,e.L.b,[]);return 0===a.length?null:mk(e,"command_substitution",n,s.endIndex,[t,...a,s])}function parseFor(e,n){const t=leaf(e,n.value,n);if(skipBlanks(e.L),"for"===n.value&&"("===peek(e.L)&&"("===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L);const a=[t,mk(e,"((",n,e.L.b,[])];for(let n=0;n<3;n++){skipBlanks(e.L);const t=parseArithCommaList(e,n<2?";":"))","assign");if(a.push(...t),n<2&&";"===peek(e.L)){const n=e.L.b;advance(e.L),a.push(mk(e,";",n,e.L.b,[]))}}if(skipBlanks(e.L),")"===peek(e.L)&&")"===peek(e.L,1)){const n=e.L.b;advance(e.L),advance(e.L),a.push(mk(e,"))",n,e.L.b,[]))}const s=saveLex(e.L),r=nextToken(e.L,"cmd");"OP"===r.type&&";"===r.value?a.push(leaf(e,";",r)):"NEWLINE"!==r.type&&restoreLex(e.L,s);const i=parseDoGroup(e);if(i)a.push(i);else if(skipNewlines(e),skipBlanks(e.L),"{"===peek(e.L)){const n=e.L.b;advance(e.L);const t=mk(e,"{",n,e.L.b,[]),s=parseStatements(e,"}");let r;if("}"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,"}",n,e.L.b,[])}else r=mk(e,"}",e.L.b,e.L.b,[]);a.push(mk(e,"compound_statement",t.startIndex,r.endIndex,[t,...s,r]))}const L=a[a.length-1];return mk(e,"c_style_for_statement",t.startIndex,L.endIndex,a)}const a=[t],s=nextToken(e.L,"arg");a.push(mk(e,"variable_name",s.start,s.end,[])),skipBlanks(e.L);const r=saveLex(e.L),i=nextToken(e.L,"arg");if("WORD"===i.type&&"in"===i.value)for(a.push(leaf(e,"in",i));;){skipBlanks(e.L);const n=peek(e.L);if(";"===n||"\n"===n||""===n)break;const t=parseWord(e);if(!t)break;a.push(t)}else restoreLex(e.L,r);const L=saveLex(e.L),c=nextToken(e.L,"cmd");"OP"===c.type&&";"===c.value?a.push(leaf(e,";",c)):"NEWLINE"!==c.type&&restoreLex(e.L,L);const o=parseDoGroup(e);o&&a.push(o);const d=a[a.length-1];return mk(e,"for_statement",t.startIndex,d.endIndex,a)}function parseDoGroup(e){skipNewlines(e);const n=saveLex(e.L),t=nextToken(e.L,"cmd");if("WORD"!==t.type||"do"!==t.value)return restoreLex(e.L,n),null;const a=leaf(e,"do",t),s=[a,...parseStatements(e,null)];consumeKeyword(e,"done",s);const r=s[s.length-1];return mk(e,"do_group",a.startIndex,r.endIndex,s)}function parseCaseItem(e){skipBlanks(e.L);const n=e.L.b,t=[];if("("===peek(e.L)){const n=e.L.b;advance(e.L),t.push(mk(e,"(",n,e.L.b,[]))}let a=!0;for(;;){skipBlanks(e.L);const n=peek(e.L);if(")"===n||""===n)break;const s=parseCasePattern(e);if(0===s.length)break;if(!a&&s.length>1){const n=s.map(n=>"extglob_pattern"===n.type?mk(e,"word",n.startIndex,n.endIndex,[]):n),a=n[0],r=n[n.length-1];t.push(mk(e,"concatenation",a.startIndex,r.endIndex,n))}else t.push(...s);if(a=!1,skipBlanks(e.L),"\\"===peek(e.L)&&"\n"===peek(e.L,1)&&(advance(e.L),advance(e.L),skipBlanks(e.L)),"|"!==peek(e.L))break;{const n=e.L.b;advance(e.L),t.push(mk(e,"|",n,e.L.b,[])),"\\"===peek(e.L)&&"\n"===peek(e.L,1)&&(advance(e.L),advance(e.L))}}if(")"===peek(e.L)){const n=e.L.b;advance(e.L),t.push(mk(e,")",n,e.L.b,[]))}const s=parseStatements(e,null);t.push(...s);const r=saveLex(e.L),i=nextToken(e.L,"cmd");if("OP"!==i.type||";;"!==i.value&&";&"!==i.value&&";;&"!==i.value?restoreLex(e.L,r):t.push(leaf(e,i.value,i)),0===t.length)return null;if(0===s.length)for(let n=0;n<t.length;n++){const a=t[n];if("extglob_pattern"!==a.type)continue;const s=sliceBytes(e,a.startIndex,a.endIndex);/^[-+?*@!][a-zA-Z]/.test(s)&&!/[*?(]/.test(s)&&(t[n]=mk(e,"word",a.startIndex,a.endIndex,[]))}const L=t[t.length-1];return mk(e,"case_item",n,L.endIndex,t)}function parseCasePattern(e){skipBlanks(e.L);const n=saveLex(e.L),t=e.L.b,a=e.L.i;let s=0,r=!1,i=!1,L=!1;for(;e.L.i<e.L.len;){const n=peek(e.L);if("\\"===n&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if('"'!==n&&"'"!==n)if("("!==n)if(s>0){if(")"===n){s--,advance(e.L);continue}if("\n"===n)break;advance(e.L)}else{if(")"===n||"|"===n||" "===n||"\t"===n||"\n"===n)break;"$"===n&&(r=!0),"["===n&&(i=!0),advance(e.L)}else s++,advance(e.L);else{for(L=!0,advance(e.L);e.L.i<e.L.len&&peek(e.L)!==n;)"\\"===peek(e.L)&&e.L.i+1<e.L.len&&advance(e.L),advance(e.L);peek(e.L)===n&&advance(e.L)}}if(e.L.b===t)return[];const c=e.src.slice(a,e.L.i),o=/[*?+@!]\(/.test(c);if(L&&!o)return restoreLex(e.L,n),function(e){const n=[];let t=e.L.b,a=e.L.i;const flushSeg=()=>{if(e.L.i>a){const s=e.src.slice(a,e.L.i),r=/[*?]/.test(s)?"extglob_pattern":"word";n.push(mk(e,r,t,e.L.b,[]))}};for(;e.L.i<e.L.len;){const s=peek(e.L);if("\\"===s&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else if('"'!==s){if("'"===s){flushSeg();const s=nextToken(e.L,"arg");n.push(leaf(e,"raw_string",s)),t=e.L.b,a=e.L.i;continue}if(")"===s||"|"===s||" "===s||"\t"===s||"\n"===s)break;advance(e.L)}else flushSeg(),n.push(parseDoubleQuoted(e)),t=e.L.b,a=e.L.i}return flushSeg(),n}(e);if(!o&&(r||i)){restoreLex(e.L,n);const t=parseWord(e);return t?[t]:[]}return[mk(e,o||/[*?]/.test(c)||/^[-+?*@!][a-zA-Z]/.test(c)?"extglob_pattern":"word",t,e.L.b,[])]}function consumeKeyword(e,n,t){skipNewlines(e);const a=saveLex(e.L),s=nextToken(e.L,"cmd");"WORD"===s.type&&s.value===n?t.push(leaf(e,n,s)):restoreLex(e.L,a)}function parseTestExpr(e,n){return parseTestOr(e,n)}function parseTestOr(e,n){let t=parseTestAnd(e,n);if(!t)return null;for(;;){skipBlanks(e.L);const a=saveLex(e.L);if("|"!==peek(e.L)||"|"!==peek(e.L,1))break;{const s=e.L.b;advance(e.L),advance(e.L);const r=mk(e,"||",s,e.L.b,[]),i=parseTestAnd(e,n);if(!i){restoreLex(e.L,a);break}t=mk(e,"binary_expression",t.startIndex,i.endIndex,[t,r,i])}}return t}function parseTestAnd(e,n){let t=parseTestUnary(e,n);if(!t)return null;for(;skipBlanks(e.L),"&"===peek(e.L)&&"&"===peek(e.L,1);){const a=e.L.b;advance(e.L),advance(e.L);const s=mk(e,"&&",a,e.L.b,[]),r=parseTestUnary(e,n);if(!r)break;t=mk(e,"binary_expression",t.startIndex,r.endIndex,[t,s,r])}return t}function parseTestUnary(e,n){if(skipBlanks(e.L),"("===peek(e.L)){const t=e.L.b;advance(e.L);const a=mk(e,"(",t,e.L.b,[]),s=parseTestOr(e,n);let r;if(skipBlanks(e.L),")"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,")",n,e.L.b,[])}else r=mk(e,")",e.L.b,e.L.b,[]);const i=s?[a,s,r]:[a,r];return mk(e,"parenthesized_expression",a.startIndex,r.endIndex,i)}return function(e,n){skipBlanks(e.L);const a=parseTestNegatablePrimary(e,n);if(!a)return null;skipBlanks(e.L);const s=peek(e.L),r=peek(e.L,1);let i=null;const L=e.L.b;if("="===s&&"="===r)advance(e.L),advance(e.L),i=mk(e,"==",L,e.L.b,[]);else if("!"===s&&"="===r)advance(e.L),advance(e.L),i=mk(e,"!=",L,e.L.b,[]);else if("="===s&&"~"===r)advance(e.L),advance(e.L),i=mk(e,"=~",L,e.L.b,[]);else if("="===s&&"="!==r)advance(e.L),i=mk(e,"=",L,e.L.b,[]);else if("<"===s&&"<"!==r)advance(e.L),i=mk(e,"<",L,e.L.b,[]);else if(">"===s&&">"!==r)advance(e.L),i=mk(e,">",L,e.L.b,[]);else if("-"===s&&isIdentStart(r)){for(advance(e.L);isIdentChar(peek(e.L));)advance(e.L);i=mk(e,"test_operator",L,e.L.b,[])}if(!i)return a;if(skipBlanks(e.L),"]]"===n){const n=i.type;if("=~"===n){skipBlanks(e.L);const n=peek(e.L);let t=null;if('"'===n||"'"===n){const a=saveLex(e.L),s='"'===n?parseDoubleQuoted(e):leaf(e,"raw_string",nextToken(e.L,"arg"));let r=e.L.i;for(;r<e.L.len&&(" "===e.src[r]||"\t"===e.src[r]);)r++;const i=e.src[r]??"",L=e.src[r+1]??"";"]"===i&&"]"===L||"&"===i&&"&"===L||"|"===i&&"|"===L||"\n"===i||""===i?t=s:restoreLex(e.L,a)}return t||(t=parseTestRegexRhs(e)),t?mk(e,"binary_expression",a.startIndex,t.endIndex,[a,i,t]):a}if("="===n){const n=parseTestRegexRhs(e);return n?mk(e,"binary_expression",a.startIndex,n.endIndex,[a,i,n]):a}if("=="===n||"!="===n){const n=function(e){skipBlanks(e.L);const n=[];let a=e.L.b,s=e.L.i,r=0;const flushSeg=()=>{if(e.L.i>s){const t=e.src.slice(s,e.L.i),r=/^\d+$/.test(t)?"number":"extglob_pattern";n.push(mk(e,r,a,e.L.b,[]))}};for(;e.L.i<e.L.len;){const i=peek(e.L);if("\\"===i&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else{if("\n"===i)break;if(0===r){if("]"===i&&"]"===peek(e.L,1))break;if(" "===i||"\t"===i){let n=e.L.i;for(;n<e.L.len&&(" "===e.L.src[n]||"\t"===e.L.src[n]);)n++;const t=e.L.src[n]??"",a=e.L.src[n+1]??"";if("]"===t&&"]"===a||"&"===t&&"&"===a||"|"===t&&"|"===a)break;advance(e.L);continue}}if("$"===i){const r=peek(e.L,1);if("("===r||"{"===r||isIdentStart(r)||t.has(r)){flushSeg();const t=parseDollarLike(e);t&&n.push(t),a=e.L.b,s=e.L.i;continue}}if('"'!==i){if("'"===i){flushSeg();const t=nextToken(e.L,"arg");n.push(leaf(e,"raw_string",t)),a=e.L.b,s=e.L.i;continue}"("===i?r++:")"===i&&r>0&&r--,advance(e.L)}else flushSeg(),n.push(parseDoubleQuoted(e)),a=e.L.b,s=e.L.i}}return flushSeg(),n}(e);if(0===n.length)return a;const s=n[n.length-1];return mk(e,"binary_expression",a.startIndex,s.endIndex,[a,i,...n])}}const c=parseTestPrimary(e,n);return c?mk(e,"binary_expression",a.startIndex,c.endIndex,[a,i,c]):a}(e,n)}function parseTestNegatablePrimary(e,n){skipBlanks(e.L);const t=peek(e.L);if("!"===t){const t=e.L.b;advance(e.L);const a=mk(e,"!",t,e.L.b,[]),s=parseTestNegatablePrimary(e,n);return s?mk(e,"unary_expression",a.startIndex,s.endIndex,[a,s]):a}if("-"===t&&isIdentStart(peek(e.L,1))){const t=e.L.b;for(advance(e.L);isIdentChar(peek(e.L));)advance(e.L);const a=mk(e,"test_operator",t,e.L.b,[]);skipBlanks(e.L);const s=parseTestPrimary(e,n);return s?mk(e,"unary_expression",a.startIndex,s.endIndex,[a,s]):a}return parseTestPrimary(e,n)}function parseTestRegexRhs(e){skipBlanks(e.L);const n=e.L.b;let t=0,a=0;for(;e.L.i<e.L.len;){const n=peek(e.L);if("\\"===n&&e.L.i+1<e.L.len)advance(e.L),advance(e.L);else{if("\n"===n)break;if(0===t&&0===a){if("]"===n&&"]"===peek(e.L,1))break;if(" "===n||"\t"===n){let n=e.L.i;for(;n<e.L.len&&(" "===e.L.src[n]||"\t"===e.L.src[n]);)n++;const t=e.L.src[n]??"",a=e.L.src[n+1]??"";if("]"===t&&"]"===a||"&"===t&&"&"===a||"|"===t&&"|"===a)break;advance(e.L);continue}}"("===n?t++:")"===n&&t>0?t--:"["===n?a++:"]"===n&&a>0&&a--,advance(e.L)}}return e.L.b===n?null:mk(e,"regex",n,e.L.b,[])}function parseTestPrimary(e,n){return skipBlanks(e.L),"]"===n&&"]"===peek(e.L)||"]]"===n&&"]"===peek(e.L)&&"]"===peek(e.L,1)?null:parseWord(e)}const s={"=":2,"+=":2,"-=":2,"*=":2,"/=":2,"%=":2,"<<=":2,">>=":2,"&=":2,"^=":2,"|=":2,"||":4,"&&":5,"|":6,"^":7,"&":8,"==":9,"!=":9,"<":10,">":10,"<=":10,">=":10,"<<":11,">>":11,"+":12,"-":12,"*":13,"/":13,"%":13,"**":14},r=new Set(["=","+=","-=","*=","/=","%=","<<=",">>=","&=","^=","|=","**"]);function parseArithExpr(e,n,t="var"){return parseArithTernary(e,n,t)}function parseArithCommaList(e,n,t="var"){const a=[];for(;;){const s=parseArithTernary(e,n,t);if(s&&a.push(s),skipBlanks(e.L),","!==peek(e.L)||isArithStop(e,n))break;advance(e.L)}return a}function parseArithTernary(e,n,t){const a=parseArithBinary(e,n,0,t);if(!a)return null;if(skipBlanks(e.L),"?"===peek(e.L)){const s=e.L.b;advance(e.L);const r=mk(e,"?",s,e.L.b,[]),i=parseArithBinary(e,":",0,t);let L;if(skipBlanks(e.L),":"===peek(e.L)){const n=e.L.b;advance(e.L),L=mk(e,":",n,e.L.b,[])}else L=mk(e,":",e.L.b,e.L.b,[]);const c=parseArithTernary(e,n,t),o=c??L,d=[a,r];return i&&d.push(i),d.push(L),c&&d.push(c),mk(e,"ternary_expression",a.startIndex,o.endIndex,d)}return a}function scanArithOp(e){const n=peek(e.L),t=peek(e.L,1),a=peek(e.L,2);return"<"===n&&"<"===t&&"="===a?["<<=",3]:">"===n&&">"===t&&"="===a?[">>=",3]:"*"===n&&"*"===t?["**",2]:"<"===n&&"<"===t?["<<",2]:">"===n&&">"===t?[">>",2]:"="===n&&"="===t?["==",2]:"!"===n&&"="===t?["!=",2]:"<"===n&&"="===t?["<=",2]:">"===n&&"="===t?[">=",2]:"&"===n&&"&"===t?["&&",2]:"|"===n&&"|"===t?["||",2]:"+"===n&&"="===t?["+=",2]:"-"===n&&"="===t?["-=",2]:"*"===n&&"="===t?["*=",2]:"/"===n&&"="===t?["/=",2]:"%"===n&&"="===t?["%=",2]:"&"===n&&"="===t?["&=",2]:"^"===n&&"="===t?["^=",2]:"|"===n&&"="===t?["|=",2]:"+"===n&&"+"!==t?["+",1]:"-"===n&&"-"!==t?["-",1]:"*"===n?["*",1]:"/"===n?["/",1]:"%"===n?["%",1]:"<"===n?["<",1]:">"===n?[">",1]:"&"===n?["&",1]:"|"===n?["|",1]:"^"===n?["^",1]:"="===n?["=",1]:null}function parseArithBinary(e,n,t,a){let i=parseArithUnary(e,n,a);if(!i)return null;for(;skipBlanks(e.L),!isArithStop(e,n)&&","!==peek(e.L);){const L=scanArithOp(e);if(!L)break;const[c,o]=L,d=s[c];if(void 0===d||d<t)break;const l=e.L.b;for(let n=0;n<o;n++)advance(e.L);const p=mk(e,c,l,e.L.b,[]),u=parseArithBinary(e,n,r.has(c)?d:d+1,a);if(!u)break;i=mk(e,"binary_expression",i.startIndex,u.endIndex,[i,p,u])}return i}function parseArithUnary(e,n,t){if(skipBlanks(e.L),isArithStop(e,n))return null;const a=peek(e.L),s=peek(e.L,1);if("+"===a&&"+"===s||"-"===a&&"-"===s){const r=e.L.b;advance(e.L),advance(e.L);const i=mk(e,a+s,r,e.L.b,[]),L=parseArithUnary(e,n,t);return L?mk(e,"unary_expression",i.startIndex,L.endIndex,[i,L]):i}if("-"===a||"+"===a||"!"===a||"~"===a){if("var"!==t&&"-"===a&&isDigit(s)){const n=e.L.b;for(advance(e.L);isDigit(peek(e.L));)advance(e.L);return mk(e,"number",n,e.L.b,[])}const r=e.L.b;advance(e.L);const i=mk(e,a,r,e.L.b,[]),L=parseArithUnary(e,n,t);return L?mk(e,"unary_expression",i.startIndex,L.endIndex,[i,L]):i}return function(e,n,t){const a=function(e,n,t){if(skipBlanks(e.L),isArithStop(e,n))return null;const a=peek(e.L);if("("===a){const n=e.L.b;advance(e.L);const a=mk(e,"(",n,e.L.b,[]),s=parseArithCommaList(e,")",t);let r;if(skipBlanks(e.L),")"===peek(e.L)){const n=e.L.b;advance(e.L),r=mk(e,")",n,e.L.b,[])}else r=mk(e,")",e.L.b,e.L.b,[]);return mk(e,"parenthesized_expression",a.startIndex,r.endIndex,[a,...s,r])}if('"'===a)return parseDoubleQuoted(e);if("$"===a)return parseDollarLike(e);if(isDigit(a)){const n=e.L.b;for(;isDigit(peek(e.L));)advance(e.L);if(e.L.b-n!==1||"0"!==a||"x"!==peek(e.L)&&"X"!==peek(e.L)){if("#"===peek(e.L))for(advance(e.L);isBaseDigit(peek(e.L));)advance(e.L)}else for(advance(e.L);isHexDigit(peek(e.L));)advance(e.L);return mk(e,"number",n,e.L.b,[])}if(isIdentStart(a)){const a=e.L.b;for(;isIdentChar(peek(e.L));)advance(e.L);const s=peek(e.L);if("assign"===t){skipBlanks(e.L);const s=peek(e.L),r=peek(e.L,1);if("="===s&&"="!==r){const s=mk(e,"variable_name",a,e.L.b,[]),r=e.L.b;advance(e.L);const i=mk(e,"=",r,e.L.b,[]),L=parseArithTernary(e,n,t);return mk(e,"variable_assignment",a,L?L.endIndex:i.endIndex,L?[s,i,L]:[s,i])}}if("["===s){const n=mk(e,"variable_name",a,e.L.b,[]),t=e.L.b;advance(e.L);const s=mk(e,"[",t,e.L.b,[]),r=parseArithTernary(e,"]","var")??parseDollarLike(e);let i;if(skipBlanks(e.L),"]"===peek(e.L)){const n=e.L.b;advance(e.L),i=mk(e,"]",n,e.L.b,[])}else i=mk(e,"]",e.L.b,e.L.b,[]);const L=r?[n,s,r,i]:[n,s,i];return mk(e,"subscript",a,i.endIndex,L)}return mk(e,"var"===t?"variable_name":"word",a,e.L.b,[])}return null}(e,n,t);if(!a)return null;const s=peek(e.L),r=peek(e.L,1);if("+"===s&&"+"===r||"-"===s&&"-"===r){const n=e.L.b;advance(e.L),advance(e.L);const t=mk(e,s+r,n,e.L.b,[]);return mk(e,"postfix_expression",a.startIndex,t.endIndex,[a,t])}return a}(e,n,t)}function isArithStop(e,n){const t=peek(e.L);return"))"===n?")"===t&&")"===peek(e.L,1):")"===n?")"===t:";"===n?";"===t:":"===n?":"===t:"]"===n?"]"===t:"}"===n?"}"===t:":}"===n?":"===t||"}"===t:""===t||"\n"===t}
@@ -1 +1 @@
1
- import{hasMalformedTokens as t,hasShellQuoteSingleQuoteBug as e,quote as n,tryParseShellCommand as i}from"./shellQuote.js";export function rearrangePipeCommand(n){if(n.includes("`"))return quoteWithEvalStdinRedirect(n);if(n.includes("$("))return quoteWithEvalStdinRedirect(n);if(/\$[A-Za-z_{]/.test(n))return quoteWithEvalStdinRedirect(n);if(function(t){return/\b(for|while|until|if|case|select)\s/.test(t)}(n))return quoteWithEvalStdinRedirect(n);const r=function(t){return t.replace(/\\+\n/g,t=>{const e=t.length-1;return e%2==1?"\\".repeat(e-1):t})}(n);if(r.includes("\n"))return quoteWithEvalStdinRedirect(n);if(e(r))return quoteWithEvalStdinRedirect(n);const o=i(r);if(!o.success)return quoteWithEvalStdinRedirect(n);const u=o.tokens;if(t(r,u))return quoteWithEvalStdinRedirect(n);const s=function(t){for(let e=0;e<t.length;e++){if(isOperator(t[e],"|"))return e}return-1}(u);if(s<=0)return quoteWithEvalStdinRedirect(n);return singleQuoteForEval([...buildCommandParts(u,0,s),"< /dev/null",...buildCommandParts(u,s,u.length)].join(" "))}function buildCommandParts(t,e,i){const r=[];let o=!1;for(let u=e;u<i;u++){const e=t[u];if("string"==typeof e&&/^[012]$/.test(e)&&u+2<i&&isOperator(t[u+1])){const n=t[u+1],i=t[u+2];if(">&"===n.op&&"string"==typeof i&&/^[012]$/.test(i)){r.push(`${e}>&${i}`),u+=2;continue}if(">"===n.op&&"/dev/null"===i){r.push(`${e}>/dev/null`),u+=2;continue}if(">"===n.op&&"string"==typeof i&&i.startsWith("&")){const t=i.slice(1);if(/^[012]$/.test(t)){r.push(`${e}>&${t}`),u+=2;continue}}}if("string"==typeof e){if(!o&&isEnvironmentVariableAssignment(e)){const t=e.indexOf("="),i=e.slice(0,t),o=e.slice(t+1),u=n([o]);r.push(`${i}=${u}`)}else o=!0,r.push(n([e]))}else isOperator(e)&&("glob"===e.op&&"pattern"in e?r.push(e.pattern):(r.push(e.op),isCommandSeparator(e.op)&&(o=!1)))}return r}function isEnvironmentVariableAssignment(t){return/^[A-Za-z_][A-Za-z0-9_]*=/.test(t)}function isCommandSeparator(t){return"&&"===t||"||"===t||";"===t}function isOperator(t,e){return!(!t||"object"!=typeof t||!("op"in t))&&(!e||t.op===e)}function quoteWithEvalStdinRedirect(t){return singleQuoteForEval(t)+" < /dev/null"}function singleQuoteForEval(t){return"'"+t.replace(/'/g,"'\"'\"'")+"'"}
1
+ import{hasMalformedTokens as t,hasShellQuoteSingleQuoteBug as e,quote as n,tryParseShellCommand as i}from"./shellQuote.js";export function rearrangePipeCommand(n){if(n.includes("`"))return quoteWithEvalStdinRedirect(n);if(n.includes("$("))return quoteWithEvalStdinRedirect(n);if(/\$[A-Za-z_{]/.test(n))return quoteWithEvalStdinRedirect(n);if(function(t){return/\b(for|while|until|if|case|select)\s/.test(t)}(n))return quoteWithEvalStdinRedirect(n);const r=function(t){return t.replace(/\\+\n/g,t=>{const e=t.length-1;return e%2==1?"\\".repeat(e-1):t})}(n);if(r.includes("\n"))return quoteWithEvalStdinRedirect(n);if(e(r))return quoteWithEvalStdinRedirect(n);const o=i(r);if(!o.success)return quoteWithEvalStdinRedirect(n);const s=o.tokens;if(t(r,s))return quoteWithEvalStdinRedirect(n);const u=function(t){for(let e=0;e<t.length;e++)if(isOperator(t[e],"|"))return e;return-1}(s);return u<=0?quoteWithEvalStdinRedirect(n):singleQuoteForEval([...buildCommandParts(s,0,u),"< /dev/null",...buildCommandParts(s,u,s.length)].join(" "))}function buildCommandParts(t,e,i){const r=[];let o=!1;for(let s=e;s<i;s++){const e=t[s];if("string"==typeof e&&/^[012]$/.test(e)&&s+2<i&&isOperator(t[s+1])){const n=t[s+1],i=t[s+2];if(">&"===n.op&&"string"==typeof i&&/^[012]$/.test(i)){r.push(`${e}>&${i}`),s+=2;continue}if(">"===n.op&&"/dev/null"===i){r.push(`${e}>/dev/null`),s+=2;continue}if(">"===n.op&&"string"==typeof i&&i.startsWith("&")){const t=i.slice(1);if(/^[012]$/.test(t)){r.push(`${e}>&${t}`),s+=2;continue}}}if("string"==typeof e)if(!o&&isEnvironmentVariableAssignment(e)){const t=e.indexOf("="),i=e.slice(0,t),o=e.slice(t+1),s=n([o]);r.push(`${i}=${s}`)}else o=!0,r.push(n([e]));else isOperator(e)&&("glob"===e.op&&"pattern"in e?r.push(e.pattern):(r.push(e.op),isCommandSeparator(e.op)&&(o=!1)))}return r}function isEnvironmentVariableAssignment(t){return/^[A-Za-z_][A-Za-z0-9_]*=/.test(t)}function isCommandSeparator(t){return"&&"===t||"||"===t||";"===t}function isOperator(t,e){return!(!t||"object"!=typeof t||!("op"in t)||e&&t.op!==e)}function quoteWithEvalStdinRedirect(t){return singleQuoteForEval(t)+" < /dev/null"}function singleQuoteForEval(t){return"'"+t.replace(/'/g,"'\"'\"'")+"'"}
@@ -1 +1 @@
1
- import{feature as e}from"../../recovery/bunBundleShim.js";import{logEvent as t}from"../../services/analytics/index.js";import{logForDebugging as n}from"../debug.js";import{ensureParserInitialized as r,getParserModule as o}from"./bashParser.js";const a=new Set(["export","declare","typeset","readonly","local","unset","unsetenv"]),i=new Set(["word","string","raw_string","number"]),s=new Set(["command_substitution","process_substitution"]),l=new Set(["command","declaration_command"]);let u=!1;function logLoadOnce(e){u||(u=!0,n(e?"tree-sitter: native module loaded":"tree-sitter: unavailable"),t("tengu_tree_sitter_load",{success:e}))}export async function ensureInitialized(){(e("TREE_SITTER_BASH")||e("TREE_SITTER_BASH_SHADOW"))&&await r()}export async function parseCommand(t){if(!t||t.length>1e4)return null;if(e("TREE_SITTER_BASH")){await r();const e=o();if(logLoadOnce(null!==e),!e)return null;try{const n=e.parse(t);if(!n)return null;const r=findCommandNode(n,null),o=function(e){if(!e||"command"!==e.type)return[];const t=[];for(const n of e.children)if("variable_assignment"===n.type)t.push(n.text);else if("command_name"===n.type||"word"===n.type)break;return t}(r);return{rootNode:n,envVars:o,commandNode:r,originalCommand:t}}catch{return null}}return null}export const PARSE_ABORTED=Symbol("parse-aborted");export async function parseCommandRaw(n){if(!n||n.length>1e4)return null;if(e("TREE_SITTER_BASH")||e("TREE_SITTER_BASH_SHADOW")){await r();const e=o();if(logLoadOnce(null!==e),!e)return null;try{const r=e.parse(n);return null===r?(t("tengu_tree_sitter_parse_abort",{cmdLength:n.length,panic:!1}),PARSE_ABORTED):r}catch{return t("tengu_tree_sitter_parse_abort",{cmdLength:n.length,panic:!0}),PARSE_ABORTED}}return null}function findCommandNode(e,t){const{type:n,children:r}=e;if(l.has(n))return e;if("variable_assignment"===n&&t)return t.children.find(t=>l.has(t.type)&&t.startIndex>e.startIndex)??null;if("pipeline"===n){for(const t of r){const n=findCommandNode(t,e);if(n)return n}return null}if("redirected_statement"===n)return r.find(e=>l.has(e.type))??null;for(const t of r){const n=findCommandNode(t,e);if(n)return n}return null}export function extractCommandArguments(e){if("declaration_command"===e.type){const t=e.children[0];return t&&a.has(t.text)?[t.text]:[]}const t=[];let n=!1;for(const r of e.children)if("variable_assignment"!==r.type)if("command_name"===r.type||!n&&"word"===r.type)n=!0,t.push(r.text);else if(i.has(r.type))t.push(stripQuotes(r.text));else if(s.has(r.type))break;return t}function stripQuotes(e){return e.length>=2&&('"'===e[0]&&'"'===e.at(-1)||"'"===e[0]&&"'"===e.at(-1))?e.slice(1,-1):e}
1
+ import{feature as e}from"bun:bundle";import{logEvent as t}from"../../services/analytics/index.js";import{logForDebugging as n}from"../debug.js";import{ensureParserInitialized as r,getParserModule as o}from"./bashParser.js";const a=new Set(["export","declare","typeset","readonly","local","unset","unsetenv"]),s=new Set(["word","string","raw_string","number"]),i=new Set(["command_substitution","process_substitution"]),l=new Set(["command","declaration_command"]);let u=!1;function logLoadOnce(e){u||(u=!0,n(e?"tree-sitter: native module loaded":"tree-sitter: unavailable"),t("tengu_tree_sitter_load",{success:e}))}export async function ensureInitialized(){(e("TREE_SITTER_BASH")||e("TREE_SITTER_BASH_SHADOW"))&&await r()}export async function parseCommand(t){if(!t||t.length>1e4)return null;if(e("TREE_SITTER_BASH")){await r();const e=o();if(logLoadOnce(null!==e),!e)return null;try{const n=e.parse(t);if(!n)return null;const r=findCommandNode(n,null),o=function(e){if(!e||"command"!==e.type)return[];const t=[];for(const n of e.children)if("variable_assignment"===n.type)t.push(n.text);else if("command_name"===n.type||"word"===n.type)break;return t}(r);return{rootNode:n,envVars:o,commandNode:r,originalCommand:t}}catch{return null}}return null}export const PARSE_ABORTED=Symbol("parse-aborted");export async function parseCommandRaw(n){if(!n||n.length>1e4)return null;if(e("TREE_SITTER_BASH")||e("TREE_SITTER_BASH_SHADOW")){await r();const e=o();if(logLoadOnce(null!==e),!e)return null;try{const r=e.parse(n);return null===r?(t("tengu_tree_sitter_parse_abort",{cmdLength:n.length,panic:!1}),PARSE_ABORTED):r}catch{return t("tengu_tree_sitter_parse_abort",{cmdLength:n.length,panic:!0}),PARSE_ABORTED}}return null}function findCommandNode(e,t){const{type:n,children:r}=e;if(l.has(n))return e;if("variable_assignment"===n&&t)return t.children.find(t=>l.has(t.type)&&t.startIndex>e.startIndex)??null;if("pipeline"===n){for(const t of r){const n=findCommandNode(t,e);if(n)return n}return null}if("redirected_statement"===n)return r.find(e=>l.has(e.type))??null;for(const t of r){const n=findCommandNode(t,e);if(n)return n}return null}export function extractCommandArguments(e){if("declaration_command"===e.type){const t=e.children[0];return t&&a.has(t.text)?[t.text]:[]}const t=[];let n=!1;for(const r of e.children)if("variable_assignment"!==r.type)if("command_name"===r.type||!n&&"word"===r.type)n=!0,t.push(r.text);else if(s.has(r.type))t.push(stripQuotes(r.text));else if(i.has(r.type))break;return t}function stripQuotes(e){return e.length>=2&&('"'===e[0]&&'"'===e.at(-1)||"'"===e[0]&&"'"===e.at(-1))?e.slice(1,-1):e}
@@ -1 +1 @@
1
- import{parse as r,quote as t}from"shell-quote";import{logError as e}from"../log.js";import{jsonStringify as n}from"../slowOperations.js";export function tryParseShellCommand(t,n){try{return{success:!0,tokens:r(t,n)}}catch(r){return r instanceof Error&&e(r),{success:!1,error:r instanceof Error?r.message:"Unknown parse error"}}}export function tryQuoteShellArgs(r){try{const e=r.map((r,t)=>{if(null==r)return String(r);const e=typeof r;if("string"===e)return r;if("number"===e||"boolean"===e)return String(r);if("object"===e)throw new Error(`Cannot quote argument at index ${t}: object values are not supported`);if("symbol"===e)throw new Error(`Cannot quote argument at index ${t}: symbol values are not supported`);if("function"===e)throw new Error(`Cannot quote argument at index ${t}: function values are not supported`);throw new Error(`Cannot quote argument at index ${t}: unsupported type ${e}`)});return{success:!0,quoted:t(e)}}catch(r){return r instanceof Error&&e(r),{success:!1,error:r instanceof Error?r.message:"Unknown quote error"}}}export function hasMalformedTokens(r,t){let e=!1,n=!1,o=0,u=0;for(let t=0;t<r.length;t++){const s=r[t];"\\"!==s||e?'"'!==s||e?"'"!==s||n||(u++,e=!e):(o++,n=!n):t++}if(o%2!=0||u%2!=0)return!0;for(const r of t){if("string"!=typeof r)continue;if((r.match(/{/g)||[]).length!==(r.match(/}/g)||[]).length)return!0;if((r.match(/\(/g)||[]).length!==(r.match(/\)/g)||[]).length)return!0;if((r.match(/\[/g)||[]).length!==(r.match(/\]/g)||[]).length)return!0;if((r.match(/(?<!\\)"/g)||[]).length%2!=0)return!0;if((r.match(/(?<!\\)'/g)||[]).length%2!=0)return!0}return!1}export function hasShellQuoteSingleQuoteBug(r){let t=!1,e=!1;for(let n=0;n<r.length;n++){const o=r[n];if("\\"!==o||t)if('"'!==o||t){if("'"!==o||e);else if(t=!t,!t){let t=0,e=n-1;for(;e>=0&&"\\"===r[e];)t++,e--;if(t>0&&t%2==1)return!0;if(t>0&&t%2==0&&-1!==r.indexOf("'",n+1))return!0}}else e=!e;else n++}return!1}export function quote(r){const o=tryQuoteShellArgs([...r]);if(o.success)return o.quoted;try{const e=r.map(r=>{if(null==r)return String(r);const t=typeof r;return"string"===t||"number"===t||"boolean"===t?String(r):n(r)});return t(e)}catch(r){throw r instanceof Error&&e(r),new Error("Failed to quote shell arguments safely")}}
1
+ import{parse as r,quote as t}from"shell-quote";import{logError as e}from"../log.js";import{jsonStringify as n}from"../slowOperations.js";export function tryParseShellCommand(t,n){try{return{success:!0,tokens:r(t,n)}}catch(r){return r instanceof Error&&e(r),{success:!1,error:r instanceof Error?r.message:"Unknown parse error"}}}export function tryQuoteShellArgs(r){try{const e=r.map((r,t)=>{if(null==r)return String(r);const e=typeof r;if("string"===e)return r;if("number"===e||"boolean"===e)return String(r);if("object"===e)throw new Error(`Cannot quote argument at index ${t}: object values are not supported`);if("symbol"===e)throw new Error(`Cannot quote argument at index ${t}: symbol values are not supported`);if("function"===e)throw new Error(`Cannot quote argument at index ${t}: function values are not supported`);throw new Error(`Cannot quote argument at index ${t}: unsupported type ${e}`)});return{success:!0,quoted:t(e)}}catch(r){return r instanceof Error&&e(r),{success:!1,error:r instanceof Error?r.message:"Unknown quote error"}}}export function hasMalformedTokens(r,t){let e=!1,n=!1,o=0,s=0;for(let t=0;t<r.length;t++){const u=r[t];"\\"!==u||e?'"'!==u||e?"'"!==u||n||(s++,e=!e):(o++,n=!n):t++}if(o%2!=0||s%2!=0)return!0;for(const r of t)if("string"==typeof r){if((r.match(/{/g)||[]).length!==(r.match(/}/g)||[]).length)return!0;if((r.match(/\(/g)||[]).length!==(r.match(/\)/g)||[]).length)return!0;if((r.match(/\[/g)||[]).length!==(r.match(/\]/g)||[]).length)return!0;if((r.match(/(?<!\\)"/g)||[]).length%2!=0)return!0;if((r.match(/(?<!\\)'/g)||[]).length%2!=0)return!0}return!1}export function hasShellQuoteSingleQuoteBug(r){let t=!1,e=!1;for(let n=0;n<r.length;n++){const o=r[n];if("\\"!==o||t)if('"'!==o||t){if("'"!==o||e);else if(t=!t,!t){let t=0,e=n-1;for(;e>=0&&"\\"===r[e];)t++,e--;if(t>0&&t%2==1)return!0;if(t>0&&t%2==0&&-1!==r.indexOf("'",n+1))return!0}}else e=!e;else n++}return!1}export function quote(r){const o=tryQuoteShellArgs([...r]);if(o.success)return o.quoted;try{const e=r.map(r=>{if(null==r)return String(r);const t=typeof r;return"string"===t||"number"===t||"boolean"===t?String(r):n(r)});return t(e)}catch(r){throw r instanceof Error&&e(r),new Error("Failed to quote shell arguments safely")}}
@@ -1 +1 @@
1
- import{quote as t}from"./shellQuote.js";function containsHeredoc(t){if(/\d\s*<<\s*\d/.test(t)||/\[\[\s*\d+\s*<<\s*\d+\s*\]\]/.test(t)||/\$\(\(.*<<.*\)\)/.test(t))return!1;return/<<-?\s*(?:(['"]?)(\w+)\1|\\(\w+))/.test(t)}export function quoteShellCommand(e,n=!0){if(containsHeredoc(e)||function(t){return/'(?:[^'\\]|\\.)*\n(?:[^'\\]|\\.)*'/.test(t)||/"(?:[^"\\]|\\.)*\n(?:[^"\\]|\\.)*"/.test(t)}(e)){const t=`'${e.replace(/'/g,"'\"'\"'")}'`;return containsHeredoc(e)?t:n?`${t} < /dev/null`:t}return t(n?[e,"<","/dev/null"]:[e])}export function hasStdinRedirect(t){return/(?:^|[\s;&|])<(?![<(])\s*\S+/.test(t)}export function shouldAddStdinRedirect(t){return!containsHeredoc(t)&&!hasStdinRedirect(t)}const e=/(\d?&?>+\s*)[Nn][Uu][Ll](?=\s|$|[|&;)\n])/g;export function rewriteWindowsNullRedirect(t){return t.replace(e,"$1/dev/null")}
1
+ import{quote as t}from"./shellQuote.js";function containsHeredoc(t){return!(/\d\s*<<\s*\d/.test(t)||/\[\[\s*\d+\s*<<\s*\d+\s*\]\]/.test(t)||/\$\(\(.*<<.*\)\)/.test(t))&&/<<-?\s*(?:(['"]?)(\w+)\1|\\(\w+))/.test(t)}export function quoteShellCommand(e,n=!0){if(containsHeredoc(e)||function(t){return/'(?:[^'\\]|\\.)*\n(?:[^'\\]|\\.)*'/.test(t)||/"(?:[^"\\]|\\.)*\n(?:[^"\\]|\\.)*"/.test(t)}(e)){const t=`'${e.replace(/'/g,"'\"'\"'")}'`;return containsHeredoc(e)?t:n?`${t} < /dev/null`:t}return t(n?[e,"<","/dev/null"]:[e])}export function hasStdinRedirect(t){return/(?:^|[\s;&|])<(?![<(])\s*\S+/.test(t)}export function shouldAddStdinRedirect(t){return!containsHeredoc(t)&&!hasStdinRedirect(t)}const e=/(\d?&?>+\s*)[Nn][Uu][Ll](?=\s|$|[|&;)\n])/g;export function rewriteWindowsNullRedirect(t){return t.replace(e,"$1/dev/null")}
@@ -1 +1 @@
1
- import{getAnthropicApiKey as n,getAuthTokenSource as o,getSubscriptionType as i,isClaudeAISubscriber as r}from"./auth.js";import{getGlobalConfig as e}from"./config.js";import{isEnvTruthy as t}from"./envUtils.js";export function hasConsoleBillingAccess(){if(t(process.env.DISABLE_COST_WARNINGS))return!1;if(r())return!1;const i=o(),c=null!==n();if(!i.hasToken&&!c)return!1;const l=e(),s=l.oauthAccount?.organizationRole,u=l.oauthAccount?.workspaceRole;return!(!s||!u)&&(["admin","billing"].includes(s)||["workspace_admin","workspace_billing"].includes(u))}let c=null;export function setMockBillingAccessOverride(n){c=n}export function hasClaudeAiBillingAccess(){if(null!==c)return c;if(!r())return!1;const n=i();if("max"===n||"pro"===n)return!0;const o=e(),t=o.oauthAccount?.organizationRole;return!!t&&["admin","billing","owner","primary_owner"].includes(t)}
1
+ import{getAnthropicApiKey as n,getAuthTokenSource as i,getSubscriptionType as o,isClaudeAISubscriber as e}from"./auth.js";import{getGlobalConfig as r}from"./config.js";import{isEnvTruthy as t}from"./envUtils.js";export function hasConsoleBillingAccess(){if(t(process.env.DISABLE_COST_WARNINGS))return!1;if(e())return!1;const o=i(),s=null!==n();if(!o.hasToken&&!s)return!1;const c=r(),l=c.oauthAccount?.organizationRole,u=c.oauthAccount?.workspaceRole;return!(!l||!u)&&(["admin","billing"].includes(l)||["workspace_admin","workspace_billing"].includes(u))}let s=null;export function setMockBillingAccessOverride(n){s=n}export function hasClaudeAiBillingAccess(){if(null!==s)return s;if(!e())return!1;const n=o();if("max"===n||"pro"===n)return!0;const i=r(),t=i.oauthAccount?.organizationRole;return!!t&&["admin","billing","owner","primary_owner"].includes(t)}
@@ -1 +1 @@
1
- import{createRequire as e}from"module";const t=e(import.meta.url);import s from"lodash-es/memoize.js";import{logForDebugging as r}from"./debug.js";import{hasNodeOption as c}from"./envUtils.js";import{getFsImplementation as i}from"./fsOperations.js";export const getCACertificates=s(()=>{const e=c("--use-system-ca")||c("--use-openssl-ca"),s=process.env.NODE_EXTRA_CA_CERTS;if(r(`CA certs: useSystemCA=${e}, extraCertsPath=${s}`),!e&&!s)return;const o=t("tls"),a=[];if(e){const e=o.getCACertificates,t=e?.("system");if(t&&t.length>0)a.push(...t),r(`CA certs: Loaded ${a.length} system CA certificates (--use-system-ca)`);else{if(!e&&!s)return void r("CA certs: --use-system-ca set but system CA API unavailable, deferring to runtime");a.push(...o.rootCertificates),r(`CA certs: Loaded ${a.length} bundled root certificates as base (--use-system-ca fallback)`)}}else a.push(...o.rootCertificates),r(`CA certs: Loaded ${a.length} bundled root certificates as base`);if(s)try{const e=i().readFileSync(s,{encoding:"utf8"});a.push(e),r(`CA certs: Appended extra certificates from NODE_EXTRA_CA_CERTS (${s})`)}catch(e){r(`CA certs: Failed to read NODE_EXTRA_CA_CERTS file (${s}): ${e}`,{level:"error"})}return a.length>0?a:void 0});export function clearCACertsCache(){getCACertificates.cache.clear?.(),r("Cleared CA certificates cache")}
1
+ import{createRequire as e}from"module";const t=e(import.meta.url);import s from"lodash-es/memoize.js";import{logForDebugging as r}from"./debug.js";import{hasNodeOption as a}from"./envUtils.js";import{getFsImplementation as o}from"./fsOperations.js";export const getCACertificates=s(()=>{const e=a("--use-system-ca")||a("--use-openssl-ca"),s=process.env.NODE_EXTRA_CA_CERTS;if(r(`CA certs: useSystemCA=${e}, extraCertsPath=${s}`),!e&&!s)return;const c=t("tls"),i=[];if(e){const e=c.getCACertificates,t=e?.("system");if(t&&t.length>0)i.push(...t),r(`CA certs: Loaded ${i.length} system CA certificates (--use-system-ca)`);else{if(!e&&!s)return void r("CA certs: --use-system-ca set but system CA API unavailable, deferring to runtime");i.push(...c.rootCertificates),r(`CA certs: Loaded ${i.length} bundled root certificates as base (--use-system-ca fallback)`)}}else i.push(...c.rootCertificates),r(`CA certs: Loaded ${i.length} bundled root certificates as base`);if(s)try{const e=o().readFileSync(s,{encoding:"utf8"});i.push(e),r(`CA certs: Appended extra certificates from NODE_EXTRA_CA_CERTS (${s})`)}catch(e){r(`CA certs: Failed to read NODE_EXTRA_CA_CERTS file (${s}): ${e}`,{level:"error"})}return i.length>0?i:void 0});export function clearCACertsCache(){getCACertificates.cache.clear?.(),r("Cleared CA certificates cache")}
@@ -1 +1 @@
1
- import{readdirSync as a}from"fs";import{stat as e}from"fs/promises";import{homedir as t,platform as r,tmpdir as o,userInfo as s}from"os";import{join as i}from"path";import{normalizeNameForMCP as n}from"../../services/mcp/normalization.js";import{logForDebugging as c}from"../debug.js";import{isFsInaccessible as p}from"../errors.js";import{execFileNoThrow as g}from"../execFileNoThrow.js";import{getPlatform as m}from"../platform.js";import{which as h}from"../which.js";export const CLAUDE_IN_CHROME_MCP_SERVER_NAME="context-in-chrome";export const CHROMIUM_BROWSERS={chrome:{name:"Google Chrome",macos:{appName:"Google Chrome",dataPath:["Library","Application Support","Google","Chrome"],nativeMessagingPath:["Library","Application Support","Google","Chrome","NativeMessagingHosts"]},linux:{binaries:["google-chrome","google-chrome-stable"],dataPath:[".config","google-chrome"],nativeMessagingPath:[".config","google-chrome","NativeMessagingHosts"]},windows:{dataPath:["Google","Chrome","User Data"],registryKey:"HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts"}},brave:{name:"Brave",macos:{appName:"Brave Browser",dataPath:["Library","Application Support","BraveSoftware","Brave-Browser"],nativeMessagingPath:["Library","Application Support","BraveSoftware","Brave-Browser","NativeMessagingHosts"]},linux:{binaries:["brave-browser","brave"],dataPath:[".config","BraveSoftware","Brave-Browser"],nativeMessagingPath:[".config","BraveSoftware","Brave-Browser","NativeMessagingHosts"]},windows:{dataPath:["BraveSoftware","Brave-Browser","User Data"],registryKey:"HKCU\\Software\\BraveSoftware\\Brave-Browser\\NativeMessagingHosts"}},arc:{name:"Arc",macos:{appName:"Arc",dataPath:["Library","Application Support","Arc","User Data"],nativeMessagingPath:["Library","Application Support","Arc","User Data","NativeMessagingHosts"]},linux:{binaries:[],dataPath:[],nativeMessagingPath:[]},windows:{dataPath:["Arc","User Data"],registryKey:"HKCU\\Software\\ArcBrowser\\Arc\\NativeMessagingHosts"}},chromium:{name:"Chromium",macos:{appName:"Chromium",dataPath:["Library","Application Support","Chromium"],nativeMessagingPath:["Library","Application Support","Chromium","NativeMessagingHosts"]},linux:{binaries:["chromium","chromium-browser"],dataPath:[".config","chromium"],nativeMessagingPath:[".config","chromium","NativeMessagingHosts"]},windows:{dataPath:["Chromium","User Data"],registryKey:"HKCU\\Software\\Chromium\\NativeMessagingHosts"}},edge:{name:"Microsoft Edge",macos:{appName:"Microsoft Edge",dataPath:["Library","Application Support","Microsoft Edge"],nativeMessagingPath:["Library","Application Support","Microsoft Edge","NativeMessagingHosts"]},linux:{binaries:["microsoft-edge","microsoft-edge-stable"],dataPath:[".config","microsoft-edge"],nativeMessagingPath:[".config","microsoft-edge","NativeMessagingHosts"]},windows:{dataPath:["Microsoft","Edge","User Data"],registryKey:"HKCU\\Software\\Microsoft\\Edge\\NativeMessagingHosts"}},vivaldi:{name:"Vivaldi",macos:{appName:"Vivaldi",dataPath:["Library","Application Support","Vivaldi"],nativeMessagingPath:["Library","Application Support","Vivaldi","NativeMessagingHosts"]},linux:{binaries:["vivaldi","vivaldi-stable"],dataPath:[".config","vivaldi"],nativeMessagingPath:[".config","vivaldi","NativeMessagingHosts"]},windows:{dataPath:["Vivaldi","User Data"],registryKey:"HKCU\\Software\\Vivaldi\\NativeMessagingHosts"}},opera:{name:"Opera",macos:{appName:"Opera",dataPath:["Library","Application Support","com.operasoftware.Opera"],nativeMessagingPath:["Library","Application Support","com.operasoftware.Opera","NativeMessagingHosts"]},linux:{binaries:["opera"],dataPath:[".config","opera"],nativeMessagingPath:[".config","opera","NativeMessagingHosts"]},windows:{dataPath:["Opera Software","Opera Stable"],registryKey:"HKCU\\Software\\Opera Software\\Opera Stable\\NativeMessagingHosts",useRoaming:!0}}};export const BROWSER_DETECTION_ORDER=["chrome","brave","arc","edge","chromium","vivaldi","opera"];export function getAllBrowserDataPaths(){const a=m(),e=t(),r=[];for(const t of BROWSER_DETECTION_ORDER){const o=CHROMIUM_BROWSERS[t];let s;switch(a){case"macos":s=o.macos.dataPath;break;case"linux":case"wsl":s=o.linux.dataPath;break;case"windows":if(o.windows.dataPath.length>0){const a=o.windows.useRoaming?i(e,"AppData","Roaming"):i(e,"AppData","Local");r.push({browser:t,path:i(a,...o.windows.dataPath)})}continue}s&&s.length>0&&r.push({browser:t,path:i(e,...s)})}return r}export function getAllNativeMessagingHostsDirs(){const a=m(),e=t(),r=[];for(const t of BROWSER_DETECTION_ORDER){const o=CHROMIUM_BROWSERS[t];switch(a){case"macos":o.macos.nativeMessagingPath.length>0&&r.push({browser:t,path:i(e,...o.macos.nativeMessagingPath)});break;case"linux":case"wsl":o.linux.nativeMessagingPath.length>0&&r.push({browser:t,path:i(e,...o.linux.nativeMessagingPath)})}}return r}export function getAllWindowsRegistryKeys(){const a=[];for(const e of BROWSER_DETECTION_ORDER){const t=CHROMIUM_BROWSERS[e];t.windows.registryKey&&a.push({browser:e,key:t.windows.registryKey})}return a}export async function detectAvailableBrowser(){const a=m();for(const r of BROWSER_DETECTION_ORDER){const o=CHROMIUM_BROWSERS[r];switch(a){case"macos":{const a=`/Applications/${o.macos.appName}.app`;try{if((await e(a)).isDirectory())return c(`[Claude in Chrome] Detected browser: ${o.name}`),r}catch(a){if(!p(a))throw a}break}case"wsl":case"linux":for(const a of o.linux.binaries)if(await h(a).catch(()=>null))return c(`[Claude in Chrome] Detected browser: ${o.name}`),r;break;case"windows":{const a=t();if(o.windows.dataPath.length>0){const t=o.windows.useRoaming?i(a,"AppData","Roaming"):i(a,"AppData","Local"),s=i(t,...o.windows.dataPath);try{if((await e(s)).isDirectory())return c(`[Claude in Chrome] Detected browser: ${o.name}`),r}catch(a){if(!p(a))throw a}}break}}}return null}export function isClaudeInChromeMCPServer(a){const e=n(a);return"context-in-chrome"===e||"claude-in-chrome"===e}const u=new Set;export function trackClaudeInChromeTabId(a){u.size>=200&&!u.has(a)&&u.clear(),u.add(a)}export function isTrackedClaudeInChromeTabId(a){return u.has(a)}export async function openInChrome(a){const e=m(),t=await detectAvailableBrowser();if(!t)return c("[Claude in Chrome] No compatible browser found"),!1;const r=CHROMIUM_BROWSERS[t];switch(e){case"macos":{const{code:e}=await g("open",["-a",r.macos.appName,a]);return 0===e}case"windows":{const{code:e}=await g("rundll32",["url,OpenURL",a]);return 0===e}case"wsl":case"linux":for(const e of r.linux.binaries){const{code:t}=await g(e,[a]);if(0===t)return!0}return!1;default:return!1}}export function getSocketDir(){return`/tmp/context-mcp-browser-bridge-${getUsername()}`}export function getSecureSocketPath(){return"win32"===r()?`\\\\.\\pipe\\${getSocketName()}`:i(getSocketDir(),`${process.pid}.sock`)}export function getAllSocketPaths(){if("win32"===r())return[`\\\\.\\pipe\\${getSocketName()}`];const e=[],t=getSocketDir();try{const r=a(t);for(const a of r)a.endsWith(".sock")&&e.push(i(t,a))}catch{}const s=`claude-mcp-browser-bridge-${getUsername()}`,n=`context-mcp-browser-bridge-${getUsername()}`,c=i(o(),s),p=`/tmp/${s}`,g=i(o(),n),m=`/tmp/${n}`;return e.includes(c)||e.push(c),c===p||e.includes(p)||e.push(p),e.includes(g)||e.push(g),g===m||e.includes(m)||e.push(m),e}function getSocketName(){return`context-mcp-browser-bridge-${getUsername()}`}function getUsername(){try{return s().username||"default"}catch{return process.env.USER||process.env.USERNAME||"default"}}
1
+ import{readdirSync as a}from"fs";import{stat as e}from"fs/promises";import{homedir as t,platform as r,tmpdir as o,userInfo as s}from"os";import{join as i}from"path";import{normalizeNameForMCP as n}from"../../services/mcp/normalization.js";import{logForDebugging as c}from"../debug.js";import{isFsInaccessible as p}from"../errors.js";import{execFileNoThrow as g}from"../execFileNoThrow.js";import{getPlatform as m}from"../platform.js";import{which as h}from"../which.js";export const CLAUDE_IN_CHROME_MCP_SERVER_NAME="context-in-chrome";export const CHROMIUM_BROWSERS={chrome:{name:"Google Chrome",macos:{appName:"Google Chrome",dataPath:["Library","Application Support","Google","Chrome"],nativeMessagingPath:["Library","Application Support","Google","Chrome","NativeMessagingHosts"]},linux:{binaries:["google-chrome","google-chrome-stable"],dataPath:[".config","google-chrome"],nativeMessagingPath:[".config","google-chrome","NativeMessagingHosts"]},windows:{dataPath:["Google","Chrome","User Data"],registryKey:"HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts"}},brave:{name:"Brave",macos:{appName:"Brave Browser",dataPath:["Library","Application Support","BraveSoftware","Brave-Browser"],nativeMessagingPath:["Library","Application Support","BraveSoftware","Brave-Browser","NativeMessagingHosts"]},linux:{binaries:["brave-browser","brave"],dataPath:[".config","BraveSoftware","Brave-Browser"],nativeMessagingPath:[".config","BraveSoftware","Brave-Browser","NativeMessagingHosts"]},windows:{dataPath:["BraveSoftware","Brave-Browser","User Data"],registryKey:"HKCU\\Software\\BraveSoftware\\Brave-Browser\\NativeMessagingHosts"}},arc:{name:"Arc",macos:{appName:"Arc",dataPath:["Library","Application Support","Arc","User Data"],nativeMessagingPath:["Library","Application Support","Arc","User Data","NativeMessagingHosts"]},linux:{binaries:[],dataPath:[],nativeMessagingPath:[]},windows:{dataPath:["Arc","User Data"],registryKey:"HKCU\\Software\\ArcBrowser\\Arc\\NativeMessagingHosts"}},chromium:{name:"Chromium",macos:{appName:"Chromium",dataPath:["Library","Application Support","Chromium"],nativeMessagingPath:["Library","Application Support","Chromium","NativeMessagingHosts"]},linux:{binaries:["chromium","chromium-browser"],dataPath:[".config","chromium"],nativeMessagingPath:[".config","chromium","NativeMessagingHosts"]},windows:{dataPath:["Chromium","User Data"],registryKey:"HKCU\\Software\\Chromium\\NativeMessagingHosts"}},edge:{name:"Microsoft Edge",macos:{appName:"Microsoft Edge",dataPath:["Library","Application Support","Microsoft Edge"],nativeMessagingPath:["Library","Application Support","Microsoft Edge","NativeMessagingHosts"]},linux:{binaries:["microsoft-edge","microsoft-edge-stable"],dataPath:[".config","microsoft-edge"],nativeMessagingPath:[".config","microsoft-edge","NativeMessagingHosts"]},windows:{dataPath:["Microsoft","Edge","User Data"],registryKey:"HKCU\\Software\\Microsoft\\Edge\\NativeMessagingHosts"}},vivaldi:{name:"Vivaldi",macos:{appName:"Vivaldi",dataPath:["Library","Application Support","Vivaldi"],nativeMessagingPath:["Library","Application Support","Vivaldi","NativeMessagingHosts"]},linux:{binaries:["vivaldi","vivaldi-stable"],dataPath:[".config","vivaldi"],nativeMessagingPath:[".config","vivaldi","NativeMessagingHosts"]},windows:{dataPath:["Vivaldi","User Data"],registryKey:"HKCU\\Software\\Vivaldi\\NativeMessagingHosts"}},opera:{name:"Opera",macos:{appName:"Opera",dataPath:["Library","Application Support","com.operasoftware.Opera"],nativeMessagingPath:["Library","Application Support","com.operasoftware.Opera","NativeMessagingHosts"]},linux:{binaries:["opera"],dataPath:[".config","opera"],nativeMessagingPath:[".config","opera","NativeMessagingHosts"]},windows:{dataPath:["Opera Software","Opera Stable"],registryKey:"HKCU\\Software\\Opera Software\\Opera Stable\\NativeMessagingHosts",useRoaming:!0}}};export const BROWSER_DETECTION_ORDER=["chrome","brave","arc","edge","chromium","vivaldi","opera"];export function getAllBrowserDataPaths(){const a=m(),e=t(),r=[];for(const t of BROWSER_DETECTION_ORDER){const o=CHROMIUM_BROWSERS[t];let s;switch(a){case"macos":s=o.macos.dataPath;break;case"linux":case"wsl":s=o.linux.dataPath;break;case"windows":if(o.windows.dataPath.length>0){const a=o.windows.useRoaming?i(e,"AppData","Roaming"):i(e,"AppData","Local");r.push({browser:t,path:i(a,...o.windows.dataPath)})}continue}s&&s.length>0&&r.push({browser:t,path:i(e,...s)})}return r}export function getAllNativeMessagingHostsDirs(){const a=m(),e=t(),r=[];for(const t of BROWSER_DETECTION_ORDER){const o=CHROMIUM_BROWSERS[t];switch(a){case"macos":o.macos.nativeMessagingPath.length>0&&r.push({browser:t,path:i(e,...o.macos.nativeMessagingPath)});break;case"linux":case"wsl":o.linux.nativeMessagingPath.length>0&&r.push({browser:t,path:i(e,...o.linux.nativeMessagingPath)})}}return r}export function getAllWindowsRegistryKeys(){const a=[];for(const e of BROWSER_DETECTION_ORDER){const t=CHROMIUM_BROWSERS[e];t.windows.registryKey&&a.push({browser:e,key:t.windows.registryKey})}return a}export async function detectAvailableBrowser(){const a=m();for(const r of BROWSER_DETECTION_ORDER){const o=CHROMIUM_BROWSERS[r];switch(a){case"macos":{const t=`/Applications/${o.macos.appName}.app`;try{if((await e(t)).isDirectory())return c(`[Claude in Chrome] Detected browser: ${o.name}`),r}catch(a){if(!p(a))throw a}break}case"wsl":case"linux":for(const a of o.linux.binaries)if(await h(a).catch(()=>null))return c(`[Claude in Chrome] Detected browser: ${o.name}`),r;break;case"windows":{const s=t();if(o.windows.dataPath.length>0){const t=o.windows.useRoaming?i(s,"AppData","Roaming"):i(s,"AppData","Local"),n=i(t,...o.windows.dataPath);try{if((await e(n)).isDirectory())return c(`[Claude in Chrome] Detected browser: ${o.name}`),r}catch(a){if(!p(a))throw a}}break}}}return null}export function isClaudeInChromeMCPServer(a){const e=n(a);return"context-in-chrome"===e||"claude-in-chrome"===e}const u=new Set;export function trackClaudeInChromeTabId(a){u.size>=200&&!u.has(a)&&u.clear(),u.add(a)}export function isTrackedClaudeInChromeTabId(a){return u.has(a)}export async function openInChrome(a){const e=m(),t=await detectAvailableBrowser();if(!t)return c("[Claude in Chrome] No compatible browser found"),!1;const r=CHROMIUM_BROWSERS[t];switch(e){case"macos":{const{code:e}=await g("open",["-a",r.macos.appName,a]);return 0===e}case"windows":{const{code:e}=await g("rundll32",["url,OpenURL",a]);return 0===e}case"wsl":case"linux":for(const e of r.linux.binaries){const{code:t}=await g(e,[a]);if(0===t)return!0}return!1;default:return!1}}export function getSocketDir(){return`/tmp/context-mcp-browser-bridge-${getUsername()}`}export function getSecureSocketPath(){return"win32"===r()?`\\\\.\\pipe\\${getSocketName()}`:i(getSocketDir(),`${process.pid}.sock`)}export function getAllSocketPaths(){if("win32"===r())return[`\\\\.\\pipe\\${getSocketName()}`];const e=[],t=getSocketDir();try{const r=a(t);for(const a of r)a.endsWith(".sock")&&e.push(i(t,a))}catch{}const s=`claude-mcp-browser-bridge-${getUsername()}`,n=`context-mcp-browser-bridge-${getUsername()}`,c=i(o(),s),p=`/tmp/${s}`,g=i(o(),n),m=`/tmp/${n}`;return e.includes(c)||e.push(c),c===p||e.includes(p)||e.push(p),e.includes(g)||e.push(g),g===m||e.includes(m)||e.push(m),e}function getSocketName(){return`context-mcp-browser-bridge-${getUsername()}`}function getUsername(){try{return s().username||"default"}catch{return process.env.USER||process.env.USERNAME||"default"}}
@@ -1 +1 @@
1
- import{readdir as o}from"fs/promises";import{homedir as r}from"os";import{join as e}from"path";import{isFsInaccessible as t}from"../errors.js";export const CHROME_EXTENSION_URL="chrome://extensions/";const n=process.env.CONTEXT_CODE_CHROME_EXTENSION_ID||"my_context_code_extension_id";const a=["chrome","brave","arc","edge","chromium","vivaldi","opera"],i={chrome:{macos:["Library","Application Support","Google","Chrome"],linux:[".config","google-chrome"],windows:{path:["Google","Chrome","User Data"]}},brave:{macos:["Library","Application Support","BraveSoftware","Brave-Browser"],linux:[".config","BraveSoftware","Brave-Browser"],windows:{path:["BraveSoftware","Brave-Browser","User Data"]}},arc:{macos:["Library","Application Support","Arc","User Data"],linux:[],windows:{path:["Arc","User Data"]}},chromium:{macos:["Library","Application Support","Chromium"],linux:[".config","chromium"],windows:{path:["Chromium","User Data"]}},edge:{macos:["Library","Application Support","Microsoft Edge"],linux:[".config","microsoft-edge"],windows:{path:["Microsoft","Edge","User Data"]}},vivaldi:{macos:["Library","Application Support","Vivaldi"],linux:[".config","vivaldi"],windows:{path:["Vivaldi","User Data"]}},opera:{macos:["Library","Application Support","com.operasoftware.Opera"],linux:[".config","opera"],windows:{path:["Opera Software","Opera Stable"],useRoaming:!0}}};export function getAllBrowserDataPathsPortable(){const o=r(),t=[];for(const r of a){const n=i[r];let a;switch(process.platform){case"darwin":a=n.macos;break;case"linux":a=n.linux;break;case"win32":if(n.windows.path.length>0){const a=n.windows.useRoaming?e(o,"AppData","Roaming"):e(o,"AppData","Local");t.push({browser:r,path:e(a,...n.windows.path)})}continue}a&&a.length>0&&t.push({browser:r,path:e(o,...a)})}return t}export async function detectExtensionInstallationPortable(r,a){if(0===r.length)return a?.("[Claude in Chrome] No browser paths to check"),{isInstalled:!1,browser:null};const i=function(){const o=[n,"fcoeoabgfenejglbffodgkkbkcdhcgfn"];return"ant"===process.env.USER_TYPE?[...o,"dihbgbndebgnbjfmelmegjepbnkhlgni","dngcpimnedloihjnnfngkgjoidhnaolf"]:o}();for(const{browser:n,path:s}of r){let r=[];try{r=await o(s,{withFileTypes:!0})}catch(o){if(t(o))continue;throw o}const c=r.filter(o=>o.isDirectory()).filter(o=>"Default"===o.name||o.name.startsWith("Profile ")).map(o=>o.name);c.length>0&&a?.(`[Claude in Chrome] Found ${n} profiles: ${c.join(", ")}`);for(const r of c)for(const t of i){const i=e(s,r,"Extensions",t);try{return await o(i),a?.(`[Claude in Chrome] Extension ${t} found in ${n} ${r}`),{isInstalled:!0,browser:n}}catch{}}}return a?.("[Claude in Chrome] Extension not found in any browser"),{isInstalled:!1,browser:null}}export async function isChromeExtensionInstalledPortable(o,r){return(await detectExtensionInstallationPortable(o,r)).isInstalled}export function isChromeExtensionInstalled(o){return isChromeExtensionInstalledPortable(getAllBrowserDataPathsPortable(),o)}
1
+ import{readdir as o}from"fs/promises";import{homedir as e}from"os";import{join as r}from"path";import{isFsInaccessible as t}from"../errors.js";export const CHROME_EXTENSION_URL="chrome://extensions/";const a=process.env.CONTEXT_CODE_CHROME_EXTENSION_ID||"my_context_code_extension_id",n=["chrome","brave","arc","edge","chromium","vivaldi","opera"],i={chrome:{macos:["Library","Application Support","Google","Chrome"],linux:[".config","google-chrome"],windows:{path:["Google","Chrome","User Data"]}},brave:{macos:["Library","Application Support","BraveSoftware","Brave-Browser"],linux:[".config","BraveSoftware","Brave-Browser"],windows:{path:["BraveSoftware","Brave-Browser","User Data"]}},arc:{macos:["Library","Application Support","Arc","User Data"],linux:[],windows:{path:["Arc","User Data"]}},chromium:{macos:["Library","Application Support","Chromium"],linux:[".config","chromium"],windows:{path:["Chromium","User Data"]}},edge:{macos:["Library","Application Support","Microsoft Edge"],linux:[".config","microsoft-edge"],windows:{path:["Microsoft","Edge","User Data"]}},vivaldi:{macos:["Library","Application Support","Vivaldi"],linux:[".config","vivaldi"],windows:{path:["Vivaldi","User Data"]}},opera:{macos:["Library","Application Support","com.operasoftware.Opera"],linux:[".config","opera"],windows:{path:["Opera Software","Opera Stable"],useRoaming:!0}}};export function getAllBrowserDataPathsPortable(){const o=e(),t=[];for(const e of n){const a=i[e];let n;switch(process.platform){case"darwin":n=a.macos;break;case"linux":n=a.linux;break;case"win32":if(a.windows.path.length>0){const n=a.windows.useRoaming?r(o,"AppData","Roaming"):r(o,"AppData","Local");t.push({browser:e,path:r(n,...a.windows.path)})}continue}n&&n.length>0&&t.push({browser:e,path:r(o,...n)})}return t}export async function detectExtensionInstallationPortable(e,n){if(0===e.length)return n?.("[Claude in Chrome] No browser paths to check"),{isInstalled:!1,browser:null};const i=function(){const o=[a,"fcoeoabgfenejglbffodgkkbkcdhcgfn"];return"ant"===process.env.USER_TYPE?[...o,"dihbgbndebgnbjfmelmegjepbnkhlgni","dngcpimnedloihjnnfngkgjoidhnaolf"]:o}();for(const{browser:a,path:s}of e){let e=[];try{e=await o(s,{withFileTypes:!0})}catch(o){if(t(o))continue;throw o}const c=e.filter(o=>o.isDirectory()).filter(o=>"Default"===o.name||o.name.startsWith("Profile ")).map(o=>o.name);c.length>0&&n?.(`[Claude in Chrome] Found ${a} profiles: ${c.join(", ")}`);for(const e of c)for(const t of i){const i=r(s,e,"Extensions",t);try{return await o(i),n?.(`[Claude in Chrome] Extension ${t} found in ${a} ${e}`),{isInstalled:!0,browser:a}}catch{}}}return n?.("[Claude in Chrome] Extension not found in any browser"),{isInstalled:!1,browser:null}}export async function isChromeExtensionInstalledPortable(o,e){return(await detectExtensionInstallationPortable(o,e)).isInstalled}export function isChromeExtensionInstalled(o){return isChromeExtensionInstalledPortable(getAllBrowserDataPathsPortable(),o)}
@@ -1 +1 @@
1
- import{feature as e}from"../recovery/bunBundleShim.js";import{createRequire as t}from"module";const s=t(import.meta.url);import o from"ignore";import n from"lodash-es/memoize.js";import{Lexer as r}from"marked";import{basename as i,dirname as c,extname as a,isAbsolute as l,join as u,parse as m,relative as p,sep as d}from"path";import f from"picomatch";import{logEvent as h}from"../services/analytics/index.js";import{getAdditionalDirectoriesForClaudeMd as y,getOriginalCwd as M}from"../bootstrap/state.js";import{truncateEntrypointContent as g}from"../memdir/memdir.js";import{getAutoMemEntrypoint as x,isAutoMemoryEnabled as E}from"../memdir/paths.js";import{getFeatureValue_CACHED_MAY_BE_STALE as C}from"../services/analytics/growthbook.js";import{getCurrentProjectConfig as w,getManagedClaudeRulesDir as j,getMemoryPath as _,getUserClaudeRulesDir as A}from"./config.js";import{logForDebugging as F}from"./debug.js";import{logForDiagnosticsNoPII as D}from"./diagLogs.js";import{getClaudeConfigHomeDir as P,isEnvTruthy as T}from"./envUtils.js";import{getErrnoCode as R}from"./errors.js";import{normalizePathForComparison as S}from"./file.js";import{cacheKeys as v}from"./fileStateCache.js";import{parseFrontmatter as I,splitPathInFrontmatter as L}from"./frontmatterParser.js";import{getFsImplementation as b,safeResolvePath as U}from"./fsOperations.js";import{findCanonicalGitRoot as O,findGitRoot as k}from"./git.js";import{executeInstructionsLoadedHooks as N,hasInstructionsLoadedHook as W}from"./hooks.js";import{expandPath as $}from"./path.js";import{pathInWorkingPath as X}from"./permissions/filesystem.js";import{isSettingSourceEnabled as H}from"./settings/constants.js";import{getInitialSettings as z}from"./settings/settings.js";const q=e("TEAMMEM")?s("../memdir/teamMemPaths.js"):null;let B=!1;export const MAX_MEMORY_CHARACTER_COUNT=4e4;const G=new Set([".md",".txt",".text",".json",".yaml",".yml",".toml",".xml",".csv",".html",".htm",".css",".scss",".sass",".less",".js",".ts",".tsx",".jsx",".mjs",".cjs",".mts",".cts",".py",".pyi",".pyw",".rb",".erb",".rake",".go",".rs",".java",".kt",".kts",".scala",".c",".cpp",".cc",".cxx",".h",".hpp",".hxx",".cs",".swift",".sh",".bash",".zsh",".fish",".ps1",".bat",".cmd",".env",".ini",".cfg",".conf",".config",".properties",".sql",".graphql",".gql",".proto",".vue",".svelte",".astro",".ejs",".hbs",".pug",".jade",".php",".pl",".pm",".lua",".r",".R",".dart",".ex",".exs",".erl",".hrl",".clj",".cljs",".cljc",".edn",".hs",".lhs",".elm",".ml",".mli",".f",".f90",".f95",".for",".cmake",".make",".makefile",".gradle",".sbt",".rst",".adoc",".asciidoc",".org",".tex",".latex",".lock",".log",".diff",".patch"]);function pathInOriginalCwd(e){return X(e,M())}export function stripHtmlComments(e){return e.includes("\x3c!--")?stripHtmlCommentsFromTokens(new r({gfm:!1}).lex(e)):{content:e,stripped:!1}}function stripHtmlCommentsFromTokens(e){let t="",s=!1;const o=/<!--[\s\S]*?-->/g;for(const n of e){if("html"===n.type){const e=n.raw.trimStart();if(e.startsWith("\x3c!--")&&e.includes("--\x3e")){const e=n.raw.replace(o,"");s=!0,e.trim().length>0&&(t+=e);continue}}t+=n.raw}return{content:t,stripped:s}}function parseMemoryFileContent(e,t,s,o){const n=a(t).toLowerCase();if(n&&!G.has(n))return F(`Skipping non-text file in @include: ${t}`),{info:null,includePaths:[]};const{content:i,paths:l}=function(e){const{frontmatter:t,content:s}=I(e);if(!t.paths)return{content:s};const o=L(t.paths).map(e=>e.endsWith("/**")?e.slice(0,-3):e).filter(e=>e.length>0);return 0===o.length||o.every(e=>"**"===e)?{content:s}:{content:s,paths:o}}(e),u=i.includes("\x3c!--"),m=u||void 0!==o?new r({gfm:!1}).lex(i):void 0,p=u&&m?stripHtmlCommentsFromTokens(m).content:i,d=m&&void 0!==o?function(e,t){const s=new Set;function extractPathsFromText(e){const o=/(?:^|\s)@((?:[^\s\\]|\\ )+)/g;let n;for(;null!==(n=o.exec(e));){let e=n[1];if(!e)continue;const o=e.indexOf("#");if(-1!==o&&(e=e.substring(0,o)),e&&(e=e.replace(/\\ /g," "),e)){if(e.startsWith("./")||e.startsWith("~/")||e.startsWith("/")&&"/"!==e||!e.startsWith("@")&&!e.match(/^[#%^&*()]+/)&&e.match(/^[a-zA-Z0-9._-]/)){const o=$(e,c(t));s.add(o)}}}}function processElements(e){for(const t of e)if("code"!==t.type&&"codespan"!==t.type){if("html"===t.type){const e=t.raw||"",s=e.trimStart();if(s.startsWith("\x3c!--")&&s.includes("--\x3e")){const t=/<!--[\s\S]*?-->/g,s=e.replace(t,"");s.trim().length>0&&extractPathsFromText(s)}continue}"text"===t.type&&extractPathsFromText(t.text||""),t.tokens&&processElements(t.tokens),t.items&&processElements(t.items)}}return processElements(e),[...s]}(m,o):[];let f=p;"AutoMem"!==s&&"TeamMem"!==s||(f=g(p).content);const h=f!==e;return{info:{path:t,type:s,content:f,globs:l,contentDiffersFromDisk:h,rawContent:h?e:void 0},includePaths:d}}async function safelyReadMemoryFileAsync(e,t,s){try{const o=b();return parseMemoryFileContent(await o.readFile(e,{encoding:"utf-8"}),e,t,s)}catch(t){return function(e,t){const s=R(e);"ENOENT"!==s&&"EISDIR"!==s&&"EACCES"===s&&h("tengu_claude_md_permission_error",{is_access_error:1,has_home_dir:t.includes(P())?1:0})}(t,e),{info:null,includePaths:[]}}}function isClaudeMdExcluded(e,t){if("User"!==t&&"Project"!==t&&"Local"!==t)return!1;const s=z().claudeMdExcludes;if(!s||0===s.length)return!1;const o=e.replaceAll("\\","/"),n=function(e){const t=b(),s=e.map(e=>e.replaceAll("\\","/"));for(const e of s){if(!e.startsWith("/"))continue;const o=e.search(/[*?{[]/),n=-1===o?e:e.slice(0,o),r=c(n);try{const o=t.realpathSync(r).replaceAll("\\","/");if(o!==r){const t=o+e.slice(r.length);s.push(t)}}catch{}}return s}(s).filter(e=>e.length>0);return 0!==n.length&&f.isMatch(o,n,{dot:!0})}export async function processMemoryFile(e,t,s,o,n=0,r){const i=S(e);if(s.has(i)||n>=5)return[];if(isClaudeMdExcluded(e,t))return[];const{resolvedPath:c,isSymlink:a}=U(b(),e);s.add(i),a&&s.add(S(c));const{info:l,includePaths:u}=await safelyReadMemoryFileAsync(e,t,c);if(!l||!l.content.trim())return[];r&&(l.parent=r);const m=[];m.push(l);for(const r of u){if(!pathInOriginalCwd(r)&&!o)continue;const i=await processMemoryFile(r,t,s,o,n+1,e);m.push(...i)}return m}export async function processMdRules({rulesDir:e,type:t,processedPaths:s,includeExternal:o,conditionalRule:n,visitedDirs:r=new Set}){if(r.has(e))return[];try{const i=b(),{resolvedPath:c,isSymlink:a}=U(i,dotContextRulesDir);r.add(e),a&&r.add(c);const l=[];let m;try{m=await i.readdir(c)}catch(e){const t=R(e);if("ENOENT"===t||"EACCES"===t||"ENOTDIR"===t)return[];throw e}for(const c of m){const a=u(e,c.name),{resolvedPath:m,isSymlink:p}=U(i,a),d=p?await i.stat(m):null,f=d?d.isDirectory():c.isDirectory(),h=d?d.isFile():c.isFile();if(f)l.push(...await processMdRules({rulesDir:m,type:t,processedPaths:s,includeExternal:o,conditionalRule:n,visitedDirs:r}));else if(h&&c.name.endsWith(".md")){const e=await processMemoryFile(m,t,s,o);l.push(...e.filter(e=>n?e.globs:!e.globs))}}return l}catch(t){return t instanceof Error&&t.message.includes("EACCES")&&h("tengu_claude_rules_md_permission_error",{is_access_error:1,has_home_dir:e.includes(P())?1:0}),[]}}export const getMemoryFiles=n(async(t=!1)=>{const s=Date.now();D("info","memory_files_started");const o=[],n=new Set,r=w(),i=t||r.hasClaudeMdExternalIncludesApproved||!1,a=_("Managed");o.push(...await processMemoryFile(a,"Managed",n,i));const l=j();if(o.push(...await processMdRules({rulesDir:l,type:"Managed",processedPaths:n,includeExternal:i,conditionalRule:!1})),H("userSettings")){const e=_("User");o.push(...await processMemoryFile(e,"User",n,!0));const t=A();o.push(...await processMdRules({rulesDir:t,type:"User",processedPaths:n,includeExternal:!0,conditionalRule:!1}))}const p=[],d=M();let f=d;for(;f!==m(f).root;)p.push(f),f=c(f);const g=k(d),C=O(d),F=null!==g&&null!==C&&S(g)!==S(C)&&X(g,C);for(const e of p.reverse()){const t=F&&X(e,C)&&!X(e,g);if(H("projectSettings")&&!t){const t=u(e,"CLAUDE.md");o.push(...await processMemoryFile(t,"Project",n,i));const s=u(e,".context","CONTEXT.md");o.push(...await processMemoryFile(s,"Project",n,i));const r=u(e,".context","CLAUDE.md");o.push(...await processMemoryFile(r,"Project",n,i));const c=u(e,".context","rules");o.push(...await processMdRules({rulesDir:c,type:"Project",processedPaths:n,includeExternal:i,conditionalRule:!1}))}if(H("localSettings")){const t=u(e,"CLAUDE.local.md");o.push(...await processMemoryFile(t,"Local",n,i))}}if(T(process.env.CONTEXT_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD)||T(process.env.CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD)){const e=y();for(const t of e){const e=u(t,"CLAUDE.md");o.push(...await processMemoryFile(e,"Project",n,i));const s=u(t,".context","CONTEXT.md");o.push(...await processMemoryFile(s,"Project",n,i));const r=u(t,".context","CLAUDE.md");o.push(...await processMemoryFile(r,"Project",n,i));const c=u(t,".context","rules");o.push(...await processMdRules({rulesDir:c,type:"Project",processedPaths:n,includeExternal:i,conditionalRule:!1}))}}if(E()){const{info:e}=await safelyReadMemoryFileAsync(x(),"AutoMem");if(e){const t=S(e.path);n.has(t)||(n.add(t),o.push(e))}}if(e("TEAMMEM")&&q.isTeamMemoryEnabled()){const{info:e}=await safelyReadMemoryFileAsync(q.getTeamMemEntrypoint(),"TeamMem");if(e){const t=S(e.path);n.has(t)||(n.add(t),o.push(e))}}const P=o.reduce((e,t)=>e+t.content.length,0);D("info","memory_files_completed",{duration_ms:Date.now()-s,file_count:o.length,total_content_length:P});const R={};for(const e of o)R[e.type]=(R[e.type]??0)+1;if(B||(B=!0,h("tengu_claudemd__initial_load",{file_count:o.length,total_content_length:P,user_count:R.User??0,project_count:R.Project??0,local_count:R.Local??0,managed_count:R.Managed??0,automem_count:R.AutoMem??0,...e("TEAMMEM")?{teammem_count:R.TeamMem??0}:{},duration_ms:Date.now()-s})),!t){const e=function(){if(!Y)return;Y=!1;const e=V;return V="session_start",e}();if(void 0!==e&&W())for(const t of o){if(!isInstructionsMemoryType(t.type))continue;const s=t.parent?"include":e;N(t.path,t.type,s,{globs:t.globs,parentFilePath:t.parent})}}return o});function isInstructionsMemoryType(e){return"User"===e||"Project"===e||"Local"===e||"Managed"===e}let V="session_start",Y=!0;export function clearMemoryFileCaches(){getMemoryFiles.cache?.clear?.()}export function resetGetMemoryFilesCache(e="session_start"){V=e,Y=!0,clearMemoryFileCaches()}export function getLargeMemoryFiles(e){return e.filter(e=>e.content.length>4e4)}export function filterInjectedMemoryFiles(e){return C("tengu_moth_copse",!1)?e.filter(e=>"AutoMem"!==e.type&&"TeamMem"!==e.type):e}export const getClaudeMds=(t,s)=>{const o=[],n=C("tengu_paper_halyard",!1);for(const r of t)if((!s||s(r.type))&&(!n||"Project"!==r.type&&"Local"!==r.type)&&r.content){const t="Project"===r.type?" (project instructions, checked into the codebase)":"Local"===r.type?" (user's private project instructions, not checked in)":e("TEAMMEM")&&"TeamMem"===r.type?" (shared team memory, synced across the organization)":"AutoMem"===r.type?" (user's auto-memory, persists across conversations)":" (user's private global instructions for all projects)",s=r.content.trim();e("TEAMMEM")&&"TeamMem"===r.type?o.push(`Contents of ${r.path}${t}:\n\n<team-memory-content source="shared">\n${s}\n</team-memory-content>`):o.push(`Contents of ${r.path}${t}:\n\n${s}`)}return 0===o.length?"":`Codebase and user instructions are shown below. Be sure to adhere to these instructions. IMPORTANT: These instructions OVERRIDE any default behavior and you MUST follow them exactly as written.\n\n${o.join("\n\n")}`};export async function getManagedAndUserConditionalRules(e,t){const s=[],o=j();if(s.push(...await processConditionedMdRules(e,o,"Managed",t,!1)),H("userSettings")){const o=A();s.push(...await processConditionedMdRules(e,o,"User",t,!0))}return s}export async function getMemoryFilesForNestedDirectory(e,t,s){const o=[];if(H("projectSettings")){const t=u(e,"CLAUDE.md");o.push(...await processMemoryFile(t,"Project",s,!1));const n=u(e,".context","CONTEXT.md");o.push(...await processMemoryFile(n,"Project",s,!1));const r=u(e,".context","CLAUDE.md");o.push(...await processMemoryFile(r,"Project",s,!1))}if(H("localSettings")){const t=u(e,"CLAUDE.local.md");o.push(...await processMemoryFile(t,"Local",s,!1))}const n=u(e,".context","rules"),r=new Set(s);o.push(...await processMdRules({dotContextRulesDir:n,type:"Project",processedPaths:r,includeExternal:!1,conditionalRule:!1})),o.push(...await processConditionedMdRules(t,n,"Project",s,!1));for(const e of r)s.add(e);return o}export async function getConditionalRulesForCwdLevelDirectory(e,t,s){return processConditionedMdRules(t,u(e,".context","rules"),"Project",s,!1)}export async function processConditionedMdRules(e,t,s,n,r){return(await processMdRules({rulesDir:t,type:s,processedPaths:n,includeExternal:r,conditionalRule:!0})).filter(n=>{if(!n.globs||0===n.globs.length)return!1;const r="Project"===s?c(c(t)):M(),i=l(e)?p(r,e):e;return!(!i||i.startsWith("..")||l(i))&&o().add(n.globs).ignores(i)})}export function getExternalClaudeMdIncludes(e){const t=[];for(const s of e)"User"!==s.type&&s.parent&&!pathInOriginalCwd(s.path)&&t.push({path:s.path,parent:s.parent});return t}export function hasExternalClaudeMdIncludes(e){return getExternalClaudeMdIncludes(e).length>0}export async function shouldShowClaudeMdExternalIncludesWarning(){const e=w();return!e.hasClaudeMdExternalIncludesApproved&&!e.hasClaudeMdExternalIncludesWarningShown&&hasExternalClaudeMdIncludes(await getMemoryFiles(!0))}export function isMemoryFilePath(e){const t=i(e);return"CLAUDE.md"===t||"CLAUDE.local.md"===t||(!("CONTEXT.md"!==t||!e.includes(`${d}.context${d}`))||!(!t.endsWith(".md")||!e.includes(`${d}.context${d}rules${d}`)))}export function getAllMemoryFilePaths(e,t){const s=new Set;for(const t of e)t.content.trim().length>0&&s.add(t.path);for(const e of v(t))isMemoryFilePath(e)&&s.add(e);return Array.from(s)}
1
+ import{createRequire as e}from"module";const t=e(import.meta.url);import{feature as s}from"bun:bundle";import o from"ignore";import n from"lodash-es/memoize.js";import{Lexer as r}from"marked";import{basename as a,dirname as i,extname as c,isAbsolute as l,join as u,parse as m,relative as d,sep as p}from"path";import f from"picomatch";import{logEvent as h}from"src/services/analytics/index.js";import{getAdditionalDirectoriesForClaudeMd as y,getOriginalCwd as g}from"../bootstrap/state.js";import{truncateEntrypointContent as M}from"../memdir/memdir.js";import{getAutoMemEntrypoint as x,isAutoMemoryEnabled as E}from"../memdir/paths.js";import{getFeatureValue_CACHED_MAY_BE_STALE as C}from"../services/analytics/growthbook.js";import{getCurrentProjectConfig as w,getManagedClaudeRulesDir as _,getMemoryPath as j,getUserClaudeRulesDir as A}from"./config.js";import{logForDebugging as D}from"./debug.js";import{logForDiagnosticsNoPII as F}from"./diagLogs.js";import{getClaudeConfigHomeDir as P,isEnvTruthy as R}from"./envUtils.js";import{getErrnoCode as T}from"./errors.js";import{normalizePathForComparison as I}from"./file.js";import{cacheKeys as S}from"./fileStateCache.js";import{parseFrontmatter as v,splitPathInFrontmatter as b}from"./frontmatterParser.js";import{getFsImplementation as L,safeResolvePath as U}from"./fsOperations.js";import{findCanonicalGitRoot as k,findGitRoot as O}from"./git.js";import{executeInstructionsLoadedHooks as N,hasInstructionsLoadedHook as W}from"./hooks.js";import{expandPath as $}from"./path.js";import{pathInWorkingPath as H}from"./permissions/filesystem.js";import{isSettingSourceEnabled as X}from"./settings/constants.js";import{getInitialSettings as z}from"./settings/settings.js";const q=s("TEAMMEM")?t("../memdir/teamMemPaths.js"):null;let G=!1;export const MAX_MEMORY_CHARACTER_COUNT=4e4;const B=new Set([".md",".txt",".text",".json",".yaml",".yml",".toml",".xml",".csv",".html",".htm",".css",".scss",".sass",".less",".js",".ts",".tsx",".jsx",".mjs",".cjs",".mts",".cts",".py",".pyi",".pyw",".rb",".erb",".rake",".go",".rs",".java",".kt",".kts",".scala",".c",".cpp",".cc",".cxx",".h",".hpp",".hxx",".cs",".swift",".sh",".bash",".zsh",".fish",".ps1",".bat",".cmd",".env",".ini",".cfg",".conf",".config",".properties",".sql",".graphql",".gql",".proto",".vue",".svelte",".astro",".ejs",".hbs",".pug",".jade",".php",".pl",".pm",".lua",".r",".R",".dart",".ex",".exs",".erl",".hrl",".clj",".cljs",".cljc",".edn",".hs",".lhs",".elm",".ml",".mli",".f",".f90",".f95",".for",".cmake",".make",".makefile",".gradle",".sbt",".rst",".adoc",".asciidoc",".org",".tex",".latex",".lock",".log",".diff",".patch"]);function pathInOriginalCwd(e){return H(e,g())}export function stripHtmlComments(e){return e.includes("\x3c!--")?stripHtmlCommentsFromTokens(new r({gfm:!1}).lex(e)):{content:e,stripped:!1}}function stripHtmlCommentsFromTokens(e){let t="",s=!1;const o=/<!--[\s\S]*?-->/g;for(const n of e){if("html"===n.type){const e=n.raw.trimStart();if(e.startsWith("\x3c!--")&&e.includes("--\x3e")){const e=n.raw.replace(o,"");s=!0,e.trim().length>0&&(t+=e);continue}}t+=n.raw}return{content:t,stripped:s}}async function safelyReadMemoryFileAsync(e,t,s){try{const o=L();return function(e,t,s,o){const n=c(t).toLowerCase();if(n&&!B.has(n))return D(`Skipping non-text file in @include: ${t}`),{info:null,includePaths:[]};const{content:a,paths:l}=function(e){const{frontmatter:t,content:s}=v(e);if(!t.paths)return{content:s};const o=b(t.paths).map(e=>e.endsWith("/**")?e.slice(0,-3):e).filter(e=>e.length>0);return 0===o.length||o.every(e=>"**"===e)?{content:s}:{content:s,paths:o}}(e),u=a.includes("\x3c!--"),m=u||void 0!==o?new r({gfm:!1}).lex(a):void 0,d=u&&m?stripHtmlCommentsFromTokens(m).content:a,p=m&&void 0!==o?function(e,t){const s=new Set;function extractPathsFromText(e){const o=/(?:^|\s)@((?:[^\s\\]|\\ )+)/g;let n;for(;null!==(n=o.exec(e));){let e=n[1];if(!e)continue;const o=e.indexOf("#");if(-1!==o&&(e=e.substring(0,o)),e&&(e=e.replace(/\\ /g," "),e)&&(e.startsWith("./")||e.startsWith("~/")||e.startsWith("/")&&"/"!==e||!e.startsWith("@")&&!e.match(/^[#%^&*()]+/)&&e.match(/^[a-zA-Z0-9._-]/))){const o=$(e,i(t));s.add(o)}}}return function processElements(e){for(const t of e)if("code"!==t.type&&"codespan"!==t.type){if("html"===t.type){const e=t.raw||"",s=e.trimStart();if(s.startsWith("\x3c!--")&&s.includes("--\x3e")){const t=/<!--[\s\S]*?-->/g,s=e.replace(t,"");s.trim().length>0&&extractPathsFromText(s)}continue}"text"===t.type&&extractPathsFromText(t.text||""),t.tokens&&processElements(t.tokens),t.items&&processElements(t.items)}}(e),[...s]}(m,o):[];let f=d;"AutoMem"!==s&&"TeamMem"!==s||(f=M(d).content);const h=f!==e;return{info:{path:t,type:s,content:f,globs:l,contentDiffersFromDisk:h,rawContent:h?e:void 0},includePaths:p}}(await o.readFile(e,{encoding:"utf-8"}),e,t,s)}catch(t){return function(e,t){const s=T(e);"ENOENT"!==s&&"EISDIR"!==s&&"EACCES"===s&&h("tengu_claude_md_permission_error",{is_access_error:1,has_home_dir:t.includes(P())?1:0})}(t,e),{info:null,includePaths:[]}}}function isClaudeMdExcluded(e,t){if("User"!==t&&"Project"!==t&&"Local"!==t)return!1;const s=z().claudeMdExcludes;if(!s||0===s.length)return!1;const o=e.replaceAll("\\","/"),n=function(e){const t=L(),s=e.map(e=>e.replaceAll("\\","/"));for(const e of s){if(!e.startsWith("/"))continue;const o=e.search(/[*?{[]/),n=-1===o?e:e.slice(0,o),r=i(n);try{const o=t.realpathSync(r).replaceAll("\\","/");if(o!==r){const t=o+e.slice(r.length);s.push(t)}}catch{}}return s}(s).filter(e=>e.length>0);return 0!==n.length&&f.isMatch(o,n,{dot:!0})}export async function processMemoryFile(e,t,s,o,n=0,r){const a=I(e);if(s.has(a)||n>=5)return[];if(isClaudeMdExcluded(e,t))return[];const{resolvedPath:i,isSymlink:c}=U(L(),e);s.add(a),c&&s.add(I(i));const{info:l,includePaths:u}=await safelyReadMemoryFileAsync(e,t,i);if(!l||!l.content.trim())return[];r&&(l.parent=r);const m=[];m.push(l);for(const r of u){if(!pathInOriginalCwd(r)&&!o)continue;const a=await processMemoryFile(r,t,s,o,n+1,e);m.push(...a)}return m}export async function processMdRules({rulesDir:e,type:t,processedPaths:s,includeExternal:o,conditionalRule:n,visitedDirs:r=new Set}){if(r.has(e))return[];try{const a=L(),{resolvedPath:i,isSymlink:c}=U(a,dotContextRulesDir);r.add(e),c&&r.add(i);const l=[];let m;try{m=await a.readdir(i)}catch(e){const t=T(e);if("ENOENT"===t||"EACCES"===t||"ENOTDIR"===t)return[];throw e}for(const i of m){const c=u(e,i.name),{resolvedPath:m,isSymlink:d}=U(a,c),p=d?await a.stat(m):null,f=p?p.isDirectory():i.isDirectory(),h=p?p.isFile():i.isFile();if(f)l.push(...await processMdRules({rulesDir:m,type:t,processedPaths:s,includeExternal:o,conditionalRule:n,visitedDirs:r}));else if(h&&i.name.endsWith(".md")){const e=await processMemoryFile(m,t,s,o);l.push(...e.filter(e=>n?e.globs:!e.globs))}}return l}catch(t){return t instanceof Error&&t.message.includes("EACCES")&&h("tengu_claude_rules_md_permission_error",{is_access_error:1,has_home_dir:e.includes(P())?1:0}),[]}}export const getMemoryFiles=n(async(e=!1)=>{const t=Date.now();F("info","memory_files_started");const o=[],n=new Set,r=w(),a=e||r.hasClaudeMdExternalIncludesApproved||!1,c=j("Managed");o.push(...await processMemoryFile(c,"Managed",n,a));const l=_();if(o.push(...await processMdRules({rulesDir:l,type:"Managed",processedPaths:n,includeExternal:a,conditionalRule:!1})),X("userSettings")){const e=j("User");o.push(...await processMemoryFile(e,"User",n,!0));const t=A();o.push(...await processMdRules({rulesDir:t,type:"User",processedPaths:n,includeExternal:!0,conditionalRule:!1}))}const d=[],p=g();let f=p;for(;f!==m(f).root;)d.push(f),f=i(f);const M=O(p),C=k(p),D=null!==M&&null!==C&&I(M)!==I(C)&&H(M,C);for(const e of d.reverse()){const t=D&&H(e,C)&&!H(e,M);if(X("projectSettings")&&!t){const t=u(e,"CLAUDE.md");o.push(...await processMemoryFile(t,"Project",n,a));const s=u(e,".context","CONTEXT.md");o.push(...await processMemoryFile(s,"Project",n,a));const r=u(e,".context","CLAUDE.md");o.push(...await processMemoryFile(r,"Project",n,a));const i=u(e,".context","rules");o.push(...await processMdRules({rulesDir:i,type:"Project",processedPaths:n,includeExternal:a,conditionalRule:!1}))}if(X("localSettings")){const t=u(e,"CLAUDE.local.md");o.push(...await processMemoryFile(t,"Local",n,a))}}if(R(process.env.CONTEXT_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD)||R(process.env.CLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD)){const e=y();for(const t of e){const e=u(t,"CLAUDE.md");o.push(...await processMemoryFile(e,"Project",n,a));const s=u(t,".context","CONTEXT.md");o.push(...await processMemoryFile(s,"Project",n,a));const r=u(t,".context","CLAUDE.md");o.push(...await processMemoryFile(r,"Project",n,a));const i=u(t,".context","rules");o.push(...await processMdRules({rulesDir:i,type:"Project",processedPaths:n,includeExternal:a,conditionalRule:!1}))}}if(E()){const{info:e}=await safelyReadMemoryFileAsync(x(),"AutoMem");if(e){const t=I(e.path);n.has(t)||(n.add(t),o.push(e))}}if(s("TEAMMEM")&&q.isTeamMemoryEnabled()){const{info:e}=await safelyReadMemoryFileAsync(q.getTeamMemEntrypoint(),"TeamMem");if(e){const t=I(e.path);n.has(t)||(n.add(t),o.push(e))}}const P=o.reduce((e,t)=>e+t.content.length,0);F("info","memory_files_completed",{duration_ms:Date.now()-t,file_count:o.length,total_content_length:P});const T={};for(const e of o)T[e.type]=(T[e.type]??0)+1;if(G||(G=!0,h("tengu_claudemd__initial_load",{file_count:o.length,total_content_length:P,user_count:T.User??0,project_count:T.Project??0,local_count:T.Local??0,managed_count:T.Managed??0,automem_count:T.AutoMem??0,...s("TEAMMEM")?{teammem_count:T.TeamMem??0}:{},duration_ms:Date.now()-t})),!e){const e=function(){if(!Y)return;Y=!1;const e=V;return V="session_start",e}();if(void 0!==e&&W())for(const t of o){if(!isInstructionsMemoryType(t.type))continue;const s=t.parent?"include":e;N(t.path,t.type,s,{globs:t.globs,parentFilePath:t.parent})}}return o});function isInstructionsMemoryType(e){return"User"===e||"Project"===e||"Local"===e||"Managed"===e}let V="session_start",Y=!0;export function clearMemoryFileCaches(){getMemoryFiles.cache?.clear?.()}export function resetGetMemoryFilesCache(e="session_start"){V=e,Y=!0,clearMemoryFileCaches()}export function getLargeMemoryFiles(e){return e.filter(e=>e.content.length>4e4)}export function filterInjectedMemoryFiles(e){return C("tengu_moth_copse",!1)?e.filter(e=>"AutoMem"!==e.type&&"TeamMem"!==e.type):e}export const getClaudeMds=(e,t)=>{const o=[],n=C("tengu_paper_halyard",!1);for(const r of e)if((!t||t(r.type))&&(!n||"Project"!==r.type&&"Local"!==r.type)&&r.content){const e="Project"===r.type?" (project instructions, checked into the codebase)":"Local"===r.type?" (user's private project instructions, not checked in)":s("TEAMMEM")&&"TeamMem"===r.type?" (shared team memory, synced across the organization)":"AutoMem"===r.type?" (user's auto-memory, persists across conversations)":" (user's private global instructions for all projects)",t=r.content.trim();s("TEAMMEM")&&"TeamMem"===r.type?o.push(`Contents of ${r.path}${e}:\n\n<team-memory-content source="shared">\n${t}\n</team-memory-content>`):o.push(`Contents of ${r.path}${e}:\n\n${t}`)}return 0===o.length?"":`Codebase and user instructions are shown below. Be sure to adhere to these instructions. IMPORTANT: These instructions OVERRIDE any default behavior and you MUST follow them exactly as written.\n\n${o.join("\n\n")}`};export async function getManagedAndUserConditionalRules(e,t){const s=[],o=_();if(s.push(...await processConditionedMdRules(e,o,"Managed",t,!1)),X("userSettings")){const o=A();s.push(...await processConditionedMdRules(e,o,"User",t,!0))}return s}export async function getMemoryFilesForNestedDirectory(e,t,s){const o=[];if(X("projectSettings")){const t=u(e,"CLAUDE.md");o.push(...await processMemoryFile(t,"Project",s,!1));const n=u(e,".context","CONTEXT.md");o.push(...await processMemoryFile(n,"Project",s,!1));const r=u(e,".context","CLAUDE.md");o.push(...await processMemoryFile(r,"Project",s,!1))}if(X("localSettings")){const t=u(e,"CLAUDE.local.md");o.push(...await processMemoryFile(t,"Local",s,!1))}const n=u(e,".context","rules"),r=new Set(s);o.push(...await processMdRules({dotContextRulesDir:n,type:"Project",processedPaths:r,includeExternal:!1,conditionalRule:!1})),o.push(...await processConditionedMdRules(t,n,"Project",s,!1));for(const e of r)s.add(e);return o}export async function getConditionalRulesForCwdLevelDirectory(e,t,s){return processConditionedMdRules(t,u(e,".context","rules"),"Project",s,!1)}export async function processConditionedMdRules(e,t,s,n,r){return(await processMdRules({rulesDir:t,type:s,processedPaths:n,includeExternal:r,conditionalRule:!0})).filter(n=>{if(!n.globs||0===n.globs.length)return!1;const r="Project"===s?i(i(t)):g(),a=l(e)?d(r,e):e;return!(!a||a.startsWith("..")||l(a))&&o().add(n.globs).ignores(a)})}export function getExternalClaudeMdIncludes(e){const t=[];for(const s of e)"User"!==s.type&&s.parent&&!pathInOriginalCwd(s.path)&&t.push({path:s.path,parent:s.parent});return t}export function hasExternalClaudeMdIncludes(e){return getExternalClaudeMdIncludes(e).length>0}export async function shouldShowClaudeMdExternalIncludesWarning(){const e=w();return!e.hasClaudeMdExternalIncludesApproved&&!e.hasClaudeMdExternalIncludesWarningShown&&hasExternalClaudeMdIncludes(await getMemoryFiles(!0))}export function isMemoryFilePath(e){const t=a(e);return"CLAUDE.md"===t||"CLAUDE.local.md"===t||!("CONTEXT.md"!==t||!e.includes(`${p}.context${p}`))||!(!t.endsWith(".md")||!e.includes(`${p}.context${p}rules${p}`))}export function getAllMemoryFilePaths(e,t){const s=new Set;for(const t of e)t.content.trim().length>0&&s.add(t.path);for(const e of S(t))isMemoryFilePath(e)&&s.add(e);return Array.from(s)}