Skip to content

Commit

Permalink
fix: migrate tag arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Aug 18, 2023
1 parent 705598e commit 33b1d0d
Show file tree
Hide file tree
Showing 18 changed files with 2,419 additions and 1,323 deletions.
6 changes: 6 additions & 0 deletions .changeset/slimy-jokes-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@marko/compat-utils": patch
"@marko/compat-v4": patch
---

Migrate tags with arguments to regular attributes or spreads.
3,579 changes: 2,257 additions & 1,322 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions packages/compat-utils/src/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ export function willMigrateTag(
}
}

export function isCoreOrMigrateTag(
tag: t.NodePath<t.MarkoTag>,
): tag is t.NodePath<t.MarkoTag & { name: t.StringLiteral }> {
const taglibId = getTagDef(tag)?.taglibId;
return taglibId ? /^@?marko[-/]/.test(taglibId) : false;
}

export function willMigrateAllAttrs(tag: t.NodePath<t.MarkoTag>) {
if (willMigrateTag(tag)) {
switch (tag.node.name.value) {
Expand Down
2 changes: 2 additions & 0 deletions packages/compat-v4/src/migrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import legacyAttributeTags from "./legacy-attribute-tags";
import keyModifier from "./key-modifier";
import refAttribute from "./ref-attribute";
import nativeTagNamespacedAttribute from "./native-tag-namespaced-attribute";
import tagArguments from "./tag-arguments";

export default [
ifDirective,
Expand All @@ -23,4 +24,5 @@ export default [
bodyOnlyIfDirective,
nonStandardTemplateLiterals,
nativeTagNamespacedAttribute,
tagArguments,
] satisfies Visitor[];
37 changes: 37 additions & 0 deletions packages/compat-v4/src/migrate/tag-arguments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { types as t } from "@marko/compiler";
import { diagnosticDeprecate } from "@marko/babel-utils";
import {
exprToAttrs,
getTagFile,
isCoreOrMigrateTag,
} from "@marko/compat-utils";

export default {
MarkoTag(tag) {
const args = tag.node.arguments;
if (!args || isCoreOrMigrateTag(tag) || !getTagFile(tag)) {
return;
}

diagnosticDeprecate(tag, {
label:
"Tag arguments have been deprecated. Instead use regular attributes, or spread attributes.",
fix() {
const attrs: t.MarkoTag["attributes"] = [];
for (const arg of args) {
if (arg.type === "SpreadElement") {
attrs.push(t.markoSpreadAttribute(arg.argument));
continue;
}

for (const attr of exprToAttrs(arg)) {
attrs.push(attr);
}
}

tag.node.arguments = null;
tag.node.attributes = attrs.concat(tag.node.attributes);
},
});
},
} satisfies t.Visitor;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${JSON.stringify(input)}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<my-tag x=1/>
<my-tag x=1 y=2 x=3/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Render {"attrs":{"data-foo":"bar","data-bar":"baz"}}
```html
{"x":1}{"x":3,"y":2}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { t as _t } from "marko/src/runtime/vdom/index.js";
const _marko_componentType = "<fixture-dir>/components/my-tag.marko",
_marko_template = _t(_marko_componentType);
export default _marko_template;
import _marko_renderer from "marko/src/runtime/components/renderer.js";
import { r as _marko_registerComponent } from "marko/src/runtime/components/registry";
_marko_registerComponent(_marko_componentType, () => _marko_template);
const _marko_component = {};
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {
out.t(JSON.stringify(input), _component);
}, {
t: _marko_componentType,
i: true,
d: true
}, _marko_component);
import _marko_defineComponent from "marko/src/runtime/components/defineComponent.js";
_marko_template.Component = _marko_defineComponent(_marko_component, _marko_template._);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { t as _t } from "marko/src/runtime/vdom/index.js";
const _marko_componentType = "<fixture-dir>/template.marko",
_marko_template = _t(_marko_componentType);
export default _marko_template;
import _myTag from "./components/my-tag.marko";
import _marko_tag from "marko/src/runtime/helpers/render-tag.js";
import _marko_renderer from "marko/src/runtime/components/renderer.js";
import { r as _marko_registerComponent } from "marko/src/runtime/components/registry";
_marko_registerComponent(_marko_componentType, () => _marko_template);
const _marko_component = {};
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {
_marko_tag(_myTag, {
"x": 1
}, out, _componentDef, "0");
_marko_tag(_myTag, {
"x": 1,
"y": 2,
"x": 3
}, out, _componentDef, "1");
}, {
t: _marko_componentType,
i: true,
d: true
}, _marko_component);
import _marko_defineComponent from "marko/src/runtime/components/defineComponent.js";
_marko_template.Component = _marko_defineComponent(_marko_component, _marko_template._);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Write
{"x":1}{"x":3,"y":2}

# Render {"attrs":{"data-foo":"bar","data-bar":"baz"}}
```html
{"x":1}{"x":3,"y":2}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { t as _t } from "marko/src/runtime/html/index.js";
const _marko_componentType = "<fixture-dir>/components/my-tag.marko",
_marko_template = _t(_marko_componentType);
export default _marko_template;
import { x as _marko_escapeXml } from "marko/src/runtime/html/helpers/escape-xml.js";
import _marko_renderer from "marko/src/runtime/components/renderer.js";
const _marko_component = {};
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {
out.w(_marko_escapeXml(JSON.stringify(input)));
}, {
t: _marko_componentType,
i: true,
d: true
}, _marko_component);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { t as _t } from "marko/src/runtime/html/index.js";
const _marko_componentType = "<fixture-dir>/template.marko",
_marko_template = _t(_marko_componentType);
export default _marko_template;
import _myTag from "./components/my-tag.marko";
import _marko_tag from "marko/src/runtime/helpers/render-tag.js";
import _marko_renderer from "marko/src/runtime/components/renderer.js";
const _marko_component = {};
_marko_template._ = _marko_renderer(function (input, out, _componentDef, _component, state, $global) {
_marko_tag(_myTag, {
"x": 1
}, out, _componentDef, "0");
_marko_tag(_myTag, {
"x": 1,
"y": 2,
"x": 3
}, out, _componentDef, "1");
}, {
t: _marko_componentType,
i: true,
d: true
}, _marko_component);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Render {"attrs":{"data-foo":"bar","data-bar":"baz"}}
```html
{"x":1}{"x":3,"y":2}
```
1 change: 1 addition & 0 deletions tests/fixtures-class/tag-arguments/components/my-tag.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- ${JSON.stringify(input)}
2 changes: 2 additions & 0 deletions tests/fixtures-class/tag-arguments/template.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<my-tag({ x: 1 })/>
<my-tag({ x: 1 }, { y: 2 }, { x: 3 })/>
10 changes: 10 additions & 0 deletions tests/fixtures-class/tag-arguments/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { FixtureConfig } from "../../main.test";

export default {
input: {
attrs: {
"data-foo": "bar",
"data-bar": "baz",
},
},
} satisfies FixtureConfig;
1 change: 0 additions & 1 deletion tests/old/render/fixtures/invoke/template.marko
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script template-helpers>
function greeting(name, out) {
debugger;
out.write('Hello ' + name + '!');
}
</script>
Expand Down

0 comments on commit 33b1d0d

Please sign in to comment.