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

Method indent

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

_ensureindentStrMethod · 0.95
isObjectFunction · 0.50
replaceMethod · 0.45

Tested by

no test coverage detected