feat: add migrator for top level input identifier

This commit is contained in:
Dylan Piercey 2022-04-20 15:10:11 -07:00
parent d898941bf7
commit 719dec0d93
No known key found for this signature in database
GPG Key ID: 8A959D84C57B3CE5
7 changed files with 134 additions and 19 deletions

View File

@ -0,0 +1,13 @@
# Render {"x":1}
```html
<div>
<span>
1
</span>
</div>
```
# Mutations
```
inserted div0
```

View File

@ -0,0 +1,16 @@
import { data as _data, write as _write, createRenderFn as _createRenderFn } from "@marko/runtime-fluurt/src/dom";
function _apply_input(_scope, input) {
if (_write(_scope, 1, input)) _data(_scope[0], input.x);
}
export const applyAttrs = function (_scope, input) {
_apply_input(_scope, input);
};
export { _apply_input };
export const template = "<div><span> </span></div>";
export const walks =
/* next(2), get, out(2) */
"E m";
export const apply = function () {};
export default _createRenderFn(template, walks, apply, applyAttrs);

View File

@ -0,0 +1,10 @@
import { markHydrateNode as _markHydrateNode, escapeXML as _escapeXML, write as _write, nextScopeId as _nextScopeId, createRenderer as _createRenderer } from "@marko/runtime-fluurt/src/html";
const _renderer = input => {
const _scope = _nextScopeId();
_write(`<div><span>${_markHydrateNode(_scope, 0)}${_escapeXML(input.x)}</span></div>`);
};
export default _renderer;
export const render = _createRenderer(_renderer);

View File

@ -0,0 +1,50 @@
# Write
<div><span><!M#0 0>1</span></div>
# Render "End"
```html
<html>
<head />
<body>
<div>
<span>
<!--M#0 0-->
1
</span>
</div>
</body>
</html>
```
# Mutations
```
inserted html0
inserted html0/head0
inserted html0/body1
inserted html0/body1/div0
inserted html0/body1/div0/span0
inserted html0/body1/div0/span0/#comment0
inserted html0/body1/div0/span0/#text1
```
# Render "Hydrate"
```html
<html>
<head />
<body>
<div>
<span>
<!--M#0 0-->
1
</span>
</div>
</body>
</html>
```
# Mutations
```
```

View File

@ -0,0 +1,5 @@
<div>
<span>
${input.x}
</span>
</div>

View File

@ -0,0 +1 @@
export const steps = [{ x: 1 }];

View File

@ -3,37 +3,57 @@ import isStatic from "../util/is-static";
import { currentProgramPath } from "./program";
const outGlobalIdentifiers = new WeakMap<t.NodePath<t.Program>, t.Identifier>();
const hasAttrsTag = new WeakSet<t.NodePath<t.Program>>();
export default {
migrate(identifier: t.NodePath<t.Identifier>) {
if (identifier.node.name === "out" && !identifier.scope.hasBinding("out")) {
if (
t.isMemberExpression(identifier.parent) &&
t.isIdentifier(identifier.parent.property) &&
identifier.parent.property.name === "global"
) {
let globalIdentifier = outGlobalIdentifiers.get(currentProgramPath);
if (!globalIdentifier) {
globalIdentifier =
currentProgramPath.scope.generateUidIdentifier("$global");
outGlobalIdentifiers.set(currentProgramPath, globalIdentifier);
const { name } = identifier.node;
if (identifier.scope.hasBinding(name)) return;
switch (identifier.node.name) {
case "input": {
if (!hasAttrsTag.has(currentProgramPath)) {
hasAttrsTag.add(currentProgramPath);
insertAfterStatic(
t.markoTag(
t.stringLiteral("get"),
t.stringLiteral("attrs"),
undefined,
t.markoTagBody(),
undefined,
globalIdentifier
identifier.node
)
);
}
identifier.parentPath.replaceWith(globalIdentifier);
} else {
throw identifier.buildCodeFrameError(
"Only out.global is supported for compatibility."
);
break;
}
case "out":
if (
t.isMemberExpression(identifier.parent) &&
t.isIdentifier(identifier.parent.property) &&
identifier.parent.property.name === "global"
) {
let globalIdentifier = outGlobalIdentifiers.get(currentProgramPath);
if (!globalIdentifier) {
globalIdentifier =
currentProgramPath.scope.generateUidIdentifier("$global");
outGlobalIdentifiers.set(currentProgramPath, globalIdentifier);
insertAfterStatic(
t.markoTag(
t.stringLiteral("get"),
undefined,
t.markoTagBody(),
undefined,
globalIdentifier
)
);
}
identifier.parentPath.replaceWith(globalIdentifier);
} else {
throw identifier.buildCodeFrameError(
"Only out.global is supported for compatibility."
);
}
break;
}
},
};