Skip to content

Commit 9d1fd8b

Browse files
committed
refactor
1 parent 32e224a commit 9d1fd8b

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/parser/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import {
4444
resolveSvelteParseContextForSvelteScript,
4545
type SvelteParseContext,
4646
} from "./svelte-parse-context";
47-
import type { StaticSvelteConfig } from "../svelte-config";
47+
import type { StaticSvelteConfigFile } from "../svelte-config";
4848
import { resolveSvelteConfig } from "../svelte-config";
4949

5050
export {
@@ -124,7 +124,7 @@ export function parseForESLint(code: string, options?: any): ParseResult {
124124
*/
125125
function parseAsSvelte(
126126
code: string,
127-
svelteConfig: StaticSvelteConfig | null,
127+
svelteConfig: StaticSvelteConfigFile | null,
128128
parserOptions: NormalizedParserOptions,
129129
): ParseResult {
130130
const ctx = new Context(code, parserOptions);

src/parser/svelte-parse-context.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type * as Compiler from "svelte/compiler";
22
import type * as SvAST from "./svelte-ast-types";
33
import type { NormalizedParserOptions } from "./parser-options";
44
import { compilerVersion, svelteVersion } from "./svelte-version";
5-
import type { StaticSvelteConfig } from "../svelte-config";
5+
import type { StaticSvelteConfigFile } from "../svelte-config";
66

77
/** The context for parsing. */
88
export type SvelteParseContext = {
@@ -15,24 +15,24 @@ export type SvelteParseContext = {
1515
/** The version of "svelte/compiler". */
1616
compilerVersion: string;
1717
/** The result of static analysis of `svelte.config.js`. */
18-
svelteConfig: StaticSvelteConfig | null;
18+
svelteConfig: StaticSvelteConfigFile | null;
1919
};
2020

2121
export function isEnableRunes(
22-
svelteConfig: StaticSvelteConfig | null,
22+
svelteConfig: StaticSvelteConfigFile | null,
2323
parserOptions: NormalizedParserOptions,
2424
): boolean {
2525
if (!svelteVersion.gte(5)) return false;
2626
if (parserOptions.svelteFeatures?.runes != null) {
2727
return Boolean(parserOptions.svelteFeatures.runes);
28-
} else if (svelteConfig?.compilerOptions?.runes != null) {
29-
return Boolean(svelteConfig.compilerOptions.runes);
28+
} else if (svelteConfig?.config.compilerOptions?.runes != null) {
29+
return Boolean(svelteConfig.config.compilerOptions.runes);
3030
}
3131
return false;
3232
}
3333

3434
export function resolveSvelteParseContextForSvelte(
35-
svelteConfig: StaticSvelteConfig | null,
35+
svelteConfig: StaticSvelteConfigFile | null,
3636
parserOptions: NormalizedParserOptions,
3737
svelteAst: Compiler.Root | SvAST.AstLegacy,
3838
): SvelteParseContext {
@@ -53,14 +53,14 @@ export function resolveSvelteParseContextForSvelte(
5353
}
5454

5555
export function resolveSvelteParseContextForSvelteScript(
56-
svelteConfig: StaticSvelteConfig | null,
56+
svelteConfig: StaticSvelteConfigFile | null,
5757
parserOptions: NormalizedParserOptions,
5858
): SvelteParseContext {
5959
return resolveSvelteParseContext(svelteConfig, parserOptions);
6060
}
6161

6262
function resolveSvelteParseContext(
63-
svelteConfig: StaticSvelteConfig | null,
63+
svelteConfig: StaticSvelteConfigFile | null,
6464
parserOptions: NormalizedParserOptions,
6565
): SvelteParseContext {
6666
return {

src/svelte-config/index.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { parseConfig } from "./parser";
44

55
/** The result of static analysis of `svelte.config.js`. */
66
export type StaticSvelteConfig = {
7-
configFilePath: string;
87
compilerOptions?: {
98
runes?: boolean;
109
};
@@ -14,8 +13,12 @@ export type StaticSvelteConfig = {
1413
};
1514
};
1615
};
16+
export type StaticSvelteConfigFile = {
17+
filePath: string;
18+
config: StaticSvelteConfig;
19+
};
1720

18-
const caches = new Map<string, StaticSvelteConfig | null>();
21+
const caches = new Map<string, StaticSvelteConfigFile | null>();
1922

2023
/**
2124
* Resolves svelte.config.js.
@@ -24,7 +27,7 @@ const caches = new Map<string, StaticSvelteConfig | null>();
2427
*/
2528
export function resolveSvelteConfig(
2629
filePath: string | undefined,
27-
): StaticSvelteConfig | null {
30+
): StaticSvelteConfigFile | null {
2831
const cwd =
2932
filePath && fs.existsSync(filePath)
3033
? path.dirname(filePath)
@@ -33,12 +36,14 @@ export function resolveSvelteConfig(
3336
if (!configFilePath) return null;
3437

3538
if (caches.has(configFilePath)) {
36-
return caches.get(configFilePath) as StaticSvelteConfig | null;
39+
return caches.get(configFilePath) || null;
3740
}
3841

3942
const code = fs.readFileSync(configFilePath, "utf8");
4043
const config = parseConfig(code);
41-
const result = config ? { ...config, configFilePath } : null;
44+
const result: StaticSvelteConfigFile | null = config
45+
? { config, filePath: configFilePath }
46+
: null;
4247
caches.set(configFilePath, result);
4348
return result;
4449
}

src/svelte-config/parser.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { StaticSvelteConfig as StaticSvelteConfigAll } from ".";
1+
import type { StaticSvelteConfig } from ".";
22
import { getEspree } from "../parser/espree";
33
import type {
44
Program,
@@ -11,8 +11,6 @@ import type { ScopeManager } from "eslint-scope";
1111
import { analyze } from "eslint-scope";
1212
import { findVariable } from "../scope";
1313

14-
type StaticSvelteConfig = Omit<StaticSvelteConfigAll, "configFilePath">;
15-
1614
export function parseConfig(code: string): StaticSvelteConfig | null {
1715
const espree = getEspree();
1816
const ast = espree.parse(code, {

0 commit comments

Comments
 (0)