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

Method indent

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

Source from the content-addressed store, hash-verified

420 */
421 indent(indentStr?: string, options?: IndentOptions): this
422 indent(indentStr?: string | IndentOptions, options?: IndentOptions): this {
423 const pattern = /^[^\r\n]/gm
424
425 if (isObject(indentStr)) {
426 options = indentStr
427 indentStr = undefined
428 }
429
430 if (indentStr === undefined) {
431 this._ensureindentStr()
432 indentStr = this.indentStr || '\t'
433 }
434
435 if (indentStr === '')
436 return this // noop
437 const resolvedIndentStr = indentStr as string
438
439 options = options || {}
440
441 // Process exclusion ranges
442 const isExcluded: Record<number, boolean> = {}
443
444 if (options.exclude) {
445 const exclusions
446 = typeof options.exclude[0] === 'number'
447 ? [options.exclude as ExclusionRange]
448 : (options.exclude as ExclusionRange[])
449 exclusions.forEach((exclusion) => {
450 for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
451 isExcluded[i] = true
452 }
453 })
454 }
455
456 let shouldIndentNextCharacter = options.indentStart !== false
457
458 // Indents every line break inside `str`, plus the very first character when
459 // `str` starts a line in the generated output. Any match after offset 0
460 // necessarily follows a line break inside `str`, so it always gets indented.
461 const indentPiece = (str: string) => {
462 if (str === '')
463 return str
464
465 const indented = str.replace(pattern, (match: string, offset: number) =>
466 offset > 0 || shouldIndentNextCharacter ? `${resolvedIndentStr}${match}` : match)
467
468 shouldIndentNextCharacter = str[str.length - 1] === '\n'
469
470 return indented
471 }
472
473 this.intro = indentPiece(this.intro)
474
475 let charIndex = 0
476 let chunk = this.firstChunk
477
478 const indentAt = (index: number) => {
479 shouldIndentNextCharacter = false

Callers

nothing calls this directly

Calls 2

_ensureindentStrMethod · 0.95
isObjectFunction · 0.50

Tested by

no test coverage detected