Skip to content

Commit 1e28b94

Browse files
committed
refactor(language-core):
reduce the generation of unnecessary parentheses
1 parent 3fac8f4 commit 1e28b94

File tree

8 files changed

+38
-49
lines changed

8 files changed

+38
-49
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ function* generateSetupFunction(
206206
]);
207207
if (arg) {
208208
setupCodeModifies.push([
209-
[`(__VLS_placeholder)`],
209+
[`__VLS_placeholder`],
210210
arg.start,
211211
arg.end
212212
]);
@@ -262,7 +262,7 @@ function* generateSetupFunction(
262262
}
263263
if (arg) {
264264
setupCodeModifies.push([
265-
[`(__VLS_placeholder)`],
265+
[`__VLS_placeholder`],
266266
arg.start,
267267
arg.end
268268
]);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ export function* generateComponent(
123123
dynamicTagInfo.tag,
124124
dynamicTagInfo.offsets[0],
125125
dynamicTagInfo.astHolder,
126-
'(',
127-
')'
126+
`(`,
127+
`)`
128128
);
129129
if (dynamicTagInfo.offsets[1] !== undefined) {
130130
yield `,`;
@@ -136,8 +136,8 @@ export function* generateComponent(
136136
dynamicTagInfo.tag,
137137
dynamicTagInfo.offsets[1],
138138
dynamicTagInfo.astHolder,
139-
'(',
140-
')'
139+
`(`,
140+
`)`
141141
);
142142
}
143143
yield `)${endOfLine}`;

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { endOfLine, wrapWith } from '../utils';
77
import { generateCamelized } from '../utils/camelized';
88
import { generateStringLiteralKey } from '../utils/stringLiteralKey';
99
import type { TemplateCodegenContext } from './context';
10+
import { generatePropExp } from './elementProps';
1011
import type { TemplateCodegenOptions } from './index';
1112
import { generateInterpolation } from './interpolation';
1213
import { generateObjectProperty } from './objectProperty';
@@ -176,21 +177,12 @@ function* generateValue(
176177
`value`
177178
);
178179
yield `: `;
179-
yield* wrapWith(
180-
exp.loc.start.offset,
181-
exp.loc.end.offset,
182-
ctx.codeFeatures.verification,
183-
...generateInterpolation(
184-
options,
185-
ctx,
186-
'template',
187-
ctx.codeFeatures.all,
188-
exp.content,
189-
exp.loc.start.offset,
190-
exp.loc,
191-
`(`,
192-
`)`
193-
)
180+
yield* generatePropExp(
181+
options,
182+
ctx,
183+
prop,
184+
exp,
185+
ctx.codeFeatures.all
194186
);
195187
}
196188

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ export function* generateEventExpression(
9898
prop: CompilerDOM.DirectiveNode
9999
): Generator<Code> {
100100
if (prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
101-
let prefix = '(';
102-
let suffix = ')';
101+
let prefix = `(`;
102+
let suffix = `)`;
103103
let isFirstMapping = true;
104104

105105
const ast = createTsAst(options.ts, prop.exp, prop.exp.content);
@@ -108,10 +108,10 @@ export function* generateEventExpression(
108108
yield `(...[$event]) => {${newLine}`;
109109
ctx.addLocalVariable('$event');
110110

111-
prefix = '';
112-
suffix = '';
111+
prefix = ``;
112+
suffix = ``;
113113
for (const blockCondition of ctx.blockConditions) {
114-
prefix += `if (!(${blockCondition})) return${endOfLine}`;
114+
prefix += `if (!${blockCondition}) return${endOfLine}`;
115115
}
116116
}
117117

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ export function* generateElementProps(
6161
&& prop.arg.loc.source.startsWith('[')
6262
&& prop.arg.loc.source.endsWith(']')
6363
) {
64-
failedPropExps?.push({ node: prop.arg, prefix: '(', suffix: ')' });
65-
failedPropExps?.push({ node: prop.exp, prefix: '() => {', suffix: '}' });
64+
failedPropExps?.push({ node: prop.arg, prefix: `(`, suffix: `)` });
65+
failedPropExps?.push({ node: prop.exp, prefix: `() => {`, suffix: `}` });
6666
}
6767
else if (
6868
!prop.arg
6969
&& prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
7070
) {
71-
failedPropExps?.push({ node: prop.exp, prefix: '(', suffix: ')' });
71+
failedPropExps?.push({ node: prop.exp, prefix: `(`, suffix: `)` });
7272
}
7373
}
7474
}
@@ -98,7 +98,7 @@ export function* generateElementProps(
9898
|| options.vueCompilerOptions.dataAttributes.some(pattern => minimatch(propName!, pattern))
9999
) {
100100
if (prop.exp && prop.exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) {
101-
failedPropExps?.push({ node: prop.exp, prefix: '(', suffix: ')' });
101+
failedPropExps?.push({ node: prop.exp, prefix: `(`, suffix: `)` });
102102
}
103103
continue;
104104
}
@@ -139,16 +139,15 @@ export function* generateElementProps(
139139
propName
140140
)
141141
),
142-
`: (`,
142+
`: `,
143143
...generatePropExp(
144144
options,
145145
ctx,
146146
prop,
147147
prop.exp,
148148
ctx.codeFeatures.all,
149149
enableCodeFeatures
150-
),
151-
`)`
150+
)
152151
);
153152
if (enableCodeFeatures) {
154153
yield* codes;
@@ -215,13 +214,12 @@ export function* generateElementProps(
215214
(prop.loc as any).name_1 ??= {},
216215
shouldCamelize
217216
),
218-
`: (`,
217+
`: `,
219218
...(
220219
prop.value
221220
? generateAttrValue(prop.value, ctx.codeFeatures.withoutNavigation)
222221
: [`true`]
223-
),
224-
`)`
222+
)
225223
);
226224
if (enableCodeFeatures) {
227225
yield* codes;
@@ -278,7 +276,7 @@ export function* generatePropExp(
278276
prop: CompilerDOM.DirectiveNode,
279277
exp: CompilerDOM.SimpleExpressionNode | undefined,
280278
features: VueCodeInformation,
281-
enableCodeFeatures: boolean
279+
enableCodeFeatures: boolean = true
282280
): Generator<Code> {
283281
const isShorthand = prop.arg?.loc.start.offset === prop.exp?.loc.start.offset;
284282

@@ -298,8 +296,8 @@ export function* generatePropExp(
298296
exp.loc.source,
299297
exp.loc.start.offset,
300298
exp.loc,
301-
'(',
302-
')'
299+
`(`,
300+
`)`
303301
);
304302
}
305303
else {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ export function* generateSlotOutlet(
5959
ctx,
6060
nameProp,
6161
nameProp.exp,
62-
ctx.codeFeatures.all,
63-
true
62+
ctx.codeFeatures.all
6463
),
6564
`]`
6665
];

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export function* generateVFor(
3737
source.content,
3838
source.loc.start.offset,
3939
source.loc,
40-
'(',
41-
')'
40+
`(`,
41+
`)`
4242
);
4343
yield `!)`; // #3102
4444
}
@@ -73,8 +73,8 @@ export function* generateVFor(
7373
prop.value.content,
7474
prop.value.loc.start.offset,
7575
prop.value.loc,
76-
'(',
77-
')'
76+
`(`,
77+
`)`
7878
);
7979
yield endOfLine;
8080
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function* generateVIf(
1313
node: CompilerDOM.IfNode
1414
): Generator<Code> {
1515

16-
let originalBlockConditionsLength = ctx.blockConditions.length;
16+
const originalBlockConditionsLength = ctx.blockConditions.length;
1717

1818
for (let i = 0; i < node.branches.length; i++) {
1919

@@ -41,8 +41,8 @@ export function* generateVIf(
4141
branch.condition.content,
4242
branch.condition.loc.start.offset,
4343
branch.condition.loc,
44-
'(',
45-
')'
44+
`(`,
45+
`)`
4646
),
4747
];
4848
for (const code of codes) {
@@ -66,7 +66,7 @@ export function* generateVIf(
6666
yield `}${newLine}`;
6767

6868
if (addedBlockCondition) {
69-
ctx.blockConditions[ctx.blockConditions.length - 1] = `!(${ctx.blockConditions[ctx.blockConditions.length - 1]})`;
69+
ctx.blockConditions[ctx.blockConditions.length - 1] = `!${ctx.blockConditions[ctx.blockConditions.length - 1]}`;
7070
}
7171
}
7272

0 commit comments

Comments
 (0)