Skip to content

Commit

Permalink
refactor(language-core):
Browse files Browse the repository at this point in the history
reduce the generation of unnecessary parentheses
  • Loading branch information
KazariEX committed Feb 21, 2025
1 parent 3fac8f4 commit 1e28b94
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 49 deletions.
4 changes: 2 additions & 2 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function* generateSetupFunction(
]);
if (arg) {
setupCodeModifies.push([
[`(__VLS_placeholder)`],
[`__VLS_placeholder`],
arg.start,
arg.end
]);
Expand Down Expand Up @@ -262,7 +262,7 @@ function* generateSetupFunction(
}
if (arg) {
setupCodeModifies.push([
[`(__VLS_placeholder)`],
[`__VLS_placeholder`],
arg.start,
arg.end
]);
Expand Down
8 changes: 4 additions & 4 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export function* generateComponent(
dynamicTagInfo.tag,
dynamicTagInfo.offsets[0],
dynamicTagInfo.astHolder,
'(',
')'
`(`,
`)`
);
if (dynamicTagInfo.offsets[1] !== undefined) {
yield `,`;
Expand All @@ -136,8 +136,8 @@ export function* generateComponent(
dynamicTagInfo.tag,
dynamicTagInfo.offsets[1],
dynamicTagInfo.astHolder,
'(',
')'
`(`,
`)`
);
}
yield `)${endOfLine}`;
Expand Down
22 changes: 7 additions & 15 deletions packages/language-core/lib/codegen/template/elementDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { endOfLine, wrapWith } from '../utils';
import { generateCamelized } from '../utils/camelized';
import { generateStringLiteralKey } from '../utils/stringLiteralKey';
import type { TemplateCodegenContext } from './context';
import { generatePropExp } from './elementProps';
import type { TemplateCodegenOptions } from './index';
import { generateInterpolation } from './interpolation';
import { generateObjectProperty } from './objectProperty';
Expand Down Expand Up @@ -176,21 +177,12 @@ function* generateValue(
`value`
);
yield `: `;
yield* wrapWith(
exp.loc.start.offset,
exp.loc.end.offset,
ctx.codeFeatures.verification,
...generateInterpolation(
options,
ctx,
'template',
ctx.codeFeatures.all,
exp.content,
exp.loc.start.offset,
exp.loc,
`(`,
`)`
)
yield* generatePropExp(
options,
ctx,
prop,
exp,
ctx.codeFeatures.all
);
}

Expand Down
10 changes: 5 additions & 5 deletions packages/language-core/lib/codegen/template/elementEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ export function* generateEventExpression(
prop: CompilerDOM.DirectiveNode
): Generator<Code> {
if (prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
let prefix = '(';
let suffix = ')';
let prefix = `(`;
let suffix = `)`;
let isFirstMapping = true;

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

prefix = '';
suffix = '';
prefix = ``;
suffix = ``;
for (const blockCondition of ctx.blockConditions) {
prefix += `if (!(${blockCondition})) return${endOfLine}`;
prefix += `if (!${blockCondition}) return${endOfLine}`;
}
}

Expand Down
24 changes: 11 additions & 13 deletions packages/language-core/lib/codegen/template/elementProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ export function* generateElementProps(
&& prop.arg.loc.source.startsWith('[')
&& prop.arg.loc.source.endsWith(']')
) {
failedPropExps?.push({ node: prop.arg, prefix: '(', suffix: ')' });
failedPropExps?.push({ node: prop.exp, prefix: '() => {', suffix: '}' });
failedPropExps?.push({ node: prop.arg, prefix: `(`, suffix: `)` });
failedPropExps?.push({ node: prop.exp, prefix: `() => {`, suffix: `}` });
}
else if (
!prop.arg
&& prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
) {
failedPropExps?.push({ node: prop.exp, prefix: '(', suffix: ')' });
failedPropExps?.push({ node: prop.exp, prefix: `(`, suffix: `)` });
}
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ export function* generateElementProps(
|| options.vueCompilerOptions.dataAttributes.some(pattern => minimatch(propName!, pattern))
) {
if (prop.exp && prop.exp.constType !== CompilerDOM.ConstantTypes.CAN_STRINGIFY) {
failedPropExps?.push({ node: prop.exp, prefix: '(', suffix: ')' });
failedPropExps?.push({ node: prop.exp, prefix: `(`, suffix: `)` });
}
continue;
}
Expand Down Expand Up @@ -139,16 +139,15 @@ export function* generateElementProps(
propName
)
),
`: (`,
`: `,
...generatePropExp(
options,
ctx,
prop,
prop.exp,
ctx.codeFeatures.all,
enableCodeFeatures
),
`)`
)
);
if (enableCodeFeatures) {
yield* codes;
Expand Down Expand Up @@ -215,13 +214,12 @@ export function* generateElementProps(
(prop.loc as any).name_1 ??= {},
shouldCamelize
),
`: (`,
`: `,
...(
prop.value
? generateAttrValue(prop.value, ctx.codeFeatures.withoutNavigation)
: [`true`]
),
`)`
)
);
if (enableCodeFeatures) {
yield* codes;
Expand Down Expand Up @@ -278,7 +276,7 @@ export function* generatePropExp(
prop: CompilerDOM.DirectiveNode,
exp: CompilerDOM.SimpleExpressionNode | undefined,
features: VueCodeInformation,
enableCodeFeatures: boolean
enableCodeFeatures: boolean = true
): Generator<Code> {
const isShorthand = prop.arg?.loc.start.offset === prop.exp?.loc.start.offset;

Expand All @@ -298,8 +296,8 @@ export function* generatePropExp(
exp.loc.source,
exp.loc.start.offset,
exp.loc,
'(',
')'
`(`,
`)`
);
}
else {
Expand Down
3 changes: 1 addition & 2 deletions packages/language-core/lib/codegen/template/slotOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ export function* generateSlotOutlet(
ctx,
nameProp,
nameProp.exp,
ctx.codeFeatures.all,
true
ctx.codeFeatures.all
),
`]`
];
Expand Down
8 changes: 4 additions & 4 deletions packages/language-core/lib/codegen/template/vFor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export function* generateVFor(
source.content,
source.loc.start.offset,
source.loc,
'(',
')'
`(`,
`)`
);
yield `!)`; // #3102
}
Expand Down Expand Up @@ -73,8 +73,8 @@ export function* generateVFor(
prop.value.content,
prop.value.loc.start.offset,
prop.value.loc,
'(',
')'
`(`,
`)`
);
yield endOfLine;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/language-core/lib/codegen/template/vIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function* generateVIf(
node: CompilerDOM.IfNode
): Generator<Code> {

let originalBlockConditionsLength = ctx.blockConditions.length;
const originalBlockConditionsLength = ctx.blockConditions.length;

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

Expand Down Expand Up @@ -41,8 +41,8 @@ export function* generateVIf(
branch.condition.content,
branch.condition.loc.start.offset,
branch.condition.loc,
'(',
')'
`(`,
`)`
),
];
for (const code of codes) {
Expand All @@ -66,7 +66,7 @@ export function* generateVIf(
yield `}${newLine}`;

if (addedBlockCondition) {
ctx.blockConditions[ctx.blockConditions.length - 1] = `!(${ctx.blockConditions[ctx.blockConditions.length - 1]})`;
ctx.blockConditions[ctx.blockConditions.length - 1] = `!${ctx.blockConditions[ctx.blockConditions.length - 1]}`;
}
}

Expand Down

0 comments on commit 1e28b94

Please sign in to comment.