@@ -2,58 +2,59 @@ const {isPrimerComponent} = require('../utils/is-primer-component')
2
2
const { getJSXOpeningElementName} = require ( '../utils/get-jsx-opening-element-name' )
3
3
const { getJSXOpeningElementAttribute} = require ( '../utils/get-jsx-opening-element-attribute' )
4
4
5
- const validHeadings = [ 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ] ;
5
+ const validHeadings = [ 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ]
6
6
7
7
const isHeadingComponent = elem => getJSXOpeningElementName ( elem ) === 'Heading'
8
8
const isUsingAsProp = elem => {
9
- const componentAs = getJSXOpeningElementAttribute ( elem , 'as' ) ;
9
+ const componentAs = getJSXOpeningElementAttribute ( elem , 'as' )
10
10
11
- if ( ! componentAs ) return ;
11
+ if ( ! componentAs ) return
12
12
13
- return componentAs . value ;
13
+ return componentAs . value
14
14
}
15
15
16
- const isValidAsUsage = value => validHeadings . includes ( value . toLowerCase ( ) ) ;
16
+ const isValidAsUsage = value => validHeadings . includes ( value . toLowerCase ( ) )
17
17
const isInvalid = elem => {
18
- const elemAs = isUsingAsProp ( elem ) ;
18
+ const elemAs = isUsingAsProp ( elem )
19
19
20
- if ( ! elemAs ) return 'nonExplicitHeadingLevel' ;
21
- if ( ! isValidAsUsage ( elemAs . value ) ) return 'invalidAsValue' ;
20
+ if ( ! elemAs ) return 'nonExplicitHeadingLevel'
21
+ if ( ! isValidAsUsage ( elemAs . value ) ) return 'invalidAsValue'
22
22
23
- return false ;
23
+ return false
24
24
}
25
25
26
26
module . exports = {
27
- meta : {
28
- type : "problem" ,
29
- schema : [
30
- {
31
- properties : {
32
- skipImportCheck : {
33
- type : 'boolean'
34
- }
35
- }
36
- }
37
- ] ,
38
- messages : {
39
- nonExplicitHeadingLevel : "Heading must have an explicit heading level applied through the `as` prop." ,
40
- invalidAsValue : "Usage of `as` must only be used for heading elements (h1-h6)."
27
+ meta : {
28
+ schema : [
29
+ {
30
+ properties : {
31
+ skipImportCheck : {
32
+ type : 'boolean'
41
33
}
42
- } ,
43
- create : function ( context ) {
44
- return {
45
- JSXOpeningElement ( jsxNode ) {
46
- if ( isPrimerComponent ( jsxNode . name , context . getScope ( jsxNode ) ) && isHeadingComponent ( jsxNode ) ) {
47
- const error = isInvalid ( jsxNode ) ;
48
-
49
- if ( error ) {
50
- context . report ( {
51
- node : jsxNode ,
52
- messageId : error
53
- } )
54
- }
55
- }
56
- }
57
- } ;
34
+ }
35
+ }
36
+ ] ,
37
+ messages : {
38
+ nonExplicitHeadingLevel : 'Heading must have an explicit heading level applied through the `as` prop.' ,
39
+ invalidAsValue : 'Usage of `as` must only be used for heading elements (h1-h6).'
58
40
}
59
- } ;
41
+ } ,
42
+ create : function ( context ) {
43
+ return {
44
+ JSXOpeningElement ( jsxNode ) {
45
+ const skipImportCheck = context . options [ 0 ] ? context . options [ 0 ] . skipImportCheck : false
46
+
47
+ if ( ( skipImportCheck || isPrimerComponent ( jsxNode . name , context . getScope ( jsxNode ) ) ) && isHeadingComponent ( jsxNode ) ) {
48
+ const error = isInvalid ( jsxNode )
49
+
50
+ if ( error ) {
51
+ context . report ( {
52
+ node : jsxNode ,
53
+ messageId : error
54
+ } )
55
+ }
56
+ }
57
+ }
58
+ }
59
+ }
60
+ }
0 commit comments