Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculator.make(fn) should work for all functions, not just 0-paramater ones #2694

Merged
merged 12 commits into from
Dec 21, 2023
6 changes: 3 additions & 3 deletions packages/components/src/components/ui/FnDocumentation.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { clsx } from "clsx";
import React, { FC, PropsWithChildren } from "react";
import { FC, PropsWithChildren } from "react";
import ReactMarkdown from "react-markdown";

import {
Expand All @@ -23,14 +23,14 @@ const StyleDefinition: FC<{ fullName: string; def: FnDefinition }> = ({
const secondaryColor = "text-slate-400";
const inputs = def.inputs.map((t, index) => (
<span key={index}>
<span className={primaryColor}>{t.getName()}</span>
<span className={primaryColor}>{t.display()}</span>
{isOptional(t) ? <span className={primaryColor}>?</span> : ""}
{index !== def.inputs.length - 1 && (
<span className={secondaryColor}>, </span>
)}
</span>
));
const output = def.output.getName();
const output = def.output.display();
return (
<div>
<span className="text-slate-500">{fullName}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ export const CalculatorInput: FC<{
result: SqValueResult | undefined;
settings: PlaygroundSettings;
}> = ({ id, input, result, settings }) => {
const { name, description } = input;
const { name, typeName, description } = input;

const newDescription = [typeName, description].filter((e) => e).join("\n\n");
// common props for all *FormField components
const commonProps = {
name: `inputs.${id}` as const,
label: name,
description,
description: newDescription,
};

return (
Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/widgets/LambdaWidget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ widgetRegistry.register("Lambda", {
const environment = value.context.project.getEnvironment();
//It's kind of awkward that the documentation isn't connected to the function itself, but that's a greater effort.
if (value.value.type === "BuiltinLambda") {
const name = value.value._value.getName();
const name = value.value._value.display();
const documentation = getFunctionDocumentation(name);
if (documentation) {
return <FnDocumentation documentation={documentation} />;
Expand Down
16 changes: 8 additions & 8 deletions packages/squiggle-lang/__tests__/reducer/frTypes_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ describe("frOr", () => {
});
});

describe("getName", () => {
describe("display", () => {
test("should return the correct name", () => {
expect(frNumberOrString.getName()).toBe("Number|String");
expect(frNumberOrString.display()).toBe("Number|String");
});
});
});
Expand All @@ -350,18 +350,18 @@ describe("frNamed", () => {
expect(namedNumberType.pack(testNumber)).toEqual(testValue);
});

test("getName", () => {
test("display", () => {
expect(namedNumberType).toBeDefined();
expect(namedNumberType.getName()).toBe("TestNumber: Number");
expect(namedNumberType.display()).toBe("TestNumber: Number");
});

test("getName with Optional Type", () => {
test("display with Optional Type", () => {
const optionalNumberType = frOptional(frNumber);
const namedOptionalNumberType = frNamed(
"OptionalTestNumber",
optionalNumberType
);
expect(namedOptionalNumberType.getName()).toBe(
expect(namedOptionalNumberType.display()).toBe(
"OptionalTestNumber?: Number"
);
});
Expand Down Expand Up @@ -398,7 +398,7 @@ describe("frForceBoxed", () => {
);
});

test("GetName", () => {
expect(frBoxedNumber.getName()).toBe("Number");
test("display", () => {
expect(frBoxedNumber.display()).toBe("Number");
});
});
2 changes: 1 addition & 1 deletion packages/squiggle-lang/src/fr/calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export const library = [
fn: value,
title: title || args.value.name || undefined,
description: description || args.value.description || undefined,
inputs: inputs || [],
inputs: inputs || value.defaultInputs(),
autorun: autorun === null || autorun === undefined ? true : autorun,
sampleCount: sampleCount || undefined,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/squiggle-lang/src/fr/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const library = [
],
}),
maker.make({
name: "getName",
name: "display",
examples: [],
definitions: [
makeDefinition([frForceBoxed(frAny())], frString, ([{ args }]) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/squiggle-lang/src/library/registry/fnDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ export function tryCallFnDefinition(

export function fnDefinitionToString(fn: FnDefinition): string {
const inputs = fn.inputs
.map((t) => t.getName() + (isOptional(t) && t.tag !== "named" ? "?" : ""))
.map((t) => t.display() + (isOptional(t) && t.tag !== "named" ? "?" : ""))
.join(", ");
const output = fn.output.getName();
const output = fn.output.display();
return `(${inputs})${output ? ` => ${output}` : ""}`;
}