Skip to content

Commit 938a11a

Browse files
committed
refactor(language-core): simplify the mapping merging process in codegen
1 parent b658051 commit 938a11a

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { Code, SfcBlock, SfcBlockAttr, VueCodeInformation } from '../../typ
55

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

1111
export function* wrapWith(
@@ -22,7 +22,7 @@ export function* wrapWith(
2222
}
2323
yield wrapCode;
2424
}
25-
yield ['', 'template', endOffset, { __combineOffsetMapping: offset }];
25+
yield ['', 'template', endOffset, { __combineOffset: offset }];
2626
}
2727

2828
export function collectVars(
@@ -116,6 +116,6 @@ export function* generateSfcBlockAttrValue(
116116
features
117117
];
118118
if (!quotes) {
119-
yield [``, 'main', offset + text.length, { __combineOffsetMapping: 2 }];
119+
yield [``, 'main', offset + text.length, { __combineOffset: 2 }];
120120
}
121121
}

packages/language-core/lib/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ export type RawVueCompilerOptions = Partial<Omit<VueCompilerOptions, 'target' |
1616
};
1717

1818
export interface VueCodeInformation extends CodeInformation {
19-
__combineLastMapping?: boolean;
20-
__combineOffsetMapping?: number;
19+
__combineOffset?: number;
2120
}
2221

2322
export type Code = Segment<VueCodeInformation>;

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -201,29 +201,19 @@ function computedPluginEmbeddedCodes(
201201
];
202202
}));
203203
const newMappings: typeof mappings = [];
204-
let lastValidMapping: typeof mappings[number] | undefined;
205204

206205
for (let i = 0; i < mappings.length; i++) {
207206
const mapping = mappings[i];
208-
if (mapping.data.__combineOffsetMapping !== undefined) {
209-
const offsetMapping = mappings[i - mapping.data.__combineOffsetMapping];
207+
if (mapping.data.__combineOffset !== undefined) {
208+
const offsetMapping = mappings[i - mapping.data.__combineOffset];
210209
if (typeof offsetMapping === 'string' || !offsetMapping) {
211-
throw new Error('Invalid offset mapping, mappings: ' + mappings.length + ', i: ' + i + ', offset: ' + mapping.data.__combineOffsetMapping);
210+
throw new Error('Invalid offset mapping, mappings: ' + mappings.length + ', i: ' + i + ', offset: ' + mapping.data.__combineOffset);
212211
}
213212
offsetMapping.sourceOffsets.push(...mapping.sourceOffsets);
214213
offsetMapping.generatedOffsets.push(...mapping.generatedOffsets);
215214
offsetMapping.lengths.push(...mapping.lengths);
216215
continue;
217216
}
218-
else if (mapping.data.__combineLastMapping) {
219-
lastValidMapping!.sourceOffsets.push(...mapping.sourceOffsets);
220-
lastValidMapping!.generatedOffsets.push(...mapping.generatedOffsets);
221-
lastValidMapping!.lengths.push(...mapping.lengths);
222-
continue;
223-
}
224-
else {
225-
lastValidMapping = mapping;
226-
}
227217
newMappings.push(mapping);
228218
}
229219

0 commit comments

Comments
 (0)