Skip to content

Commit

Permalink
log property access from function body for from/to
Browse files Browse the repository at this point in the history
  • Loading branch information
OrionReed committed Dec 2, 2024
1 parent 7cedd6b commit 7bf64b6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"preview": "vite build && vite preview"
},
"dependencies": {
"@babel/parser": "^7.26.2",
"leaflet": "^1.9.4",
"perfect-arrows": "^0.3.7",
"perfect-freehand": "^1.2.2"
Expand All @@ -19,4 +20,4 @@
"typescript": "^5.7.2",
"vite": "^6.0.0"
}
}
}
43 changes: 43 additions & 0 deletions src/folk-event-propagator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { css } from './common/tags.ts';
import { FolkRope } from './folk-rope.ts';
import * as parser from '@babel/parser';

const styles = new CSSStyleSheet();
styles.replaceSync(css`
Expand Down Expand Up @@ -90,6 +91,8 @@ export class FolkEventPropagator extends FolkRope {
const functionBody = codeLines.join('\n');

try {
parseAst(functionBody);

this.#function = new Function('from', 'to', 'event', functionBody);
} catch (error) {
console.warn('Failed to parse expression:', error, functionBody);
Expand Down Expand Up @@ -208,3 +211,43 @@ export class FolkEventPropagator extends FolkRope {
}
};
}

function parseAst(functionBody: string) {
const ast = parser.parse(functionBody, {
sourceType: 'script',
});

const toProps = new Set<string>();
const fromProps = new Set<string>();

function walkAst(node: any) {
if (!node || typeof node !== 'object') return;

if (node.type === 'MemberExpression' && node.object?.type === 'Identifier') {
const objName = node.object.name;
if (objName !== 'to' && objName !== 'from') return;

const propSet = objName === 'to' ? toProps : fromProps;

if (node.property?.type === 'Identifier') {
propSet.add(node.property.name);
} else if (node.property?.type === 'StringLiteral') {
propSet.add(node.property.value);
}
}

// Recursively walk through all properties
for (const key in node) {
if (Array.isArray(node[key])) {
node[key].forEach(walkAst);
} else if (node[key] && typeof node[key] === 'object') {
walkAst(node[key]);
}
}
}

walkAst(ast);

console.log('Properties accessed on to:', Array.from(toProps));
console.log('Properties accessed on from:', Array.from(fromProps));
}

0 comments on commit 7bf64b6

Please sign in to comment.