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

Method indent

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

_ensureindentStrMethod · 0.95
isObjectFunction · 0.50
replaceMethod · 0.45

Tested by

no test coverage detected