Skip to content

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

fix(lint): let Vue template rules see the template (#1161)

Danila Poyarkov3ee081bdff7c
diff --git a/src/components/LayersPanel.vue b/src/components/LayersPanel.vue
index a674a784c..269651a8d 100644
--- a/src/components/LayersPanel.vue
+++ b/src/components/LayersPanel.vue
@@ -3,7 +3,7 @@ import { SplitterGroup, SplitterPanel, SplitterResizeHandle } from 'reka-ui'
import { tv } from 'tailwind-variants'
import { computed, ref } from 'vue'
-import { useI18n } from '@open-pencil/vue'
+import { useI18n, vTestId } from '@open-pencil/vue'
import AppMenu from '@/components/shell/AppMenu.vue'
import SegmentedControl from '@/components/ui/select/SegmentedControl.vue'
@@ -45,9 +45,7 @@ const splitterStyles = tv(splitterTheme)({ direction: 'vertical' })
>
<template #option="{ option }">
<span
- :data-test-id="
- option.value === 'file' ? 'left-panel-layers-tab' : 'left-panel-assets-tab'
- "
+ v-test-id="option.value === 'file' ? 'left-panel-layers-tab' : 'left-panel-assets-tab'"
class="truncate"
>
{{ option.label }}
diff --git a/src/components/font-settings/FontSettingsPopover.vue b/src/components/font-settings/FontSettingsPopover.vue
index 065c9ddff..015e236ae 100644
--- a/src/components/font-settings/FontSettingsPopover.vue
+++ b/src/components/font-settings/FontSettingsPopover.vue
@@ -4,7 +4,7 @@ import { onMounted } from 'vue'
import { WEB_FONT_PROVIDER_IDS, WEB_FONT_PROVIDER_LABELS } from '@open-pencil/core/text'
import type { WebFontProviderId } from '@open-pencil/core/text'
-import { useI18n, useRetainedPopup } from '@open-pencil/vue'
+import { useI18n, useRetainedPopup, vTestId } from '@open-pencil/vue'
import { isTauri } from '@/app/tauri/env'
import AppButton from '@/components/ui/button/AppButton.vue'
@@ -191,7 +191,7 @@ onMounted(() => {
class="size-3 accent-accent disabled:opacity-50"
:checked="isProviderEnabled(provider)"
:disabled="busyAction !== null || !onlineFontsEnabled"
- :data-test-id="`font-settings-provider-${provider}`"
+ v-test-id="`font-settings-provider-${provider}`"
@change="onProviderToggle(provider, $event)"
/>
</label>
diff --git a/tools/checks/lint/src/rules/quality/tailwind-classes.ts b/tools/checks/lint/src/rules/quality/tailwind-classes.ts
index 6c52ffde2..d57d7d5a6 100644
--- a/tools/checks/lint/src/rules/quality/tailwind-classes.ts
+++ b/tools/checks/lint/src/rules/quality/tailwind-classes.ts
@@ -6,7 +6,6 @@ import {
VUE_DIRECTIVE_NODE,
VUE_ELEMENT_NODE,
isVueSourceFile,
- vueComponentSource,
vueTemplateAst,
walkVueTemplateAst
} from '#lint/support/vue.ts'
@@ -157,8 +156,7 @@ export const noUnknownTailwindClassesRule = defineRule({
return {
Program(node) {
if (!isVueSourceFile(file)) return
- const source = vueComponentSource(context.sourceCode.getText(), file)
- const template = vueTemplateAst(source, file)
+ const template = vueTemplateAst(context.sourceCode.getText(), file)
if (!template) return
walkVueTemplateAst(template, (templateNode) => {
if (templateNode.type !== VUE_ELEMENT_NODE) return
diff --git a/tools/checks/lint/src/rules/vue/test-ids.ts b/tools/checks/lint/src/rules/vue/test-ids.ts
index f57d457e5..1b4bfd2c3 100644
--- a/tools/checks/lint/src/rules/vue/test-ids.ts
+++ b/tools/checks/lint/src/rules/vue/test-ids.ts
@@ -2,6 +2,7 @@ import { normalizedFilename } from '#lint/support/context.ts'
import type { RuleDefinition } from '#lint/support/types.ts'
import type { VueTemplateNode } from '#lint/support/vue.ts'
import {
+ VUE_ELEMENT_NODE,
VUE_DIRECTIVE_NODE,
hasExpressionCall,
isStaticVueAttribute,
@@ -10,6 +11,7 @@ import {
walkVueTemplateAst
} from '#lint/support/vue.ts'
import type { TSESTree } from '@typescript-eslint/utils'
+import { ElementTypes } from '@vue/compiler-core'
const TEST_ID_FORMAT = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/
@@ -42,10 +44,16 @@ const noRawTestIdStringProps = {
}
} satisfies RuleDefinition
+/**
+ * A dynamic test id on an HTML element goes through `v-test-id`. A component takes
+ * `:data-test-id` as an attribute, which reaches the element it forwards attributes to; the
+ * directive would tag the component's root instead.
+ */
const noDynamicDataTestIdInVue = {
meta: {
docs: {
- description: 'Disallow dynamic :data-test-id in Vue components — use v-test-id'
+ description:
+ 'Disallow dynamic :data-test-id on HTML elements in Vue templates — use v-test-id'
}
},
create(context) {
@@ -59,12 +67,18 @@ const noDynamicDataTestIdInVue = {
let hasDynamicDataTestId = false
walkVueTemplateAst(template, (templateNode) => {
if (hasDynamicDataTestId) return
- if (isVueBindDirective(templateNode, 'data-test-id')) hasDynamicDataTestId = true
+ if (
+ templateNode.type === VUE_ELEMENT_NODE &&
+ templateNode.tagType === ElementTypes.ELEMENT &&
+ templateNode.props.some((prop) => isVueBindDirective(prop, 'data-test-id'))
+ )
+ hasDynamicDataTestId = true
})
if (!hasDynamicDataTestId) return
context.report({
node,
- message: 'Use v-test-id for dynamic/configurable test ids instead of :data-test-id.'
+ message:
+ 'Use v-test-id for a dynamic test id on an HTML element instead of :data-test-id.'
})
}
}
diff --git a/tools/checks/lint/src/support/vue.ts b/tools/checks/lint/src/support/vue.ts
index a9fcdc05f..55d6a7be4 100644
--- a/tools/checks/lint/src/support/vue.ts
+++ b/tools/checks/lint/src/support/vue.ts
@@ -26,7 +26,7 @@ function vueComponentSource(source: string, filename: string): string {
}
function vueSfcDescriptor(source: string, filename: string) {
- return parseVueSfc(source, { filename }).descriptor
+ return parseVueSfc(vueComponentSource(source, filename), { filename }).descriptor
}
function vueTemplateAst(source: string, filename: string): RootNode | null {
diff --git a/tools/checks/lint/tests/vue-template-source.test.ts b/tools/checks/lint/tests/vue-template-source.test.ts
new file mode 100644
index 000000000..28b1083b2
--- /dev/null
+++ b/tools/checks/lint/tests/vue-template-source.test.ts
@@ -0,0 +1,30 @@
+import { expect, test } from 'bun:test'
+
+import { lintFiles, ruleDiagnostics } from './helpers/lint.ts'
+
+const SCRIPT = '<script setup lang="ts">\nconst id = "save"\n</script>\n'
+
+// Oxlint hands JS plugins only a component's <script>; template rules read the rest from the file.
+test('template rules see the template of a component Oxlint lints', async () => {
+ const rule = 'no-native-title-attributes-in-vue'
+ const diagnostics = await lintFiles(
+ {
+ 'src/components/SaveButton.vue': `${SCRIPT}<template><button title="Save">{{ id }}</button></template>\n`
+ },
+ { [`open-pencil/${rule}`]: 'error' }
+ )
+ expect(ruleDiagnostics(diagnostics, rule)).toHaveLength(1)
+})
+
+test('a dynamic test id goes through v-test-id on an element and stays an attribute on a component', async () => {
+ const rule = 'no-dynamic-data-test-id-in-vue'
+ const diagnostics = await lintFiles(
+ {
+ 'src/components/OnElement.vue': `${SCRIPT}<template><span :data-test-id="id" /></template>\n`,
+ 'src/components/OnComponent.vue': `${SCRIPT}<template><AppSelect :data-test-id="id" /></template>\n`
+ },
+ { [`open-pencil/${rule}`]: 'error' }
+ )
+ const files = ruleDiagnostics(diagnostics, rule).map((item) => item.filename?.split('/').pop())
+ expect(files).toEqual(['OnElement.vue'])
+})