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

Method indent

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

_ensureindentStrMethod · 0.95
isObjectFunction · 0.50

Tested by

no test coverage detected