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

Method indent

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

Source from the content-addressed store, hash-verified

451 */
452 indent(indentStr?: string, options?: IndentOptions): this
453 indent(indentStr?: string | IndentOptions, options?: IndentOptions): this {
454 const pattern = /^[^\r\n]/gm
455
456 if (isObject(indentStr)) {
457 options = indentStr
458 indentStr = undefined
459 }
460
461 if (indentStr === undefined) {
462 this._ensureindentStr()
463 indentStr = this.indentStr || '\t'
464 }
465
466 if (indentStr === '')
467 return this // noop
468 const resolvedIndentStr = indentStr as string
469
470 options = options || {}
471
472 // Process exclusion ranges
473 const isExcluded: Record<number, boolean> = {}
474
475 if (options.exclude) {
476 const exclusions
477 = typeof options.exclude[0] === 'number'
478 ? [options.exclude as ExclusionRange]
479 : (options.exclude as ExclusionRange[])
480 exclusions.forEach((exclusion) => {
481 for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
482 isExcluded[i] = true
483 }
484 })
485 }
486
487 let shouldIndentNextCharacter = options.indentStart !== false
488
489 // Indents every line break inside `str`, plus the very first character when
490 // `str` starts a line in the generated output. Any match after offset 0
491 // necessarily follows a line break inside `str`, so it always gets indented.
492 const indentPiece = (str: string) => {
493 if (str === '')
494 return str
495
496 const indented = str.replace(pattern, (match: string, offset: number) =>
497 offset > 0 || shouldIndentNextCharacter ? `${resolvedIndentStr}${match}` : match)
498
499 shouldIndentNextCharacter = str[str.length - 1] === '\n'
500
501 return indented
502 }
503
504 this.intro = indentPiece(this.intro)
505
506 let charIndex = 0
507 let chunk = this.firstChunk
508
509 const indentAt = (index: number) => {
510 shouldIndentNextCharacter = false

Callers

nothing calls this directly

Calls 2

_ensureindentStrMethod · 0.95
isObjectFunction · 0.90

Tested by

no test coverage detected