Skip to content

Commit 60a56e4

Browse files
committed
fix
1 parent f0170fa commit 60a56e4

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

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

+26-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ 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';
21+
import semver from 'semver';
2122

2223
type WarningTargetNode =
2324
| (AST.SvelteProgram & ASTNodeWithParent)
@@ -114,7 +115,7 @@ function getSvelteCompileWarningsWithoutCache(context: RuleContext): SvelteCompi
114115
transformResults.push(...transformScripts(context, text));
115116

116117
if (!transformResults.length) {
117-
const warnings = getWarningsFromCode(text);
118+
const warnings = getWarningsFromCode(text, context);
118119
return {
119120
...processIgnore(
120121
warnings.warnings,
@@ -295,7 +296,7 @@ function getSvelteCompileWarningsWithoutCache(context: RuleContext): SvelteCompi
295296
}
296297

297298
const code = remapContext.postprocess();
298-
const baseWarnings = getWarningsFromCode(code);
299+
const baseWarnings = getWarningsFromCode(code, context);
299300

300301
const warnings: Warning[] = [];
301302
for (const warn of baseWarnings.warnings) {
@@ -400,17 +401,37 @@ function* transformScripts(context: RuleContext, text: string) {
400401
}
401402
}
402403

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+
403417
/**
404418
* Get compile warnings
405419
*/
406-
function getWarningsFromCode(code: string): {
420+
function getWarningsFromCode(
421+
code: string,
422+
context: RuleContext
423+
): {
407424
warnings: Warning[];
408425
kind: 'warn' | 'error';
409426
} {
410427
try {
411428
const result = compiler.compile(code, {
412429
generate: false,
413-
customElement: true
430+
...(semver.satisfies(compiler.VERSION, '>=4.0.0-0')
431+
? { customElement: true }
432+
: hasTagOption(context.getSourceCode().ast)
433+
? { customElement: true }
434+
: {})
414435
});
415436

416437
return { warnings: result.warnings as Warning[], kind: 'warn' };

0 commit comments

Comments
 (0)