Skip to content

Commit be2ec76

Browse files
committed
[Fix] button-has-type: improve error message when an identifier is used as the value
Fixes #1874.
1 parent 1544d84 commit be2ec76

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/rules/button-has-type.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ module.exports = {
7171
});
7272
}
7373

74-
function checkValue(node, value) {
74+
function checkValue(node, value, quote = x => `"${x}"`) {
7575
if (!(value in configuration)) {
7676
context.report({
7777
node: node,
78-
message: `"${value}" is an invalid value for button type attribute`
78+
message: `${quote(value)} is an invalid value for button type attribute`
7979
});
8080
} else if (!configuration[value]) {
8181
context.report({
8282
node: node,
83-
message: `"${value}" is a forbidden value for button type attribute`
83+
message: `${quote(value)} is a forbidden value for button type attribute`
8484
});
8585
}
8686
}
@@ -98,7 +98,12 @@ module.exports = {
9898
return;
9999
}
100100

101-
checkValue(node, getLiteralPropValue(typeProp));
101+
const propValue = getLiteralPropValue(typeProp);
102+
if (!propValue && typeProp.value && typeProp.value.expression) {
103+
checkValue(node, typeProp.value.expression.name, x => `\`${x}\``);
104+
} else {
105+
checkValue(node, propValue);
106+
}
102107
},
103108
CallExpression: function(node) {
104109
if (!isCreateElement(node, context)) {

tests/lib/rules/button-has-type.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ ruleTester.run('button-has-type', rule, {
6969
message: '"foo" is an invalid value for button type attribute'
7070
}]
7171
},
72+
{
73+
code: '<button type={foo}/>',
74+
errors: [{
75+
message: '`foo` is an invalid value for button type attribute'
76+
}]
77+
},
7278
{
7379
code: '<button type="reset"/>',
7480
options: [{reset: false}],

0 commit comments

Comments
 (0)