Skip to content

Double-parsing occurs of svelte scripts #768

@43081j

Description

@43081j

here we analyse the typescript and parse the script:

const tsCtx = analyzeTypeScriptInSvelte(code, attrs, parserOptions, context);
const result = parseScriptInSvelte(tsCtx.script, attrs, parserOptions);

this basically:

  • parses the script + render + rootScope in order to analyse it, and tries to avoid type checking by turning off projects in TSESLint
  • parses the script as the main AST, with type checking enabled

The first results in this code path:

const result = parseScriptWithoutAnalyzeScope(
code.script + code.render + code.rootScope,
attrs,
withoutProjectParserOptions(parserOptions),
) as unknown as TSESParseForESLintResult;

The second results in this code path:

const result = parseScriptWithoutAnalyzeScope(code, attrs, options);
result._virtualScriptCode = code;
return result;

This means we ultimately end up parsing the script twice, though the first one is parsing it as part of a slightly larger source.

The TSESLint parser basically parses and type checks the same code twice for every file because of this.

Suggestion 1

We could probably just parse the larger of the two: script + render + rootScope.

Whatever expects just the script further down the line would need to be updated to account for the fact that it will now get a larger AST (extra stuff on the end).

Suggestion 2

Alternatively, we could parse script and share it between these two calls. Then we'd have to update the analysis function to parse render + rootScope as its own separate AST.

But if there's any logic that depends on having the full AST (all 3 parts), this won't really work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions