Skip to content

fix: false positives for custom-element with svelte v3 in svelte/valid-compile #604

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

Merged
merged 4 commits into from
Nov 3, 2023
Merged
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/beige-ducks-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": patch
---

fix: false positives for custom-element with svelte v3 in `svelte/valid-compile`
30 changes: 25 additions & 5 deletions src/shared/svelte-compile-warns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { transform as transformWithStylus } from './transform/stylus';
import type { IgnoreItem } from './ignore-comment';
import { getSvelteIgnoreItems } from './ignore-comment';
import { extractLeadingComments } from './extract-leading-comments';
import { getLangValue } from '../../utils/ast-utils';
import { findAttribute, getLangValue } from '../../utils/ast-utils';
import path from 'path';
import fs from 'fs';
import semver from 'semver';
Expand Down Expand Up @@ -115,7 +115,7 @@ function getSvelteCompileWarningsWithoutCache(context: RuleContext): SvelteCompi
transformResults.push(...transformScripts(context, text));

if (!transformResults.length) {
const warnings = getWarningsFromCode(text);
const warnings = getWarningsFromCode(text, context);
return {
...processIgnore(
warnings.warnings,
Expand Down Expand Up @@ -296,7 +296,7 @@ function getSvelteCompileWarningsWithoutCache(context: RuleContext): SvelteCompi
}

const code = remapContext.postprocess();
const baseWarnings = getWarningsFromCode(code);
const baseWarnings = getWarningsFromCode(code, context);

const warnings: Warning[] = [];
for (const warn of baseWarnings.warnings) {
Expand Down Expand Up @@ -401,17 +401,37 @@ function* transformScripts(context: RuleContext, text: string) {
}
}

function hasTagOption(program: AST.SvelteProgram) {
return program.body.some((body) => {
if (body.type !== 'SvelteElement' || body.kind !== 'special') {
return false;
}
if (body.name.name !== 'svelte:options') {
return false;
}

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

/**
* Get compile warnings
*/
function getWarningsFromCode(code: string): {
function getWarningsFromCode(
code: string,
context: RuleContext
): {
warnings: Warning[];
kind: 'warn' | 'error';
} {
try {
const result = compiler.compile(code, {
generate: false,
...(semver.satisfies(compiler.VERSION, '>=4.0.0-0') ? { customElement: true } : {})
...(semver.satisfies(compiler.VERSION, '>=4.0.0-0')
? { customElement: true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ask]

Why always we can set { customElement: true } if Svelte version is 4?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In v3, if customElement:true is set, a warning will be returned if <svelte:options tag> is missing, but in v4, there will be no warning even if <svelte:options customElement> is missing.

: hasTagOption(context.getSourceCode().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 @@
<svelte:options tag="my-component" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelte": "^3.0.0"
}