Skip to content

Commit

Permalink
chore: reduce internal functions of createPropsCode
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Apr 29, 2023
1 parent 455d293 commit 514a430
Showing 1 changed file with 40 additions and 44 deletions.
84 changes: 40 additions & 44 deletions packages/vue-language-core/src/generators/template.ts
Expand Up @@ -563,7 +563,7 @@ export function generate(
`const ${var_functionalComponent} = (await import('${sharedTypesImport}')).asFunctionalComponent(`,
`${var_originalComponent}, `,
`new ${var_originalComponent}({`,
...createPropsCode(node, props, 'slots'),
...createPropsCode(node, props, 'extraReferences'),
'}));\n',
);

Expand Down Expand Up @@ -600,7 +600,7 @@ export function generate(
: dynamicTagExp ? ['', 'template', startTagOffset, capabilitiesPresets.diagnosticOnly]
: '',
'{ ',
...createPropsCode(node, props, 'props', propsFailedExps),
...createPropsCode(node, props, 'normal', propsFailedExps),
'}',
// diagnostic end
tagOffsets.length ? ['', 'template', tagOffsets[0] + tag.length, capabilitiesPresets.diagnosticOnly]
Expand Down Expand Up @@ -943,7 +943,7 @@ export function generate(
}
}

function createPropsCode(node: CompilerDOM.ElementNode, props: CompilerDOM.ElementNode['props'], mode: 'props' | 'slots', propsFailedExps?: CompilerDOM.SimpleExpressionNode[]): Code[] {
function createPropsCode(node: CompilerDOM.ElementNode, props: CompilerDOM.ElementNode['props'], mode: 'normal' | 'extraReferences', propsFailedExps?: CompilerDOM.SimpleExpressionNode[]): Code[] {

let styleAttrNum = 0;
let classAttrNum = 0;
Expand All @@ -961,6 +961,25 @@ export function generate(

const codes: Code[] = [];

let caps_all: FileRangeCapabilities = capabilitiesPresets.all;
let caps_diagnosticOnly: FileRangeCapabilities = capabilitiesPresets.diagnosticOnly;
let caps_attr: FileRangeCapabilities = capabilitiesPresets.attr;

if (mode === 'extraReferences') {
caps_all = {
references: caps_all.references,
rename: caps_all.rename,
};
caps_diagnosticOnly = {
references: caps_diagnosticOnly.references,
rename: caps_diagnosticOnly.rename,
};
caps_attr = {
references: caps_attr.references,
rename: caps_attr.rename,
};
}

for (const prop of props) {
if (
prop.type === CompilerDOM.NodeTypes.DIRECTIVE
Expand Down Expand Up @@ -1009,15 +1028,15 @@ export function generate(
'',
'template',
prop.loc.start.offset,
getCaps(capabilitiesPresets.diagnosticOnly),
caps_diagnosticOnly,
]);
if (!prop.arg) {
codes.push(
...createObjectPropertyCode([
attrNameText,
'template',
[prop.loc.start.offset, prop.loc.start.offset + prop.loc.source.indexOf('=')],
getCaps(capabilitiesPresets.attr),
caps_attr,
], (prop.loc as any).name_1 ?? ((prop.loc as any).name_1 = {})),
);
}
Expand All @@ -1028,7 +1047,7 @@ export function generate(
'template',
[prop.arg.loc.start.offset, prop.arg.loc.start.offset + attrNameText.length], // patch style attr,
{
...getCaps(capabilitiesPresets.attr),
...caps_attr,
rename: {
normalize: camelize,
apply: getRenameApply(attrNameText),
Expand All @@ -1044,7 +1063,7 @@ export function generate(
'template',
[prop.arg.loc.start.offset, prop.arg.loc.end.offset],
{
...getCaps(capabilitiesPresets.attr),
...caps_attr,
rename: {
normalize: camelize,
apply: getRenameApply(attrNameText),
Expand All @@ -1060,18 +1079,17 @@ export function generate(
prop.exp.loc.source,
prop.exp.loc,
prop.exp.loc.start.offset,
getCaps(capabilitiesPresets.all),
caps_all,
'(',
')',
),
);
const fb = getFormatBrackets(formatBrackets.normal);
if (fb) {
if (mode === 'normal') {
formatCodes.push(
...createFormatCode(
prop.exp.loc.source,
prop.exp.loc.start.offset,
fb,
formatBrackets.normal,
),
);
}
Expand All @@ -1084,7 +1102,7 @@ export function generate(
'',
'template',
prop.loc.end.offset,
getCaps(capabilitiesPresets.diagnosticOnly),
caps_diagnosticOnly,
]);
codes.push(', ');
// original name
Expand All @@ -1095,7 +1113,7 @@ export function generate(
'template',
[prop.arg.loc.start.offset, prop.arg.loc.end.offset],
{
...getCaps(capabilitiesPresets.attr),
...caps_attr,
rename: {
normalize: camelize,
apply: getRenameApply(attrNameText),
Expand Down Expand Up @@ -1123,9 +1141,7 @@ export function generate(
codes.push(', ');
}
}
else if (
prop.type === CompilerDOM.NodeTypes.ATTRIBUTE
) {
else if (prop.type === CompilerDOM.NodeTypes.ATTRIBUTE) {

let attrNameText = prop.name;

Expand All @@ -1152,15 +1168,15 @@ export function generate(
'',
'template',
prop.loc.start.offset,
getCaps(capabilitiesPresets.diagnosticOnly),
caps_diagnosticOnly,
]);
codes.push(
...createObjectPropertyCode([
propName,
'template',
[prop.loc.start.offset, prop.loc.start.offset + prop.name.length],
{
...getCaps(capabilitiesPresets.attr),
...caps_attr,
rename: {
normalize: camelize,
apply: getRenameApply(prop.name),
Expand All @@ -1180,7 +1196,7 @@ export function generate(
'',
'template',
prop.loc.end.offset,
getCaps(capabilitiesPresets.diagnosticOnly),
caps_diagnosticOnly,
]);
codes.push(', ');
// original name
Expand All @@ -1191,7 +1207,7 @@ export function generate(
'template',
prop.loc.start.offset,
{
...getCaps(capabilitiesPresets.attr),
...caps_attr,
rename: {
normalize: camelize,
apply: getRenameApply(prop.name),
Expand Down Expand Up @@ -1222,19 +1238,18 @@ export function generate(
prop.exp.content,
prop.exp.loc,
prop.exp.loc.start.offset,
getCaps(capabilitiesPresets.all),
caps_all,
'(',
')',
),
', ',
);
const fb = getFormatBrackets(formatBrackets.normal);
if (fb) {
if (mode === 'normal') {
formatCodes.push(
...createFormatCode(
prop.exp.content,
prop.exp.loc.start.offset,
fb,
formatBrackets.normal,
),
);
}
Expand All @@ -1247,25 +1262,6 @@ export function generate(

return codes;

function getCaps(caps: FileRangeCapabilities): FileRangeCapabilities {
if (mode === 'props') {
return caps;
}
else {
return {
references: caps.references,
rename: caps.rename,
};
}
}
function getFormatBrackets(b: [string, string]) {
if (mode === 'props') {
return b;
}
else {
return undefined;
}
}
function generateAttrValue(attrNode: CompilerDOM.TextNode) {
const char = attrNode.loc.source.startsWith("'") ? "'" : '"';
codes.push(char);
Expand All @@ -1284,7 +1280,7 @@ export function generate(
toUnicodeIfNeed(content),
'template',
[start, end],
getCaps(capabilitiesPresets.all),
caps_all,
]);
codes.push(char);
}
Expand Down

0 comments on commit 514a430

Please sign in to comment.