Skip to content

Commit

Permalink
refactor(language-core): defer the calculation of `linkedCodeMappings…
Browse files Browse the repository at this point in the history
…` offsets (#5220)
  • Loading branch information
KazariEX authored Feb 28, 2025
1 parent 61d737a commit f2088e2
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 34 deletions.
7 changes: 1 addition & 6 deletions packages/language-core/lib/codegen/globalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ export function getGlobalTypesFileName({
checkUnknownProps,
checkUnknownEvents,
checkUnknownComponents,
].map(v => {
if (typeof v === 'boolean') {
return v ? 1 : 0;
}
return v;
}).join('_') + '.d.ts';
].map(v => (typeof v === 'boolean' ? Number(v) : v)).join('_') + '.d.ts';
}

export function generateGlobalTypes({
Expand Down
14 changes: 4 additions & 10 deletions packages/language-core/lib/codegen/script/componentSelf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ export function* generateComponentSelf(
if (!templateUsageVars.has(varName) && !templateCodegenCtx.accessExternalVariables.has(varName)) {
continue;
}
const templateOffset = options.getGeneratedLength();
yield `${varName}: ${varName} as typeof `;

const scriptOffset = options.getGeneratedLength();
const token = Symbol(varName.length);
yield ['', undefined, 0, { __linkedToken: token }];
yield `${varName}: ${varName} as typeof `;
yield ['', undefined, 0, { __linkedToken: token }];
yield `${varName},${newLine}`;

options.linkedCodeMappings.push({
sourceOffsets: [scriptOffset],
generatedOffsets: [templateOffset],
lengths: [varName.length],
data: undefined,
});
}
}
yield `}${endOfLine}`; // return {
Expand Down
1 change: 0 additions & 1 deletion packages/language-core/lib/codegen/script/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export function createScriptCodegenContext(options: ScriptCodegenOptions) {
return {
generatedTemplate: false,
generatedPropsType: false,
scriptSetupGeneratedOffset: undefined as number | undefined,
bypassDefineComponent: options.lang === 'js' || options.lang === 'jsx',
bindingNames: new Set([
...options.scriptRanges?.bindings.map(
Expand Down
3 changes: 0 additions & 3 deletions packages/language-core/lib/codegen/script/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Mapping } from '@volar/language-core';
import * as path from 'path-browserify';
import type * as ts from 'typescript';
import type { ScriptRanges } from '../../parsers/scriptRanges';
Expand Down Expand Up @@ -27,8 +26,6 @@ export interface ScriptCodegenOptions {
templateCodegen: TemplateCodegenContext & { codes: Code[]; } | undefined;
destructuredPropNames: Set<string>;
templateRefNames: Set<string>;
getGeneratedLength: () => number;
linkedCodeMappings: Mapping[];
appendGlobalTypes: boolean;
}

Expand Down
2 changes: 0 additions & 2 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ function* generateSetupFunction(
scriptSetupRanges: ScriptSetupRanges,
syntax: 'return' | 'export default' | undefined
): Generator<Code> {
ctx.scriptSetupGeneratedOffset = options.getGeneratedLength() - scriptSetupRanges.importSectionEndOffset;

let setupCodeModifies: [Code[], number, number][] = [];
for (const { comments } of scriptSetupRanges.defineProp) {
if (comments) {
Expand Down
11 changes: 0 additions & 11 deletions packages/language-core/lib/plugins/vue-tsx.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { Mapping } from '@volar/language-core';
import { camelize, capitalize } from '@vue/shared';
import { computed } from 'alien-signals';
import * as path from 'path-browserify';
Expand Down Expand Up @@ -46,7 +45,6 @@ const plugin: VueLanguagePlugin = ctx => {
const tsx = codegen.getGeneratedScript();
if (tsx) {
embeddedFile.content = [...tsx.codes];
embeddedFile.linkedCodeMappings = [...tsx.linkedCodeMappings];
}
}
},
Expand Down Expand Up @@ -221,9 +219,6 @@ function createTsx(
});

const getGeneratedScript = computed(() => {
const linkedCodeMappings: Mapping[] = [];
let generatedLength = 0;

const codes: Code[] = [];
const codegen = generateScript({
ts,
Expand All @@ -238,8 +233,6 @@ function createTsx(
templateCodegen: getGeneratedTemplate(),
destructuredPropNames: getSetupDestructuredPropNames(),
templateRefNames: getSetupTemplateRefNames(),
getGeneratedLength: () => generatedLength,
linkedCodeMappings,
appendGlobalTypes,
});
fileEditTimes.set(fileName, (fileEditTimes.get(fileName) ?? 0) + 1);
Expand All @@ -248,16 +241,12 @@ function createTsx(
while (!current.done) {
const code = current.value;
codes.push(code);
generatedLength += typeof code === 'string'
? code.length
: code[0].length;
current = codegen.next();
}

return {
...current.value,
codes,
linkedCodeMappings,
};
});

Expand Down
1 change: 1 addition & 0 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type RawVueCompilerOptions = Partial<Omit<VueCompilerOptions, 'target' |

export interface VueCodeInformation extends CodeInformation {
__combineOffset?: number;
__linkedToken?: symbol;
}

export type Code = Segment<VueCodeInformation>;
Expand Down
19 changes: 18 additions & 1 deletion packages/language-core/lib/virtualFile/computedEmbeddedCodes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VirtualCode } from '@volar/language-core';
import type { Mapping, VirtualCode } from '@volar/language-core';
import { computed } from 'alien-signals';
import { toString } from 'muggle-string';
import type * as ts from 'typescript';
Expand Down Expand Up @@ -201,6 +201,7 @@ function computedPluginEmbeddedCodes(
];
}));
const newMappings: typeof mappings = [];
const tokenMappings = new Map<symbol, Mapping>();

for (let i = 0; i < mappings.length; i++) {
const mapping = mappings[i];
Expand All @@ -214,6 +215,22 @@ function computedPluginEmbeddedCodes(
offsetMapping.lengths.push(...mapping.lengths);
continue;
}
if (mapping.data.__linkedToken !== undefined) {
const token = mapping.data.__linkedToken;
if (tokenMappings.has(token)) {
const prevMapping = tokenMappings.get(token)!;
code.linkedCodeMappings.push({
sourceOffsets: [prevMapping.generatedOffsets[0]],
generatedOffsets: [mapping.generatedOffsets[0]],
lengths: [Number(token.description)],
data: undefined,
});
}
else {
tokenMappings.set(token, mapping);
}
continue;
}
newMappings.push(mapping);
}

Expand Down

0 comments on commit f2088e2

Please sign in to comment.