Skip to content

AI-native design editor. Open-source Figma alternative.

fix(core): check controls inside plural groups (#1473)

Danny Ward8d440b937e13
diff --git a/changes/1473/fixed.md b/changes/1473/fixed.md
new file mode 100644
index 000000000..7c4f061df
--- /dev/null
+++ b/changes/1473/fixed.md
@@ -0,0 +1 @@
+- The touch-target-size lint rule checks the controls in a row named in the plural, such as `Footer links`, instead of reporting the row as one target.
diff --git a/packages/core/src/lint/rules/touch-target-size.ts b/packages/core/src/lint/rules/touch-target-size.ts
index 599164f39..34f7e7b17 100644
--- a/packages/core/src/lint/rules/touch-target-size.ts
+++ b/packages/core/src/lint/rules/touch-target-size.ts
@@ -25,11 +25,18 @@ const CONTROL_WORDS = new Set([
'action'
])
+function isPluralControlWord(word: string): boolean {
+ return word.endsWith('s') && CONTROL_WORDS.has(word.slice(0, -1))
+}
+
function isInteractive(name: string): boolean {
- return nameWords(name).some(
- (word) =>
- CONTROL_WORDS.has(word) || (word.endsWith('s') && CONTROL_WORDS.has(word.slice(0, -1)))
- )
+ return nameWords(name).some((word) => CONTROL_WORDS.has(word) || isPluralControlWord(word))
+}
+
+function isPluralOnlyControlGroup(name: string, childCount: number): boolean {
+ if (childCount < 2) return false
+ const words = nameWords(name)
+ return words.some(isPluralControlWord) && !words.some((word) => CONTROL_WORDS.has(word))
}
export default defineRule({
@@ -41,13 +48,20 @@ export default defineRule({
},
match: ['FRAME', 'COMPONENT', 'INSTANCE', 'RECTANGLE', 'ELLIPSE'],
check(node, context) {
- if (!isInteractive(node.name)) return
+ // A row of controls named in the plural is not one target; its controls are.
+ if (!isInteractive(node.name) || isPluralOnlyControlGroup(node.name, node.childIds.length))
+ return
const config = context.getConfig() as { minSize?: number } | undefined
const minSize = config?.minSize ?? DEFAULT_MIN_SIZE
if (node.width >= minSize && node.height >= minSize) return
// A control nested in another control (the icon of a button) is not its own target.
- for (let parent = context.getParent(node); parent; parent = context.getParent(parent)) {
- if (isInteractive(parent.name)) return
+ for (let ancestor = context.getParent(node); ancestor; ancestor = context.getParent(ancestor)) {
+ if (
+ isInteractive(ancestor.name) &&
+ !isPluralOnlyControlGroup(ancestor.name, ancestor.childIds.length)
+ ) {
+ return
+ }
}
context.report({
node,
diff --git a/packages/core/tests/lint/rules.test.ts b/packages/core/tests/lint/rules.test.ts
index 48579ae81..47907ea96 100644
--- a/packages/core/tests/lint/rules.test.ts
+++ b/packages/core/tests/lint/rules.test.ts
@@ -454,6 +454,41 @@ describe('touch-target-size', () => {
expect(messages[0]?.data).toEqual({ width: 60, height: 24, minSize: 44 })
})
+ test('checks the controls in a row named in the plural, not the row', () => {
+ const graph = new SceneGraph()
+ const pageId = graph.getPages()[0].id
+ const group = graph.createNode('FRAME', pageId, {
+ name: 'Footer links',
+ width: 280,
+ height: 20
+ })
+ const first = graph.createNode('FRAME', group.id, { name: 'Link', width: 80, height: 20 })
+ graph.createNode('RECTANGLE', group.id, { name: 'Divider', width: 1, height: 16 })
+ const second = graph.createNode('FRAME', group.id, { name: 'Link', width: 80, height: 20 })
+
+ const messages = lint(graph, 'touch-target-size')
+
+ expect(messages.map((message) => message.nodeId)).toEqual([first.id, second.id])
+ })
+
+ test('keeps singularly named controls as targets even when they contain children', () => {
+ const graph = new SceneGraph()
+ const pageId = graph.getPages()[0].id
+ const button = graph.createNode('FRAME', pageId, { name: 'Button', width: 40, height: 20 })
+ graph.createNode('FRAME', button.id, { name: 'Icon', width: 16, height: 16 })
+
+ expect(lint(graph, 'touch-target-size').map((message) => message.nodeId)).toEqual([button.id])
+ })
+
+ test('keeps a plural-named layer as a target when it has fewer than two children', () => {
+ const graph = new SceneGraph()
+ const pageId = graph.getPages()[0].id
+ const group = graph.createNode('FRAME', pageId, { name: 'Links', width: 40, height: 20 })
+ graph.createNode('FRAME', group.id, { name: 'Link', width: 80, height: 20 })
+
+ expect(lint(graph, 'touch-target-size').map((message) => message.nodeId)).toEqual([group.id])
+ })
+
test('matches control names as whole words', () => {
const graph = new SceneGraph()
const pageId = graph.getPages()[0].id