Skip to content

Commit

Permalink
Support trivial template literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypnosphi committed Aug 9, 2020
1 parent c1cceba commit 6836793
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
28 changes: 17 additions & 11 deletions lib/rules/button-has-type.js
Expand Up @@ -95,18 +95,24 @@ module.exports = {
}

function checkExpression(node, expression) {
if (expression.type === 'Literal') {
checkValue(node, expression.value);
return;
}

if (expression.type === 'ConditionalExpression') {
checkExpression(node, expression.consequent);
checkExpression(node, expression.alternate);
return;
switch (expression.type) {
case 'Literal':
checkValue(node, expression.value);
return;
case 'TemplateLiteral':
if (expression.expressions.length === 0) {
checkValue(node, expression.quasis[0].value.raw);
} else {
reportComplex(expression);
}
return;
case 'ConditionalExpression':
checkExpression(node, expression.consequent);
checkExpression(node, expression.alternate);
return;
default:
reportComplex(expression);
}

reportComplex(expression);
}

return {
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/button-has-type.js
Expand Up @@ -33,6 +33,8 @@ ruleTester.run('button-has-type', rule, {
{code: '<button type="submit"/>'},
{code: '<button type="reset"/>'},
{code: '<button type={"button"}/>'},
{code: '<button type={\'button\'}/>'},
{code: '<button type={`button`}/>'},
{code: '<button type={condition ? "button" : "submit"}/>'},
{
code: '<button type="button"/>',
Expand Down Expand Up @@ -91,6 +93,18 @@ 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={`button${foo}`}/>',
errors: [{
message: 'The button type attribute must be specified by a static string or a trivial ternary expression'
}]
},
{
code: '<button type="reset"/>',
options: [{reset: false}],
Expand Down

0 comments on commit 6836793

Please sign in to comment.