Skip to content

Commit

Permalink
refactor(language-core): simplify the mapping merging process in codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 16, 2025
1 parent b658051 commit 938a11a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 18 deletions.
6 changes: 3 additions & 3 deletions packages/language-core/lib/codegen/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Code, SfcBlock, SfcBlockAttr, VueCodeInformation } from '../../typ

export const newLine = `\n`;
export const endOfLine = `;${newLine}`;
export const combineLastMapping: VueCodeInformation = { __combineLastMapping: true };
export const combineLastMapping: VueCodeInformation = { __combineOffset: 1 };
export const variableNameRegex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;

export function* wrapWith(
Expand All @@ -22,7 +22,7 @@ export function* wrapWith(
}
yield wrapCode;
}
yield ['', 'template', endOffset, { __combineOffsetMapping: offset }];
yield ['', 'template', endOffset, { __combineOffset: offset }];
}

export function collectVars(
Expand Down Expand Up @@ -116,6 +116,6 @@ export function* generateSfcBlockAttrValue(
features
];
if (!quotes) {
yield [``, 'main', offset + text.length, { __combineOffsetMapping: 2 }];
yield [``, 'main', offset + text.length, { __combineOffset: 2 }];
}
}
3 changes: 1 addition & 2 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export type RawVueCompilerOptions = Partial<Omit<VueCompilerOptions, 'target' |
};

export interface VueCodeInformation extends CodeInformation {
__combineLastMapping?: boolean;
__combineOffsetMapping?: number;
__combineOffset?: number;
}

export type Code = Segment<VueCodeInformation>;
Expand Down
16 changes: 3 additions & 13 deletions packages/language-core/lib/virtualFile/computedEmbeddedCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,19 @@ function computedPluginEmbeddedCodes(
];
}));
const newMappings: typeof mappings = [];
let lastValidMapping: typeof mappings[number] | undefined;

for (let i = 0; i < mappings.length; i++) {
const mapping = mappings[i];
if (mapping.data.__combineOffsetMapping !== undefined) {
const offsetMapping = mappings[i - mapping.data.__combineOffsetMapping];
if (mapping.data.__combineOffset !== undefined) {
const offsetMapping = mappings[i - mapping.data.__combineOffset];
if (typeof offsetMapping === 'string' || !offsetMapping) {
throw new Error('Invalid offset mapping, mappings: ' + mappings.length + ', i: ' + i + ', offset: ' + mapping.data.__combineOffsetMapping);
throw new Error('Invalid offset mapping, mappings: ' + mappings.length + ', i: ' + i + ', offset: ' + mapping.data.__combineOffset);
}
offsetMapping.sourceOffsets.push(...mapping.sourceOffsets);
offsetMapping.generatedOffsets.push(...mapping.generatedOffsets);
offsetMapping.lengths.push(...mapping.lengths);
continue;
}
else if (mapping.data.__combineLastMapping) {
lastValidMapping!.sourceOffsets.push(...mapping.sourceOffsets);
lastValidMapping!.generatedOffsets.push(...mapping.generatedOffsets);
lastValidMapping!.lengths.push(...mapping.lengths);
continue;
}
else {
lastValidMapping = mapping;
}
newMappings.push(mapping);
}

Expand Down

0 comments on commit 938a11a

Please sign in to comment.