mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
feat: add migrator for top level input identifier
This commit is contained in:
parent
d898941bf7
commit
719dec0d93
@ -0,0 +1,13 @@
|
||||
# Render {"x":1}
|
||||
```html
|
||||
<div>
|
||||
<span>
|
||||
1
|
||||
</span>
|
||||
</div>
|
||||
```
|
||||
|
||||
# Mutations
|
||||
```
|
||||
inserted div0
|
||||
```
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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
|
||||
```
|
||||
|
||||
```
|
||||
@ -0,0 +1,5 @@
|
||||
<div>
|
||||
<span>
|
||||
${input.x}
|
||||
</span>
|
||||
</div>
|
||||
@ -0,0 +1 @@
|
||||
export const steps = [{ x: 1 }];
|
||||
@ -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;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user