Skip to content

Commit 5df1121

Browse files
authored
fix: false positives for regular components in valid-compile/custom_element_props_identifier (#1241) (#1243)
1 parent c1631e2 commit 5df1121

9 files changed

+36
-8
lines changed

.changeset/light-bars-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': patch
3+
---
4+
5+
fix false positives for regular components in valid-compile/custom_element_props_identifier

packages/eslint-plugin-svelte/src/shared/svelte-compile-warns/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { extractLeadingComments } from './extract-leading-comments.js';
1717
import { findAttribute, getLangValue } from '../../utils/ast-utils.js';
1818
import path from 'path';
1919
import fs from 'fs';
20-
import semver from 'semver';
2120

2221
type WarningTargetNode =
2322
| (AST.SvelteProgram & ASTNodeWithParent)
@@ -383,7 +382,7 @@ function* transformScripts(context: RuleContext, text: string) {
383382
}
384383
}
385384

386-
function hasTagOption(program: AST.SvelteProgram) {
385+
function isCustomElement(program: AST.SvelteProgram) {
387386
return program.body.some((body) => {
388387
if (body.type !== 'SvelteElement' || body.kind !== 'special') {
389388
return false;
@@ -392,7 +391,7 @@ function hasTagOption(program: AST.SvelteProgram) {
392391
return false;
393392
}
394393

395-
return Boolean(findAttribute(body, 'tag'));
394+
return Boolean(findAttribute(body, 'tag')) || Boolean(findAttribute(body, 'customElement'));
396395
});
397396
}
398397

@@ -409,11 +408,7 @@ function getWarningsFromCode(
409408
try {
410409
const result = compiler.compile(code, {
411410
generate: false,
412-
...(semver.satisfies(compiler.VERSION, '>=4.0.0-0')
413-
? { customElement: true }
414-
: hasTagOption(context.sourceCode.ast)
415-
? { customElement: true }
416-
: {})
411+
...(isCustomElement(context.sourceCode.ast) ? { customElement: true } : {})
417412
});
418413

419414
return { warnings: result.warnings as Warning[], kind: 'warn' };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
- message: |-
2+
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.
3+
https://svelte.dev/e/custom_element_props_identifier(custom_element_props_identifier)
4+
line: 4
5+
column: 6
6+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<svelte:options customElement="my-component" />
2+
3+
<script>
4+
let props = $props();
5+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": ">=5.33.4"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>
2+
let props = $props();
3+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": ">=5.33.4"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<svelte:options customElement={{ tag: 'my-component', props: {} }} />
2+
3+
<script>
4+
let props = $props();
5+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": ">=5.33.4"
3+
}

0 commit comments

Comments
 (0)