Skip to content

Commit f2088e2

Browse files
authored
refactor(language-core): defer the calculation of linkedCodeMappings offsets (#5220)
1 parent 61d737a commit f2088e2

File tree

8 files changed

+24
-34
lines changed

8 files changed

+24
-34
lines changed

packages/language-core/lib/codegen/globalTypes.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ export function getGlobalTypesFileName({
1414
checkUnknownProps,
1515
checkUnknownEvents,
1616
checkUnknownComponents,
17-
].map(v => {
18-
if (typeof v === 'boolean') {
19-
return v ? 1 : 0;
20-
}
21-
return v;
22-
}).join('_') + '.d.ts';
17+
].map(v => (typeof v === 'boolean' ? Number(v) : v)).join('_') + '.d.ts';
2318
}
2419

2520
export function generateGlobalTypes({

packages/language-core/lib/codegen/script/componentSelf.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,12 @@ export function* generateComponentSelf(
3333
if (!templateUsageVars.has(varName) && !templateCodegenCtx.accessExternalVariables.has(varName)) {
3434
continue;
3535
}
36-
const templateOffset = options.getGeneratedLength();
37-
yield `${varName}: ${varName} as typeof `;
3836

39-
const scriptOffset = options.getGeneratedLength();
37+
const token = Symbol(varName.length);
38+
yield ['', undefined, 0, { __linkedToken: token }];
39+
yield `${varName}: ${varName} as typeof `;
40+
yield ['', undefined, 0, { __linkedToken: token }];
4041
yield `${varName},${newLine}`;
41-
42-
options.linkedCodeMappings.push({
43-
sourceOffsets: [scriptOffset],
44-
generatedOffsets: [templateOffset],
45-
lengths: [varName.length],
46-
data: undefined,
47-
});
4842
}
4943
}
5044
yield `}${endOfLine}`; // return {

packages/language-core/lib/codegen/script/context.ts

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export function createScriptCodegenContext(options: ScriptCodegenOptions) {
1818
return {
1919
generatedTemplate: false,
2020
generatedPropsType: false,
21-
scriptSetupGeneratedOffset: undefined as number | undefined,
2221
bypassDefineComponent: options.lang === 'js' || options.lang === 'jsx',
2322
bindingNames: new Set([
2423
...options.scriptRanges?.bindings.map(

packages/language-core/lib/codegen/script/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Mapping } from '@volar/language-core';
21
import * as path from 'path-browserify';
32
import type * as ts from 'typescript';
43
import type { ScriptRanges } from '../../parsers/scriptRanges';
@@ -27,8 +26,6 @@ export interface ScriptCodegenOptions {
2726
templateCodegen: TemplateCodegenContext & { codes: Code[]; } | undefined;
2827
destructuredPropNames: Set<string>;
2928
templateRefNames: Set<string>;
30-
getGeneratedLength: () => number;
31-
linkedCodeMappings: Mapping[];
3229
appendGlobalTypes: boolean;
3330
}
3431

packages/language-core/lib/codegen/script/scriptSetup.ts

-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ function* generateSetupFunction(
9696
scriptSetupRanges: ScriptSetupRanges,
9797
syntax: 'return' | 'export default' | undefined
9898
): Generator<Code> {
99-
ctx.scriptSetupGeneratedOffset = options.getGeneratedLength() - scriptSetupRanges.importSectionEndOffset;
100-
10199
let setupCodeModifies: [Code[], number, number][] = [];
102100
for (const { comments } of scriptSetupRanges.defineProp) {
103101
if (comments) {

packages/language-core/lib/plugins/vue-tsx.ts

-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Mapping } from '@volar/language-core';
21
import { camelize, capitalize } from '@vue/shared';
32
import { computed } from 'alien-signals';
43
import * as path from 'path-browserify';
@@ -46,7 +45,6 @@ const plugin: VueLanguagePlugin = ctx => {
4645
const tsx = codegen.getGeneratedScript();
4746
if (tsx) {
4847
embeddedFile.content = [...tsx.codes];
49-
embeddedFile.linkedCodeMappings = [...tsx.linkedCodeMappings];
5048
}
5149
}
5250
},
@@ -221,9 +219,6 @@ function createTsx(
221219
});
222220

223221
const getGeneratedScript = computed(() => {
224-
const linkedCodeMappings: Mapping[] = [];
225-
let generatedLength = 0;
226-
227222
const codes: Code[] = [];
228223
const codegen = generateScript({
229224
ts,
@@ -238,8 +233,6 @@ function createTsx(
238233
templateCodegen: getGeneratedTemplate(),
239234
destructuredPropNames: getSetupDestructuredPropNames(),
240235
templateRefNames: getSetupTemplateRefNames(),
241-
getGeneratedLength: () => generatedLength,
242-
linkedCodeMappings,
243236
appendGlobalTypes,
244237
});
245238
fileEditTimes.set(fileName, (fileEditTimes.get(fileName) ?? 0) + 1);
@@ -248,16 +241,12 @@ function createTsx(
248241
while (!current.done) {
249242
const code = current.value;
250243
codes.push(code);
251-
generatedLength += typeof code === 'string'
252-
? code.length
253-
: code[0].length;
254244
current = codegen.next();
255245
}
256246

257247
return {
258248
...current.value,
259249
codes,
260-
linkedCodeMappings,
261250
};
262251
});
263252

packages/language-core/lib/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type RawVueCompilerOptions = Partial<Omit<VueCompilerOptions, 'target' |
1717

1818
export interface VueCodeInformation extends CodeInformation {
1919
__combineOffset?: number;
20+
__linkedToken?: symbol;
2021
}
2122

2223
export type Code = Segment<VueCodeInformation>;

packages/language-core/lib/virtualFile/computedEmbeddedCodes.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { VirtualCode } from '@volar/language-core';
1+
import type { Mapping, VirtualCode } from '@volar/language-core';
22
import { computed } from 'alien-signals';
33
import { toString } from 'muggle-string';
44
import type * as ts from 'typescript';
@@ -201,6 +201,7 @@ function computedPluginEmbeddedCodes(
201201
];
202202
}));
203203
const newMappings: typeof mappings = [];
204+
const tokenMappings = new Map<symbol, Mapping>();
204205

205206
for (let i = 0; i < mappings.length; i++) {
206207
const mapping = mappings[i];
@@ -214,6 +215,22 @@ function computedPluginEmbeddedCodes(
214215
offsetMapping.lengths.push(...mapping.lengths);
215216
continue;
216217
}
218+
if (mapping.data.__linkedToken !== undefined) {
219+
const token = mapping.data.__linkedToken;
220+
if (tokenMappings.has(token)) {
221+
const prevMapping = tokenMappings.get(token)!;
222+
code.linkedCodeMappings.push({
223+
sourceOffsets: [prevMapping.generatedOffsets[0]],
224+
generatedOffsets: [mapping.generatedOffsets[0]],
225+
lengths: [Number(token.description)],
226+
data: undefined,
227+
});
228+
}
229+
else {
230+
tokenMappings.set(token, mapping);
231+
}
232+
continue;
233+
}
217234
newMappings.push(mapping);
218235
}
219236

0 commit comments

Comments
 (0)