Skip to content

Commit

Permalink
fix: issue rendering attrs & return after a get
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Oct 28, 2021
1 parent 2907aa2 commit 907ab23
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 7 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Hello world
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Hello world
</div>
5 changes: 5 additions & 0 deletions src/__tests__/fixtures/attrs/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ describe(
fixture("./templates/basic.marko", { value: "World" })
);

describe(
"<attrs> after get",
fixture("./templates/after-get/index.marko", { value: "World" })
);

describe(
"<attrs> error args",
fixture("./templates/error-args.marko", { value: "World" })
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<set="Hello">
<${input.renderBody}/>
</set>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<get/data="provider"/>
<attrs/{ value }/>
<div>${data} ${value}</div>
3 changes: 3 additions & 0 deletions src/__tests__/fixtures/attrs/templates/after-get/index.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<provider>
<receiver value="world"/>
</provider>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello world
2 changes: 2 additions & 0 deletions src/__tests__/fixtures/return/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import fixture, { FixtureHelpers } from "../../fixture";

describe("<return> basic", fixture("./templates/basic.marko"));

describe("<return> after get", fixture("./templates/after-get/index.marko"));

describe("<return> spread", fixture("./templates/spread.marko"));

describe("<return> read multiple", fixture("./templates/read-multiple.marko"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<set="world">
<${input.renderBody}/>
</set>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<get/data="provider"/>
<return=data/>

4 changes: 4 additions & 0 deletions src/__tests__/fixtures/return/templates/after-get/index.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<provider>
<receiver/value/>
Hello ${value}
</provider>
3 changes: 2 additions & 1 deletion src/components/attrs/translate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { types as t } from "@marko/compiler";
import deepFreeze from "../../util/deep-freeze/transform";
import isAtRoot from "../../util/is-at-root";
const usedTag = new WeakSet<t.Hub>();

export = (tag: t.NodePath<t.MarkoTag>) => {
Expand All @@ -8,7 +9,7 @@ export = (tag: t.NodePath<t.MarkoTag>) => {
? "can only be used once within a template"
: !tagVar
? "requires a tag variable to be assigned to"
: !tag.parentPath.parentPath!.isProgram()
: !isAtRoot(tag)
? "can only used at the root of the template"
: tag.node.attributes.length > 0
? "does not support attributes"
Expand Down
3 changes: 2 additions & 1 deletion src/components/return/translate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { types as t } from "@marko/compiler";
import isAtRoot from "../../util/is-at-root";
const usedTag = new WeakSet<t.Hub>();

export = (tag: t.NodePath<t.MarkoTag>) => {
const errorMessage = usedTag.has(tag.hub)
? "can only be used once within a template"
: tag.node.var
? "does not support a tag variable"
: !tag.parentPath.parentPath!.isProgram()
: !isAtRoot(tag)
? "can only used at the root of the template"
: !tag.node.attributes.length
? "requires a default attribute"
Expand Down
9 changes: 9 additions & 0 deletions src/util/is-at-root.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { types as t } from "@marko/compiler";
import isCoreTag from "./is-core-tag";

export default function isAtRoot(tag: t.NodePath<t.MarkoTag>) {
const parentPath = tag.parentPath.parentPath!;
// Special case `<get>` since it currently wraps it's children
// which is an implementation detail.
return parentPath!.isProgram() || isCoreTag("get", parentPath);
}

0 comments on commit 907ab23

Please sign in to comment.