Skip to content
  • Sponsor sveltejs/svelte-eslint-parser

  • Notifications You must be signed in to change notification settings
  • Fork 23

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
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
@@ -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
23 changes: 23 additions & 0 deletions src/parser/parser-options.ts
Original file line number Diff line number Diff line change
@@ -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;
}
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
@@ -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>;
@@ -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;


Unchanged files with check annotations Beta

import type {} from "svelte"; // FIXME: Workaround to get type information for "svelte/compiler"

Check warning on line 1 in src/parser/template.ts

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: Workaround to get type...'
import { parse } from "svelte/compiler";
import type * as Compiler from "./svelte-ast-types-for-v5.js";
import type * as SvAST from "./svelte-ast-types.js";
};
// Link declaration and declarations for backward compatibility.
// TODO Remove in v2 and later.

Check warning on line 22 in src/parser/converts/const.ts

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO Remove in v2 and later.'
Object.defineProperty(mustache, "declaration", {
get() {
return mustache.declarations[0];
(key as any).parent = sAttr;
ctx.scriptLet.addObjectShorthandProperty(attribute.key, sAttr, (es) => {
if (
// FIXME: Older parsers may use the same node. In that case, do not replace.

Check warning on line 174 in src/parser/converts/attr.ts

GitHub Actions / lint

Unexpected 'fixme' comment: 'FIXME: Older parsers may use the same...'
// We will drop support for ESLint v7 in the next major version and remove this branch.
es.key !== es.value
) {
/**
* @deprecated Use `declarations` instead.
*/
declaration: ESTree.VariableDeclarator; // TODO Remove in v2 and later.

Check warning on line 269 in src/ast/html.ts

GitHub Actions / lint

Unexpected 'todo' comment: 'TODO Remove in v2 and later.'
declarations: [ESTree.VariableDeclarator];
parent:
| SvelteProgram