Skip to content

Commit 1bc4d66

Browse files
committed
Move linkComponents settings to util
1 parent f268cef commit 1bc4d66

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

lib/rules/jsx-no-target-blank.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
'use strict';
66

77
const docsUrl = require('../util/docsUrl');
8-
9-
const DEFAULTS = ['a'];
8+
const linkComponentsUtil = require('../util/linkComponents');
109

1110
// ------------------------------------------------------------------------------
1211
// Rule Definition
@@ -65,20 +64,15 @@ module.exports = {
6564
create: function(context) {
6665
const configuration = context.options[0] || {};
6766
const enforceDynamicLinks = configuration.enforceDynamicLinks || 'always';
68-
69-
const elements = new Map((DEFAULTS.concat(context.settings.linkComponents || [])).map(value => {
70-
const name = typeof value === 'string' ? value : value.name;
71-
const linkAttribute = typeof value === 'string' ? 'href' : value.linkAttribute;
72-
return [name, linkAttribute];
73-
}));
67+
const components = linkComponentsUtil.getLinkComponents(context);
7468

7569
return {
7670
JSXAttribute: function(node) {
77-
if (!elements.has(node.parent.name.name) || !isTargetBlank(node) || hasSecureRel(node.parent)) {
71+
if (!components.has(node.parent.name.name) || !isTargetBlank(node) || hasSecureRel(node.parent)) {
7872
return;
7973
}
8074

81-
const linkAttribute = elements.get(node.parent.name.name);
75+
const linkAttribute = components.get(node.parent.name.name);
8276

8377
if (hasExternalLink(node.parent, linkAttribute) || (enforceDynamicLinks === 'always' && hasDynamicLink(node.parent, linkAttribute))) {
8478
context.report(node, 'Using target="_blank" without rel="noopener noreferrer" ' +

lib/util/linkComponents.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @fileoverview Utility functions for propWrapperFunctions setting
3+
*/
4+
'use strict';
5+
6+
const DEFAULT_LINK_COMPONENTS = ['a'];
7+
const DEFAULT_LINK_ATTRIBUTE = 'href';
8+
9+
function getLinkComponents(context) {
10+
const settings = context.settings || {};
11+
return new Map((DEFAULT_LINK_COMPONENTS.concat(settings.linkComponents || [])).map(value => {
12+
const name = typeof value === 'string' ? value : value.name;
13+
const linkAttribute = typeof value === 'string' ? DEFAULT_LINK_ATTRIBUTE : value.linkAttribute;
14+
return [name, linkAttribute];
15+
}));
16+
}
17+
18+
module.exports = {
19+
getLinkComponents: getLinkComponents
20+
};

tests/util/linkComponents.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* eslint-env mocha */
2+
'use strict';
3+
4+
const assert = require('assert');
5+
const linkComponentsUtil = require('../../lib/util/linkComponents');
6+
7+
describe('linkComponentsFunctions', () => {
8+
describe('getLinkComponents', () => {
9+
it('returns a default map of components', () => {
10+
const context = {};
11+
assert.deepStrictEqual(linkComponentsUtil.getLinkComponents(context), new Map([
12+
['a', 'href']
13+
]));
14+
});
15+
16+
it('returns a map of components', () => {
17+
const linkComponents = [
18+
'Hyperlink',
19+
{
20+
name: 'Link',
21+
linkAttribute: 'to'
22+
}
23+
];
24+
const context = {
25+
settings: {
26+
linkComponents: linkComponents
27+
}
28+
};
29+
assert.deepStrictEqual(linkComponentsUtil.getLinkComponents(context), new Map([
30+
['a', 'href'],
31+
['Hyperlink', 'href'],
32+
['Link', 'to']
33+
]));
34+
});
35+
});
36+
});

0 commit comments

Comments
 (0)