import detectIndent from "detect-indent"; import beautify from "js-beautify"; import _ from "lodash"; import replaceAsync from "string-replace-async"; import * as util from "../util"; import { Processor } from "./processor"; export class ComponentAttributeProcessor extends Processor { private componentAttributes: string[] = []; async preProcess(content: string): Promise { return await this.preserveComponentAttribute(content); } async postProcess(content: string): Promise { return await this.restoreComponentAttribute(content); } private async preserveComponentAttribute(content: string): Promise { const prefixes = Array.isArray(this.formatter.options.componentPrefix) && this.formatter.options.componentPrefix.length > 0 ? this.formatter.options.componentPrefix : ["x-", "livewire:"]; const regex = new RegExp( `(?<=<(${prefixes.join( "|", )})[^<]*?\\s):{1,2}(?)[\\w\-_.]*?=(["'])(?!=>)[^\\2]*?\\2(?=[^>]*?\/*?>)`, "gim", ); return _.replace( content, regex, (match: any) => `${this.storeComponentAttribute(match)}`, ); } private async restoreComponentAttribute(content: string): Promise { return replaceAsync( content, new RegExp(`${this.getComponentAttributePlaceholder("(\\d+)")}`, "gim"), async (_match: any, p1: any) => { const placeholder = this.getComponentAttributePlaceholder(p1); const matchedLine = content.match( new RegExp(`^(.*?)${placeholder}`, "gmi"), ) ?? [""]; const indent = detectIndent(matchedLine[0]); const matched = this.componentAttributes[p1]; const formatted = await replaceAsync( matched, /(:{1,2}.*?=)(["'])(.*?)(?=\2)/gis, async (match, p2: string, p3: string, p4: string) => { if (p4 === "") { return match; } if (matchedLine[0].startsWith("