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

feat(graindoc)!: Allow @since and @returns once per export #1946

Merged
merged 4 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 54 additions & 38 deletions compiler/graindoc/docblock.re
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ exception

exception MissingParamType({name: string});
exception MissingReturnType;
exception AttributeAppearsMultipleTimes({attr: string});
exception
InvalidAttribute({
name: string,
Expand Down Expand Up @@ -106,6 +107,10 @@ let () =
| MissingReturnType =>
let msg = "Unable to find a return type. Please file an issue!";
Some(msg);
| AttributeAppearsMultipleTimes({attr}) =>
let msg =
Printf.sprintf("Attribute @%s is only allowed to appear once.", attr);
Some(msg);
| InvalidAttribute({name, attr}) =>
let msg = Printf.sprintf("Invalid attribute @%s on %s", attr, name);
Some(msg);
Expand Down Expand Up @@ -272,16 +277,18 @@ let for_value_description =
examples,
)
| Since({attr_version}) =>
// TODO(#787): Should we fail if more than one `@since` attribute?
(
deprecations,
Some({since_version: attr_version}),
history,
params,
returns,
throws,
examples,
)
switch (since) {
| Some(_) => raise(AttributeAppearsMultipleTimes({attr: "since"}))
| None => (
deprecations,
Some({since_version: attr_version}),
history,
params,
returns,
throws,
examples,
)
}
| History({attr_version: history_version, attr_desc: history_msg}) => (
deprecations,
since,
Expand Down Expand Up @@ -309,20 +316,25 @@ let for_value_description =
examples,
);
| Returns({attr_desc: returns_msg}) =>
let returns_type =
switch (return_type) {
| Some(typ) => Printtyp.string_of_type_sch(typ)
| None => raise(MissingReturnType)
};
(
deprecations,
since,
history,
params,
Some({returns_msg, returns_type}),
throws,
examples,
);
switch (returns) {
| Some(_) =>
raise(AttributeAppearsMultipleTimes({attr: "returns"}))
| None =>
let returns_type =
switch (return_type) {
| Some(typ) => Printtyp.string_of_type_sch(typ)
| None => raise(MissingReturnType)
};
(
deprecations,
since,
history,
params,
Some({returns_msg, returns_type}),
throws,
examples,
);
}
| Throws({attr_type: throw_type, attr_desc: throw_msg}) => (
deprecations,
since,
Expand Down Expand Up @@ -389,13 +401,15 @@ let for_type_declaration =
examples,
)
| Since({attr_version}) =>
// TODO(#787): Should we fail if more than one `@since` attribute?
(
deprecations,
Some({since_version: attr_version}),
history,
examples,
)
switch (since) {
| Some(_) => raise(AttributeAppearsMultipleTimes({attr: "since"}))
| None => (
deprecations,
Some({since_version: attr_version}),
history,
examples,
)
}
| History({attr_version: history_version, attr_desc: history_msg}) => (
deprecations,
since,
Expand Down Expand Up @@ -516,13 +530,15 @@ and for_signature_items =
examples,
)
| Since({attr_version}) =>
// TODO(#787): Should we fail if more than one `@since` attribute?
(
deprecations,
Some({since_version: attr_version}),
history,
examples,
)
switch (since) {
| Some(_) => raise(AttributeAppearsMultipleTimes({attr: "since"}))
| None => (
deprecations,
Some({since_version: attr_version}),
history,
examples,
)
}
| History({attr_version: history_version, attr_desc: history_msg}) => (
deprecations,
since,
Expand Down
7 changes: 7 additions & 0 deletions compiler/test/graindoc/singleReturn.input.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module SingleReturn

/**
* @returns first return
* @returns second return
*/
provide let test = () => 1
7 changes: 7 additions & 0 deletions compiler/test/graindoc/singleSince.input.gr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module SingleSince

/**
* @since v1.0.0
* @since v1.0.0
*/
provide let test = () => print("t")
11 changes: 11 additions & 0 deletions compiler/test/runner.re
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,14 @@ let makeGrainDocRunner = (test, name, filename, arguments) => {
},
);
};

let makeGrainDocErrorRunner = (test, name, filename, expected, arguments) => {
test(
name,
({expect}) => {
let infile = gaindoc_in_file(filename);
let (result, _) = doc(infile, arguments);
expect.string(result).toMatch(expected);
},
);
};
13 changes: 13 additions & 0 deletions compiler/test/suites/graindoc.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("graindoc", ({test, testSkip}) => {
Sys.backend_type == Other("js_of_ocaml") ? testSkip : test;

let assertGrianDocOutput = makeGrainDocRunner(test_or_skip);
let assertGrainDocError = makeGrainDocErrorRunner(test_or_skip);
();
assertGrianDocOutput("noDoc", "noDoc", [||]);
assertGrianDocOutput(
Expand All @@ -18,4 +19,16 @@ describe("graindoc", ({test, testSkip}) => {
);
assertGrianDocOutput("since", "since", [|"--current-version=v0.2.0"|]);
assertGrianDocOutput("example", "example", [|"--current-version=v0.2.0"|]);
assertGrainDocError(
"singleSince",
"singleSince",
"Attribute @since is only allowed to appear once.",
[|"--current-version=v0.2.0"|],
);
assertGrainDocError(
"singleReturn",
"singleReturn",
"Attribute @returns is only allowed to appear once.",
[|"--current-version=v0.2.0"|],
);
});
Loading