Skip to content

Commit

Permalink
wip: change default attr name
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Nov 2, 2022
1 parent 2a87db7 commit 1fbb304
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/components/const/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import assertNoAssignments from "../../util/assert-no-assignments";

export = (tag: t.NodePath<t.MarkoTag>) => {
const tagVar = tag.node.var!;
const defaultAttr = getAttr(tag, "default")!;
const valueAttr = getAttr(tag, "value")!;
const errorMessage = !tagVar
? "requires a tag variable to be assigned to"
: !defaultAttr
: !valueAttr
? "must be initialized with a value"
: tag.node.attributes.length > 1
? "only supports the 'default' attribute"
? "only supports the 'value' attribute"
: tag.node.body.body.length
? "does not support body content"
: tag.node.body.params.length
Expand All @@ -32,7 +32,7 @@ export = (tag: t.NodePath<t.MarkoTag>) => {
t.variableDeclaration("const", [
t.variableDeclarator(
tagVar,
deepFreeze(tag.hub.file, defaultAttr.node.value)
deepFreeze(tag.hub.file, valueAttr.node.value)
),
])
);
Expand Down
8 changes: 4 additions & 4 deletions src/components/effect/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import getAttr from "../../util/get-attr";
export = function translate(tag: t.NodePath<t.MarkoTag>) {
const { file } = tag.hub;

const defaultAttr = getAttr(tag, "default")!;
const valueAttr = getAttr(tag, "value")!;
const errorMessage = tag.node.var
? "does not support a tag variable"
: !defaultAttr
: !valueAttr
? "must be initialized with a value"
: tag.node.attributes.length > 1
? "only supports the 'default' attribute"
? "only supports the 'value' attribute"
: tag.node.body.body.length
? "does not support body content"
: tag.node.body.params.length
Expand All @@ -37,7 +37,7 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
importRuntimeDefault(file, "components/effect", "effect"),
[
(file as any)._componentInstanceIdentifier,
getAttr(tag, "default")!.node.value,
getAttr(tag, "value")!.node.value,
]
)
)
Expand Down
14 changes: 7 additions & 7 deletions src/components/get/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ 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 valueAttr = getAttr(tag, "value")!;
const errorMessage =
defaultAttr && tag.node.attributes.length > 1
? "only supports the 'default' attribute"
valueAttr && tag.node.attributes.length > 1
? "only supports the 'value' attribute"
: !tag.node.var
? "requires a tag variable"
: tag.node.arguments
Expand All @@ -23,7 +23,7 @@ export = function transform(tag: t.NodePath<t.MarkoTag>) {
throw tag.get("name").buildCodeFrameError(`The <get> tag ${errorMessage}.`);
}

if (!defaultAttr) {
if (!valueAttr) {
for (const name in tag.get("var").getBindingIdentifiers()) {
for (const violation of tag.scope.getOwnBinding(name)!
.constantViolations) {
Expand All @@ -46,7 +46,7 @@ export = function transform(tag: t.NodePath<t.MarkoTag>) {
return;
}

let fromValue = defaultAttr.node.value;
let fromValue = valueAttr.node.value;

if (t.isStringLiteral(fromValue)) {
const literalValue = fromValue.value;
Expand All @@ -64,12 +64,12 @@ export = function transform(tag: t.NodePath<t.MarkoTag>) {
if (fromTag) {
fromValue = importDefault(file, `<${literalValue}>`, "context");
} else {
throw defaultAttr.buildCodeFrameError(
throw valueAttr.buildCodeFrameError(
`<get> could not find provider matching "${literalValue}".`
);
}
}

defaultAttr.set("value", fromValue);
valueAttr.set("value", fromValue);
}
};
8 changes: 4 additions & 4 deletions src/components/if/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ export = function transform(tag: t.NodePath<t.MarkoTag>) {
if (isApi(tag, "class") || seen.has(tag)) return;
seen.add(tag);

const defaultAttr = getAttr(tag, "default")!;
const valueAttr = getAttr(tag, "value")!;
const errorMessage = tag.node.var
? "does not support a tag variable"
: tag.node.arguments?.length
? "does not support arguments, the tags api uses '<if=condition>' instead"
: !defaultAttr
: !valueAttr
? "must be given a value"
: tag.node.attributes.length > 1
? "only supports the 'default' attribute"
? "only supports the 'value' attribute"
: !tag.node.body.body.length
? "requires body content"
: tag.node.body.params.length
Expand All @@ -31,6 +31,6 @@ export = function transform(tag: t.NodePath<t.MarkoTag>) {
);
}

tag.node.arguments = [defaultAttr.node.value];
tag.node.arguments = [valueAttr.node.value];
tag.node.attributes = [];
};
20 changes: 10 additions & 10 deletions src/components/let/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ 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> | undefined;
let valueAttr: t.NodePath<t.MarkoAttribute> | undefined;
let changeAttr: t.NodePath<t.MarkoAttribute> | undefined;
let errorMessage: string | undefined;

for (const attr of tag.get("attributes")) {
if (attr.isMarkoAttribute()) {
switch (attr.node.name) {
case "default":
defaultAttr = attr;
case "value":
valueAttr = attr;
continue;
case "defaultChange":
case "valueChange":
changeAttr = attr;
continue;
}
Expand Down Expand Up @@ -46,16 +46,16 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
}

file.path.scope.crawl();
const defaultValue = defaultAttr
? defaultAttr.node.value
const value = valueAttr
? valueAttr.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, defaultValue)),
t.variableDeclarator(tagVar, deepFreeze(file, value)),
])
);
} else {
Expand All @@ -68,7 +68,7 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
t.assignmentExpression(
"=",
t.memberExpression(meta.state, keyString, true),
deepFreeze(file, defaultValue)
deepFreeze(file, value)
)
);

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

