banhaten 0.5.0 → 0.6.0

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 (290) hide show
  1. package/CHANGELOG.md +177 -139
  2. package/CONTRIBUTING.md +46 -46
  3. package/README.md +192 -192
  4. package/SECURITY.md +15 -15
  5. package/banhaten.config.example.json +11 -11
  6. package/docs/design-spacing.md +13 -13
  7. package/docs/design-system/README.md +22 -22
  8. package/docs/design-system/adoption-quality-remediation.md +87 -87
  9. package/docs/design-system/agent-usage.md +13 -13
  10. package/docs/design-system/appearance-presets.md +189 -189
  11. package/docs/design-system/appearances/default.md +94 -94
  12. package/docs/design-system/appearances/rounded.md +95 -95
  13. package/docs/design-system/appearances/sharp.md +95 -95
  14. package/docs/design-system/component-docs-pattern.md +81 -81
  15. package/docs/design-system/component-implementation-ownership-audit.md +245 -245
  16. package/docs/design-system/component-quality-audit-2026-08-20.md +225 -225
  17. package/docs/design-system/component-showcase-consistency-report.md +219 -219
  18. package/docs/design-system/component-token-consistency-audit.md +173 -173
  19. package/docs/design-system/components/README.md +91 -90
  20. package/docs/design-system/components/accordion.md +52 -52
  21. package/docs/design-system/components/activity-feed.md +93 -93
  22. package/docs/design-system/components/activity-heatmap.md +56 -56
  23. package/docs/design-system/components/alert-dialog.md +71 -71
  24. package/docs/design-system/components/alert.md +103 -97
  25. package/docs/design-system/components/app-header.md +97 -0
  26. package/docs/design-system/components/aspect-ratio.md +45 -45
  27. package/docs/design-system/components/attribute.md +101 -89
  28. package/docs/design-system/components/autocomplete.md +75 -75
  29. package/docs/design-system/components/avatar.md +53 -53
  30. package/docs/design-system/components/badge.md +55 -55
  31. package/docs/design-system/components/banner.md +86 -86
  32. package/docs/design-system/components/breadcrumbs.md +181 -177
  33. package/docs/design-system/components/button-group.md +97 -97
  34. package/docs/design-system/components/button.md +80 -80
  35. package/docs/design-system/components/card.md +102 -88
  36. package/docs/design-system/components/carousel.md +46 -46
  37. package/docs/design-system/components/catalog-components.md +46 -46
  38. package/docs/design-system/components/checkbox.md +53 -53
  39. package/docs/design-system/components/collapsible.md +63 -63
  40. package/docs/design-system/components/command.md +61 -61
  41. package/docs/design-system/components/context-menu.md +45 -45
  42. package/docs/design-system/components/date-picker.md +80 -80
  43. package/docs/design-system/components/divider.md +102 -102
  44. package/docs/design-system/components/empty-state.md +56 -56
  45. package/docs/design-system/components/field.md +70 -70
  46. package/docs/design-system/components/file-upload.md +186 -186
  47. package/docs/design-system/components/hover-card.md +47 -47
  48. package/docs/design-system/components/icons.md +48 -48
  49. package/docs/design-system/components/input-group.md +57 -57
  50. package/docs/design-system/components/input-otp.md +56 -56
  51. package/docs/design-system/components/input.md +111 -109
  52. package/docs/design-system/components/item.md +58 -58
  53. package/docs/design-system/components/kbd.md +49 -49
  54. package/docs/design-system/components/label.md +49 -49
  55. package/docs/design-system/components/menu.md +94 -73
  56. package/docs/design-system/components/menubar.md +46 -46
  57. package/docs/design-system/components/modal.md +104 -104
  58. package/docs/design-system/components/native-select.md +53 -53
  59. package/docs/design-system/components/navigation-menu.md +49 -49
  60. package/docs/design-system/components/onboarding-step-list-item.md +81 -81
  61. package/docs/design-system/components/page-header.md +127 -93
  62. package/docs/design-system/components/pagination.md +56 -56
  63. package/docs/design-system/components/popover.md +59 -59
  64. package/docs/design-system/components/progress-slider.md +49 -49
  65. package/docs/design-system/components/progress.md +76 -76
  66. package/docs/design-system/components/radio-card.md +50 -50
  67. package/docs/design-system/components/radio-group.md +56 -56
  68. package/docs/design-system/components/resizable.md +43 -43
  69. package/docs/design-system/components/scroll-area.md +50 -50
  70. package/docs/design-system/components/segmented-control.md +54 -54
  71. package/docs/design-system/components/select.md +68 -64
  72. package/docs/design-system/components/sheet.md +66 -66
  73. package/docs/design-system/components/sidebar.md +167 -111
  74. package/docs/design-system/components/skeleton.md +74 -74
  75. package/docs/design-system/components/slideout.md +64 -64
  76. package/docs/design-system/components/slider.md +62 -62
  77. package/docs/design-system/components/social-button.md +49 -49
  78. package/docs/design-system/components/spinner.md +62 -62
  79. package/docs/design-system/components/steps.md +65 -65
  80. package/docs/design-system/components/table.md +499 -422
  81. package/docs/design-system/components/tabs.md +74 -59
  82. package/docs/design-system/components/tag.md +79 -79
  83. package/docs/design-system/components/textarea.md +49 -49
  84. package/docs/design-system/components/timeline.md +82 -82
  85. package/docs/design-system/components/toast.md +57 -57
  86. package/docs/design-system/components/toggle.md +80 -80
  87. package/docs/design-system/components/toolbar.md +89 -89
  88. package/docs/design-system/components/tooltip.md +92 -92
  89. package/docs/design-system/components/typography.md +18 -18
  90. package/docs/design-system/density-and-elevation.md +65 -65
  91. package/docs/design-system/design-system-test-missing-items.md +368 -368
  92. package/docs/design-system/direction-and-localization.md +23 -23
  93. package/docs/design-system/form-recipes.md +73 -73
  94. package/docs/design-system/gap-promotion-policy.md +21 -21
  95. package/docs/design-system/icons.md +74 -74
  96. package/docs/design-system/installation.md +70 -70
  97. package/docs/design-system/launch-comparison-shadcn.md +98 -98
  98. package/docs/design-system/registry-and-cli.md +64 -64
  99. package/docs/design-system/release-policy.md +30 -30
  100. package/docs/design-system/roadmap.md +14 -14
  101. package/docs/design-system/support-matrix.md +23 -23
  102. package/docs/design-system/tabs.md +54 -54
  103. package/docs/design-system/template-gap-audit.md +32 -32
  104. package/docs/design-system/templates.md +59 -59
  105. package/docs/design-system/token-governance.md +72 -72
  106. package/package.json +98 -98
  107. package/registry/component-contract.json +40 -40
  108. package/registry/components/accordion.tsx +155 -155
  109. package/registry/components/alert-dialog.tsx +297 -297
  110. package/registry/components/alert.tsx +309 -293
  111. package/registry/components/app-header.tsx +151 -0
  112. package/registry/components/aspect-ratio.tsx +30 -30
  113. package/registry/components/attribute.tsx +489 -454
  114. package/registry/components/autocomplete.tsx +598 -598
  115. package/registry/components/avatar.tsx +385 -385
  116. package/registry/components/badge.tsx +630 -629
  117. package/registry/components/button-group.tsx +565 -565
  118. package/registry/components/button.tsx +128 -127
  119. package/registry/components/card.tsx +651 -638
  120. package/registry/components/carousel.tsx +296 -296
  121. package/registry/components/chart.tsx +203 -203
  122. package/registry/components/checkbox.tsx +247 -247
  123. package/registry/components/collapsible.tsx +109 -109
  124. package/registry/components/command.tsx +208 -208
  125. package/registry/components/context-menu.tsx +236 -236
  126. package/registry/components/date-picker.tsx +1289 -1289
  127. package/registry/components/expanded/ActivityFeed.tsx +227 -227
  128. package/registry/components/expanded/ActivityHeatmap.tsx +64 -64
  129. package/registry/components/expanded/Banner.tsx +179 -179
  130. package/registry/components/expanded/Breadcrumbs.tsx +135 -131
  131. package/registry/components/expanded/CatalogComponentsShowcase.tsx +205 -205
  132. package/registry/components/expanded/CatalogDivider.tsx +49 -49
  133. package/registry/components/expanded/EmptyState.tsx +155 -155
  134. package/registry/components/expanded/FileUpload.tsx +543 -543
  135. package/registry/components/expanded/OnboardingStepListItem.tsx +60 -60
  136. package/registry/components/expanded/PageHeader.tsx +209 -179
  137. package/registry/components/expanded/Slideout.tsx +108 -108
  138. package/registry/components/expanded/Steps.tsx +402 -402
  139. package/registry/components/expanded/Table.tsx +1109 -1092
  140. package/registry/components/expanded/Timeline.tsx +203 -203
  141. package/registry/components/expanded/activityFeed.css +248 -248
  142. package/registry/components/expanded/activityHeatmap.css +66 -66
  143. package/registry/components/expanded/banner.css +260 -260
  144. package/registry/components/expanded/breadcrumbs.css +157 -140
  145. package/registry/components/expanded/catalogComponentsShowcase.css +87 -87
  146. package/registry/components/expanded/divider.css +75 -75
  147. package/registry/components/expanded/emptyState.css +111 -111
  148. package/registry/components/expanded/fileUpload.css +457 -457
  149. package/registry/components/expanded/onboardingStepListItem.css +135 -135
  150. package/registry/components/expanded/pageHeader.css +284 -248
  151. package/registry/components/expanded/slideout.css +276 -276
  152. package/registry/components/expanded/steps.css +356 -356
  153. package/registry/components/expanded/table.css +655 -606
  154. package/registry/components/expanded/timeline.css +286 -286
  155. package/registry/components/field.tsx +283 -283
  156. package/registry/components/hover-card.tsx +48 -48
  157. package/registry/components/input-content.ts +107 -107
  158. package/registry/components/input-group.tsx +130 -130
  159. package/registry/components/input-otp.tsx +584 -584
  160. package/registry/components/input.tsx +992 -990
  161. package/registry/components/item.tsx +189 -189
  162. package/registry/components/kbd.tsx +246 -246
  163. package/registry/components/label.tsx +78 -78
  164. package/registry/components/menu.tsx +1091 -978
  165. package/registry/components/menubar.tsx +226 -226
  166. package/registry/components/modal.tsx +872 -872
  167. package/registry/components/native-select.tsx +205 -205
  168. package/registry/components/navigation-menu.tsx +171 -171
  169. package/registry/components/pagination.css +36 -36
  170. package/registry/components/pagination.tsx +560 -560
  171. package/registry/components/popover.tsx +338 -338
  172. package/registry/components/progress-slider.tsx +216 -216
  173. package/registry/components/radio-card.tsx +676 -676
  174. package/registry/components/radio-group.tsx +570 -570
  175. package/registry/components/resizable.tsx +108 -108
  176. package/registry/components/scroll-area.tsx +91 -91
  177. package/registry/components/segmented-control.tsx +87 -87
  178. package/registry/components/select-content.tsx +218 -218
  179. package/registry/components/select.tsx +937 -937
  180. package/registry/components/sheet.tsx +305 -305
  181. package/registry/components/sidebar.tsx +1535 -1433
  182. package/registry/components/skeleton.css +54 -54
  183. package/registry/components/skeleton.tsx +482 -482
  184. package/registry/components/slider.tsx +380 -380
  185. package/registry/components/social-button.tsx +387 -387
  186. package/registry/components/spinner.tsx +78 -78
  187. package/registry/components/table-elements.tsx +287 -128
  188. package/registry/components/tabs.css +246 -222
  189. package/registry/components/tabs.tsx +245 -241
  190. package/registry/components/tag.tsx +423 -423
  191. package/registry/components/textarea.tsx +610 -610
  192. package/registry/components/toast.tsx +136 -136
  193. package/registry/components/toggle.tsx +256 -256
  194. package/registry/components/toolbar.tsx +539 -539
  195. package/registry/components/tooltip.tsx +433 -433
  196. package/registry/examples/alert-demo.tsx +34 -34
  197. package/registry/examples/alert-dialog-demo.tsx +42 -42
  198. package/registry/examples/app-header-demo.tsx +40 -0
  199. package/registry/examples/aspect-ratio-demo.tsx +11 -11
  200. package/registry/examples/attribute-demo.tsx +65 -65
  201. package/registry/examples/autocomplete-demo.tsx +109 -109
  202. package/registry/examples/avatar-demo.tsx +132 -132
  203. package/registry/examples/badge-demo.tsx +69 -69
  204. package/registry/examples/button-demo.tsx +86 -86
  205. package/registry/examples/button-group-demo.tsx +63 -63
  206. package/registry/examples/card-demo.tsx +86 -86
  207. package/registry/examples/carousel-demo.tsx +43 -43
  208. package/registry/examples/chart-demo.tsx +18 -18
  209. package/registry/examples/checkbox-demo.tsx +62 -62
  210. package/registry/examples/collapsible-demo.tsx +16 -16
  211. package/registry/examples/collection-filter-bar-demo.tsx +135 -135
  212. package/registry/examples/command-demo.tsx +42 -42
  213. package/registry/examples/command-palette-demo.tsx +235 -235
  214. package/registry/examples/context-menu-demo.tsx +29 -29
  215. package/registry/examples/control-density-contract-demo.tsx +65 -65
  216. package/registry/examples/date-picker-demo.tsx +127 -127
  217. package/registry/examples/divider-demo.tsx +17 -17
  218. package/registry/examples/expanded/activity-heatmap-demo.tsx +29 -29
  219. package/registry/examples/expanded/banner-demo.tsx +48 -48
  220. package/registry/examples/expanded/breadcrumbs-demo.tsx +59 -59
  221. package/registry/examples/expanded/empty-state-demo.tsx +39 -39
  222. package/registry/examples/expanded/file-upload-demo.tsx +60 -60
  223. package/registry/examples/expanded/onboarding-step-demo.tsx +11 -11
  224. package/registry/examples/expanded/slideout-demo.tsx +15 -15
  225. package/registry/examples/expanded/steps-demo.tsx +29 -29
  226. package/registry/examples/expanded/tabs-demo.tsx +13 -13
  227. package/registry/examples/expanded/timeline-demo.tsx +18 -18
  228. package/registry/examples/field-demo.tsx +51 -51
  229. package/registry/examples/hover-card-demo.tsx +23 -23
  230. package/registry/examples/input-actions-demo.tsx +36 -36
  231. package/registry/examples/input-demo.tsx +87 -87
  232. package/registry/examples/input-group-demo.tsx +16 -16
  233. package/registry/examples/input-otp-demo.tsx +72 -72
  234. package/registry/examples/item-demo.tsx +46 -46
  235. package/registry/examples/kbd-demo.tsx +12 -12
  236. package/registry/examples/label-demo.tsx +20 -20
  237. package/registry/examples/menu-demo.tsx +122 -122
  238. package/registry/examples/menubar-demo.tsx +34 -34
  239. package/registry/examples/modal-demo.tsx +74 -74
  240. package/registry/examples/native-select-demo.tsx +16 -16
  241. package/registry/examples/navigation-menu-demo.tsx +29 -29
  242. package/registry/examples/pagination-demo.tsx +20 -20
  243. package/registry/examples/popover-demo.tsx +547 -547
  244. package/registry/examples/progress-demo.tsx +37 -37
  245. package/registry/examples/progress-slider-demo.tsx +29 -29
  246. package/registry/examples/radio-card-demo.tsx +51 -51
  247. package/registry/examples/radio-group-demo.tsx +62 -62
  248. package/registry/examples/resizable-demo.tsx +22 -22
  249. package/registry/examples/results-toolbar-demo.tsx +44 -44
  250. package/registry/examples/scroll-area-demo.tsx +15 -15
  251. package/registry/examples/segmented-control-demo.tsx +39 -39
  252. package/registry/examples/select-demo.tsx +87 -87
  253. package/registry/examples/sheet-demo.tsx +47 -47
  254. package/registry/examples/sidebar-demo.tsx +101 -101
  255. package/registry/examples/slider-demo.tsx +31 -31
  256. package/registry/examples/social-button-demo.tsx +61 -61
  257. package/registry/examples/spinner-demo.tsx +59 -59
  258. package/registry/examples/tabs-demo.tsx +13 -13
  259. package/registry/examples/tag-demo.tsx +29 -29
  260. package/registry/examples/textarea-demo.tsx +79 -79
  261. package/registry/examples/toast-demo.tsx +35 -35
  262. package/registry/examples/toggle-demo.tsx +59 -59
  263. package/registry/examples/tooltip-demo.tsx +115 -115
  264. package/registry/guidance/USAGE.md +27 -27
  265. package/registry/hooks/use-direction.ts +207 -207
  266. package/registry/index.json +387 -17
  267. package/registry/styles/globals.css +46 -0
  268. package/registry.json +24 -2
  269. package/schema/config.schema.json +51 -51
  270. package/schema/registry.schema.json +194 -194
  271. package/schema/tokens.schema.json +167 -167
  272. package/src/cli/command-line.js +194 -194
  273. package/src/cli/config-editors.js +9 -9
  274. package/src/cli/doctor.js +399 -399
  275. package/src/cli/file-transaction.js +302 -302
  276. package/src/cli/global-css.js +153 -153
  277. package/src/cli/index.js +1002 -1002
  278. package/src/cli/install-plan.js +592 -592
  279. package/src/cli/install-receipt.js +171 -171
  280. package/src/cli/jsonc.js +203 -203
  281. package/src/cli/package-install-state.js +254 -254
  282. package/src/cli/package-manager.js +238 -238
  283. package/src/cli/package-plan.js +185 -185
  284. package/src/cli/package-version.js +97 -97
  285. package/src/cli/registry-query.js +207 -207
  286. package/src/cli/reporting.js +55 -55
  287. package/src/cli/schema-validation.js +19 -19
  288. package/src/cli/source-scanner.js +686 -686
  289. package/src/cli/vite-config.js +478 -478
  290. package/tokens/banhaten.tokens.json +184 -0
