Skip to content

Commit

Permalink
feat: omitting default attribute on get is now out.global
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Feb 18, 2022
1 parent 89ed1f5 commit 24867d4
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default (
expect: chai.expect,
...helpers,
fireEvent,
...(await helpers.render(template, input)),
...(await helpers.render(template, { ...input })),
} as FixtureHelpers;

await snapshot("html", fixtureHelpers.container);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
src/__tests__/fixtures/get-and-set/templates/error-get-args.marko(2,4): The <get> tag 'default' attribute is required.
src/__tests__/fixtures/get-and-set/templates/error-get-args.marko(2,4): The <get> tag requires a tag variable.
1 | <set=1>
> 2 | <get(y)/x="."/>
| ^^^
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
src/__tests__/fixtures/get-and-set/templates/error-get-args.marko(2,4): The <get> tag 'default' attribute is required.
src/__tests__/fixtures/get-and-set/templates/error-get-args.marko(2,4): The <get> tag requires a tag variable.
1 | <set=1>
> 2 | <get(y)/x="."/>
| ^^^
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
hello
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
hello
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
src/__tests__/fixtures/get-and-set/templates/error-assign-global-context.marko(3,3): Cannot mutate the global context.
1 | <get/{ message }/>
2 | <button onClick() {
> 3 | message = "World";
| ^^^^^^^^^^^^^^^^^
4 | }>${message}</button>
5 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
src/__tests__/fixtures/get-and-set/templates/error-assign-global-context.marko(3,3): Cannot mutate the global context.
1 | <get/{ message }/>
2 | <button onClick() {
> 3 | message = "World";
| ^^^^^^^^^^^^^^^^^
4 | }>${message}</button>
5 |
15 changes: 11 additions & 4 deletions src/__tests__/fixtures/get-and-set/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ describe(
fixture("./templates/provide-across-children/index.marko")
);

describe(
"<get> global context",
fixture("./templates/get-global-context.marko", {
$global: { message: "hello" },
})
);

describe("error <get> args", fixture("./templates/error-get-args.marko"));

describe(
Expand All @@ -111,13 +118,13 @@ describe(
);

describe(
"error <get> missing tag",
fixture("./templates/error-get-missing-tag.marko")
"<get> mutate global context",
fixture("./templates/error-assign-global-context.marko")
);

describe(
"error <get> no default attr",
fixture("./templates/error-get-no-default-attr.marko")
"error <get> missing tag",
fixture("./templates/error-get-missing-tag.marko")
);

describe(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<get/{ message }/>
<button onClick() {
message = "World";
}>${message}</button>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<get/{ message }/>
<div>${message}</div>
48 changes: 35 additions & 13 deletions src/components/get/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,46 @@ import getAttr from "../../util/get-attr";
export = function transform(tag: t.NodePath<t.MarkoTag>) {
const file = tag.hub.file;
const defaultAttr = getAttr(tag, "default")!;
const errorMessage = !defaultAttr
? "'default' attribute is required"
: tag.node.attributes.length > 1
? "only supports the 'default' attribute"
: !tag.node.var
? "requires a tag variable"
: tag.node.arguments
? "does not support arguments"
: tag.node.body.params.length
? "does not support tag body parameters"
: tag.node.body.body.length
? "does not support body content"
: undefined;
const errorMessage =
defaultAttr && tag.node.attributes.length > 1
? "only supports the 'default' attribute"
: !tag.node.var
? "requires a tag variable"
: tag.node.arguments
? "does not support arguments"
: tag.node.body.params.length
? "does not support tag body parameters"
: tag.node.body.body.length
? "does not support body content"
: undefined;

if (errorMessage) {
throw tag.get("name").buildCodeFrameError(`The <get> tag ${errorMessage}.`);
}

if (!defaultAttr) {
for (const name in tag.get("var").getBindingIdentifiers()) {
for (const violation of tag.scope.getOwnBinding(name)!
.constantViolations) {
throw violation.buildCodeFrameError(
"Cannot mutate the global context."
);
}
}

tag.replaceWith(
t.markoScriptlet([
t.variableDeclaration("const", [
t.variableDeclarator(
tag.node.var!,
t.memberExpression(t.identifier("out"), t.identifier("global"))
),
]),
])
);
return;
}

let fromValue = defaultAttr.node.value;

if (t.isStringLiteral(fromValue)) {
Expand Down

0 comments on commit 24867d4

Please sign in to comment.