Skip to content

Commit 68d673a

Browse files
committed
[pi] finished first take on polymorphic identifiers
SQUASHED: AUTO-COMMIT-src-client-reactive-babel-plugin-active-expression-rewriting-plugin-utils.js,AUTO-COMMIT-src-client-reactive-babel-plugin-polymorphic-identifiers-index.js,AUTO-COMMIT-src-client-reactive-babel-plugin-polymorphic-identifiers-playground.js,AUTO-COMMIT-src-client-reactive-babel-plugin-polymorphic-identifiers-plugin-explorer.workspace,AUTO-COMMIT-src-client-reactive-babel-plugin-polymorphic-identifiers-polymorphic-identifiers.js,AUTO-COMMIT-src-client-reactive-polymorphic-identifiers-polymorphic-identifiers.js,AUTO-COMMIT-src-client-reactive-test-polymorphic-identifiers-polymorphic-identifiers.spec.js,AUTO-COMMIT-src-components-tools-plugin-selector-list.json,AUTO-COMMIT-src-systemjs-config.js,
1 parent ccdfc43 commit 68d673a

File tree

8 files changed

+109
-13
lines changed

8 files changed

+109
-13
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
here we go....
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"source":"/src/client/reactive/babel-plugin-polymorphic-identifiers/playground.js","plugin":"https://lively-kernel.org/lively4/aexpr/src/client/reactive/babel-plugin-polymorphic-identifiers/polymorphic-identifiers.js","options":{"autoUpdateAST":true,"autoUpdateTransformation":false,"autoExecute":true,"systemJS":false,"autoRunTests":false,"autoSaveWorkspace":false},"pluginSelection":[{"url":"https://lively-kernel.org/lively4/aexpr/src/client/reactive/babel-plugin-polymorphic-identifiers/polymorphic-identifiers.js"}]}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
const SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER = 'SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER';
3+
4+
export default function ({ types: t, template }) {
5+
return {
6+
name: 'polymorphic-identifiers',
7+
visitor: {
8+
Program: {
9+
enter(path, state) {
10+
function addCustomTemplate(file, name) {
11+
const declar = file.declarations[name];
12+
if (declar) {
13+
return declar;
14+
}
15+
16+
const identifier = file.declarations[name] = file.addImport("polymorphic-identifiers", name, name);
17+
identifier[SHARED_FLAG_GENERATED_IMPORT_IDENTIFIER] = true;
18+
return identifier;
19+
}
20+
21+
function hasDirective(path, name) {
22+
let foundDirective = false;
23+
path.traverse({
24+
Directive(path) {
25+
if (path.get("value").node.value === name) {
26+
foundDirective = true;
27+
}
28+
}
29+
});
30+
return foundDirective;
31+
}
32+
33+
debugger
34+
const shouldTransform = state.opts.executedIn === 'workspace' || hasDirective(path, "pi");
35+
if (!shouldTransform) {
36+
return;
37+
}
38+
39+
path.traverse({
40+
TaggedTemplateExpression(path) {
41+
if (path.node.visitedByPI) {
42+
return;
43+
}
44+
path.node.visitedByPI = true;
45+
46+
const tagTemplate = template(`MAKE_REF(TAG_NODE, {
47+
thisReference: this,
48+
evalFunction: str => eval(str)
49+
})`);
50+
const tagPath = path.get('tag');
51+
tagPath.replaceWith(tagTemplate({
52+
MAKE_REF: addCustomTemplate(state.file, 'makeRef'),
53+
TAG_NODE: tagPath.node
54+
}));
55+
56+
path.replaceWith(t.memberExpression(path.node, t.identifier('access')));
57+
58+
const parentPath = path.parentPath;
59+
if (parentPath.isBinaryExpression() && path.parentKey === 'left' && parentPath.node.operator === "<<") {
60+
parentPath.replaceWith(parentPath.node.right);
61+
62+
// find something we can embed an assignment expression in
63+
const preStatementAncestor = parentPath.find(p => {
64+
const parent = p.parentPath;
65+
return parent && (parent.isStatement() || (parent.isArrowFunctionExpression() && p.parentKey === "body"))
66+
});
67+
preStatementAncestor.replaceWith(t.assignmentExpression('=', path.node, preStatementAncestor.node));
68+
}
69+
}
70+
});
71+
}
72+
}
73+
}
74+
};
75+
}

src/client/reactive/polymorphic-identifiers/polymorphic-identifiers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
32
export class PIReference {
43
static get isPIReference() { return true; }
54

@@ -8,7 +7,8 @@ export class PIReference {
87
}
98

109
create(strings, ...expressions) {
11-
// debugger
10+
this.strings = strings;
11+
this.expressions = expressions;
1212
}
1313

1414
get access() {

src/client/reactive/test/polymorphic-identifiers/polymorphic-identifiers.spec.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,19 @@ describe("PI", function() {
117117
})``.access).to.equal('bar')
118118
});
119119

