Skip to content

Commit

Permalink
fix: handle closing tags with orphaned brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-8 committed Sep 29, 2023
1 parent 10d30f9 commit 0892b8a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/rules/__snapshots__/brackets-same-line.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ exports[`brackets-same-line > invalid > <div
exports[`brackets-same-line > invalid > <div
/> 1`] = `"<div/>"`;
exports[`brackets-same-line > invalid > <div>Text</div
> 1`] = `"<div>Text</div>"`;
3 changes: 3 additions & 0 deletions src/rules/brackets-same-line.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const invalids = [
>
Text</div>`,

`<div>Text</div
>`,

`<div
/>`,
]
Expand Down
22 changes: 22 additions & 0 deletions src/rules/brackets-same-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ export default createEslintRule<Options, MessageIds>({
},
})
}

if (!node.endTag)
return

const endBracketOrphaned = node.endTag.loc.start.line !== node.endTag.loc.end.line
if (endBracketOrphaned) {
context.report({
// @ts-expect-error - type conversion from SvelteAttribute to node not handled
node: node.endTag,
loc: {
start: node.endTag.loc.start,
end: node.endTag.loc.end,
},
messageId: 'bracketOnNextLine',
*fix(fixer) {
const from = node.endTag!.range[0]
const to = node.endTag!.range[1]
const code = context.getSourceCode().text.slice(from, to)
yield fixer.replaceTextRange([from, to], code.replace(/\s+/g, ''))
},
})
}
},
}
},
Expand Down

0 comments on commit 0892b8a

Please sign in to comment.