Skip to content

Commit 32faaad

Browse files
committed
refactor(language-core): overload wrapWith to use in more sources
1 parent 4669a54 commit 32faaad

18 files changed

+105
-75
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { camelize } from '@vue/shared';
22
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
33
import type { Code, Sfc, TextRange } from '../../types';
44
import { codeFeatures } from '../codeFeatures';
5-
import { combineLastMapping, endOfLine, generateSfcBlockSection, newLine } from '../utils';
5+
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
66
import { generateCamelized } from '../utils/camelized';
7+
import { wrapWith } from '../utils/wrapWith';
78
import { generateComponent, generateEmitsOption } from './component';
89
import { generateComponentSelf } from './componentSelf';
910
import type { ScriptCodegenContext } from './context';
@@ -215,9 +216,13 @@ function* generateSetupFunction(
215216
`])`
216217
] : [
217218
` as __VLS_StyleModules[`,
218-
['', scriptSetup.name, exp.start, codeFeatures.verification],
219-
`'$style'`,
220-
['', scriptSetup.name, exp.end, combineLastMapping],
219+
...wrapWith(
220+
exp.start,
221+
exp.end,
222+
scriptSetup.name,
223+
codeFeatures.verification,
224+
`'$style'`
225+
),
221226
`])`
222227
],
223228
callExp.end,
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Code } from '../../types';
22
import { codeFeatures } from '../codeFeatures';
3-
import { newLine } from '../utils';
3+
import { combineLastMapping, newLine } from '../utils';
4+
import { wrapWith } from '../utils/wrapWith';
45

