@@ -2,58 +2,59 @@ const {isPrimerComponent} = require('../utils/is-primer-component')
22const { getJSXOpeningElementName} = require ( '../utils/get-jsx-opening-element-name' )
33const { getJSXOpeningElementAttribute} = require ( '../utils/get-jsx-opening-element-attribute' )
44
5- const validHeadings = [ 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ] ;
5+ const validHeadings = [ 'h1' , 'h2' , 'h3' , 'h4' , 'h5' , 'h6' ]
66
77const isHeadingComponent = elem => getJSXOpeningElementName ( elem ) === 'Heading'
88const isUsingAsProp = elem => {
9- const componentAs = getJSXOpeningElementAttribute ( elem , 'as' ) ;
9+ const componentAs = getJSXOpeningElementAttribute ( elem , 'as' )
1010
11- if ( ! componentAs ) return ;
11+ if ( ! componentAs ) return
1212
13- return componentAs . value ;
13+ return componentAs . value
1414}
1515
16- const isValidAsUsage = value => validHeadings . includes ( value . toLowerCase ( ) ) ;
16+ const isValidAsUsage = value => validHeadings . includes ( value . toLowerCase ( ) )
1717const isInvalid = elem => {
18- const elemAs = isUsingAsProp ( elem ) ;
18+ const elemAs = isUsingAsProp ( elem )
1919
20- if ( ! elemAs ) return 'nonExplicitHeadingLevel' ;
21- if ( ! isValidAsUsage ( elemAs . value ) ) return 'invalidAsValue' ;
20+ if ( ! elemAs ) return 'nonExplicitHeadingLevel'
21+ if ( ! isValidAsUsage ( elemAs . value ) ) return 'invalidAsValue'
2222
23- return false ;
23+ return false
2424}
2525
2626module . 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'
4133 }
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).'
5840 }
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