Skip to content

fix: false positives for regular components in valid-compile/custom_element_props_identifier (#1241) #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/light-bars-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': patch
---

fix false positives for regular components in valid-compile/custom_element_props_identifier
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { extractLeadingComments } from './extract-leading-comments.js';
import { findAttribute, getLangValue } from '../../utils/ast-utils.js';
import path from 'path';
import fs from 'fs';
import semver from 'semver';

type WarningTargetNode =
| (AST.SvelteProgram & ASTNodeWithParent)
Expand Down Expand Up @@ -383,7 +382,7 @@ function* transformScripts(context: RuleContext, text: string) {
}
}

function hasTagOption(program: AST.SvelteProgram) {
function isCustomElement(program: AST.SvelteProgram) {
return program.body.some((body) => {
if (body.type !== 'SvelteElement' || body.kind !== 'special') {
return false;
Expand All @@ -392,7 +391,7 @@ function hasTagOption(program: AST.SvelteProgram) {
return false;
}

return Boolean(findAttribute(body, 'tag'));
return Boolean(findAttribute(body, 'tag')) || Boolean(findAttribute(body, 'customElement'));
});
}

Expand All @@ -409,11 +408,7 @@ function getWarningsFromCode(
try {
const result = compiler.compile(code, {
generate: false,
...(semver.satisfies(compiler.VERSION, '>=4.0.0-0')
? { customElement: true }
: hasTagOption(context.sourceCode.ast)
? { customElement: true }
: {})
...(isCustomElement(context.sourceCode.ast) ? { customElement: true } : {})
});

return { warnings: result.warnings as Warning[], kind: 'warn' };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- message: |-
Using a rest element or a non-destructured declaration with `$props()` means that Svelte can't infer what properties to expose when creating a custom element. Consider destructuring all the props or explicitly specifying the `customElement.props` option.
https://svelte.dev/e/custom_element_props_identifier(custom_element_props_identifier)
line: 4
column: 6
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<svelte:options customElement="my-component" />

<script>
let props = $props();
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelte": ">=5.33.4"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<script>
let props = $props();
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelte": ">=5.33.4"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<svelte:options customElement={{ tag: 'my-component', props: {} }} />

<script>
let props = $props();
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelte": ">=5.33.4"
}