Skip to content

Commit

Permalink
Move linkComponents settings to util
Browse files Browse the repository at this point in the history
  • Loading branch information
gbakernet committed Jan 11, 2019
1 parent f268cef commit 1bc4d66
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
14 changes: 4 additions & 10 deletions lib/rules/jsx-no-target-blank.js
Expand Up @@ -5,8 +5,7 @@
'use strict';

const docsUrl = require('../util/docsUrl');

const DEFAULTS = ['a'];
const linkComponentsUtil = require('../util/linkComponents');

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -65,20 +64,15 @@ module.exports = {
create: function(context) {
const configuration = context.options[0] || {};
const enforceDynamicLinks = configuration.enforceDynamicLinks || 'always';

const elements = new Map((DEFAULTS.concat(context.settings.linkComponents || [])).map(value => {
const name = typeof value === 'string' ? value : value.name;
const linkAttribute = typeof value === 'string' ? 'href' : value.linkAttribute;
return [name, linkAttribute];
}));
const components = linkComponentsUtil.getLinkComponents(context);

return {
JSXAttribute: function(node) {
if (!elements.has(node.parent.name.name) || !isTargetBlank(node) || hasSecureRel(node.parent)) {
if (!components.has(node.parent.name.name) || !isTargetBlank(node) || hasSecureRel(node.parent)) {
return;
}

const linkAttribute = elements.get(node.parent.name.name);
const linkAttribute = components.get(node.parent.name.name);

if (hasExternalLink(node.parent, linkAttribute) || (enforceDynamicLinks === 'always' && hasDynamicLink(node.parent, linkAttribute))) {
context.report(node, 'Using target="_blank" without rel="noopener noreferrer" ' +
Expand Down
20 changes: 20 additions & 0 deletions lib/util/linkComponents.js
@@ -0,0 +1,20 @@
/**
* @fileoverview Utility functions for propWrapperFunctions setting
*/
'use strict';

const DEFAULT_LINK_COMPONENTS = ['a'];
const DEFAULT_LINK_ATTRIBUTE = 'href';

function getLinkComponents(context) {
const settings = context.settings || {};
return new Map((DEFAULT_LINK_COMPONENTS.concat(settings.linkComponents || [])).map(value => {
const name = typeof value === 'string' ? value : value.name;
const linkAttribute = typeof value === 'string' ? DEFAULT_LINK_ATTRIBUTE : value.linkAttribute;
return [name, linkAttribute];
}));
}

module.exports = {
getLinkComponents: getLinkComponents
};
36 changes: 36 additions & 0 deletions tests/util/linkComponents.js
@@ -0,0 +1,36 @@
/* eslint-env mocha */
'use strict';

const assert = require('assert');
const linkComponentsUtil = require('../../lib/util/linkComponents');

describe('linkComponentsFunctions', () => {
describe('getLinkComponents', () => {
it('returns a default map of components', () => {
const context = {};
assert.deepStrictEqual(linkComponentsUtil.getLinkComponents(context), new Map([
['a', 'href']
]));
});

it('returns a map of components', () => {
const linkComponents = [
'Hyperlink',
{
name: 'Link',
linkAttribute: 'to'
}
];
const context = {
settings: {
linkComponents: linkComponents
}
};
assert.deepStrictEqual(linkComponentsUtil.getLinkComponents(context), new Map([
['a', 'href'],
['Hyperlink', 'href'],
['Link', 'to']
]));
});
});
});

0 comments on commit 1bc4d66

Please sign in to comment.