From 3e1a32ecbd5b731a87dcb40d397539c5f00c364d Mon Sep 17 00:00:00 2001 From: onsetsu Date: Wed, 5 Jun 2024 09:36:23 +0200 Subject: [PATCH] sample for data_bindings SQUASHED: AUTO-COMMIT-src-client-reactive-active-expression-rewriting-active-expression-rewriting.js,AUTO-COMMIT-src-client-reactive-babel-plugin-active-expression-rewriting-index.js,AUTO-COMMIT-src-client-reactive-babel-plugin-sample-data-bindings-babel-plugin-squiggly-binding.js,AUTO-COMMIT-src-client-reactive-babel-plugin-sample-data-bindings-new-implementation-test-plugin.js,AUTO-COMMIT-src-client-reactive-babel-plugin-sample-data-bindings-new-implementation-test-plugin.workspace,AUTO-COMMIT-src-client-reactive-babel-plugin-sample-data-bindings-sample-data-bindings-example.js,AUTO-COMMIT-src-client-reactive-babel-plugin-sample-data-bindings-sample-data-bindings.js,AUTO-COMMIT-src-client-reactive-babel-plugin-sample-data-bindings-sample-data-bindings.workspace,AUTO-COMMIT-src-components-tools-lively-plugin-explorer.js,AUTO-COMMIT-src-components-tools-lively-plugin-explorer-playground.workspace,AUTO-COMMIT-src-plugin-babel.js,AUTO-COMMIT-src-systemjs-config.js, --- .../index.js | 11 ++- .../sample-data-bindings-example.js | 8 ++ .../sample-data-bindings.js | 77 +++++++++++++++++++ .../sample-data-bindings.workspace | 23 ++++++ ...ively-plugin-explorer-playground.workspace | 6 +- .../tools/lively-plugin-explorer.js | 10 ++- src/plugin-babel.js | 9 +++ src/systemjs-config.js | 1 + 8 files changed, 137 insertions(+), 8 deletions(-) create mode 100644 src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings-example.js create mode 100644 src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.js create mode 100644 src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.workspace diff --git a/src/client/reactive/babel-plugin-active-expression-rewriting/index.js b/src/client/reactive/babel-plugin-active-expression-rewriting/index.js index 40b8acdb1..27f35f0d2 100644 --- a/src/client/reactive/babel-plugin-active-expression-rewriting/index.js +++ b/src/client/reactive/babel-plugin-active-expression-rewriting/index.js @@ -58,9 +58,14 @@ export function getSourceLocation(node, state, template, t) { fileName = 'workspace:' + fileName.split('unnamed_module_')[1]; } if (!node.loc) { - - console.error("Make sure to add loc information manually when inserting an AE or assignment while transforming" + node.left.name + " = " + node.right.name); - return t.identifier("undefined"); + // console.error("Make sure to add loc information manually when inserting an AE or assignment while transforming" + node.left.name + " = " + node.right.name); + // return t.identifier("undefined"); + return template(`({ + file: '${fileName}', + end: { column: 0, line: 1 }, + start: { column: 0, line: 1 }, + source: '' + })`)({}).expression; } if (node.loc === "sourceless") { return t.identifier("undefined"); diff --git a/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings-example.js b/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings-example.js new file mode 100644 index 000000000..a40a48d22 --- /dev/null +++ b/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings-example.js @@ -0,0 +1,8 @@ +let target = 0 +let source = 2 + +target <~ 32 * source + +source = 10 + +target diff --git a/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.js b/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.js new file mode 100644 index 000000000..31ef596f9 --- /dev/null +++ b/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.js @@ -0,0 +1,77 @@ +console.log(123) +export default function ({ template, types: t }) { + return { + name: 'sample data bindings (<~)', + visitor: { + Program(program) { + // handle squiggly arrow operator + function leftRightOfSquigglyArrow(path) { + if (!path.isBinaryExpression()) { return [] } + if (path.node.operator !== '<') { return [] } + + let right + const expression = path.get('right') + if (expression.isUnaryExpression() && expression.node.operator === '~') { + right = expression.get('argument') + } else { + expression.traverse({ + UnaryExpression(unary) { + if (unary.node.operator !== '~') { return } + if (expression.node.loc.start.index !== unary.node.loc.start.index) { return } + unary.stop() + unary.replaceWith(unary.get('argument').node) + right = expression + } + }) + } + debugger + if (!right) { return [] } + + const left = path.get('left') + if (!left.isLVal()) { + throw left.buildCodeFrameError("Unassignable left-hand side of data binding") + } + + return [left, right] + } + + program.traverse({ + ExpressionStatement(expressionStatementPath) { + const path = expressionStatementPath.get('expression'); + const [left, right] = leftRightOfSquigglyArrow(path); + if (!left || !right) { + // path.replaceWith(t.numberLiteral(123)) + return + } + + const valueName = right.scope.generateUidIdentifier('value') + const bindingTemplate = template(`aexpr(() => EXPRESSION) +.dataflow(${valueName.name} => REFERENCE = ${valueName.name})`) + path.replaceWith(bindingTemplate({ + REFERENCE: left.node, + EXPRESSION: right.node, + })) + } + }) + } + } + } +} + + + + + + + + + + + +// const valueName = right.scope.generateUidIdentifier('value') +// const bindingTemplate = template(`aexpr(() => EXPRESSION) +// .dataflow(${valueName.name} => REFERENCE = ${valueName.name})`) +// path.replaceWith(bindingTemplate({ +// REFERENCE: left.node, +// EXPRESSION: right.node, +// })) diff --git a/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.workspace b/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.workspace new file mode 100644 index 000000000..58f901ab8 --- /dev/null +++ b/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.workspace @@ -0,0 +1,23 @@ +{ + "source": "/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings-example.js", + "sources": [ + "/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings-example.js" + ], + "options": { + "systemJS": false, + "autoExecute": true, + "autoRunTests": false, + "autoUpdateAST": true, + "autoUpdateTransformation": true, + "autoSaveWorkspace": true + }, + "plugin": "src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.js", + "openPlugins": [ + "src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.js" + ], + "pluginSelection": [ + { + "url": "src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.js" + } + ] +} \ No newline at end of file diff --git a/src/components/tools/lively-plugin-explorer-playground.workspace b/src/components/tools/lively-plugin-explorer-playground.workspace index 50236222e..15510d8e9 100644 --- a/src/components/tools/lively-plugin-explorer-playground.workspace +++ b/src/components/tools/lively-plugin-explorer-playground.workspace @@ -3,7 +3,7 @@ "sources": [ "/demos/stefan/aexpr-diss-inter-tagger-reliability/aexpr-diss-signals-aexpr-input.js" ], - "plugin": "src/external/babel-plugin-doit-result.js", + "plugin": "src/client/reactive/babel-plugin-active-expression-rewriting/index.js", "options": { "autoUpdateAST": true, "autoUpdateTransformation": true, @@ -14,10 +14,10 @@ }, "pluginSelection": [ { - "url": "src/external/babel-plugin-doit-result.js" + "url": "src/client/reactive/babel-plugin-active-expression-rewriting/index.js" } ], "openPlugins": [ - "src/external/babel-plugin-doit-result.js" + "src/client/reactive/babel-plugin-active-expression-rewriting/index.js" ] } \ No newline at end of file diff --git a/src/components/tools/lively-plugin-explorer.js b/src/components/tools/lively-plugin-explorer.js index 276627cb9..fc6406ecd 100644 --- a/src/components/tools/lively-plugin-explorer.js +++ b/src/components/tools/lively-plugin-explorer.js @@ -14,9 +14,15 @@ import files from "src/client/files.js" export default class PluginExplorer extends Morph { - static get defaultPluginURL() { return lively4url + "/src/components/tools/lively-ast-explorer-example-plugin.js"; } + static get defaultPluginURL() { + // return lively4url + "/src/components/tools/lively-ast-explorer-example-plugin.js"; + return lively4url + 'src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.js' + } - static get defaultWorkspacePath() { return "/src/components/tools/lively-plugin-explorer-playground.workspace"; } + static get defaultWorkspacePath() { + // return "/src/components/tools/lively-plugin-explorer-playground.workspace"; + return '/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.workspace'; + } /*MD ## UI Accessing MD*/ diff --git a/src/plugin-babel.js b/src/plugin-babel.js index c1e86969b..969e611ce 100644 --- a/src/plugin-babel.js +++ b/src/plugin-babel.js @@ -317,6 +317,9 @@ async function basePlugins() { async function livelyPlugins() { return [ + [await importDefaultOf('babel-plugin-sample-data-bindings'), { + executedIn: "file" + }], [await importDefaultOf('babel-plugin-active-expression-rewriting'), { executedIn: "file" }], @@ -418,6 +421,9 @@ async function aexprViaDirectivePlugins(options = {}) { [await importDefaultOf('babel-plugin-ILA'), { executedIn: 'file' }], + [await importDefaultOf('babel-plugin-sample-data-bindings'), { + executedIn: 'file' + }], [await importDefaultOf('babel-plugin-databindings'), { executedIn: 'file' }], @@ -494,6 +500,9 @@ async function workspacePlugins(options = {}) { result.push([await importDefaultOf('babel-plugin-ILA'), { executedIn: 'file' }]) + result.push([await importDefaultOf('babel-plugin-sample-data-bindings'), { + executedIn: 'file' + }]) result.push([await importDefaultOf('babel-plugin-databindings'), { executedIn: 'file' }]) diff --git a/src/systemjs-config.js b/src/systemjs-config.js index 7eba8d552..b085c72c4 100644 --- a/src/systemjs-config.js +++ b/src/systemjs-config.js @@ -443,6 +443,7 @@ System.config({ 'active-expression-proxies': lively4url + '/src/client/reactive/active-expression-proxies/active-expression-proxies.js', 'babel-plugin-active-expression-rewriting': lively4url + '/src/client/reactive/babel-plugin-active-expression-rewriting/index.js', 'babel-plugin-ILA': lively4url + '/src/client/reactive/babel-plugin-ILA/index.js', + 'babel-plugin-sample-data-bindings': lively4url + '/src/client/reactive/babel-plugin-sample-data-bindings/sample-data-bindings.js', 'babel-plugin-databindings': lively4url + '/src/client/reactive/babel-plugin-databindings/index.js', 'babel-plugin-databindings-post-process': lively4url + '/src/client/reactive/babel-plugin-databindings/post-process.js', 'babel-plugin-active-expression-proxies': lively4url + '/src/client/reactive/babel-plugin-active-expression-proxies/index.js',