@@ -1,686 +1,686 @@
1
- // Target-agnostic scanner primitives shared by the JSONC and Vite config
2
- // editors. Nothing here names a product: messages describe the syntax problem
3
- // only, and each caller prefixes the file and config dialect it was reading.
4
- // Keeping that split is what stops a malformed tsconfig from being reported as
5
- // a broken Vite config.
6
-
7
- export function findExportDefaultExpressionStart(source) {
8
- let index = 0
9
-
10
- while (index < source.length) {
11
- const next = skipIgnoredSyntax(source, index)
12
- if (next !== index) {
13
- index = next
14
- continue
15
- }
16
-
17
- if (!matchIdentifier(source, index, "export")) {
18
- index += 1
19
- continue
20
- }
21
-
22
- const defaultStart = skipWhitespaceAndComments(source, index + "export".length)
23
- if (!matchIdentifier(source, defaultStart, "default")) {
24
- index += "export".length
25
- continue
26
- }
27
-
28
- return skipWhitespaceAndComments(source, defaultStart + "default".length)
29
- }
30
-
31
- return -1
32
- }
33
-
34
- export function createBracketRange(source, start, open) {
35
- const close = open === "{" ? "}" : open === "[" ? "]" : ")"
36
- const end = findMatchingBracket(source, start, open, close)
37
- return { start, end: end + 1 }
38
- }
39
-
40
- export function expectObjectLiteralValue(source, property, message) {
41
- const valueStart = skipWhitespaceAndComments(source, property.valueStart)
42
- if (source[valueStart] !== "{") throw new Error(message)
43
- return createBracketRange(source, valueStart, "{")
44
- }
45
-
46
- export function expectArrayLiteralValue(source, property, message) {
47
- const valueStart = skipWhitespaceAndComments(source, property.valueStart)
48
- if (source[valueStart] !== "[") throw new Error(message)
49
- return createBracketRange(source, valueStart, "[")
50
- }
51
-
52
- export function findObjectProperty(source, objectRange, propertyName) {
53
- const bodyEnd = objectRange.end - 1
54
- let index = objectRange.start + 1
55
-
56
- while (index < bodyEnd) {
57
- index = skipWhitespaceCommentsAndCommas(source, index, bodyEnd)
58
- if (index >= bodyEnd) return null
59
-
60
- const propertyStart = index
61
- const name = readPropertyName(source, index)
62
- const propertyEnd = findTopLevelCommaOrEnd(source, propertyStart, bodyEnd)
63
-
64
- if (!name) {
65
- index = propertyEnd + 1
66
- continue
67
- }
68
-
69
- index = skipWhitespaceAndComments(source, name.end)
70
- if (source[index] !== ":") {
71
- if (name.name === propertyName) {
72
- throw new Error(
73
- `${propertyName} must be a normal object property.`
74
- )
75
- }
76
- index = propertyEnd + 1
77
- continue
78
- }
79
-
80
- if (name.name === propertyName) {
81
- return {
82
- name: name.name,
83
- start: propertyStart,
84
- end: propertyEnd,
85
- valueStart: skipWhitespaceAndComments(source, index + 1),
86
- valueEnd: propertyEnd,
87
- }
88
- }
89
-
90
- index = propertyEnd + 1
91
- }
92
-
93
- return null
94
- }
95
-
96
- export function readObjectPropertyNames(source, objectRange) {
97
- const bodyEnd = objectRange.end - 1
98
- const names = []
99
- let index = objectRange.start + 1
100
-
101
- while (index < bodyEnd) {
102
- index = skipWhitespaceCommentsAndCommas(source, index, bodyEnd)
103
- if (index >= bodyEnd) break
104
-
105
- const propertyEnd = findTopLevelCommaOrEnd(source, index, bodyEnd)
106
- const name = readPropertyName(source, index)
107
- if (name) names.push(name.name)
108
- index = propertyEnd + 1
109
- }
110
-
111
- return names
112
- }
113
-
114
- export function readPropertyName(source, index) {
115
- if (source[index] === "\"" || source[index] === "'") {
116
- const literal = readStringLiteral(source, index)
117
- return literal ? { name: literal.value, end: literal.end } : null
118
- }
119
-
120
- return readIdentifierAt(source, index)
121
- }
122
-
123
- export function insertObjectProperty(source, objectRange, propertyText) {
124
- const insertAt = objectRange.start + 1
125
- const bodyEnd = objectRange.end - 1
126
- const body = source.slice(insertAt, bodyEnd)
127
- const hasContent = body.trim().length > 0
128
- const propertyIndent = detectObjectChildIndent(source, objectRange)
129
- const closeIndent = getLineIndent(source, bodyEnd)
130
- const indentedProperty = indentMultiline(propertyText, propertyIndent)
131
-
132
- if (!hasContent) {
133
- return `${source.slice(0, insertAt)}\n${indentedProperty}\n${closeIndent}${source.slice(insertAt)}`
134
- }
135
-
136
- const needsTrailingNewline = source[insertAt] !== "\n" && source[insertAt] !== "\r"
137
- const insertion = `\n${indentedProperty}${needsTrailingNewline ? "\n" : ""}`
138
- return `${source.slice(0, insertAt)}${insertion}${source.slice(insertAt)}`
139
- }
140
-
141
- export function appendObjectProperty(source, objectRange, propertyText) {
142
- const bodyStart = objectRange.start + 1
143
- const bodyEnd = objectRange.end - 1
144
- const body = source.slice(bodyStart, bodyEnd)
145
-
146
- if (body.trim().length === 0) {
147
- return insertObjectProperty(source, objectRange, propertyText)
148
- }
149
-
150
- const contentEnd = trimEndIndex(source, bodyStart, bodyEnd)
151
- const hasNewline = bodyIncludesNewline(source, bodyStart, bodyEnd)
152
- const hasTrailingComma = previousCodeChar(source, contentEnd) === ","
153
- const propertyIndent = detectObjectChildIndent(source, objectRange)
154
- const indentedProperty = indentMultiline(propertyText, propertyIndent)
155
-
156
- if (!hasNewline) {
157
- const separator = hasTrailingComma ? " " : ", "
158
- return `${source.slice(0, contentEnd)}${separator}${propertyText}${source.slice(contentEnd)}`
159
- }
160
-
161
- const separator = hasTrailingComma ? "\n" : ",\n"
162
- return `${source.slice(0, contentEnd)}${separator}${indentedProperty}${source.slice(contentEnd)}`
163
- }
164
-
165
- function detectObjectChildIndent(source, objectRange) {
166
- const bodyEnd = objectRange.end - 1
167
- let index = objectRange.start + 1
168
-
169
- while (index < bodyEnd) {
170
- if (source[index] === "\n") {
171
- const next = skipHorizontalWhitespace(source, index + 1)
172
- if (next < bodyEnd && source[next] !== "\n" && source[next] !== "\r") {
173
- return source.slice(index + 1, next)
174
- }
175
- }
176
- index += 1
177
- }
178
-
179
- return `${getLineIndent(source, objectRange.start)} `
180
- }
181
-
182
- export function appendArrayElement(source, arrayRange, elementText) {
183
- const bodyStart = arrayRange.start + 1
184
- const bodyEnd = arrayRange.end - 1
185
- const body = source.slice(bodyStart, bodyEnd)
186
-
187
- if (body.trim().length === 0) {
188
- return `${source.slice(0, bodyStart)}${elementText}${source.slice(bodyStart)}`
189
- }
190
-
191
- const contentEnd = trimEndIndex(source, bodyStart, bodyEnd)
192
- const hasNewline = bodyIncludesNewline(source, bodyStart, bodyEnd)
193
- const hasTrailingComma = previousCodeChar(source, contentEnd) === ","
194
-
195
- if (!hasNewline) {
196
- const separator = hasTrailingComma ? " " : ", "
197
- return `${source.slice(0, contentEnd)}${separator}${elementText}${source.slice(contentEnd)}`
198
- }
199
-
200
- const closeIndent = getLineIndent(source, bodyEnd)
201
- const elementIndent = `${closeIndent} `
202
- const separator = hasTrailingComma ? "\n" : ",\n"
203
- return `${source.slice(0, contentEnd)}${separator}${elementIndent}${elementText},${source.slice(contentEnd)}`
204
- }
205
-
206
- export function arrayHasCallExpression(source, arrayRange, localName) {
207
- const bodyEnd = arrayRange.end - 1
208
- let index = arrayRange.start + 1
209
-
210
- while (index < bodyEnd) {
211
- index = skipWhitespaceCommentsAndCommas(source, index, bodyEnd)
212
- if (index >= bodyEnd) return false
213
-
214
- const elementEnd = findTopLevelCommaOrEnd(source, index, bodyEnd)
215
- if (startsWithCallExpression(source, index, elementEnd, localName)) {
216
- return true
217
- }
218
- index = elementEnd + 1
219
- }
220
-
221
- return false
222
- }
223
-
224
- function startsWithCallExpression(source, start, end, localName) {
225
- const index = skipWhitespaceAndComments(source, start)
226
- if (!matchIdentifier(source, index, localName)) return false
227
-
228
- const callStart = skipWhitespaceAndComments(source, index + localName.length)
229
- if (source[callStart] !== "(") return false
230
-
231
- const callEnd = createBracketRange(source, callStart, "(").end
232
- const next = skipWhitespaceAndComments(source, callEnd)
233
- return next <= end
234
- }
235
-
236
- export function parseImportDeclarations(source) {
237
- const declarations = []
238
- let index = 0
239
-
240
- while (index < source.length) {
241
- const next = skipIgnoredSyntax(source, index)
242
- if (next !== index) {
243
- index = next
244
- continue
245
- }
246
-
247
- if (!matchIdentifier(source, index, "import")) {
248
- index += 1
249
- continue
250
- }
251
-
252
- const clauseStart = skipWhitespaceAndComments(source, index + "import".length)
253
- if (source[clauseStart] === ".") {
254
- index = clauseStart + 1
255
- continue
256
- }
257
- if (source[clauseStart] === "(") {
258
- index = clauseStart + 1
259
- continue
260
- }
261
-
262
- const end = findImportDeclarationEnd(source, index)
263
- const declaration = source.slice(index, end)
264
- const moduleSpecifier = getImportModuleSpecifier(declaration)
265
-
266
- if (moduleSpecifier) {
267
- declarations.push({
268
- start: index,
269
- end,
270
- moduleSpecifier,
271
- defaultLocal: getDefaultImportLocal(declaration),
272
- namedLocals: getNamedImportLocals(declaration),
273
- })
274
- }
275
-
276
- index = end
277
- }
278
-
279
- return declarations
280
- }
281
-
282
- function findImportDeclarationEnd(source, start) {
283
- const stack = []
284
- let sawModuleSpecifier = false
285
- let index = start
286
-
287
- while (index < source.length) {
288
- const skipped = skipComment(source, index)
289
- if (skipped !== index) {
290
- index = skipped
291
- continue
292
- }
293
-
294
- if (source[index] === "\"" || source[index] === "'") {
295
- const literal = readStringLiteral(source, index)
296
- if (!literal) return source.length
297
- sawModuleSpecifier = true
298
- index = literal.end
299
- continue
300
- }
301
-
302
- if (isOpeningBracket(source[index])) {
303
- stack.push(expectedClosingBracket(source[index]))
304
- index += 1
305
- continue
306
- }
307
-
308
- if (isClosingBracket(source[index])) {
309
- if (stack.at(-1) === source[index]) stack.pop()
310
- index += 1
311
- continue
312
- }
313
-
314
- if (stack.length === 0 && source[index] === ";") return index + 1
315
- if (
316
- stack.length === 0 &&
317
- sawModuleSpecifier &&
318
- (source[index] === "\n" || source[index] === "\r")
319
- ) {
320
- return index + 1
321
- }
322
-
323
- index += 1
324
- }
325
-
326
- return source.length
327
- }
328
-
329
- function getImportModuleSpecifier(declaration) {
330
- let moduleSpecifier = null
331
- let index = 0
332
-
333
- while (index < declaration.length) {
334
- const skipped = skipComment(declaration, index)
335
- if (skipped !== index) {
336
- index = skipped
337
- continue
338
- }
339
-
340
- if (declaration[index] === "\"" || declaration[index] === "'") {
341
- const literal = readStringLiteral(declaration, index)
342
- if (!literal) break
343
- moduleSpecifier = literal.value
344
- index = literal.end
345
- continue
346
- }
347
-
348
- index += 1
349
- }
350
-
351
- return moduleSpecifier
352
- }
353
-
354
- function getDefaultImportLocal(declaration) {
355
- let index = skipWhitespaceAndComments(declaration, "import".length)
356
-
357
- if (matchIdentifier(declaration, index, "type")) {
358
- index = skipWhitespaceAndComments(declaration, index + "type".length)
359
- }
360
-
361
- if (declaration[index] === "\"" || declaration[index] === "{" || declaration[index] === "*") {
362
- return null
363
- }
364
-
365
- const local = readIdentifierAt(declaration, index)
366
- return local?.name ?? null
367
- }
368
-
369
- function getNamedImportLocals(declaration) {
370
- const open = declaration.indexOf("{")
371
- if (open === -1) return []
372
-
373
- const close = findMatchingBracket(declaration, open, "{", "}")
374
- const namedImportBody = declaration.slice(open + 1, close)
375
- const locals = []
376
-
377
- for (const specifier of splitTopLevelList(namedImportBody)) {
378
- const trimmed = specifier.trim()
379
- if (!trimmed) continue
380
-
381
- const withoutType = trimmed.startsWith("type ")
382
- ? trimmed.slice("type ".length).trim()
383
- : trimmed
384
- const asIndex = withoutType.toLowerCase().lastIndexOf(" as ")
385
- const localName = asIndex === -1
386
- ? withoutType
387
- : withoutType.slice(asIndex + " as ".length).trim()
388
- const local = readIdentifierAt(localName, 0)
389
-
390
- if (local) locals.push(local.name)
391
- }
392
-
393
- return locals
394
- }
395
-
396
- function splitTopLevelList(source) {
397
- const items = []
398
- let start = 0
399
- let index = 0
400
-
401
- while (index < source.length) {
402
- const skipped = skipIgnoredSyntax(source, index)
403
- if (skipped !== index) {
404
- index = skipped
405
- continue
406
- }
407
-
408
- if (source[index] === ",") {
409
- items.push(source.slice(start, index))
410
- start = index + 1
411
- }
412
- index += 1
413
- }
414
-
415
- items.push(source.slice(start))
416
- return items
417
- }
418
-
419
- export function prependImportLines(source, lines) {
420
- if (lines.length === 0) return source
421
-
422
- let insertAt = 0
423
- if (source.startsWith("#!")) {
424
- const lineEnd = source.indexOf("\n")
425
- insertAt = lineEnd === -1 ? source.length : lineEnd + 1
426
- }
427
-
428
- const prefix = `${lines.join("\n")}\n`
429
- return `${source.slice(0, insertAt)}${prefix}${source.slice(insertAt)}`
430
- }
431
-
432
- export function findTopLevelCommaOrEnd(source, start, end) {
433
- const stack = []
434
- let index = start
435
-
436
- while (index < end) {
437
- const next = skipIgnoredSyntax(source, index)
438
- if (next !== index) {
439
- index = next
440
- continue
441
- }
442
-
443
- if (isOpeningBracket(source[index])) {
444
- stack.push(expectedClosingBracket(source[index]))
445
- index += 1
446
- continue
447
- }
448
-
449
- if (isClosingBracket(source[index])) {
450
- if (stack.at(-1) === source[index]) stack.pop()
451
- index += 1
452
- continue
453
- }
454
-
455
- if (stack.length === 0 && source[index] === ",") return index
456
- index += 1
457
- }
458
-
459
- return end
460
- }
461
-
462
- export function findMatchingBracket(source, start, open, close) {
463
- if (source[start] !== open) {
464
- throw new Error("malformed JavaScript object.")
465
- }
466
-
467
- const stack = []
468
- let index = start
469
-
470
- while (index < source.length) {
471
- const next = skipIgnoredSyntax(source, index)
472
- if (next !== index) {
473
- index = next
474
- continue
475
- }
476
-
477
- if (isOpeningBracket(source[index])) {
478
- stack.push(expectedClosingBracket(source[index]))
479
- index += 1
480
- continue
481
- }
482
-
483
- if (isClosingBracket(source[index])) {
484
- const expected = stack.pop()
485
- if (expected !== source[index]) {
486
- throw new Error("malformed JavaScript brackets.")
487
- }
488
- if (stack.length === 0) return index
489
- index += 1
490
- continue
491
- }
492
-
493
- index += 1
494
- }
495
-
496
- throw new Error(`missing closing ${close}.`)
497
- }
498
-
499
- function skipIgnoredSyntax(source, index) {
500
- const commentEnd = skipComment(source, index)
501
- if (commentEnd !== index) return commentEnd
502
-
503
- if (source[index] === "\"" || source[index] === "'" || source[index] === "`") {
504
- return skipQuotedText(source, index)
505
- }
506
-
507
- return index
508
- }
509
-
510
- function skipComment(source, index) {
511
- if (source[index] !== "/") return index
512
-
513
- if (source[index + 1] === "/") {
514
- const lineEnd = source.indexOf("\n", index + 2)
515
- return lineEnd === -1 ? source.length : lineEnd + 1
516
- }
517
-
518
- if (source[index + 1] === "*") {
519
- const blockEnd = source.indexOf("*/", index + 2)
520
- return blockEnd === -1 ? source.length : blockEnd + 2
521
- }
522
-
523
- return index
524
- }
525
-
526
- function skipQuotedText(source, index) {
527
- const quote = source[index]
528
- index += 1
529
-
530
- while (index < source.length) {
531
- if (source[index] === "\\") {
532
- index += 2
533
- continue
534
- }
535
-
536
- if (source[index] === quote) return index + 1
537
- index += 1
538
- }
539
-
540
- return source.length
541
- }
542
-
543
- export function readStringLiteral(source, index) {
544
- const quote = source[index]
545
- if (quote !== "\"" && quote !== "'") return null
546
-
547
- let value = ""
548
- index += 1
549
-
550
- while (index < source.length) {
551
- if (source[index] === "\\") {
552
- value += source[index + 1] ?? ""
553
- index += 2
554
- continue
555
- }
556
-
557
- if (source[index] === quote) {
558
- return { value, end: index + 1 }
559
- }
560
-
561
- value += source[index]
562
- index += 1
563
- }
564
-
565
- return null
566
- }
567
-
568
- export function readIdentifierAt(source, index) {
569
- if (!isIdentifierStart(source[index])) return null
570
-
571
- let end = index + 1
572
- while (end < source.length && isIdentifierPart(source[end])) {
573
- end += 1
574
- }
575
-
576
- return { name: source.slice(index, end), end }
577
- }
578
-
579
- export function matchIdentifier(source, index, name) {
580
- if (source.slice(index, index + name.length) !== name) return false
581
-
582
- const before = source[index - 1]
583
- const after = source[index + name.length]
584
- return !isIdentifierPart(before) && !isIdentifierPart(after)
585
- }
586
-
587
- function isIdentifierStart(char) {
588
- return Boolean(char && /[A-Za-z_$]/.test(char))
589
- }
590
-
591
- function isIdentifierPart(char) {
592
- return Boolean(char && /[A-Za-z0-9_$]/.test(char))
593
- }
594
-
595
- function isOpeningBracket(char) {
596
- return char === "{" || char === "[" || char === "("
597
- }
598
-
599
- function isClosingBracket(char) {
600
- return char === "}" || char === "]" || char === ")"
601
- }
602
-
603
- function expectedClosingBracket(char) {
604
- if (char === "{") return "}"
605
- if (char === "[") return "]"
606
- return ")"
607
- }
608
-
609
- export function skipWhitespaceAndComments(source, index) {
610
- while (index < source.length) {
611
- const next = skipComment(source, index)
612
- if (next !== index) {
613
- index = next
614
- continue
615
- }
616
-
617
- if (!isWhitespace(source[index])) return index
618
- index += 1
619
- }
620
-
621
- return index
622
- }
623
-
624
- export function skipWhitespaceCommentsAndCommas(source, index, end) {
625
- while (index < end) {
626
- const next = skipWhitespaceAndComments(source, index)
627
- if (next !== index) {
628
- index = next
629
- continue
630
- }
631
-
632
- if (source[index] !== ",") return index
633
- index += 1
634
- }
635
-
636
- return index
637
- }
638
-
639
- function skipHorizontalWhitespace(source, index) {
640
- while (source[index] === " " || source[index] === "\t") {
641
- index += 1
642
- }
643
- return index
644
- }
645
-
646
- export function isWhitespace(char) {
647
- return char === " " || char === "\t" || char === "\n" || char === "\r"
648
- }
649
-
650
- function indentMultiline(text, indent) {
651
- return text
652
- .split("\n")
653
- .map((line) => `${indent}${line}`)
654
- .join("\n")
655
- }
656
-
657
- function getLineIndent(source, index) {
658
- let lineStart = index
659
- while (lineStart > 0 && source[lineStart - 1] !== "\n") {
660
- lineStart -= 1
661
- }
662
-
663
- return source.slice(lineStart, skipHorizontalWhitespace(source, lineStart))
664
- }
665
-
666
- function trimEndIndex(source, start, end) {
667
- while (end > start && isWhitespace(source[end - 1])) {
668
- end -= 1
669
- }
670
- return end
671
- }
672
-
673
- function previousCodeChar(source, index) {
674
- index -= 1
675
- while (index >= 0 && isWhitespace(source[index])) {
676
- index -= 1
677
- }
678
- return source[index]
679
- }
680
-
681
- function bodyIncludesNewline(source, start, end) {
682
- for (let index = start; index < end; index += 1) {
683
- if (source[index] === "\n" || source[index] === "\r") return true
684
- }
685
- return false
686
- }
1
+ // Target-agnostic scanner primitives shared by the JSONC and Vite config
2
+ // editors. Nothing here names a product: messages describe the syntax problem
3
+ // only, and each caller prefixes the file and config dialect it was reading.
4
+ // Keeping that split is what stops a malformed tsconfig from being reported as
5
+ // a broken Vite config.
6
+
7
+ export function findExportDefaultExpressionStart(source) {
8
+ let index = 0
9
+
10
+ while (index < source.length) {
11
+ const next = skipIgnoredSyntax(source, index)
12
+ if (next !== index) {
13
+ index = next
14
+ continue
15
+ }
16
+
17
+ if (!matchIdentifier(source, index, "export")) {
18
+ index += 1
19
+ continue
20
+ }
21
+
22
+ const defaultStart = skipWhitespaceAndComments(source, index + "export".length)
23
+ if (!matchIdentifier(source, defaultStart, "default")) {
24
+ index += "export".length
25
+ continue
26
+ }
27
+
28
+ return skipWhitespaceAndComments(source, defaultStart + "default".length)
29
+ }
30
+
31
+ return -1
32
+ }
33
+
34
+ export function createBracketRange(source, start, open) {
35
+ const close = open === "{" ? "}" : open === "[" ? "]" : ")"
36
+ const end = findMatchingBracket(source, start, open, close)
37
+ return { start, end: end + 1 }
38
+ }
39
+
40
+ export function expectObjectLiteralValue(source, property, message) {
41
+ const valueStart = skipWhitespaceAndComments(source, property.valueStart)
42
+ if (source[valueStart] !== "{") throw new Error(message)
43
+ return createBracketRange(source, valueStart, "{")
44
+ }
45
+
46
+ export function expectArrayLiteralValue(source, property, message) {
47
+ const valueStart = skipWhitespaceAndComments(source, property.valueStart)
48
+ if (source[valueStart] !== "[") throw new Error(message)
49
+ return createBracketRange(source, valueStart, "[")
50
+ }
51
+
52
+ export function findObjectProperty(source, objectRange, propertyName) {
53
+ const bodyEnd = objectRange.end - 1
54
+ let index = objectRange.start + 1
55
+
56
+ while (index < bodyEnd) {
57
+ index = skipWhitespaceCommentsAndCommas(source, index, bodyEnd)
58
+ if (index >= bodyEnd) return null
59
+
60
+ const propertyStart = index
61
+ const name = readPropertyName(source, index)
62
+ const propertyEnd = findTopLevelCommaOrEnd(source, propertyStart, bodyEnd)
63
+
64
+ if (!name) {
65
+ index = propertyEnd + 1
66
+ continue
67
+ }
68
+
69
+ index = skipWhitespaceAndComments(source, name.end)
70
+ if (source[index] !== ":") {
71
+ if (name.name === propertyName) {
72
+ throw new Error(
73
+ `${propertyName} must be a normal object property.`
74
+ )
75
+ }
76
+ index = propertyEnd + 1
77
+ continue
78
+ }
79
+
80
+ if (name.name === propertyName) {
81
+ return {
82
+ name: name.name,
83
+ start: propertyStart,
84
+ end: propertyEnd,
85
+ valueStart: skipWhitespaceAndComments(source, index + 1),
86
+ valueEnd: propertyEnd,
87
+ }
88
+ }
89
+
90
+ index = propertyEnd + 1
91
+ }
92
+
93
+ return null
94
+ }
95
+
96
+ export function readObjectPropertyNames(source, objectRange) {
97
+ const bodyEnd = objectRange.end - 1
98
+ const names = []
99
+ let index = objectRange.start + 1
100
+
101
+ while (index < bodyEnd) {
102
+ index = skipWhitespaceCommentsAndCommas(source, index, bodyEnd)
103
+ if (index >= bodyEnd) break
104
+
105
+ const propertyEnd = findTopLevelCommaOrEnd(source, index, bodyEnd)
106
+ const name = readPropertyName(source, index)
107
+ if (name) names.push(name.name)
108
+ index = propertyEnd + 1
109
+ }
110
+
111
+ return names
112
+ }
113
+
114
+ export function readPropertyName(source, index) {
115
+ if (source[index] === "\"" || source[index] === "'") {
116
+ const literal = readStringLiteral(source, index)
117
+ return literal ? { name: literal.value, end: literal.end } : null
118
+ }
119
+
120
+ return readIdentifierAt(source, index)
121
+ }
122
+
123
+ export function insertObjectProperty(source, objectRange, propertyText) {
124
+ const insertAt = objectRange.start + 1
125
+ const bodyEnd = objectRange.end - 1
126
+ const body = source.slice(insertAt, bodyEnd)
127
+ const hasContent = body.trim().length > 0
128
+ const propertyIndent = detectObjectChildIndent(source, objectRange)
129
+ const closeIndent = getLineIndent(source, bodyEnd)
130
+ const indentedProperty = indentMultiline(propertyText, propertyIndent)
131
+
132
+ if (!hasContent) {
133
+ return `${source.slice(0, insertAt)}\n${indentedProperty}\n${closeIndent}${source.slice(insertAt)}`
134
+ }
135
+
136
+ const needsTrailingNewline = source[insertAt] !== "\n" && source[insertAt] !== "\r"
137
+ const insertion = `\n${indentedProperty}${needsTrailingNewline ? "\n" : ""}`
138
+ return `${source.slice(0, insertAt)}${insertion}${source.slice(insertAt)}`
139
+ }
140
+
141
+ export function appendObjectProperty(source, objectRange, propertyText) {
142
+ const bodyStart = objectRange.start + 1
143
+ const bodyEnd = objectRange.end - 1
144
+ const body = source.slice(bodyStart, bodyEnd)
145
+
146
+ if (body.trim().length === 0) {
147
+ return insertObjectProperty(source, objectRange, propertyText)
148
+ }
149
+
150
+ const contentEnd = trimEndIndex(source, bodyStart, bodyEnd)
151
+ const hasNewline = bodyIncludesNewline(source, bodyStart, bodyEnd)
152
+ const hasTrailingComma = previousCodeChar(source, contentEnd) === ","
153
+ const propertyIndent = detectObjectChildIndent(source, objectRange)
154
+ const indentedProperty = indentMultiline(propertyText, propertyIndent)
155
+
156
+ if (!hasNewline) {
157
+ const separator = hasTrailingComma ? " " : ", "
158
+ return `${source.slice(0, contentEnd)}${separator}${propertyText}${source.slice(contentEnd)}`
159
+ }
160
+
161
+ const separator = hasTrailingComma ? "\n" : ",\n"
162
+ return `${source.slice(0, contentEnd)}${separator}${indentedProperty}${source.slice(contentEnd)}`
163
+ }
164
+
165
+ function detectObjectChildIndent(source, objectRange) {
166
+ const bodyEnd = objectRange.end - 1
167
+ let index = objectRange.start + 1
168
+
169
+ while (index < bodyEnd) {
170
+ if (source[index] === "\n") {
171
+ const next = skipHorizontalWhitespace(source, index + 1)
172
+ if (next < bodyEnd && source[next] !== "\n" && source[next] !== "\r") {
173
+ return source.slice(index + 1, next)
174
+ }
175
+ }
176
+ index += 1
177
+ }
178
+
179
+ return `${getLineIndent(source, objectRange.start)} `
180
+ }
181
+
182
+ export function appendArrayElement(source, arrayRange, elementText) {
183
+ const bodyStart = arrayRange.start + 1
184
+ const bodyEnd = arrayRange.end - 1
185
+ const body = source.slice(bodyStart, bodyEnd)
186
+
187
+ if (body.trim().length === 0) {
188
+ return `${source.slice(0, bodyStart)}${elementText}${source.slice(bodyStart)}`
189
+ }
190
+
191
+ const contentEnd = trimEndIndex(source, bodyStart, bodyEnd)
192
+ const hasNewline = bodyIncludesNewline(source, bodyStart, bodyEnd)
193
+ const hasTrailingComma = previousCodeChar(source, contentEnd) === ","
194
+
195
+ if (!hasNewline) {
196
+ const separator = hasTrailingComma ? " " : ", "
197
+ return `${source.slice(0, contentEnd)}${separator}${elementText}${source.slice(contentEnd)}`
198
+ }
199
+
200
+ const closeIndent = getLineIndent(source, bodyEnd)
201
+ const elementIndent = `${closeIndent} `
202
+ const separator = hasTrailingComma ? "\n" : ",\n"
203
+ return `${source.slice(0, contentEnd)}${separator}${elementIndent}${elementText},${source.slice(contentEnd)}`
204
+ }
205
+
206
+ export function arrayHasCallExpression(source, arrayRange, localName) {
207
+ const bodyEnd = arrayRange.end - 1
208
+ let index = arrayRange.start + 1
209
+
210
+ while (index < bodyEnd) {
211
+ index = skipWhitespaceCommentsAndCommas(source, index, bodyEnd)
212
+ if (index >= bodyEnd) return false
213
+
214
+ const elementEnd = findTopLevelCommaOrEnd(source, index, bodyEnd)
215
+ if (startsWithCallExpression(source, index, elementEnd, localName)) {
216
+ return true
217
+ }
218
+ index = elementEnd + 1
219
+ }
220
+
221
+ return false
222
+ }
223
+
224
+ function startsWithCallExpression(source, start, end, localName) {
225
+ const index = skipWhitespaceAndComments(source, start)
226
+ if (!matchIdentifier(source, index, localName)) return false
227
+
228
+ const callStart = skipWhitespaceAndComments(source, index + localName.length)
229
+ if (source[callStart] !== "(") return false
230
+
231
+ const callEnd = createBracketRange(source, callStart, "(").end
232
+ const next = skipWhitespaceAndComments(source, callEnd)
233
+ return next <= end
234
+ }
235
+
236
+ export function parseImportDeclarations(source) {
237
+ const declarations = []
238
+ let index = 0
239
+
240
+ while (index < source.length) {
241
+ const next = skipIgnoredSyntax(source, index)
242
+ if (next !== index) {
243
+ index = next
244
+ continue
245
+ }
246
+
247
+ if (!matchIdentifier(source, index, "import")) {
248
+ index += 1
249
+ continue
250
+ }
251
+
252
+ const clauseStart = skipWhitespaceAndComments(source, index + "import".length)
253
+ if (source[clauseStart] === ".") {
254
+ index = clauseStart + 1
255
+ continue
256
+ }
257
+ if (source[clauseStart] === "(") {
258
+ index = clauseStart + 1
259
+ continue
260
+ }
261
+
262
+ const end = findImportDeclarationEnd(source, index)
263
+ const declaration = source.slice(index, end)
264
+ const moduleSpecifier = getImportModuleSpecifier(declaration)
265
+
266
+ if (moduleSpecifier) {
267
+ declarations.push({
268
+ start: index,
269
+ end,
270
+ moduleSpecifier,
271
+ defaultLocal: getDefaultImportLocal(declaration),
272
+ namedLocals: getNamedImportLocals(declaration),
273
+ })
274
+ }
275
+
276
+ index = end
277
+ }
278
+
279
+ return declarations
280
+ }
281
+
282
+ function findImportDeclarationEnd(source, start) {
283
+ const stack = []
284
+ let sawModuleSpecifier = false
285
+ let index = start
286
+
287
+ while (index < source.length) {
288
+ const skipped = skipComment(source, index)
289
+ if (skipped !== index) {
290
+ index = skipped
291
+ continue
292
+ }
293
+
294
+ if (source[index] === "\"" || source[index] === "'") {
295
+ const literal = readStringLiteral(source, index)
296
+ if (!literal) return source.length
297
+ sawModuleSpecifier = true
298
+ index = literal.end
299
+ continue
300
+ }
301
+
302
+ if (isOpeningBracket(source[index])) {
303
+ stack.push(expectedClosingBracket(source[index]))
304
+ index += 1
305
+ continue
306
+ }
307
+
308
+ if (isClosingBracket(source[index])) {
309
+ if (stack.at(-1) === source[index]) stack.pop()
310
+ index += 1
311
+ continue
312
+ }
313
+
314
+ if (stack.length === 0 && source[index] === ";") return index + 1
315
+ if (
316
+ stack.length === 0 &&
317
+ sawModuleSpecifier &&
318
+ (source[index] === "\n" || source[index] === "\r")
319
+ ) {
320
+ return index + 1
321
+ }
322
+
323
+ index += 1
324
+ }
325
+
326
+ return source.length
327
+ }
328
+
329
+ function getImportModuleSpecifier(declaration) {
330
+ let moduleSpecifier = null
331
+ let index = 0
332
+
333
+ while (index < declaration.length) {
334
+ const skipped = skipComment(declaration, index)
335
+ if (skipped !== index) {
336
+ index = skipped
337
+ continue
338
+ }
339
+
340
+ if (declaration[index] === "\"" || declaration[index] === "'") {
341
+ const literal = readStringLiteral(declaration, index)
342
+ if (!literal) break
343
+ moduleSpecifier = literal.value
344
+ index = literal.end
345
+ continue
346
+ }
347
+
348
+ index += 1
349
+ }
350
+
351
+ return moduleSpecifier
352
+ }
353
+
354
+ function getDefaultImportLocal(declaration) {
355
+ let index = skipWhitespaceAndComments(declaration, "import".length)
356
+
357
+ if (matchIdentifier(declaration, index, "type")) {
358
+ index = skipWhitespaceAndComments(declaration, index + "type".length)
359
+ }
360
+
361
+ if (declaration[index] === "\"" || declaration[index] === "{" || declaration[index] === "*") {
362
+ return null
363
+ }
364
+
365
+ const local = readIdentifierAt(declaration, index)
366
+ return local?.name ?? null
367
+ }
368
+
369
+ function getNamedImportLocals(declaration) {
370
+ const open = declaration.indexOf("{")
371
+ if (open === -1) return []
372
+
373
+ const close = findMatchingBracket(declaration, open, "{", "}")
374
+ const namedImportBody = declaration.slice(open + 1, close)
375
+ const locals = []
376
+
377
+ for (const specifier of splitTopLevelList(namedImportBody)) {
378
+ const trimmed = specifier.trim()
379
+ if (!trimmed) continue
380
+
381
+ const withoutType = trimmed.startsWith("type ")
382
+ ? trimmed.slice("type ".length).trim()
383
+ : trimmed
384
+ const asIndex = withoutType.toLowerCase().lastIndexOf(" as ")
385
+ const localName = asIndex === -1
386
+ ? withoutType
387
+ : withoutType.slice(asIndex + " as ".length).trim()
388
+ const local = readIdentifierAt(localName, 0)
389
+
390
+ if (local) locals.push(local.name)
391
+ }
392
+
393
+ return locals
394
+ }
395
+
396
+ function splitTopLevelList(source) {
397
+ const items = []
398
+ let start = 0
399
+ let index = 0
400
+
401
+ while (index < source.length) {
402
+ const skipped = skipIgnoredSyntax(source, index)
403
+ if (skipped !== index) {
404
+ index = skipped
405
+ continue
406
+ }
407
+
408
+ if (source[index] === ",") {
409
+ items.push(source.slice(start, index))
410
+ start = index + 1
411
+ }
412
+ index += 1
413
+ }
414
+
415
+ items.push(source.slice(start))
416
+ return items
417
+ }
418
+
419
+ export function prependImportLines(source, lines) {
420
+ if (lines.length === 0) return source
421
+
422
+ let insertAt = 0
423
+ if (source.startsWith("#!")) {
424
+ const lineEnd = source.indexOf("\n")
425
+ insertAt = lineEnd === -1 ? source.length : lineEnd + 1
426
+ }
427
+
428
+ const prefix = `${lines.join("\n")}\n`
429
+ return `${source.slice(0, insertAt)}${prefix}${source.slice(insertAt)}`
430
+ }
431
+
432
+ export function findTopLevelCommaOrEnd(source, start, end) {
433
+ const stack = []
434
+ let index = start
435
+
436
+ while (index < end) {
437
+ const next = skipIgnoredSyntax(source, index)
438
+ if (next !== index) {
439
+ index = next
440
+ continue
441
+ }
442
+
443
+ if (isOpeningBracket(source[index])) {
444
+ stack.push(expectedClosingBracket(source[index]))
445
+ index += 1
446
+ continue
447
+ }
448
+
449
+ if (isClosingBracket(source[index])) {
450
+ if (stack.at(-1) === source[index]) stack.pop()
451
+ index += 1
452
+ continue
453
+ }
454
+
455
+ if (stack.length === 0 && source[index] === ",") return index
456
+ index += 1
457
+ }
458
+
459
+ return end
460
+ }
461
+
462
+ export function findMatchingBracket(source, start, open, close) {
463
+ if (source[start] !== open) {
464
+ throw new Error("malformed JavaScript object.")
465
+ }
466
+
467
+ const stack = []
468
+ let index = start
469
+
470
+ while (index < source.length) {
471
+ const next = skipIgnoredSyntax(source, index)
472
+ if (next !== index) {
473
+ index = next
474
+ continue
475
+ }
476
+
477
+ if (isOpeningBracket(source[index])) {
478
+ stack.push(expectedClosingBracket(source[index]))
479
+ index += 1
480
+ continue
481
+ }
482
+
483
+ if (isClosingBracket(source[index])) {
484
+ const expected = stack.pop()
485
+ if (expected !== source[index]) {
486
+ throw new Error("malformed JavaScript brackets.")
487
+ }
488
+ if (stack.length === 0) return index
489
+ index += 1
490
+ continue
491
+ }
492
+
493
+ index += 1
494
+ }
495
+
496
+ throw new Error(`missing closing ${close}.`)
497
+ }
498
+
499
+ function skipIgnoredSyntax(source, index) {
500
+ const commentEnd = skipComment(source, index)
501
+ if (commentEnd !== index) return commentEnd
502
+
503
+ if (source[index] === "\"" || source[index] === "'" || source[index] === "`") {
504
+ return skipQuotedText(source, index)
505
+ }
506
+
507
+ return index
508
+ }
509
+
510
+ function skipComment(source, index) {
511
+ if (source[index] !== "/") return index
512
+
513
+ if (source[index + 1] === "/") {
514
+ const lineEnd = source.indexOf("\n", index + 2)
515
+ return lineEnd === -1 ? source.length : lineEnd + 1
516
+ }
517
+
518
+ if (source[index + 1] === "*") {
519
+ const blockEnd = source.indexOf("*/", index + 2)
520
+ return blockEnd === -1 ? source.length : blockEnd + 2
521
+ }
522
+
523
+ return index
524
+ }
525
+
526
+ function skipQuotedText(source, index) {
527
+ const quote = source[index]
528
+ index += 1
529
+
530
+ while (index < source.length) {
531
+ if (source[index] === "\\") {
532
+ index += 2
533
+ continue
534
+ }
535
+
536
+ if (source[index] === quote) return index + 1
537
+ index += 1
538
+ }
539
+
540
+ return source.length
541
+ }
542
+
543
+ export function readStringLiteral(source, index) {
544
+ const quote = source[index]
545
+ if (quote !== "\"" && quote !== "'") return null
546
+
547
+ let value = ""
548
+ index += 1
549
+
550
+ while (index < source.length) {
551
+ if (source[index] === "\\") {
552
+ value += source[index + 1] ?? ""
553
+ index += 2
554
+ continue
555
+ }
556
+
557
+ if (source[index] === quote) {
558
+ return { value, end: index + 1 }
559
+ }
560
+
561
+ value += source[index]
562
+ index += 1
563
+ }
564
+
565
+ return null
566
+ }
567
+
568
+ export function readIdentifierAt(source, index) {
569
+ if (!isIdentifierStart(source[index])) return null
570
+
571
+ let end = index + 1
572
+ while (end < source.length && isIdentifierPart(source[end])) {
573
+ end += 1
574
+ }
575
+
576
+ return { name: source.slice(index, end), end }
577
+ }
578
+
579
+ export function matchIdentifier(source, index, name) {
580
+ if (source.slice(index, index + name.length) !== name) return false
581
+
582
+ const before = source[index - 1]
583
+ const after = source[index + name.length]
584
+ return !isIdentifierPart(before) && !isIdentifierPart(after)
585
+ }
586
+
587
+ function isIdentifierStart(char) {
588
+ return Boolean(char && /[A-Za-z_$]/.test(char))
589
+ }
590
+
591
+ function isIdentifierPart(char) {
592
+ return Boolean(char && /[A-Za-z0-9_$]/.test(char))
593
+ }
594
+
595
+ function isOpeningBracket(char) {
596
+ return char === "{" || char === "[" || char === "("
597
+ }
598
+
599
+ function isClosingBracket(char) {
600
+ return char === "}" || char === "]" || char === ")"
601
+ }
602
+
603
+ function expectedClosingBracket(char) {
604
+ if (char === "{") return "}"
605
+ if (char === "[") return "]"
606
+ return ")"
607
+ }
608
+
609
+ export function skipWhitespaceAndComments(source, index) {
610
+ while (index < source.length) {
611
+ const next = skipComment(source, index)
612
+ if (next !== index) {
613
+ index = next
614
+ continue
615
+ }
616
+
617
+ if (!isWhitespace(source[index])) return index
618
+ index += 1
619
+ }
620
+
621
+ return index
622
+ }
623
+
624
+ export function skipWhitespaceCommentsAndCommas(source, index, end) {
625
+ while (index < end) {
626
+ const next = skipWhitespaceAndComments(source, index)
627
+ if (next !== index) {
628
+ index = next
629
+ continue
630
+ }
631
+
632
+ if (source[index] !== ",") return index
633
+ index += 1
634
+ }
635
+
636
+ return index
637
+ }
638
+
639
+ function skipHorizontalWhitespace(source, index) {
640
+ while (source[index] === " " || source[index] === "\t") {
641
+ index += 1
642
+ }
643
+ return index
644
+ }
645
+
646
+ export function isWhitespace(char) {
647
+ return char === " " || char === "\t" || char === "\n" || char === "\r"
648
+ }
649
+
650
+ function indentMultiline(text, indent) {
651
+ return text
652
+ .split("\n")
653
+ .map((line) => `${indent}${line}`)
654
+ .join("\n")
655
+ }
656
+
657
+ function getLineIndent(source, index) {
658
+ let lineStart = index
659
+ while (lineStart > 0 && source[lineStart - 1] !== "\n") {
660
+ lineStart -= 1
661
+ }
662
+
663
+ return source.slice(lineStart, skipHorizontalWhitespace(source, lineStart))
664
+ }
665
+
666
+ function trimEndIndex(source, start, end) {
667
+ while (end > start && isWhitespace(source[end - 1])) {
668
+ end -= 1
669
+ }
670
+ return end
671
+ }
672
+
673
+ function previousCodeChar(source, index) {
674
+ index -= 1
675
+ while (index >= 0 && isWhitespace(source[index])) {
676
+ index -= 1
677
+ }
678
+ return source[index]
679
+ }
680
+
681
+ function bodyIncludesNewline(source, start, end) {
682
+ for (let index = start; index < end; index += 1) {
683
+ if (source[index] === "\n" || source[index] === "\r") return true
684
+ }
685
+ return false
686
+ }