Skip to content

Commit

Permalink
[Fix] button-has-type: improve error message when an identifier is …
Browse files Browse the repository at this point in the history
…used as the value

Fixes #1874.
  • Loading branch information
ljharb committed Jul 3, 2018
1 parent 1544d84 commit be2ec76
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/rules/button-has-type.js
Expand Up @@ -71,16 +71,16 @@ module.exports = {
});
}

function checkValue(node, value) {
function checkValue(node, value, quote = x => `"${x}"`) {
if (!(value in configuration)) {
context.report({
node: node,
message: `"${value}" is an invalid value for button type attribute`
message: `${quote(value)} is an invalid value for button type attribute`
});
} else if (!configuration[value]) {
context.report({
node: node,
message: `"${value}" is a forbidden value for button type attribute`
message: `${quote(value)} is a forbidden value for button type attribute`
});
}
}
Expand All @@ -98,7 +98,12 @@ module.exports = {
return;
}

checkValue(node, getLiteralPropValue(typeProp));
const propValue = getLiteralPropValue(typeProp);
if (!propValue && typeProp.value && typeProp.value.expression) {
checkValue(node, typeProp.value.expression.name, x => `\`${x}\``);
} else {
checkValue(node, propValue);
}
},
CallExpression: function(node) {
if (!isCreateElement(node, context)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/lib/rules/button-has-type.js
Expand Up @@ -69,6 +69,12 @@ ruleTester.run('button-has-type', rule, {
message: '"foo" is an invalid value for button type attribute'
}]
},
{
code: '<button type={foo}/>',
errors: [{
message: '`foo` is an invalid value for button type attribute'
}]
},
{
code: '<button type="reset"/>',
options: [{reset: false}],
Expand Down

0 comments on commit be2ec76

Please sign in to comment.