Skip to content

Commit da71ff4

Browse files
gbakernetljharb
authored andcommitted
[New] add linkComponents setting
1 parent 16121d4 commit da71ff4

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ You should also specify settings that will be shared across all the plugin rules
4848
"forbidExtraProps",
4949
{"property": "freeze", "object": "Object"},
5050
{"property": "myFavoriteWrapper"}
51+
],
52+
"linkComponents": [
53+
// Components used as alternatives to <a> for linking, eg. <Link to={ url } />
54+
"Hyperlink",
55+
{"name": "Link", "linkAttribute": "to"}
5156
]
5257
}
5358
}

lib/util/linkComponents.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
if (typeof value === 'string') {
13+
return [value, DEFAULT_LINK_ATTRIBUTE];
14+
}
15+
return [value.name, value.linkAttribute];
16+
}));
17+
}
18+
19+
module.exports = {
20+
getLinkComponents: getLinkComponents
21+
};

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)