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

Method indent

src/MagicString.js:207–293  ·  view source on GitHub ↗
(indentStr, options)

Source from the content-addressed store, hash-verified

205 }
206
207 indent(indentStr, options) {
208 const pattern = /^[^\r\n]/gm;
209
210 if (isObject(indentStr)) {
211 options = indentStr;
212 indentStr = undefined;
213 }
214
215 if (indentStr === undefined) {
216 this._ensureindentStr();
217 indentStr = this.indentStr || '\t';
218 }
219
220 if (indentStr === '') return this; // noop
221
222 options = options || {};
223
224 // Process exclusion ranges
225 const isExcluded = {};
226
227 if (options.exclude) {
228 const exclusions =
229 typeof options.exclude[0] === 'number' ? [options.exclude] : options.exclude;
230 exclusions.forEach((exclusion) => {
231 for (let i = exclusion[0]; i < exclusion[1]; i += 1) {
232 isExcluded[i] = true;
233 }
234 });
235 }
236
237 let shouldIndentNextCharacter = options.indentStart !== false;
238 const replacer = (match) => {
239 if (shouldIndentNextCharacter) return `${indentStr}${match}`;
240 shouldIndentNextCharacter = true;
241 return match;
242 };
243
244 this.intro = this.intro.replace(pattern, replacer);
245
246 let charIndex = 0;
247 let chunk = this.firstChunk;
248
249 while (chunk) {
250 const end = chunk.end;
251
252 if (chunk.edited) {
253 if (!isExcluded[charIndex]) {
254 chunk.content = chunk.content.replace(pattern, replacer);
255
256 if (chunk.content.length) {
257 shouldIndentNextCharacter = chunk.content[chunk.content.length - 1] === '\n';
258 }
259 }
260 } else {
261 charIndex = chunk.start;
262
263 while (charIndex < end) {
264 if (!isExcluded[charIndex]) {

Callers

nothing calls this directly

Calls 5

_ensureindentStrMethod · 0.95
_splitChunkMethod · 0.95
isObjectFunction · 0.50
replaceMethod · 0.45
prependRightMethod · 0.45

Tested by

no test coverage detected