Skip to content

Commit

Permalink
Fix tests and code style
Browse files Browse the repository at this point in the history
  • Loading branch information
burtek committed Jan 9, 2024
1 parent 48b1250 commit 90bcd30
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
5 changes: 3 additions & 2 deletions lib/rules/jsx-no-script-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

'use strict';

const includes = require('array-includes');
const docsUrl = require('../util/docsUrl');
const linkComponentsUtil = require('../util/linkComponents');
const report = require('../util/report');
Expand All @@ -28,8 +29,8 @@ function shouldVerifyProp(node, config) {

if (!name || !parentName || !config.has(parentName)) return false;

const attributes = config.get(parentName)
return attributes.includes(name);
const attributes = config.get(parentName);
return includes(attributes, name);
}

function parseLegacyOption(config, option) {
Expand Down
5 changes: 3 additions & 2 deletions lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

'use strict';

const includes = require('array-includes');
const docsUrl = require('../util/docsUrl');
const linkComponentsUtil = require('../util/linkComponents');
const report = require('../util/report');
Expand Down Expand Up @@ -49,15 +50,15 @@ function attributeValuePossiblyBlank(attribute) {
}

function hasExternalLink(node, linkAttributes, warnOnSpreadAttributes, spreadAttributeIndex) {
const linkIndex = findLastIndex(node.attributes, (attr) => attr.name && linkAttributes.includes(attr.name.name));
const linkIndex = findLastIndex(node.attributes, (attr) => attr.name && includes(linkAttributes, attr.name.name));
const foundExternalLink = linkIndex !== -1 && ((attr) => attr.value && attr.value.type === 'Literal' && /^(?:\w+:|\/\/)/.test(attr.value.value))(
node.attributes[linkIndex]);
return foundExternalLink || (warnOnSpreadAttributes && linkIndex < spreadAttributeIndex);
}

function hasDynamicLink(node, linkAttributes) {
const dynamicLinkIndex = findLastIndex(node.attributes, (attr) => attr.name
&& linkAttributes.includes(attr.name.name)
&& includes(linkAttributes, attr.name.name)
&& attr.value
&& attr.value.type === 'JSXExpressionContainer');
if (dynamicLinkIndex !== -1) {
Expand Down
4 changes: 2 additions & 2 deletions lib/util/linkComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function getFormComponents(context) {
if (typeof value === 'string') {
return [value, [DEFAULT_FORM_ATTRIBUTE]];
}
return [value.name, value.formAttributes || [value.formAttribute]];
return [value.name, [].concat(value.formAttribute)];
}));
}

Expand All @@ -39,7 +39,7 @@ function getLinkComponents(context) {
if (typeof value === 'string') {
return [value, [DEFAULT_LINK_ATTRIBUTE]];
}
return [value.name, value.linkAttributes || [value.linkAttribute]];
return [value.name, [].concat(value.linkAttribute)];
}));
}

Expand Down
12 changes: 6 additions & 6 deletions tests/lib/rules/jsx-no-script-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ ruleTester.run('jsx-no-script-url', rule, {
errors: [{ messageId: 'noScriptURL' }],
settings: {
linkComponents: [
{ name: 'Foo', linkAttribute: ['to'] }
]
{ name: 'Foo', linkAttribute: 'to' },
],
},
},
{
code: '<Foo href="javascript:"></Foo>',
errors: [{ messageId: 'noScriptURL' }],
settings: {
linkComponents: [
{ name: 'Foo', linkAttribute: ['to'] }
]
{ name: 'Foo', linkAttribute: ['to', 'href'] },
],
},
},
{
Expand Down Expand Up @@ -120,8 +120,8 @@ ruleTester.run('jsx-no-script-url', rule, {
],
settings: {
linkComponents: [
{ name: 'Foo', linkAttribute: ['to', 'href'] }
]
{ name: 'Foo', linkAttribute: ['to', 'href'] },
],
},
},
]),
Expand Down
5 changes: 5 additions & 0 deletions tests/lib/rules/jsx-no-target-blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ ruleTester.run('jsx-no-target-blank', rule, {
options: [{ enforceDynamicLinks: 'never' }],
settings: { linkComponents: { name: 'Link', linkAttribute: 'to' } },
},
{
code: '<Link target="_blank" to={ dynamicLink }></Link>',
options: [{ enforceDynamicLinks: 'never' }],
settings: { linkComponents: { name: 'Link', linkAttribute: ['to'] } },
},
{
code: '<a href="foobar" target="_blank" rel="noopener"></a>',
options: [{ allowReferrer: true }],
Expand Down

0 comments on commit 90bcd30

Please sign in to comment.