Skip to content

Commit

Permalink
Have validator return Map of passingVarDefinitions instead of a list …
Browse files Browse the repository at this point in the history
…and a count. For now just flatten it in runValidatorPlan itself to prep for recursive calls...
  • Loading branch information
thsparks committed May 1, 2024
1 parent fa33f81 commit 1c8bb79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
9 changes: 8 additions & 1 deletion pxteditor/code-validation/runValidatorPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,12 @@ function runVariableUsageValidation(usedBlocks: Blockly.Block[], inputs: pxt.blo
count: inputs.count,
name: inputs.name
});
return [blockResults.passingVarDefinitions, blockResults.passed];

// Flatten the map of passing variable definition blocks
const passingVarDefinitions: Blockly.Block[] = [];
for (const blocks of blockResults.passingVarDefinitions.values()) {
passingVarDefinitions.push(...blocks);
}

return [passingVarDefinitions, blockResults.passed];
}
21 changes: 7 additions & 14 deletions pxteditor/code-validation/validateVariableUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ export function validateVariableUsage({
count: number;
name?: String;
}): {
passingVarDefinitions: Blockly.Block[];
passingVarCount: number;
passingVarDefinitions: Map<string, Blockly.Block[]>;
passed: boolean;
} {
const varDefinitionBlocks: Map<string, Blockly.Block[]> = new Map();
const usedVars: Set<string> = new Set(); // Use set so we don't double count vars
const usedVars: Set<string> = new Set();

for (const block of usedBlocks) {
if (!block.isEnabled()) {
Expand All @@ -45,18 +44,12 @@ export function validateVariableUsage({

// Var passes check if it is both used and defined.
// We return the definition blocks to allow for recursively checking how the var was set.
// Track passingVarCount separately, since a var could have more than one set block.
const passingVarDefinitions: Blockly.Block[] = [];
let passingVarCount = 0;
for (const varName of usedVars) {
const varBlocks = varDefinitionBlocks.get(varName) ?? [];
if (varBlocks.length > 0) {
passingVarCount++;
for (const varBlock of varBlocks) {
passingVarDefinitions.push(varBlock);
}
const passingVarDefinitions = new Map<string, Blockly.Block[]>();
for (const [varName, definitionBlocks] of varDefinitionBlocks) {
if (usedVars.has(varName)) {
passingVarDefinitions.set(varName, definitionBlocks);
}
}

return { passingVarDefinitions, passingVarCount, passed: passingVarCount >= count };
return { passingVarDefinitions, passed: passingVarDefinitions.size >= count };
}

0 comments on commit 1c8bb79

Please sign in to comment.