MCPcopy Create free account
hub / github.com/Rich-Harris/magic-string / indent

Method indent

src/MagicString.ts:323–446  ·  view source on GitHub ↗
(indentStr?: string | IndentOptions, options?: IndentOptions)

Source from the content-addressed store, hash-verified

321 */
322 indent(indentStr?: string, options?: IndentOptions): this
323 indent(indentStr?: string | IndentOptions, options?: IndentOptions): this {
324 const pattern = /^[^\r\n]/gm
325
326 if (isObject(indentStr)) {
327 options = indentStr
328 indentStr = undefined
329 }
330
331 if (indentStr === undefined) {
332 this._ensureindentStr()
333 indentStr = this.indentStr || '\t'
334 }
335
336 if (indentStr === '')
337 return this // noop
338 const resolvedIndentStr = indentStr as string
339
340 options = options || {}
341
342 // Process exclusion ranges
343 const isExcluded: Record<number, boolean> = {}
344
345 if (options.exclude) {
346 const exclusions
347 = typeof options.exclude[0] === 'number'
348 ? [options.exclude as ExclusionRange]
349 : (options.exclude as ExclusionRange[])
350 exclusions.forEach((exclusion) => {
351 for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
352 isExcluded[i] = true
353 }
354 })
355 }
356
357 let shouldIndentNextCharacter = options.indentStart !== false
358 const replacer = (match: string) => {
359 if (shouldIndentNextCharacter)
360 return `${resolvedIndentStr}${match}`
361 shouldIndentNextCharacter = true
362 return match
363 }
364
365 this.intro = this.intro.replace(pattern, replacer)
366
367 let charIndex = 0
368 let chunk = this.firstChunk
369
370 const indentAt = (index: number) => {
371 shouldIndentNextCharacter = false
372
373 if (index === chunk!.start) {
374 chunk!.prependRight(resolvedIndentStr)
375 }
376 else {
377 this._splitChunk(chunk!, index)
378 chunk = chunk!.next
379 chunk!.prependRight(resolvedIndentStr)
380 }

Callers

nothing calls this directly

Calls 3

_ensureindentStrMethod · 0.95
isObjectFunction · 0.50
replaceMethod · 0.45

Tested by

no test coverage detected