@@ -12,21 +12,22 @@ import {
12
12
} from "../../../scope" ;
13
13
import { addElementsToSortedArray , sortedLastIndex } from "../../../utils" ;
14
14
import { parseScriptWithoutAnalyzeScope } from "../../script" ;
15
- import { TypeScriptContext } from "../context" ;
15
+ import { VirtualTypeScriptContext } from "../context" ;
16
16
import type { TSESParseForESLintResult } from "../types" ;
17
17
import type ESTree from "estree" ;
18
18
19
19
const RESERVED_NAMES = new Set < string > ( [ "$$props" , "$$restProps" , "$$slots" ] ) ;
20
20
/**
21
21
* Analyze <script> block script
22
22
* Modify the source code to provide correct type information for svelte special variables and scopes.
23
+ * See https://github.com/ota-meshi/svelte-eslint-parser/blob/main/docs/internal-mechanism.md#scope-types
23
24
*/
24
25
export function analyzeScript (
25
26
code : { script : string ; render : string } ,
26
27
attrs : Record < string , string | undefined > ,
27
28
parserOptions : any
28
- ) : TypeScriptContext {
29
- const ctx = new TypeScriptContext ( code . script + code . render ) ;
29
+ ) : VirtualTypeScriptContext {
30
+ const ctx = new VirtualTypeScriptContext ( code . script + code . render ) ;
30
31
ctx . appendOriginal ( / ^ \s * / u. exec ( code . script ) ! [ 0 ] . length ) ;
31
32
32
33
// We will need to parse TypeScript once to extract the reactive variables.
@@ -54,14 +55,13 @@ export function analyzeScript(
54
55
ctx . appendScript ( `function ${ renderFunctionName } (){` ) ;
55
56
ctx . appendOriginalToEnd ( ) ;
56
57
ctx . appendScript ( `}` ) ;
57
- ctx . restoreContext . addRestoreStatementProcess ( ( node , context ) => {
58
+ ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
58
59
if (
59
60
node . type !== "FunctionDeclaration" ||
60
61
node . id . name !== renderFunctionName
61
62
) {
62
63
return false ;
63
64
}
64
- const result = context . result ;
65
65
const program = result . ast ;
66
66
program . body . splice ( program . body . indexOf ( node ) , 1 , ...node . body . body ) ;
67
67
for ( const body of node . body . body ) {
@@ -81,7 +81,7 @@ export function analyzeScript(
81
81
*/
82
82
function analyzeStoreReferenceNamesAndExtractThroughIdentifiers (
83
83
result : TSESParseForESLintResult ,
84
- ctx : TypeScriptContext
84
+ ctx : VirtualTypeScriptContext
85
85
) {
86
86
const scopeManager = result . scopeManager ;
87
87
const programScope = getProgramScope ( scopeManager as ScopeManager ) ;
@@ -110,14 +110,13 @@ function analyzeStoreReferenceNamesAndExtractThroughIdentifiers(
110
110
: never
111
111
: T;`
112
112
) ;
113
- ctx . restoreContext . addRestoreStatementProcess ( ( node , context ) => {
113
+ ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
114
114
if (
115
115
node . type !== "TSTypeAliasDeclaration" ||
116
116
node . id . name !== storeValueTypeName
117
117
) {
118
118
return false ;
119
119
}
120
- const result = context . result ;
121
120
const program = result . ast ;
122
121
program . body . splice ( program . body . indexOf ( node ) , 1 ) ;
123
122
@@ -135,7 +134,7 @@ function analyzeStoreReferenceNamesAndExtractThroughIdentifiers(
135
134
ctx . appendScript (
136
135
`declare let ${ nm } : ${ storeValueTypeName } <typeof ${ realName } >;`
137
136
) ;
138
- ctx . restoreContext . addRestoreStatementProcess ( ( node , context ) => {
137
+ ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
139
138
if (
140
139
node . type !== "VariableDeclaration" ||
141
140
! node . declare ||
@@ -145,7 +144,6 @@ function analyzeStoreReferenceNamesAndExtractThroughIdentifiers(
145
144
) {
146
145
return false ;
147
146
}
148
- const result = context . result ;
149
147
const program = result . ast ;
150
148
program . body . splice ( program . body . indexOf ( node ) , 1 ) ;
151
149
@@ -171,7 +169,7 @@ function analyzeStoreReferenceNamesAndExtractThroughIdentifiers(
171
169
function analyzeReactiveScopes (
172
170
result : TSESParseForESLintResult ,
173
171
throughIds : ( TSESTree . Identifier | TSESTree . JSXIdentifier ) [ ] ,
174
- ctx : TypeScriptContext
172
+ ctx : VirtualTypeScriptContext
175
173
) {
176
174
for ( const statement of result . ast . body ) {
177
175
if ( statement . type === "LabeledStatement" && statement . label . name === "$" ) {
@@ -214,7 +212,7 @@ function transformForDeclareReactiveVar(
214
212
id : TSESTree . Identifier | TSESTree . ArrayPattern | TSESTree . ObjectPattern ,
215
213
expression : TSESTree . AssignmentExpression ,
216
214
tokens : TSESTree . Token [ ] ,
217
- ctx : TypeScriptContext
215
+ ctx : VirtualTypeScriptContext
218
216
) : void {
219
217
// e.g.
220
218
// From:
@@ -281,7 +279,7 @@ function transformForDeclareReactiveVar(
281
279
ctx . appendScript ( `}` ) ;
282
280
283
281
// eslint-disable-next-line complexity -- ignore X(
284
- ctx . restoreContext . addRestoreStatementProcess ( ( node , context ) => {
282
+ ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
285
283
if ( ( node as any ) . type !== "SvelteReactiveStatement" ) {
286
284
return false ;
287
285
}
@@ -303,7 +301,6 @@ function transformForDeclareReactiveVar(
303
301
) {
304
302
return false ;
305
303
}
306
- const result = context . result ;
307
304
const program = result . ast ;
308
305
const nextIndex = program . body . indexOf ( node ) + 1 ;
309
306
const fnDecl = program . body [ nextIndex ] ;
@@ -375,7 +372,7 @@ function transformForDeclareReactiveVar(
375
372
*/
376
373
function transformForReactiveStatement (
377
374
statement : TSESTree . LabeledStatement ,
378
- ctx : TypeScriptContext
375
+ ctx : VirtualTypeScriptContext
379
376
) {
380
377
const functionId = ctx . generateUniqueId ( "reactiveStatementScopeFunction" ) ;
381
378
const originalBody = statement . body ;
@@ -390,7 +387,7 @@ function transformForReactiveStatement(
390
387
}
391
388
ctx . appendOriginal ( statement . range [ 1 ] ) ;
392
389
393
- ctx . restoreContext . addRestoreStatementProcess ( ( node , context ) => {
390
+ ctx . restoreContext . addRestoreStatementProcess ( ( node , result ) => {
394
391
if ( ( node as any ) . type !== "SvelteReactiveStatement" ) {
395
392
return false ;
396
393
}
@@ -406,7 +403,6 @@ function transformForReactiveStatement(
406
403
}
407
404
reactiveStatement . body . parent = reactiveStatement ;
408
405
409
- const result = context . result ;
410
406
const scopeManager = result . scopeManager as ScopeManager ;
411
407
removeFunctionScope ( body , scopeManager ) ;
412
408
return true ;
0 commit comments