if (t.isFunction(changeAttr.node.value)) {
setFnId = changeFnId;
decls.push(t.variableDeclarator(tagVar, defaultValue));
decls.push(t.variableDeclarator(tagVar, value));
} else {
setFnId = tag.scope.generateUidIdentifier(`${tagVar.name}Set`);
decls.push(
Expand All @@ -103,7 +103,7 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
),
t.variableDeclarator(
tagVar,
t.conditionalExpression(changeFnId, defaultValue, getStateExpr)
t.conditionalExpression(changeFnId, value, getStateExpr)
)
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/components/set/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
for (const attr of tag.get("attributes")) {
if (attr.isMarkoAttribute()) {
switch (attr.node.name) {
case "default":
case "defaultChange":
case "value":
case "valueChange":
continue;
}
}
Expand All @@ -22,7 +22,7 @@ export = function translate(tag: t.NodePath<t.MarkoTag>) {
errorMessage =
errorMessage ||
(!tag.node.attributes.length
? "requires a default attribute"
? "requires a value attribute"
: tag.node.var
? "does not support a tag variable"
: tag.node.arguments?.length
Expand Down
2 changes: 1 addition & 1 deletion src/transform/cached-function/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const depsVisitor = {
const nestedState: DepsVisitorState = state.shallow
? state
: { root: state.root, shallow: true };
getAttr(bindingTag, "default")!.traverse(depsVisitor, nestedState);
getAttr(bindingTag, "value")!.traverse(depsVisitor, nestedState);
isDep = !!nestedState.deps;
} else {
isDep = true;
Expand Down
2 changes: 1 addition & 1 deletion src/transform/custom-tag-var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default {
const { node } = tag;
const tagVar = node.var as t.PatternLike;
const tagVarReplacement = t.objectPattern([
t.objectProperty(t.identifier("default"), tagVar),
t.objectProperty(t.identifier("value"), tagVar),
]);
const meta = closest(tag.parentPath)!;
const returnValueId = tag.scope.generateUidIdentifier(
Expand Down
2 changes: 1 addition & 1 deletion src/transform/feature-detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const featureDetectionVisitor = {
const name = ref.node.name;

if (
(name === "input" || name === "component" || name === "out") &&
(name === "component" || name === "out") &&
!ref.scope.hasBinding(name)
) {
addFeature(state, "class", `${name} template global`, ref);
Expand Down
2 changes: 1 addition & 1 deletion src/transform/tag-body-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
t.assignmentPattern(
t.objectPattern([
t.objectProperty(
t.identifier("default"),
t.identifier("value"),
t.assignmentPattern(
t.arrayPattern(body.node.params as t.PatternLike[]),
t.arrayExpression([])
Expand Down

0 comments on commit 1fbb304

Please sign in to comment.