Skip to content

Commit

Permalink
Nested block fix
Browse files Browse the repository at this point in the history
  • Loading branch information
navdeep5 committed Nov 21, 2024
1 parent 7bf09c8 commit 70c608b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ type Location = {
endIndex: number;
};

type NestedBlockLocation = {
location: Location;
parentBlockTypes: string[];
};

type NestedPresetBlockMap = {
[key: string]: NestedBlockLocation[];
};

type BlockValidationResult = {
hasLocalBlocks: boolean;
rootThemeBlockTypes: BlockTypeMap;
presetBlockTypes: BlockTypeMap;
nestedPresetBlockTypes: NestedPresetBlockMap;
};

function isLiteralNode(node: JSONNode): node is LiteralNode {
Expand Down Expand Up @@ -67,11 +77,16 @@ export const reportError =
});
};

function getParentBlockTypes(ancestors: JSONNode[]): string[] {
// TODO: Implement this
return [];
}

export function collectAndValidateBlockTypes(jsonFile: JSONNode): BlockValidationResult {
const rootThemeBlockTypes: BlockTypeMap = {};
const rootLocalBlockTypes: BlockTypeMap = {};
const presetBlockTypes: BlockTypeMap = {};

const nestedPresetBlockTypes: NestedPresetBlockMap = {};
visit<SourceCodeType.JSON, void>(jsonFile, {
Property(node, ancestors) {
// Process 'type' and 'name' properties within 'blocks'
Expand All @@ -92,10 +107,15 @@ export function collectAndValidateBlockTypes(jsonFile: JSONNode): BlockValidatio
};

// Determine the target map for block types based on their context
let targetMap: BlockTypeMap;
type TargetMapType = BlockTypeMap | NestedPresetBlockMap | undefined;

// Determine the target map for block types based on their context
let targetMap: TargetMapType;
const inPresets = isInArrayWithParentKey(ancestors, 'presets');

if (inPresets && isInPresetsArray(ancestors)) {
if (inPresets && !isInPresetsArray(ancestors)) {
targetMap = nestedPresetBlockTypes;
} else if (inPresets && isInPresetsArray(ancestors)) {
targetMap = presetBlockTypes;
} else if (hasNameProperty) {
targetMap = rootLocalBlockTypes;
Expand All @@ -104,9 +124,18 @@ export function collectAndValidateBlockTypes(jsonFile: JSONNode): BlockValidatio
}

// Add the block type to the appropriate map
if (typeof typeValue === 'string') {
targetMap[typeValue] = targetMap[typeValue] || [];
targetMap[typeValue].push(typeLocation);
if (targetMap && typeof typeValue === 'string') {
if (targetMap === nestedPresetBlockTypes) {
const parentTypes = getParentBlockTypes(ancestors);
targetMap[typeValue] = targetMap[typeValue] || [];
targetMap[typeValue].push({
location: typeLocation,
parentBlockTypes: parentTypes,
});
} else {
(targetMap as BlockTypeMap)[typeValue] = (targetMap as BlockTypeMap)[typeValue] || [];
(targetMap as BlockTypeMap)[typeValue].push(typeLocation);
}
}
}
},
Expand All @@ -116,6 +145,7 @@ export function collectAndValidateBlockTypes(jsonFile: JSONNode): BlockValidatio
hasLocalBlocks: Object.keys(rootLocalBlockTypes).length > 0,
rootThemeBlockTypes,
presetBlockTypes,
nestedPresetBlockTypes,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const ValidBlockTarget: LiquidCheckDefinition = {
const jsonFile = toJSONAST(jsonString);
if (jsonFile instanceof Error) return;

const { hasLocalBlocks, rootThemeBlockTypes, presetBlockTypes } =
const { hasLocalBlocks, rootThemeBlockTypes, presetBlockTypes, nestedPresetBlockTypes } =
collectAndValidateBlockTypes(jsonFile as JSONNode);

if (hasLocalBlocks) return;
Expand Down Expand Up @@ -88,6 +88,8 @@ export const ValidBlockTarget: LiquidCheckDefinition = {
}
}
}

// TODO: Validate nested preset block types via cross file check
},
};
},
Expand Down

0 comments on commit 70c608b

Please sign in to comment.