Skip to content

perf(typescript): strip projectService from parser options when needed #704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spicy-toes-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-eslint-parser": patch
---

Strip `projectService` from TS options when type information not needed
7 changes: 2 additions & 5 deletions src/parser/converts/root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getOptionsFromRoot,
} from "../compat.js";
import { sortNodes } from "../sort.js";
import { withoutProjectParserOptions } from "../parser-options.js";

/**
* Convert root
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions src/parser/parser-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Comment on lines +118 to +129
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

17 changes: 7 additions & 10 deletions src/parser/typescript/analyze/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SvelteHTMLElement>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
Loading