From 7f4d62e886a8a100942963cef711e584ceba3031 Mon Sep 17 00:00:00 2001 From: Michael Cousins Date: Thu, 17 Apr 2025 11:08:19 -0400 Subject: [PATCH] perf(typescript): strip `projectService` from parser options when needed --- .changeset/spicy-toes-raise.md | 5 +++++ src/parser/converts/root.ts | 7 ++----- src/parser/parser-options.ts | 23 +++++++++++++++++++++++ src/parser/typescript/analyze/index.ts | 17 +++++++---------- 4 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 .changeset/spicy-toes-raise.md diff --git a/.changeset/spicy-toes-raise.md b/.changeset/spicy-toes-raise.md new file mode 100644 index 00000000..533807fa --- /dev/null +++ b/.changeset/spicy-toes-raise.md @@ -0,0 +1,5 @@ +--- +"svelte-eslint-parser": patch +--- + +Strip `projectService` from TS options when type information not needed diff --git a/src/parser/converts/root.ts b/src/parser/converts/root.ts index 5e5e8de3..81614868 100644 --- a/src/parser/converts/root.ts +++ b/src/parser/converts/root.ts @@ -27,6 +27,7 @@ import { getOptionsFromRoot, } from "../compat.js"; import { sortNodes } from "../sort.js"; +import { withoutProjectParserOptions } from "../parser-options.js"; /** * Convert root @@ -259,11 +260,7 @@ function convertGenericsAttribute(script: SvelteScriptElement, ctx: Context) { result = parseScriptWithoutAnalyzeScope( scriptLet, ctx.sourceCode.scripts.attrs, - { - ...ctx.parserOptions, - // Without typings - project: null, - }, + withoutProjectParserOptions(ctx.parserOptions), ) as unknown as TSESParseForESLintResult; } catch { // ignore diff --git a/src/parser/parser-options.ts b/src/parser/parser-options.ts index fd338485..44d43af1 100644 --- a/src/parser/parser-options.ts +++ b/src/parser/parser-options.ts @@ -9,6 +9,8 @@ import { getParserForLang, type UserOptionParser } from "./resolve-parser.js"; export type NormalizedParserOptions = { parser?: UserOptionParser; project?: string | string[] | null; + projectService?: unknown; + EXPERIMENTAL_useProjectService?: unknown; ecmaVersion: number | "latest"; sourceType: "module" | "script"; @@ -104,3 +106,24 @@ export function isTypeScript( return false; } + +/** + * Remove typing-related options from parser options. + * + * Allows the typescript-eslint parser to parse a file without + * trying to collect typing information from TypeScript. + * + * See https://typescript-eslint.io/packages/parser#withoutprojectparseroptionsparseroptions + */ +export function withoutProjectParserOptions( + options: NormalizedParserOptions, +): NormalizedParserOptions { + const { + project: _strippedProject, + projectService: _strippedProjectService, + EXPERIMENTAL_useProjectService: _strippedExperimentalUseProjectService, + ...result + } = options; + + return result; +} diff --git a/src/parser/typescript/analyze/index.ts b/src/parser/typescript/analyze/index.ts index 9d784e7a..cea34041 100644 --- a/src/parser/typescript/analyze/index.ts +++ b/src/parser/typescript/analyze/index.ts @@ -23,6 +23,7 @@ import type { NormalizedParserOptions } from "../../parser-options.js"; import { setParent } from "../set-parent.js"; import { getGlobalsForSvelte, globalsForRunes } from "../../globals.js"; import type { SvelteParseContext } from "../../svelte-parse-context.js"; +import { withoutProjectParserOptions } from "../../parser-options.js"; export type AnalyzeTypeScriptContext = { slots: Set; @@ -53,11 +54,7 @@ export function analyzeTypeScriptInSvelte( const result = parseScriptWithoutAnalyzeScope( code.script + code.render + code.rootScope, attrs, - { - ...parserOptions, - // Without typings - project: null, - }, + withoutProjectParserOptions(parserOptions), ) as unknown as TSESParseForESLintResult; ctx._beforeResult = result; @@ -118,11 +115,11 @@ export function analyzeTypeScript( const ctx = new VirtualTypeScriptContext(code); ctx.appendOriginal(/^\s*/u.exec(code)![0].length); - const result = parseScriptWithoutAnalyzeScope(code, attrs, { - ...parserOptions, - // Without typings - project: null, - }) as unknown as TSESParseForESLintResult; + const result = parseScriptWithoutAnalyzeScope( + code, + attrs, + withoutProjectParserOptions(parserOptions), + ) as unknown as TSESParseForESLintResult; ctx._beforeResult = result;