File tree Expand file tree Collapse file tree 3 files changed +60
-10
lines changed
Expand file tree Collapse file tree 3 files changed +60
-10
lines changed Original file line number Diff line number Diff line change 55'use strict' ;
66
77const 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" ' +
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments