Skip to content

Commit 66aa6fe

Browse files
committed
Handle conditional expressions in :is
1 parent 1756a8a commit 66aa6fe

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
66

77
## [Unreleased]
88

9+
### Changed
10+
11+
- When you pass a conditional expression into an `:is`, then `getElementType` would not be able to handle it. It doesn't truly handle it now, as it just ends up returning `null` for the value of the `:is` expression, but at least now it doesn't break.
12+
913
## [0.5.0] - 2020-09-05
1014

1115
### Changed

src/rules/__tests__/interactive-supports-focus.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ makeRuleTester("interactive-supports-focus", rule, {
6060
"<div role='textbox' tabindex='0' @click='void 0' />",
6161
"<div role='textbox' aria-disabled='true' @click='void 0' />",
6262
"<Foo.Bar @click='void 0' aria-hidden='false' />",
63-
"<Input @click='void 0' type='hidden' />"
63+
"<Input @click='void 0' type='hidden' />",
64+
`<component role="button" :is="foo ? 'a' : 'button'" />`
6465
],
6566
invalid: [
6667
...rule.interactiveRoles.flatMap((role) =>

src/utils/getAttributeValue.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@ const getAttributeValue = (node) => {
1010
}
1111

1212
if (key.name.name === "bind" && value.expression) {
13-
return value.type === "Literal" ? value.expression.value : value.expression;
13+
// <div :height="100" />
14+
if (value.expression.type === "Literal") {
15+
return value.expression.value;
16+
}
17+
18+
// TODO we're effectively using this as just a placeholder to let rules know
19+
// that a value has been passed in for this attribute. We should replace
20+
// this with a stronger API to either explicitly handel all of the different
21+
// types of values or just return a special symbol or something else.
22+
return value.expression;
1423
}
1524

1625
return null;

src/utils/getElementType.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
const getElementAttributeValue = require("./getElementAttributeValue");
22
const makeKebabCase = require("./makeKebabCase");
33

4-
const getElementType = (node) =>
5-
makeKebabCase(getElementAttributeValue(node, "is") || node.rawName);
4+
const getElementType = (node) => {
5+
let is = getElementAttributeValue(node, "is");
6+
7+
// If we could not parse the `is` value into a simple literal, we're going to
8+
// have to ignore it because we're not smart enough to handle multiple values
9+
// yet.
10+
if (typeof is !== "string") {
11+
is = null;
12+
}
13+
14+
return makeKebabCase(is || node.rawName);
15+
};
616

717
module.exports = getElementType;

0 commit comments

Comments
 (0)