120-
// it("inserts default constructors", () => {
121-
// class A {
122-
// constructor() {
123-
// this.prop = 42;
124-
// }
125-
// }
126-
// class B extends A {}
120+
});
121+
describe("AEs", function() {
122+
123+
it("AExprs insert default constructors", () => {
124+
class A {
125+
constructor(prop) {
126+
this.prop = prop;
127+
}
128+
}
129+
class B extends A {}
127130

128-
// const b = new B();
129-
// expect(b.prop).to.equal(42);
130-
// });
131+
const b = new B(42);
132+
expect(b.prop).to.equal(42);
133+
});
131134

132135
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["https://lively-kernel.org/lively4/lively4-tom/demos/tom/babel-plugin-tracer.js","https://lively-kernel.org/lively4/lively4-tom/demos/tom/defect-demo-plugin.js","https://lively-kernel.org/lively4/lively4-tom/demos/tom/enumerationPlugin.js","https://lively-kernel.org/lively4/lively4-tom/demos/tom/playground.js","https://lively-kernel.org/lively4/lively4-tom/demos/tom/plugin-backup.js","https://lively-kernel.org/lively4/lively4-tom/src/client/reactive/babel-plugin-active-expression-rewriting/index.js","https://lively-kernel.org/lively4/lively4-tom/src/external/babel-plugin-locals.js","https://lively-kernel.org/lively4/lively4-tom/src/external/babel-plugin-var-recorder-dev.js"]
1+
["https://lively-kernel.org/lively4/aexpr/src/client/reactive/babel-plugin-polymorphic-identifiers/index.js","https://lively-kernel.org/lively4/aexpr/src/client/reactive/babel-plugin-polymorphic-identifiers/polymorphic-identifiers.js","https://lively-kernel.org/lively4/lively4-tom/demos/tom/babel-plugin-tracer.js","https://lively-kernel.org/lively4/lively4-tom/demos/tom/defect-demo-plugin.js","https://lively-kernel.org/lively4/lively4-tom/demos/tom/enumerationPlugin.js","https://lively-kernel.org/lively4/lively4-tom/demos/tom/playground.js","https://lively-kernel.org/lively4/lively4-tom/demos/tom/plugin-backup.js","https://lively-kernel.org/lively4/lively4-tom/src/client/reactive/babel-plugin-active-expression-rewriting/index.js","https://lively-kernel.org/lively4/lively4-tom/src/external/babel-plugin-locals.js","https://lively-kernel.org/lively4/lively4-tom/src/external/babel-plugin-var-recorder-dev.js"]

src/systemjs-config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ SystemJS.config({
9090
'babel-plugin-var-recorder-dev': lively4url + '/src/external/babel-plugin-var-recorder-dev.js',
9191
'workspace-loader': lively4url + '/src/client/workspace-loader.js',
9292

93+
// support for polymorphic identifiers
94+
'babel-plugin-polymorphic-identifiers': lively4url + '/src/client/reactive/babel-plugin-polymorphic-identifiers/polymorphic-identifiers.js',
95+
'polymorphic-identifiers': lively4url + '/src/client/reactive/polymorphic-identifiers/polymorphic-identifiers.js',
96+
9397
// utils
9498
'lang': lively4url + '/src/client/lang/lang.js',
9599
'lang-ext': lively4url + '/src/client/lang/lang-ext.js',
@@ -136,6 +140,7 @@ const aexprViaDirective = {
136140
stage2: false,
137141
stage3: false,
138142
plugins: [
143+
'babel-plugin-polymorphic-identifiers',
139144
['babel-plugin-rp19-jsx', {
140145
executedIn: 'file'
141146
}],
@@ -215,6 +220,9 @@ SystemJS.config({
215220
stage3: false,
216221
plugins: [
217222
// lively4url + '/demos/swe/debugging-plugin.js',
223+
['babel-plugin-polymorphic-identifiers', {
224+
executedIn: 'workspace'
225+
}],
218226
['babel-plugin-rp19-jsx', {
219227
executedIn: 'workspace'
220228
}],
@@ -247,6 +255,9 @@ SystemJS.config({
247255
stage2: false,
248256
stage3: false,
249257
plugins: [
258+
['babel-plugin-polymorphic-identifiers', {
259+
executedIn: 'workspace'
260+
}],
250261
['babel-plugin-rp19-jsx', {
251262
executedIn: 'workspace'
252263
}],
@@ -270,6 +281,9 @@ SystemJS.config({
270281
stage2: false,
271282
stage3: false,
272283
plugins: [
284+
['babel-plugin-polymorphic-identifiers', {
285+
executedIn: 'workspace'
286+
}],
273287
['babel-plugin-rp19-jsx', {
274288
executedIn: 'workspace'
275289
}],

0 commit comments

Comments
 (0)