Fix tests and attr type check

This commit is contained in:
Abhas
2018-09-24 20:50:46 +05:30
parent 5d1144b5a5
commit e54c0a47bc
2 changed files with 22 additions and 4 deletions
+5 -3
View File
@@ -38,14 +38,16 @@ module.exports = {
return {
"JSXOpeningElement": function (node) {
console.log(node.attributes);
const hasClassNameWithoutExpr = node.attributes.some((attr) => (
attr.type === "JSXIdentifier" && attr.name.name === "className" &&
attr.name.type === "JSXIdentifier" && attr.name.name === "className" &&
(attr.value === null || attr.value.type !== 'JSXExpressionContainer')
));
const hasStyleName = node.attributes.some((attr) => attr.type === "JSXIdentifier" && attr.name.name === "styleName");
const hasStyleName = node.attributes.some((attr) => attr.name.type === "JSXIdentifier" && attr.name.name === "styleName");
if (hasClassNameWithoutExpr && hasStyleName) {
const nodeAttrClassName = node.attributes.find((attr) => attr.type === "JSXIdentifier" && attr.name.name === "className");
const nodeAttrClassName = node.attributes.find((attr) => attr.name.type === "JSXIdentifier" && attr.name.name === "className");
context.report(nodeAttrClassName, reportText);
}
}
+17 -1
View File
@@ -21,7 +21,8 @@ var ruleTester = new RuleTester({
parserOptions: {
"ecmaFeatures": {
"jsx": true
}
},
ecmaVersion: 7,
}
});
ruleTester.run("no-classname-with-stylename", rule, {
@@ -30,6 +31,8 @@ ruleTester.run("no-classname-with-stylename", rule, {
"<span styleName='my-card' />",
"<span className='card' />",
"<span className={123} styleName='my-card' />",
"<span {...({a:1})} className={'card'} styleName='my-card' />",
"<span {...someVar} className={'card'} styleName='my-card' />",
"<span className={'card'} styleName='my-card' />",
"<span className={true ? 'card' : 'no-card'} styleName='my-card' />"
// give me some code that won't trigger a warning
@@ -53,6 +56,19 @@ ruleTester.run("no-classname-with-stylename", rule, {
errors: [{
// message: "Disallow string className alongwith styleName in the same JSX tag",
}]
},
{
code: "<span {...({qwe:234})} className='card' styleName='my-card' />",
errors: [{
// message: "Disallow string className alongwith styleName in the same JSX tag",
}]
},
{
code: "<span {...someVar} className='card' styleName='my-card' />",
errors: [{
// message: "Disallow string className alongwith styleName in the same JSX tag",
}]
}
]
});