Skip to content

Commit

Permalink
feat: allow missing default attribute for let tag
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 8, 2022
1 parent c396076 commit e7c77b3
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-boxes-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@marko/tags-api-preview": patch
---

Allow missing default attribute for let tag.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
undefined
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
undefined
</div>
7 changes: 2 additions & 5 deletions src/__tests__/fixtures/let/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ describe(
])
);

describe("<let> no default attr", fixture("./templates/no-default-attr.marko"));

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

describe(
Expand All @@ -147,11 +149,6 @@ describe(
fixture("./templates/error-extra-attr.marko")
);

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

describe(
"<let> error no tag var",
fixture("./templates/error-no-tag-var.marko")
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions src/__tests__/fixtures/let/templates/no-default-attr.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<let/value/>
<div>${typeof value}</div>
19 changes: 8 additions & 11 deletions src/components/let/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
const { file } = tag.hub;
const server = file.markoOpts.output === "html";
const tagVar = tag.node.var as t.Identifier;
let defaultAttr!: t.NodePath<t.MarkoAttribute>;
let defaultAttr: t.NodePath<t.MarkoAttribute> | undefined;
let changeAttr: t.NodePath<t.MarkoAttribute> | undefined;
let errorMessage: string | undefined;

Expand All @@ -33,8 +33,6 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
? "requires a tag variable"
: !t.isIdentifier(tagVar)
? "tag variable cannot be destructured"
: !defaultAttr
? "must be initialized with a value"
: tag.node.body.body.length
? "does not support body content"
: tag.node.body.params.length
Expand All @@ -48,13 +46,16 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
}

file.path.scope.crawl();
const defaultValue = defaultAttr
? defaultAttr.node.value
: t.unaryExpression("void", t.numericLiteral(0));
const binding = tag.scope.getBinding(tagVar.name)!;

if (server) {
file.path.scope.crawl();
tag.replaceWith(
t.variableDeclaration("const", [
t.variableDeclarator(tagVar, deepFreeze(file, defaultAttr.node.value)),
t.variableDeclarator(tagVar, deepFreeze(file, defaultValue)),
])
);
} else {
Expand All @@ -67,7 +68,7 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
t.assignmentExpression(
"=",
t.memberExpression(meta.state, keyString, true),
deepFreeze(file, defaultAttr.node.value)
deepFreeze(file, defaultValue)
)
);

Expand All @@ -82,7 +83,7 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {

if (t.isFunction(changeAttr.node.value)) {
setFnId = changeFnId;
decls.push(t.variableDeclarator(tagVar, defaultAttr.node.value));
decls.push(t.variableDeclarator(tagVar, defaultValue));
} else {
setFnId = tag.scope.generateUidIdentifier(`${tagVar.name}Set`);
decls.push(
Expand All @@ -102,11 +103,7 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
),
t.variableDeclarator(
tagVar,
t.conditionalExpression(
changeFnId,
defaultAttr.node.value,
getStateExpr
)
t.conditionalExpression(changeFnId, defaultValue, getStateExpr)
)
);
}
Expand Down
7 changes: 6 additions & 1 deletion src/util/deep-freeze/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { types as t } from "@marko/compiler";
import { importDefault } from "@marko/babel-utils";

export default (file: t.BabelFile, value: t.Expression) => {
if (file.markoOpts.optimize || t.isLiteral(value)) {
if (
file.markoOpts.optimize ||
t.isLiteral(value) ||
t.isBinaryExpression(value) ||
t.isUnaryExpression(value)
) {
return value;
}

Expand Down

0 comments on commit e7c77b3

Please sign in to comment.