sveltejs/eslint-plugin-svelte

New rule: `no undefined variable in the DOM`

开放

#747 创建于 2024年5月2日

 (10 条评论) (1 个反应) (0 位负责人)TypeScript (72 个派生)auto 404
enhancementhelp wantednew rule

仓库指标

星标
 (400 个星标)
PR 合并指标
 (平均合并 3天 5小时) (30 天内合并 6 个 PR)

描述

Motivation

When using a variable (say a props) that can be null or undefined directly in the DOM, it will be rendered as a string "null" or "undefined". Hence, it is probablly never going to be a good thing to use a possibly null or undefined variable directly in the DOM.

Description

The rule would highlight any variable or expression used in the DOM that resolves to a type involving a union type to null or undefined.

Examples

<script lang="ts">
  export let prop: string | undefined = undefined;

  const frameworks = ["svelte", "react"]
</script>

<!-- ✓ GOOD -->
{#if prop}
  {prop}
{/if}

<!-- ✗ BAD -->
{prop}
{frameworks.find(framework => framework === prop)}

Additional comments

No response

贡献者指南