Skip to content

Commit 0436da6

Browse files
mcousbaseballyama
andauthored
perf(typescript): strip projectService from parser options when needed (#704)
Co-authored-by: Yuichiro Yamashita <xydybaseball@gmail.com>
1 parent 3f5405b commit 0436da6

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

.changeset/spicy-toes-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte-eslint-parser": patch
3+
---
4+
5+
Strip `projectService` from TS options when type information not needed

src/parser/converts/root.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
getOptionsFromRoot,
2828
} from "../compat.js";
2929
import { sortNodes } from "../sort.js";
30+
import { withoutProjectParserOptions } from "../parser-options.js";
3031

3132
/**
3233
* Convert root
@@ -259,11 +260,7 @@ function convertGenericsAttribute(script: SvelteScriptElement, ctx: Context) {
259260
result = parseScriptWithoutAnalyzeScope(
260261
scriptLet,
261262
ctx.sourceCode.scripts.attrs,
262-
{
263-
...ctx.parserOptions,
264-
// Without typings
265-
project: null,
266-
},
263+
withoutProjectParserOptions(ctx.parserOptions),
267264
) as unknown as TSESParseForESLintResult;
268265
} catch {
269266
// ignore

src/parser/parser-options.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { getParserForLang, type UserOptionParser } from "./resolve-parser.js";
99
export type NormalizedParserOptions = {
1010
parser?: UserOptionParser;
1111
project?: string | string[] | null;
12+
projectService?: unknown;
13+
EXPERIMENTAL_useProjectService?: unknown;
1214

1315
ecmaVersion: number | "latest";
1416
sourceType: "module" | "script";
@@ -104,3 +106,24 @@ export function isTypeScript(
104106

105107
return false;
106108
}
109+
110+
/**
111+
* Remove typing-related options from parser options.
112+
*
113+
* Allows the typescript-eslint parser to parse a file without
114+
* trying to collect typing information from TypeScript.
115+
*
116+
* See https://typescript-eslint.io/packages/parser#withoutprojectparseroptionsparseroptions
117+
*/
118+
export function withoutProjectParserOptions(
119+
options: NormalizedParserOptions,
120+
): NormalizedParserOptions {
121+
const {
122+
project: _strippedProject,
123+
projectService: _strippedProjectService,
124+
EXPERIMENTAL_useProjectService: _strippedExperimentalUseProjectService,
125+
...result
126+
} = options;
127+
128+
return result;
129+
}

src/parser/typescript/analyze/index.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type { NormalizedParserOptions } from "../../parser-options.js";
2323
import { setParent } from "../set-parent.js";
2424
import { getGlobalsForSvelte, globalsForRunes } from "../../globals.js";
2525
import type { SvelteParseContext } from "../../svelte-parse-context.js";
26+
import { withoutProjectParserOptions } from "../../parser-options.js";
2627

2728
export type AnalyzeTypeScriptContext = {
2829
slots: Set<SvelteHTMLElement>;
@@ -53,11 +54,7 @@ export function analyzeTypeScriptInSvelte(
5354
const result = parseScriptWithoutAnalyzeScope(
5455
code.script + code.render + code.rootScope,
5556
attrs,
56-
{
57-
...parserOptions,
58-
// Without typings
59-
project: null,
60-
},
57+
withoutProjectParserOptions(parserOptions),
6158
) as unknown as TSESParseForESLintResult;
6259

6360
ctx._beforeResult = result;
@@ -118,11 +115,11 @@ export function analyzeTypeScript(
118115
const ctx = new VirtualTypeScriptContext(code);
119116
ctx.appendOriginal(/^\s*/u.exec(code)![0].length);
120117

121-
const result = parseScriptWithoutAnalyzeScope(code, attrs, {
122-
...parserOptions,
123-
// Without typings
124-
project: null,
125-
}) as unknown as TSESParseForESLintResult;
118+
const result = parseScriptWithoutAnalyzeScope(
119+
code,
120+
attrs,
121+
withoutProjectParserOptions(parserOptions),
122+
) as unknown as TSESParseForESLintResult;
126123

127124
ctx._beforeResult = result;
128125

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy