diff --git a/package.json b/package.json index e1daa0ed..6fc69e38 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "svelte": "^3.37.0" }, "devDependencies": { - "@ota-meshi/eslint-plugin": "^0.7.0", + "@ota-meshi/eslint-plugin": "^0.8.0", "@ota-meshi/eslint-plugin-svelte": "^0.8.1", "@types/eslint": "^7.2.0", "@types/eslint-scope": "^3.7.0", diff --git a/src/context/index.ts b/src/context/index.ts index db1f7085..73fdd0ed 100644 --- a/src/context/index.ts +++ b/src/context/index.ts @@ -246,7 +246,7 @@ function* extractBlocks(code: string): IterableIterator<{ const codeRange: [number, number] = [startTagEnd, endTagStart] const attrRe = - /(?[^\s=]+)(?:=(?:"(?[^"]*)"|'(?[^"]*)'|(?[^\s=]+)))?/giu + /(?[^\s=]+)(?:=(?:"(?[^"]*)"|'(?[^"]*)'|(?[^\s=]+)))?/gu const attrs: Record = {} let attrRes while ((attrRes = attrRe.exec(attributes))) { diff --git a/src/parser/converts/root.ts b/src/parser/converts/root.ts index 44a09d0d..031154b6 100644 --- a/src/parser/converts/root.ts +++ b/src/parser/converts/root.ts @@ -137,19 +137,31 @@ function extractAttributes( ctx: Context, ) { const script = element.type === "SvelteScriptElement" - const code = - " ".repeat(element.range[0]) + - ctx.sourceCode.template - .slice(...element.range) - .replace( - script - ? /([\s\S]*?)<\/script>/giu - : /([\s\S]*?)<\/style>/giu, - (_tag, attributes: string | undefined, context: string) => - `${script ? "
${" ".repeat(context.length)}
`, - ) + + let code = " ".repeat(element.range[0]) + + const elementCode = ctx.sourceCode.template.slice(...element.range) + const startRegex = script + ? //giu + : //giu + const endTag = script ? "" : "" + let re + let index = 0 + while ((re = startRegex.exec(elementCode))) { + const [, attributes] = re + + const endTagIndex = elementCode.indexOf(endTag, startRegex.lastIndex) + if (endTagIndex >= 0) { + const contextLength = endTagIndex - startRegex.lastIndex + code += elementCode.slice(index, re.index) + code += `${script ? "
` + code += `${" ".repeat(contextLength)}
` + startRegex.lastIndex = index = endTagIndex + endTag.length + } else { + break + } + } + code += elementCode.slice(index) const svelteAst = parse(code) as SvAST.Ast const fakeElement = svelteAst.html.children.find(