56
export function* generateClassProperty(
67
styleIndex: number,
@@ -9,26 +10,20 @@ export function* generateClassProperty(
910
propertyType: string
1011
): Generator<Code> {
1112
yield `${newLine} & { `;
12-
yield [
13-
'',
14-
'style_' + styleIndex,
13+
yield* wrapWith(
1514
offset,
16-
codeFeatures.navigation,
17-
];
18-
yield `'`;
19-
yield [
20-
classNameWithDot.slice(1),
21-
'style_' + styleIndex,
22-
offset + 1,
23-
codeFeatures.navigation,
24-
];
25-
yield `'`;
26-
yield [
27-
'',
28-
'style_' + styleIndex,
2915
offset + classNameWithDot.length,
16+
'style_' + styleIndex,
3017
codeFeatures.navigation,
31-
];
18+
`'`,
19+
[
20+
classNameWithDot.slice(1),
21+
'style_' + styleIndex,
22+
offset + 1,
23+
combineLastMapping
24+
],
25+
`'`
26+
);
3227
yield `: ${propertyType}`;
3328
yield ` }`;
3429
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import type * as CompilerDOM from '@vue/compiler-dom';
22
import type { Code, VueCodeInformation } from '../../types';
33
import { codeFeatures } from '../codeFeatures';
44
import { InlayHintInfo } from '../inlayHints';
5-
import { endOfLine, newLine, wrapWith } from '../utils';
5+
import { endOfLine, newLine } from '../utils';
6+
import { wrapWith } from '../utils/wrapWith';
67
import type { TemplateCodegenOptions } from './index';
78

89
export type TemplateCodegenContext = ReturnType<typeof createTemplateCodegenContext>;

packages/language-core/lib/codegen/template/element.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { camelize, capitalize } from '@vue/shared';
33
import type { Code, VueCodeInformation } from '../../types';
44
import { getSlotsPropertyName, hyphenateTag } from '../../utils/shared';
55
import { createVBindShorthandInlayHintInfo } from '../inlayHints';
6-
import { endOfLine, identifierRegex, newLine, normalizeAttributeValue, wrapWith } from '../utils';
6+
import { endOfLine, identifierRegex, newLine, normalizeAttributeValue } from '../utils';
77
import { generateCamelized } from '../utils/camelized';
8+
import { wrapWith } from '../utils/wrapWith';
89
import type { TemplateCodegenContext } from './context';
910
import { generateElementChildren } from './elementChildren';
1011
import { generateElementDirectives } from './elementDirectives';

packages/language-core/lib/codegen/template/elementDirectives.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { camelize } from '@vue/shared';
33
import type { Code } from '../../types';
44
import { hyphenateAttr } from '../../utils/shared';
55
import { codeFeatures } from '../codeFeatures';
6-
import { endOfLine, wrapWith } from '../utils';
6+
import { endOfLine } from '../utils';
77
import { generateCamelized } from '../utils/camelized';
88
import { generateStringLiteralKey } from '../utils/stringLiteralKey';
9+
import { wrapWith } from '../utils/wrapWith';
910
import type { TemplateCodegenContext } from './context';
1011
import { generatePropExp } from './elementProps';
1112
import type { TemplateCodegenOptions } from './index';

packages/language-core/lib/codegen/template/elementEvents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import * as CompilerDOM from '@vue/compiler-dom';
22
import { camelize, capitalize } from '@vue/shared';
33
import type * as ts from 'typescript';
44
import type { Code } from '../../types';
5-
import { combineLastMapping, createTsAst, endOfLine, identifierRegex, newLine, wrapWith } from '../utils';
5+
import { combineLastMapping, createTsAst, endOfLine, identifierRegex, newLine } from '../utils';
66
import { generateCamelized } from '../utils/camelized';
7+
import { wrapWith } from '../utils/wrapWith';
78
import type { TemplateCodegenContext } from './context';
89
import type { TemplateCodegenOptions } from './index';
910
import { generateInterpolation } from './interpolation';
@@ -81,7 +82,6 @@ export function* generateEventArg(
8182
start + name.length,
8283
features,
8384
`'`,
84-
['', 'template', start, combineLastMapping],
8585
directive,
8686
...generateCamelized(
8787
capitalize(name),

packages/language-core/lib/codegen/template/elementProps.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import type { Code, VueCodeInformation, VueCompilerOptions } from '../../types';
66
import { hyphenateAttr, hyphenateTag } from '../../utils/shared';
77
import { codeFeatures } from '../codeFeatures';
88
import { createVBindShorthandInlayHintInfo } from '../inlayHints';
9-
import { identifierRegex, newLine, wrapWith } from '../utils';
9+
import { identifierRegex, newLine } from '../utils';
1010
import { generateCamelized } from '../utils/camelized';
1111
import { generateUnicode } from '../utils/unicode';
12+
import { wrapWith } from '../utils/wrapWith';
1213
import type { TemplateCodegenContext } from './context';
1314
import { generateModifiers } from './elementDirectives';
1415
import { generateEventArg, generateEventExpression } from './elementEvents';

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import * as CompilerDOM from '@vue/compiler-dom';
22
import type * as ts from 'typescript';
33
import type { Code, Sfc, VueCompilerOptions } from '../../types';
44
import { getSlotsPropertyName } from '../../utils/shared';
5-
import { endOfLine, newLine, wrapWith } from '../utils';
5+
import { endOfLine, newLine } from '../utils';
6+
import { wrapWith } from '../utils/wrapWith';
67
import { TemplateCodegenContext, createTemplateCodegenContext } from './context';
78
import { generateObjectProperty } from './objectProperty';
89
import { generateStyleScopedClassReferences } from './styleScopedClasses';

packages/language-core/lib/codegen/template/objectProperty.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { camelize } from '@vue/shared';
22
import type { Code, VueCodeInformation } from '../../types';
3-
import { combineLastMapping, identifierRegex, wrapWith } from '../utils';
3+
import { combineLastMapping, identifierRegex } from '../utils';
44
import { generateCamelized } from '../utils/camelized';
55
import { generateStringLiteralKey } from '../utils/stringLiteralKey';
6+
import { wrapWith } from '../utils/wrapWith';
67
import type { TemplateCodegenContext } from './context';
78
import type { TemplateCodegenOptions } from './index';
89
import { generateInterpolation } from './interpolation';

packages/language-core/lib/codegen/template/slotOutlet.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as CompilerDOM from '@vue/compiler-dom';
22
import type { Code } from '../../types';
33
import { createVBindShorthandInlayHintInfo } from '../inlayHints';
4-
import { endOfLine, newLine, wrapWith } from '../utils';
4+
import { endOfLine, newLine } from '../utils';
5+
import { wrapWith } from '../utils/wrapWith';
56
import type { TemplateCodegenContext } from './context';
67
import { generateElementChildren } from './elementChildren';
78
import { generateElementProps, generatePropExp } from './elementProps';

packages/language-core/lib/codegen/template/styleScopedClasses.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getNodeText } from '../../parsers/scriptSetupRanges';
44
import type { Code } from '../../types';
55
import { endOfLine, normalizeAttributeValue } from '../utils';
66
import { generateEscaped } from '../utils/escaped';
7+
import { wrapWith } from '../utils/wrapWith';
78
import type { TemplateCodegenContext } from './context';
89
import type { TemplateCodegenOptions } from './index';
910

@@ -25,29 +26,21 @@ export function* generateStyleScopedClassReferences(
2526
}
2627
for (const { source, className, offset } of ctx.scopedClasses) {
2728
yield `/** @type {__VLS_StyleScopedClasses[`;
28-
yield [
29-
'',
30-
source,
29+
yield* wrapWith(
3130
offset - (withDot ? 1 : 0),
32-
ctx.codeFeatures.navigation,
33-
];
34-
yield `'`;
35-
36-
// fix https://github.com/vuejs/language-tools/issues/4537
37-
yield* generateEscaped(
38-
className,
39-
source,
40-
offset,
41-
ctx.codeFeatures.navigationAndAdditionalCompletion,
42-
classNameEscapeRegex
43-
);
44-
yield `'`;
45-
yield [
46-
'',
47-
source,
4831
offset + className.length,
32+
source,
4933
ctx.codeFeatures.navigation,
50-
];
34+
`'`,
35+
...generateEscaped(
36+
className,
37+
source,
38+
offset,
39+
ctx.codeFeatures.navigationAndAdditionalCompletion,
40+
classNameEscapeRegex
41+
),
42+
`'`
43+
)
5144
yield `]} */${endOfLine}`;
5245
}
5346
}

packages/language-core/lib/codegen/template/vSlot.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as CompilerDOM from '@vue/compiler-dom';
22
import type { Code } from '../../types';
3-
import { collectVars, createTsAst, endOfLine, newLine, wrapWith } from '../utils';
3+
import { collectVars, createTsAst, endOfLine, newLine } from '../utils';
4+
import { wrapWith } from '../utils/wrapWith';
45
import type { TemplateCodegenContext } from './context';
56
import type { TemplateCodegenOptions } from './index';
67
import { generateObjectProperty } from './objectProperty';

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ export function* generateCamelized(
55
code: string,
66
source: string,
77
offset: number,
8-
info: VueCodeInformation
8+
features: VueCodeInformation
99
): Generator<Code> {
1010
const parts = code.split('-');
11+
const startCombineOffset = features.__combineOffset ?? 0;
12+
1113
for (let i = 0; i < parts.length; i++) {
1214
const part = parts[i];
1315
if (part !== '') {
@@ -16,15 +18,15 @@ export function* generateCamelized(
1618
part,
1719
source,
1820
offset,
19-
info,
21+
features,
2022
];
2123
}
2224
else {
2325
yield [
2426
capitalize(part),
2527
source,
2628
offset,
27-
{ __combineOffset: i },
29+
{ __combineOffset: startCombineOffset + i },
2830
];
2931
}
3032
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export function* generateEscaped(
88
escapeTarget: RegExp
99
): Generator<Code> {
1010
const parts = text.split(escapeTarget);
11+
const startCombineOffset = features.__combineOffset ?? 0;
1112
let isEscapeTarget = false;
1213

1314
for (let i = 0; i < parts.length; i++) {
@@ -19,7 +20,7 @@ export function* generateEscaped(
1920
part,
2021
source,
2122
offset,
22-
i === 0 ? features : { __combineOffset: i },
23+
i === 0 ? features : { __combineOffset: startCombineOffset + i },
2324
];
2425
offset += part.length;
2526
isEscapeTarget = !isEscapeTarget;

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,6 @@ export const endOfLine = `;${newLine}`;
88
export const combineLastMapping: VueCodeInformation = { __combineOffset: 1 };
99
export const identifierRegex = /^[a-zA-Z_$][0-9a-zA-Z_$]*$/;
1010

11-
export function* wrapWith(
12-
startOffset: number,
13-
endOffset: number,
14-
features: VueCodeInformation,
15-
...wrapCodes: Code[]
16-
): Generator<Code> {
17-
yield ['', 'template', startOffset, features];
18-
let offset = 1;
19-
for (const wrapCode of wrapCodes) {
20-
if (typeof wrapCode !== 'string') {
21-
offset++;
22-
}
23-
yield wrapCode;
24-
}
25-
yield ['', 'template', endOffset, { __combineOffset: offset }];
26-
}
27-
2811
export function collectVars(
2912
ts: typeof import('typescript'),
3013
node: ts.Node,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Code, VueCodeInformation } from '../../types';
2-
import { combineLastMapping, wrapWith } from './index';
2+
import { combineLastMapping } from './index';
3+
import { wrapWith } from './wrapWith';
34

45
export function* generateStringLiteralKey(code: string, offset?: number, info?: VueCodeInformation): Generator<Code> {
56
if (offset === undefined || !info) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Code, VueCodeInformation } from '../../types';
2-
import { wrapWith } from './index';
2+
import { wrapWith } from './wrapWith';
33

44
export function* generateUnicode(code: string, offset: number, info: VueCodeInformation): Generator<Code> {
55
if (needToUnicode(code)) {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import type { Code, VueCodeInformation } from "../../types";
2+
3+
export function wrapWith(
4+
startOffset: number,
5+
endOffset: number,
6+
features: VueCodeInformation,
7+
...codes: Code[]
8+
): Generator<Code>;
9+
10+
export function wrapWith(
11+
startOffset: number,
12+
endOffset: number,
13+
source: string,
14+
features: VueCodeInformation,
15+
...codes: Code[]
16+
): Generator<Code>;
17+
18+
export function* wrapWith(
19+
startOffset: number,
20+
endOffset: number,
21+
...args: any[]
22+
): Generator<Code> {
23+
let source = 'template';
24+
let features: VueCodeInformation;
25+
let codes: Code[];
26+
if (typeof args[0] === 'string') {
27+
[source, features, ...codes] = args;
28+
}
29+
else {
30+
[features, ...codes] = args;
31+
}
32+
33+
yield ['', source, startOffset, features];
34+
let offset = 1;
35+
for (const code of codes) {
36+
if (typeof code !== 'string') {
37+
offset++;
38+
}
39+
yield code;
40+
}
41+
yield ['', source, endOffset, { __combineOffset: offset }];
42+
}

0 commit comments

Comments
 (0)