From 3e658bce0c8c16fe3c75fa05e94c0fa028f888ac Mon Sep 17 00:00:00 2001 From: ota-meshi Date: Fri, 3 Nov 2023 09:57:13 +0900 Subject: [PATCH 1/4] fix: false positives for custom-element with svelte v3 in `svelte/valid-compile` --- src/shared/svelte-compile-warns/index.ts | 2 +- .../valid/svelte3-options-custom-element-input.svelte | 1 + .../valid/svelte3-options-custom-element-requirements.json | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/rules/valid-compile/valid/svelte3-options-custom-element-input.svelte create mode 100644 tests/fixtures/rules/valid-compile/valid/svelte3-options-custom-element-requirements.json diff --git a/src/shared/svelte-compile-warns/index.ts b/src/shared/svelte-compile-warns/index.ts index daf158947..0ba809e55 100644 --- a/src/shared/svelte-compile-warns/index.ts +++ b/src/shared/svelte-compile-warns/index.ts @@ -411,7 +411,7 @@ function getWarningsFromCode(code: string): { try { const result = compiler.compile(code, { generate: false, - ...(semver.satisfies(compiler.VERSION, '>=4.0.0-0') ? { customElement: true } : {}) + customElement: true }); return { warnings: result.warnings as Warning[], kind: 'warn' }; diff --git a/tests/fixtures/rules/valid-compile/valid/svelte3-options-custom-element-input.svelte b/tests/fixtures/rules/valid-compile/valid/svelte3-options-custom-element-input.svelte new file mode 100644 index 000000000..13ed52de4 --- /dev/null +++ b/tests/fixtures/rules/valid-compile/valid/svelte3-options-custom-element-input.svelte @@ -0,0 +1 @@ + diff --git a/tests/fixtures/rules/valid-compile/valid/svelte3-options-custom-element-requirements.json b/tests/fixtures/rules/valid-compile/valid/svelte3-options-custom-element-requirements.json new file mode 100644 index 000000000..1bd27fa6f --- /dev/null +++ b/tests/fixtures/rules/valid-compile/valid/svelte3-options-custom-element-requirements.json @@ -0,0 +1,3 @@ +{ + "svelte": "^3.0.0" +} From 2d3ef2bfafca9c4ad64c09873a1831ca03ec6510 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Fri, 3 Nov 2023 09:58:10 +0900 Subject: [PATCH 2/4] Create beige-ducks-add.md --- .changeset/beige-ducks-add.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/beige-ducks-add.md diff --git a/.changeset/beige-ducks-add.md b/.changeset/beige-ducks-add.md new file mode 100644 index 000000000..e884e13f4 --- /dev/null +++ b/.changeset/beige-ducks-add.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-svelte": patch +--- + +fix: false positives for custom-element with svelte v3 in `svelte/valid-compile` From f0170fae4a0d15c4d3d15fc40e0b8260b36c2954 Mon Sep 17 00:00:00 2001 From: ota-meshi Date: Fri, 3 Nov 2023 10:04:33 +0900 Subject: [PATCH 3/4] fix --- src/shared/svelte-compile-warns/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/shared/svelte-compile-warns/index.ts b/src/shared/svelte-compile-warns/index.ts index 0ba809e55..48974ae71 100644 --- a/src/shared/svelte-compile-warns/index.ts +++ b/src/shared/svelte-compile-warns/index.ts @@ -18,7 +18,6 @@ import { extractLeadingComments } from './extract-leading-comments'; import { getLangValue } from '../../utils/ast-utils'; import path from 'path'; import fs from 'fs'; -import semver from 'semver'; type WarningTargetNode = | (AST.SvelteProgram & ASTNodeWithParent) From 60a56e4f4a01bc152cf192af5a123c2f7ab1d92b Mon Sep 17 00:00:00 2001 From: ota-meshi Date: Fri, 3 Nov 2023 10:14:19 +0900 Subject: [PATCH 4/4] fix --- src/shared/svelte-compile-warns/index.ts | 31 ++++++++++++++++++++---- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/shared/svelte-compile-warns/index.ts b/src/shared/svelte-compile-warns/index.ts index 48974ae71..5ec8bd0b8 100644 --- a/src/shared/svelte-compile-warns/index.ts +++ b/src/shared/svelte-compile-warns/index.ts @@ -15,9 +15,10 @@ 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'; type WarningTargetNode = | (AST.SvelteProgram & ASTNodeWithParent) @@ -114,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, @@ -295,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) { @@ -400,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, - customElement: true + ...(semver.satisfies(compiler.VERSION, '>=4.0.0-0') + ? { customElement: true } + : hasTagOption(context.getSourceCode().ast) + ? { customElement: true } + : {}) }); return { warnings: result.warnings as Warning[], kind: 'warn' };