Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Catch unresolved partial parameters
Browse files Browse the repository at this point in the history
Edit the `remark-includes` linter to ensure that all parameters declared
within a partial (using `{{ param }}` syntax) are either (a) assigned by
the user or (b) given a default value. Otherwise, when we move to the
new docs engine, builds will fail on unresolved parameters.
  • Loading branch information
ptgott committed Oct 8, 2024
1 parent 48e9ee6 commit 6eeca8e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/fixtures/includes-vars-erroneous-include.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Installation

Here is a test for including variables in an MDX file.

(!install-version.mdx version="10" unsupport="9" !)
15 changes: 15 additions & 0 deletions server/remark-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,21 @@ const resolveIncludes = ({
content = content.replace(varRegexp, finalVal);
}

// Catch unresolved parameters, which can break docs builds
const paramRE = new RegExp(`{{ ?\\w+ ?}}`, "g");
const unresolvedParams = Array.from(content.matchAll(paramRE));
if (unresolvedParams.length > 0) {
const errs = unresolvedParams
.map((el) => {
return el[0];
})
.join(",");

error =
`${includePath}: the following partial parameters were not assigned and have no default value: ` +
errs;
}

return content;
} else {
error = `Wrong import path ${includePath} in file ${filePath}.`;
Expand Down
21 changes: 21 additions & 0 deletions uvu-tests/remark-includes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,27 @@ Suite("Resolves template variables in includes", () => {
assert.equal(result, expected);
});

Suite("Throws an error if a variable is unresolved and has no default", () => {
const value = readFileSync(
resolve("server/fixtures/includes-vars-erroneous-include.mdx"),
"utf-8"
);

const out = transformer(
{
value,
path: "/content/4.0/docs/pages/filename.mdx",
},
{ lint: true }
);

assert.equal(out.messages.length, 1);
assert.equal(
out.messages[0].reason,
"The following partial parameters were not assigned and have no default value: {{ unsupported }}"
);
});

Suite(
"Resolves relative links in partials based on the path of the partial",
() => {
Expand Down

0 comments on commit 6eeca8e

Please sign in to comment.