Skip to content

Commit a4b2885

Browse files
committed
chore: refactor
1 parent 17855d3 commit a4b2885

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

src/parser/typescript/analyze/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function analyzeStoreReferenceNames(
7878

7979
if (maybeStoreRefNames.size) {
8080
const storeValueTypeName = ctx.generateUniqueId("StoreValueType");
81-
ctx.appendScript(
81+
ctx.appendVirtualScript(
8282
`type ${storeValueTypeName}<T> = T extends null | undefined
8383
? T
8484
: T extends object & { subscribe(run: infer F, ...args: any): any }
@@ -108,7 +108,7 @@ function analyzeStoreReferenceNames(
108108

109109
for (const nm of maybeStoreRefNames) {
110110
const realName = nm.slice(1);
111-
ctx.appendScript(
111+
ctx.appendVirtualScript(
112112
`declare let ${nm}: ${storeValueTypeName}<typeof ${realName}>;`
113113
);
114114
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
@@ -193,9 +193,9 @@ function analyzeRenderScopes(
193193
) {
194194
ctx.appendOriginal(code.script.length);
195195
const renderFunctionName = ctx.generateUniqueId("render");
196-
ctx.appendScript(`function ${renderFunctionName}(){`);
196+
ctx.appendVirtualScript(`function ${renderFunctionName}(){`);
197197
ctx.appendOriginalToEnd();
198-
ctx.appendScript(`}`);
198+
ctx.appendVirtualScript(`}`);
199199
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
200200
if (
201201
node.type !== "FunctionDeclaration" ||
@@ -303,15 +303,15 @@ function transformForDeclareReactiveVar(
303303
}
304304
ctx.appendOriginal(expression.range[0]);
305305
ctx.skipUntilOriginalOffset(id.range[0]);
306-
ctx.appendScript("let ");
306+
ctx.appendVirtualScript("let ");
307307
ctx.appendOriginal(eq ? eq.range[1] : expression.right.range[0]);
308-
ctx.appendScript(`${functionId}();\nfunction ${functionId}(){return `);
308+
ctx.appendVirtualScript(`${functionId}();\nfunction ${functionId}(){return `);
309309
for (const token of closeParens) {
310310
ctx.appendOriginal(token.range[0]);
311311
ctx.skipOriginalOffset(token.range[1] - token.range[0]);
312312
}
313313
ctx.appendOriginal(statement.range[1]);
314-
ctx.appendScript(`}`);
314+
ctx.appendVirtualScript(`}`);
315315

316316
// eslint-disable-next-line complexity -- ignore X(
317317
ctx.restoreContext.addRestoreStatementProcess((node, result) => {
@@ -419,13 +419,13 @@ function transformForReactiveStatement(
419419
const functionId = ctx.generateUniqueId("reactiveStatementScopeFunction");
420420
const originalBody = statement.body;
421421
ctx.appendOriginal(originalBody.range[0]);
422-
ctx.appendScript(`function ${functionId}()`);
422+
ctx.appendVirtualScript(`function ${functionId}()`);
423423
if (originalBody.type !== "BlockStatement") {
424-
ctx.appendScript(`{`);
424+
ctx.appendVirtualScript(`{`);
425425
}
426426
ctx.appendOriginal(originalBody.range[1]);
427427
if (originalBody.type !== "BlockStatement") {
428-
ctx.appendScript(`}`);
428+
ctx.appendVirtualScript(`}`);
429429
}
430430
ctx.appendOriginal(statement.range[1]);
431431

src/parser/typescript/context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ export class VirtualTypeScriptContext {
4848
this.consumedIndex = index;
4949
}
5050

51-
public appendScript(fragment: string): void {
51+
public appendVirtualScript(virtualFragment: string): void {
5252
const start = this.script.length;
53-
this.script += fragment;
54-
this.restoreContext.fragmentRange(start, this.script.length);
53+
this.script += virtualFragment;
54+
this.restoreContext.addVirtualFragmentRange(start, this.script.length);
5555
}
5656

5757
public generateUniqueId(base: string): string {

src/parser/typescript/restore.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class RestoreContext {
2323

2424
private readonly offsets: { original: number; dist: number }[] = [];
2525

26-
private readonly fragments: { start: number; end: number }[] = [];
26+
private readonly virtualFragments: { start: number; end: number }[] = [];
2727

2828
private readonly restoreStatementProcesses: RestoreStatementProcess[] = [];
2929

@@ -39,13 +39,13 @@ export class RestoreContext {
3939
this.offsets.push(offset);
4040
}
4141

42-
public fragmentRange(start: number, end: number): void {
43-
const peek = this.fragments[this.fragments.length - 1];
42+
public addVirtualFragmentRange(start: number, end: number): void {
43+
const peek = this.virtualFragments[this.virtualFragments.length - 1];
4444
if (peek && peek.end === start) {
4545
peek.end = end;
4646
return;
4747
}
48-
this.fragments.push({ start, end });
48+
this.virtualFragments.push({ start, end });
4949
}
5050

5151
/**
@@ -55,7 +55,7 @@ export class RestoreContext {
5555
remapLocations(result, {
5656
remapLocation: (n) => this.remapLocation(n),
5757
removeToken: (token) =>
58-
this.fragments.some(
58+
this.virtualFragments.some(
5959
(f) => f.start <= token.range[0] && token.range[1] <= f.end
6060
),
6161
});
@@ -76,13 +76,13 @@ export class RestoreContext {
7676

7777
private remapLocation(node: TSESTree.Node | TSESTree.Token): void {
7878
let [start, end] = node.range;
79-
const startFragment = this.fragments.find(
79+
const startFragment = this.virtualFragments.find(
8080
(f) => f.start <= start && start < f.end
8181
);
8282
if (startFragment) {
8383
start = startFragment.end;
8484
}
85-
const endFragment = this.fragments.find(
85+
const endFragment = this.virtualFragments.find(
8686
(f) => f.start < end && end <= f.end
8787
);
8888
if (endFragment) {

tests/fixtures/parser/ast/ts-issue226-output.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,7 @@
13021302
},
13031303
"range": [
13041304
235,
1305-
298
1305+
299
13061306
],
13071307
"loc": {
13081308
"start": {
@@ -1311,7 +1311,7 @@
13111311
},
13121312
"end": {
13131313
"line": 11,
1314-
"column": 66
1314+
"column": 67
13151315
}
13161316
}
13171317
},

tests/fixtures/parser/ast/ts-issue226-scope-output.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10472,7 +10472,7 @@
1047210472
},
1047310473
"range": [
1047410474
235,
10475-
298
10475+
299
1047610476
],
1047710477
"loc": {
1047810478
"start": {
@@ -10481,7 +10481,7 @@
1048110481
},
1048210482
"end": {
1048310483
"line": 11,
10484-
"column": 66
10484+
"column": 67
1048510485
}
1048610486
}
1048710487
}

0 commit comments

Comments
 (0)