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

Method indent

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

_ensureindentStrMethod · 0.95
isObjectFunction · 0.50
replaceMethod · 0.45

Tested by

no test coverage detected