Skip to content

Commit

Permalink
Merge pull request #35 from figma/jake/inheritance-ordering
Browse files Browse the repository at this point in the history
jake/inheritance ordering
  • Loading branch information
jake-figma authored Feb 14, 2024
2 parents 6019363 + b4b54e0 commit ee92bac
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 48 deletions.
32 changes: 20 additions & 12 deletions code.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,19 @@
codegenResultTemplates.push(...matchingCodegenResults);
}
}
if (globalTemplates) {
const componentTemplates = "key" in snippetNode && globalTemplates.components ? globalTemplates.components[snippetNode.key] || [] : [];
const typeTemplates = globalTemplates.types ? globalTemplates.types[snippetNode.type] || [] : [];
if (globalTemplates.components) {
const componentTemplates = "key" in snippetNode ? globalTemplates.components[snippetNode.key] || [] : [];
codegenResultTemplates.push(...matchingTemplates(componentTemplates));
codegenResultTemplates.push(...matchingTemplates(typeTemplates));
}
if (!Object.keys(seenSnippetTemplates).length && !codegenResultTemplates.length && globalTemplates.types) {
const typeTemplates = globalTemplates.types[snippetNode.type] || [];
const seenKey = JSON.stringify(typeTemplates);
if (!seenSnippetTemplates[seenKey]) {
seenSnippetTemplates[seenKey] = 1;
const defaultTemplates = !typeTemplates.length && globalTemplates.types.DEFAULT ? globalTemplates.types.DEFAULT : [];
codegenResultTemplates.push(...matchingTemplates(typeTemplates));
codegenResultTemplates.push(...matchingTemplates(defaultTemplates));
}
}
const children = "children" in node ? node.children : [];
const nodeSnippetTemplateData = await hydrateSnippets(
Expand All @@ -173,17 +181,17 @@
);
nodeSnippetTemplateDataArray.push(nodeSnippetTemplateData);
}
await processSnippetTemplatesForNode(node);
if (node.type === "INSTANCE") {
if (node.type === "COMPONENT" && node.parent && node.parent.type === "COMPONENT_SET") {
await processSnippetTemplatesForNode(node.parent);
} else if (node.type === "INSTANCE") {
if (node.mainComponent) {
await processSnippetTemplatesForNode(node.mainComponent);
if (node.mainComponent.parent && node.mainComponent.parent.type === "COMPONENT_SET") {
await processSnippetTemplatesForNode(node.mainComponent.parent);
}
await processSnippetTemplatesForNode(node.mainComponent);
}
} else if (node.type === "COMPONENT" && node.parent && node.parent.type === "COMPONENT_SET") {
await processSnippetTemplatesForNode(node.parent);
}
await processSnippetTemplatesForNode(node);
return nodeSnippetTemplateDataArray;
}
function transformStringWithFilter(string, rawString, filter = "hyphen") {
Expand Down Expand Up @@ -237,16 +245,16 @@
} else if (param === "figma.children" && recursionIndex < MAX_RECURSION) {
const indentMatch = line.match(/^[ \t]+/);
const indent2 = indentMatch ? indentMatch[0] : "";
const value = await findChildrenSnippets(
const childrenValue = await findChildrenSnippets(
codegenResult,
nodeChildren,
indent2,
recursionIndex + 1,
globalTemplates
);
if (value) {
if (childrenValue) {
line = line.replace(/^[ \t]+/, "");
line = line.replace(match, value);
line = line.replace(match, childrenValue);
} else {
succeeded = false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
type CodeSnippetGlobalTemplates = {
components?: CodegenResultTemplatesByComponentKey;
types?: { [K in NodeType]?: CodegenResult[] };
types?: { [K in NodeType | "DEFAULT"]?: CodegenResult[] };
};

/**
Expand Down
61 changes: 38 additions & 23 deletions src/snippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,30 @@ export async function nodeSnippetTemplateDataArrayFromNode(
codegenResultTemplates.push(...matchingCodegenResults);
}
}
if (globalTemplates) {
if (globalTemplates.components) {
const componentTemplates =
"key" in snippetNode && globalTemplates.components
"key" in snippetNode
? globalTemplates.components[snippetNode.key] || []
: [];
const typeTemplates = globalTemplates.types
? globalTemplates.types[snippetNode.type] || []
: [];
codegenResultTemplates.push(...matchingTemplates(componentTemplates));
codegenResultTemplates.push(...matchingTemplates(typeTemplates));
}

if (
!Object.keys(seenSnippetTemplates).length &&
!codegenResultTemplates.length &&
globalTemplates.types
) {
const typeTemplates = globalTemplates.types[snippetNode.type] || [];
const seenKey = JSON.stringify(typeTemplates);
if (!seenSnippetTemplates[seenKey]) {
seenSnippetTemplates[seenKey] = 1;
const defaultTemplates =
!typeTemplates.length && globalTemplates.types.DEFAULT
? globalTemplates.types.DEFAULT
: [];
codegenResultTemplates.push(...matchingTemplates(typeTemplates));
codegenResultTemplates.push(...matchingTemplates(defaultTemplates));
}
}
const children = "children" in node ? node.children : [];
const nodeSnippetTemplateData = await hydrateSnippets(
Expand All @@ -129,31 +143,32 @@ export async function nodeSnippetTemplateDataArrayFromNode(
}

/**
* Templates on the given node
*/
await processSnippetTemplatesForNode(node);

/**
* Templates via inheritance from component lineage
* Templates via inheritance from component lineage.
* Starting at the top with component sets, then components, then instances.
*/
if (node.type === "INSTANCE") {
if (
node.type === "COMPONENT" &&
node.parent &&
node.parent.type === "COMPONENT_SET"
) {
await processSnippetTemplatesForNode(node.parent);
} else if (node.type === "INSTANCE") {
if (node.mainComponent) {
await processSnippetTemplatesForNode(node.mainComponent);
if (
node.mainComponent.parent &&
node.mainComponent.parent.type === "COMPONENT_SET"
) {
await processSnippetTemplatesForNode(node.mainComponent.parent);
}
await processSnippetTemplatesForNode(node.mainComponent);
}
} else if (
node.type === "COMPONENT" &&
node.parent &&
node.parent.type === "COMPONENT_SET"
) {
await processSnippetTemplatesForNode(node.parent);
}

/**
* Templates on the given node
*/
await processSnippetTemplatesForNode(node);

return nodeSnippetTemplateDataArray;
}

Expand Down Expand Up @@ -247,16 +262,16 @@ export async function hydrateSnippets(
) {
const indentMatch = line.match(/^[ \t]+/);
const indent = indentMatch ? indentMatch[0] : "";
const value = await findChildrenSnippets(
const childrenValue = await findChildrenSnippets(
codegenResult,
nodeChildren,
indent,
recursionIndex + 1,
globalTemplates
);
if (value) {
if (childrenValue) {
line = line.replace(/^[ \t]+/, "");
line = line.replace(match, value);
line = line.replace(match, childrenValue);
} else {
succeeded = false;
}
Expand Down
32 changes: 20 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,19 @@
codegenResultTemplates.push(...matchingCodegenResults);
}
}
if (globalTemplates) {
const componentTemplates = "key" in snippetNode && globalTemplates.components ? globalTemplates.components[snippetNode.key] || [] : [];
const typeTemplates = globalTemplates.types ? globalTemplates.types[snippetNode.type] || [] : [];
if (globalTemplates.components) {
const componentTemplates = "key" in snippetNode ? globalTemplates.components[snippetNode.key] || [] : [];
codegenResultTemplates.push(...matchingTemplates(componentTemplates));
codegenResultTemplates.push(...matchingTemplates(typeTemplates));
}
if (!Object.keys(seenSnippetTemplates).length && !codegenResultTemplates.length && globalTemplates.types) {
const typeTemplates = globalTemplates.types[snippetNode.type] || [];
const seenKey = JSON.stringify(typeTemplates);
if (!seenSnippetTemplates[seenKey]) {
seenSnippetTemplates[seenKey] = 1;
const defaultTemplates = !typeTemplates.length && globalTemplates.types.DEFAULT ? globalTemplates.types.DEFAULT : [];
codegenResultTemplates.push(...matchingTemplates(typeTemplates));
codegenResultTemplates.push(...matchingTemplates(defaultTemplates));
}
}
const children = "children" in node ? node.children : [];
const nodeSnippetTemplateData = await hydrateSnippets(
Expand All @@ -361,17 +369,17 @@
);
nodeSnippetTemplateDataArray.push(nodeSnippetTemplateData);
}
await processSnippetTemplatesForNode(node);
if (node.type === "INSTANCE") {
if (node.type === "COMPONENT" && node.parent && node.parent.type === "COMPONENT_SET") {
await processSnippetTemplatesForNode(node.parent);
} else if (node.type === "INSTANCE") {
if (node.mainComponent) {
await processSnippetTemplatesForNode(node.mainComponent);
if (node.mainComponent.parent && node.mainComponent.parent.type === "COMPONENT_SET") {
await processSnippetTemplatesForNode(node.mainComponent.parent);
}
await processSnippetTemplatesForNode(node.mainComponent);
}
} else if (node.type === "COMPONENT" && node.parent && node.parent.type === "COMPONENT_SET") {
await processSnippetTemplatesForNode(node.parent);
}
await processSnippetTemplatesForNode(node);
return nodeSnippetTemplateDataArray;
}
function transformStringWithFilter(string, rawString, filter = "hyphen") {
Expand Down Expand Up @@ -425,16 +433,16 @@
} else if (param === "figma.children" && recursionIndex < MAX_RECURSION) {
const indentMatch = line.match(/^[ \t]+/);
const indent2 = indentMatch ? indentMatch[0] : "";
const value = await findChildrenSnippets(
const childrenValue = await findChildrenSnippets(
codegenResult,
nodeChildren,
indent2,
recursionIndex + 1,
globalTemplates
);
if (value) {
if (childrenValue) {
line = line.replace(/^[ \t]+/, "");
line = line.replace(match, value);
line = line.replace(match, childrenValue);
} else {
succeeded = false;
}
Expand Down

0 comments on commit ee92bac

Please sign in to comment.