Skip to content

Commit 796c0ad

Browse files
authored
fix: false positives for custom-element with svelte v3 in svelte/valid-compile (#604)
1 parent 4b7f4d8 commit 796c0ad

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

.changeset/beige-ducks-add.md

+5
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 custom-element with svelte v3 in `svelte/valid-compile`

src/shared/svelte-compile-warns/index.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { transform as transformWithStylus } from './transform/stylus';
1515
import type { IgnoreItem } from './ignore-comment';
1616
import { getSvelteIgnoreItems } from './ignore-comment';
1717
import { extractLeadingComments } from './extract-leading-comments';
18-
import { getLangValue } from '../../utils/ast-utils';
18+
import { findAttribute, getLangValue } from '../../utils/ast-utils';
1919
import path from 'path';
2020
import fs from 'fs';
2121
import semver from 'semver';
@@ -115,7 +115,7 @@ function getSvelteCompileWarningsWithoutCache(context: RuleContext): SvelteCompi
115115
transformResults.push(...transformScripts(context, text));
116116

117117
if (!transformResults.length) {
118-
const warnings = getWarningsFromCode(text);
118+
const warnings = getWarningsFromCode(text, context);
119119
return {
120120
...processIgnore(
121121
warnings.warnings,
@@ -296,7 +296,7 @@ function getSvelteCompileWarningsWithoutCache(context: RuleContext): SvelteCompi
296296
}
297297

298298
const code = remapContext.postprocess();
299-
const baseWarnings = getWarningsFromCode(code);
299+
const baseWarnings = getWarningsFromCode(code, context);
300300

301301
const warnings: Warning[] = [];
302302
for (const warn of baseWarnings.warnings) {
@@ -401,17 +401,37 @@ function* transformScripts(context: RuleContext, text: string) {
401401
}
402402
}
403403

404+
function hasTagOption(program: AST.SvelteProgram) {
405+
return program.body.some((body) => {
406+
if (body.type !== 'SvelteElement' || body.kind !== 'special') {
407+
return false;
408+
}
409+
if (body.name.name !== 'svelte:options') {
410+
return false;
411+
}
412+
413+
return Boolean(findAttribute(body, 'tag'));
414+
});
415+
}
416+
404417
/**
405418
* Get compile warnings
406419
*/
407-
function getWarningsFromCode(code: string): {
420+
function getWarningsFromCode(
421+
code: string,
422+
context: RuleContext
423+
): {
408424
warnings: Warning[];
409425
kind: 'warn' | 'error';
410426
} {
411427
try {
412428
const result = compiler.compile(code, {
413429
generate: false,
414-
...(semver.satisfies(compiler.VERSION, '>=4.0.0-0') ? { customElement: true } : {})
430+
...(semver.satisfies(compiler.VERSION, '>=4.0.0-0')
431+
? { customElement: true }
432+
: hasTagOption(context.getSourceCode().ast)
433+
? { customElement: true }
434+
: {})
415435
});
416436

417437
return { warnings: result.warnings as Warning[], kind: 'warn' };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<svelte:options tag="my-component" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": "^3.0.0"
3+
}

0 commit comments

Comments
 (0)