@@ -296,6 +296,35 @@ ruleTester.run('jsx-no-literals', rule, {
296296 ` ,
297297 options : [ { noStrings : true , allowedStrings : [ '—' , '—' ] } ] ,
298298 } ,
299+ {
300+ code : `
301+ <img src="image.jpg" alt="text" />
302+ ` ,
303+ options : [ { restrictedAttributes : [ 'className' , 'id' ] } ] ,
304+ } ,
305+ {
306+ code : `
307+ <div className="allowed" />
308+ ` ,
309+ options : [ { restrictedAttributes : [ 'className' ] , allowedStrings : [ 'allowed' ] } ] ,
310+ } ,
311+ {
312+ code : `
313+ <div className="test" title="hello" />
314+ ` ,
315+ options : [ {
316+ noStrings : true ,
317+ ignoreProps : true ,
318+ restrictedAttributes : [ 'className' ] ,
319+ allowedStrings : [ 'test' ] ,
320+ } ] ,
321+ } ,
322+ {
323+ code : `
324+ <div className="test" id="foo" />
325+ ` ,
326+ options : [ { restrictedAttributes : [ ] } ] ,
327+ } ,
299328 {
300329 code : `
301330 <T>foo</T>
@@ -476,6 +505,45 @@ ruleTester.run('jsx-no-literals', rule, {
476505 ` ,
477506 options : [ { elementOverrides : { div : { allowElement : true } } } ] ,
478507 } ,
508+ {
509+ code : `
510+ <div>
511+ <Input type="text" />
512+ <Button className="primary" />
513+ <Image src="photo.jpg" />
514+ </div>
515+ ` ,
516+ options : [ {
517+ elementOverrides : {
518+ Input : { restrictedAttributes : [ 'placeholder' ] } ,
519+ Button : { restrictedAttributes : [ 'type' ] } ,
520+ } ,
521+ } ] ,
522+ } ,
523+ {
524+ code : `
525+ <div title="container">
526+ <Button className="btn" />
527+ </div>
528+ ` ,
529+ options : [ {
530+ restrictedAttributes : [ 'className' ] ,
531+ elementOverrides : {
532+ Button : { restrictedAttributes : [ 'disabled' ] } ,
533+ } ,
534+ } ] ,
535+ } ,
536+ {
537+ code : `
538+ <Button className="btn" />
539+ ` ,
540+ options : [ {
541+ noAttributeStrings : true ,
542+ elementOverrides : {
543+ Button : { restrictedAttributes : [ 'type' ] } ,
544+ } ,
545+ } ] ,
546+ } ,
479547 ] ) ,
480548
481549 invalid : parsers . all ( [
@@ -845,6 +913,68 @@ ruleTester.run('jsx-no-literals', rule, {
845913 } ,
846914 ] ,
847915 } ,
916+ {
917+ code : `
918+ <div className="test" />
919+ ` ,
920+ options : [ { restrictedAttributes : [ 'className' ] } ] ,
921+ errors : [ {
922+ messageId : 'restrictedAttributeString' ,
923+ data : { text : '"test"' , attribute : 'className' } ,
924+ } ] ,
925+ } ,
926+ {
927+ code : `
928+ <div className="test" id="foo" title="bar" />
929+ ` ,
930+ options : [ { restrictedAttributes : [ 'className' , 'id' ] } ] ,
931+ errors : [
932+ { messageId : 'restrictedAttributeString' , data : { text : '"test"' , attribute : 'className' } } ,
933+ { messageId : 'restrictedAttributeString' , data : { text : '"foo"' , attribute : 'id' } } ,
934+ ] ,
935+ } ,
936+ {
937+ code : `
938+ <div src="image.jpg" />
939+ ` ,
940+ options : [ {
941+ noAttributeStrings : true ,
942+ restrictedAttributes : [ 'className' ] ,
943+ } ] ,
944+ errors : [ { messageId : 'noStringsInAttributes' , data : { text : '"image.jpg"' } } ] ,
945+ } ,
946+ {
947+ code : `
948+ <div title="text">test</div>
949+ ` ,
950+ options : [ {
951+ restrictedAttributes : [ 'title' ] ,
952+ noStrings : true ,
953+ } ] ,
954+ errors : [
955+ { messageId : 'restrictedAttributeString' , data : { text : '"text"' , attribute : 'title' } } ,
956+ { messageId : 'noStringsInJSX' , data : { text : 'test' } } ,
957+ ] ,
958+ } ,
959+ {
960+ code : `
961+ <div className="test" title="hello" />
962+ ` ,
963+ options : [ { noStrings : true , ignoreProps : false , restrictedAttributes : [ 'className' ] } ] ,
964+ errors : [
965+ { messageId : 'restrictedAttributeString' , data : { text : '"test"' , attribute : 'className' } } ,
966+ { messageId : 'invalidPropValue' , data : { text : 'title="hello"' } } ,
967+ ] ,
968+ } ,
969+ {
970+ code : `
971+ <div className="test" title="hello" />
972+ ` ,
973+ options : [ { noStrings : true , ignoreProps : true , restrictedAttributes : [ 'className' ] } ] ,
974+ errors : [
975+ { messageId : 'restrictedAttributeString' , data : { text : '"test"' , attribute : 'className' } } ,
976+ ] ,
977+ } ,
848978 {
849979 code : `
850980 <div>
@@ -1169,5 +1299,75 @@ ruleTester.run('jsx-no-literals', rule, {
11691299 options : [ { elementOverrides : { div : { allowElement : true } } } ] ,
11701300 errors : [ { messageId : 'literalNotInJSXExpression' , data : { text : 'foo' } } ] ,
11711301 } ,
1302+ {
1303+ code : `
1304+ <div>
1305+ <div type="text" />
1306+ <Button type="submit" />
1307+ </div>
1308+ ` ,
1309+ options : [ {
1310+ elementOverrides : {
1311+ Button : { restrictedAttributes : [ 'type' ] } ,
1312+ } ,
1313+ } ] ,
1314+ errors : [
1315+ { messageId : 'restrictedAttributeStringInElement' , data : { text : '"submit"' , attribute : 'type' , element : 'Button' } } ,
1316+ ] ,
1317+ } ,
1318+ {
1319+ code : `
1320+ <div>
1321+ <Input placeholder="Enter text" type="password" />
1322+ <Button type="submit" disabled="true" />
1323+ </div>
1324+ ` ,
1325+ options : [ {
1326+ elementOverrides : {
1327+ Input : { restrictedAttributes : [ 'placeholder' ] } ,
1328+ Button : { restrictedAttributes : [ 'disabled' ] } ,
1329+ } ,
1330+ } ] ,
1331+ errors : [
1332+ { messageId : 'restrictedAttributeStringInElement' , data : { text : '"Enter text"' , attribute : 'placeholder' , element : 'Input' } } ,
1333+ { messageId : 'restrictedAttributeStringInElement' , data : { text : '"true"' , attribute : 'disabled' , element : 'Button' } } ,
1334+ ] ,
1335+ } ,
1336+ {
1337+ code : `
1338+ <div>
1339+ <div className="wrapper" id="main" />
1340+ <Button className="btn" id="submit-btn" />
1341+ </div>
1342+ ` ,
1343+ options : [ {
1344+ restrictedAttributes : [ 'className' ] ,
1345+ elementOverrides : {
1346+ Button : { restrictedAttributes : [ 'id' ] } ,
1347+ } ,
1348+ } ] ,
1349+ errors : [
1350+ { messageId : 'restrictedAttributeString' , data : { text : '"wrapper"' , attribute : 'className' } } ,
1351+ { messageId : 'restrictedAttributeStringInElement' , data : { text : '"submit-btn"' , attribute : 'id' , element : 'Button' } } ,
1352+ ] ,
1353+ } ,
1354+ {
1355+ code : `
1356+ <div>
1357+ <div foo1="bar1" />
1358+ <T foo2="bar2" />
1359+ </div>
1360+ ` ,
1361+ options : [ {
1362+ noAttributeStrings : true ,
1363+ elementOverrides : {
1364+ T : { restrictedAttributes : [ 'foo2' ] } ,
1365+ } ,
1366+ } ] ,
1367+ errors : [
1368+ { messageId : 'noStringsInAttributes' , data : { text : '"bar1"' } } ,
1369+ { messageId : 'restrictedAttributeStringInElement' , data : { text : '"bar2"' , attribute : 'foo2' , element : 'T' } } ,
1370+ ] ,
1371+ } ,
11721372 ] ) ,
11731373} ) ;
0 commit comments