Skip to content

Update dependency @ota-meshi/eslint-plugin to ^0.8.0 #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/context/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function* extractBlocks(code: string): IterableIterator<{
const codeRange: [number, number] = [startTagEnd, endTagStart]

const attrRe =
/(?<key>[^\s=]+)(?:=(?:"(?<val1>[^"]*)"|'(?<val2>[^"]*)'|(?<val3>[^\s=]+)))?/giu
/(?<key>[^\s=]+)(?:=(?:"(?<val1>[^"]*)"|'(?<val2>[^"]*)'|(?<val3>[^\s=]+)))?/gu
const attrs: Record<string, string | undefined> = {}
let attrRes
while ((attrRes = attrRe.exec(attributes))) {
Expand Down
38 changes: 25 additions & 13 deletions src/parser/converts/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
? /<script(\s[\s\S]*?)?>([\s\S]*?)<\/script>/giu
: /<style(\s[\s\S]*?)?>([\s\S]*?)<\/style>/giu,
(_tag, attributes: string | undefined, context: string) =>
`${script ? "<div " : "<div "}${
attributes || ""
}>${" ".repeat(context.length)}</div>`,
)

let code = " ".repeat(element.range[0])

const elementCode = ctx.sourceCode.template.slice(...element.range)
const startRegex = script
? /<script(\s[\s\S]*?)?>/giu
: /<style(\s[\s\S]*?)?>/giu
const endTag = script ? "</script>" : "</style>"
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 ? "<div " : "<div "}${attributes || ""}>`
code += `${" ".repeat(contextLength)}</div>`
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(
Expand Down