Skip to content

Commit

Permalink
(fix) anchor missing attribute code action duplicate rel attribute (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros authored Jan 9, 2023
1 parent 0cf75cf commit a240c90
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ async function createQuickfixActions(

return codeActions;
}

function createSvelteAnchorMissingAttributeQuickfixAction(
textDocument: OptionalVersionedTextDocumentIdentifier,
transpiled: ITranspiledSvelteDocument,
Expand All @@ -118,16 +117,21 @@ function createSvelteAnchorMissingAttributeQuickfixAction(
): CodeAction {
// Assert non-null because the node target attribute is required for 'security-anchor-rel-noreferrer'
const targetAttribute = node.attributes.find((i: any) => i.name == 'target')!;
const targetAttributePosition = positionAt(targetAttribute.end, content, lineOffsets);
const relAttribute = node.attributes.find((i: any) => i.name == 'rel');

const relNoReferrerTextEdit = TextEdit.insert(targetAttributePosition, ' rel="noreferrer"');
const codeActionTextEdit = relAttribute
? TextEdit.insert(positionAt(relAttribute.end - 1, content, lineOffsets), ' noreferrer')
: TextEdit.insert(
positionAt(targetAttribute.end, content, lineOffsets),
' rel="noreferrer"'
);

return CodeAction.create(
'(svelte) Add missing attribute rel="noreferrer"',
{
documentChanges: [
TextDocumentEdit.create(textDocument, [
mapObjWithRangeToOriginal(transpiled, relNoReferrerTextEdit)
mapObjWithRangeToOriginal(transpiled, codeActionTextEdit)
])
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,88 @@ describe('SveltePlugin#getCodeAction', () => {
}
]);
});

const svelteAnchorMissingAttributeCodeActionRel =
'svelte-anchor-missing-attribute-code-action-rel.svelte';

it('Should not duplicate rel attribute', async () => {
(
await expectCodeActionFor(svelteAnchorMissingAttributeCodeActionRel, {
diagnostics: [
{
severity: DiagnosticSeverity.Warning,
code: 'security-anchor-rel-noreferrer',
range: Range.create(
{ line: 0, character: 0 },
{ line: 0, character: 70 }
),
message:
'Security: Anchor with "target=_blank" should have rel attribute containing the value "noreferrer"',
source: 'svelte'
}
]
})
).toEqual([
{
edit: {
documentChanges: [
{
edits: [
{
newText: ' noreferrer',
range: {
end: {
character: 58,
line: 0
},
start: {
character: 58,
line: 0
}
}
}
],
textDocument: {
uri: getUri(svelteAnchorMissingAttributeCodeActionRel),
version: null
}
}
]
},
title: '(svelte) Add missing attribute rel="noreferrer"',
kind: 'quickfix'
},
{
edit: {
documentChanges: [
{
edits: [
{
newText: `<!-- svelte-ignore security-anchor-rel-noreferrer -->${EOL}`,
range: {
end: {
character: 0,
line: 0
},
start: {
character: 0,
line: 0
}
}
}
],
textDocument: {
uri: getUri(svelteAnchorMissingAttributeCodeActionRel),
version: null
}
}
]
},
title: '(svelte) Disable security-anchor-rel-noreferrer for this line',
kind: 'quickfix'
}
]);
});
});

describe('It should provide svelte ignore code actions ', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<a href="https://svelte.dev" target="_blank" rel="noopener">Svelte</a>

0 comments on commit a240c90

Please sign in to comment.