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

Align front-matter linter with prettier #27828

Merged
merged 1 commit into from
Jul 7, 2023
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
6 changes: 3 additions & 3 deletions scripts/front-matter_linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function lintFrontMatter(filesAndDirectories, options) {
).flat();

options.config = JSON.parse(
await fs.readFile("./front-matter-config.json", "utf-8")
await fs.readFile("./front-matter-config.json", "utf-8"),
);

options.validator = getAjvValidator(options.config.schema);
Expand All @@ -58,7 +58,7 @@ async function lintFrontMatter(filesAndDirectories, options) {
try {
const [error, fixableError, content] = await checkFrontMatter(
file,
options
options,
);
if (content) {
fs.writeFile(file, content);
Expand Down Expand Up @@ -115,7 +115,7 @@ program
return;
}
return lintFrontMatter(files, options);
})
}),
);

program.run();
8 changes: 5 additions & 3 deletions scripts/front-matter_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import path from "node:path";

import YAML from "js-yaml";
import * as prettier from "prettier";
import AJV from "ajv";
import grayMatter from "gray-matter";
import addFormats from "ajv-formats";
Expand Down Expand Up @@ -53,9 +54,9 @@ export async function checkFrontMatter(filePath, options) {

if (!options.fix && !isInOrder) {
fixableError = `${getRelativePath(
filePath
filePath,
)}\n\t Front matter attributes are not in required order: ${order.join(
"->"
"->",
)}`;
}

Expand Down Expand Up @@ -85,7 +86,8 @@ export async function checkFrontMatter(filePath, options) {
quotingType: '"',
});
yml = yml.replace(/[\s\n]+$/g, "");
content = `---\n${yml}\n---\n${document.content}`;
yml = await prettier.format(yml, { parser: "yaml" });
content = `---\n${yml}---\n${document.content}`;
} else {
content = null;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/up-to-date-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ if (installedYariVersion < availableYariVersion) {
`the version referenced in package.json (${availableYariVersion}). ` +
`Please make sure your main git branch is up-to-date with ` +
`https://github.com/mdn/content/, then run yarn install to ` +
`install the latest Yari.`
`install the latest Yari.`,
);
}
6 changes: 3 additions & 3 deletions scripts/update-interface-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const idls = await Promise.all(
Object.entries(idlnames)
.sort(([k1], [k2]) => k1.localeCompare(k2))
.map(([, { parsed: jsonIdlPath }]) =>
fs.readFile(path.join(webrefPath, jsonIdlPath), "utf-8").then(JSON.parse)
)
fs.readFile(path.join(webrefPath, jsonIdlPath), "utf-8").then(JSON.parse),
),
);

const interfaceData = idls.reduce((interfaceData, idl) => {
Expand All @@ -31,5 +31,5 @@ const interfaceData = idls.reduce((interfaceData, idl) => {

await fs.writeFile(
"files/jsondata/InterfaceData.json",
JSON.stringify([interfaceData], null, 2) + "\n"
JSON.stringify([interfaceData], null, 2) + "\n",
);
57 changes: 28 additions & 29 deletions tests/front-matter_linter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,41 @@ const options = {};

options.config = JSON.parse(
fs.readFileSync(
fileURLToPath(new URL("config.json", SAMPLES_DIRECTORY), "utf-8")
)
fileURLToPath(new URL("config.json", SAMPLES_DIRECTORY), "utf-8"),
),
);
options.validator = getAjvValidator(options.config.schema);

function getPath(filePath) {
return fileURLToPath(new URL(filePath, SAMPLES_DIRECTORY));
}

function getContent(filePath) {
return fs.readFileSync(getPath(filePath), "utf-8");
}

describe("Test front-matter linter", () => {
it("should use double quotes and remove unwanted quotes", async () => {
const filePath = fileURLToPath(
new URL("./double_quotes.md", SAMPLES_DIRECTORY)
);
const validPath = fileURLToPath(
new URL("./double_quotes.valid.md", SAMPLES_DIRECTORY)
);
const validContent = fs.readFileSync(validPath, "utf-8");
const filePath = getPath("./double_quotes.md");
const validContent = getContent("./double_quotes.valid.md");

options.fix = true;
const result = await checkFrontMatter(filePath, options);
await expect(result).toEqual([null, null, validContent]);
});

it("should use single quotes to enclose double quoted words", async () => {
const filePath = getPath("./single_quotes.md");
const validContent = getContent("./single_quotes.valid.md");

options.fix = true;
const result = await checkFrontMatter(filePath, options);
await expect(result).toEqual([null, null, validContent]);
});

it("should enforce the attribute order", async () => {
const filePath = fileURLToPath(
new URL("./attribute_order.md", SAMPLES_DIRECTORY)
);
const validPath = fileURLToPath(
new URL("./attribute_order.valid.md", SAMPLES_DIRECTORY)
);
const validContent = fs.readFileSync(validPath, "utf-8");
const filePath = getPath("./attribute_order.md");
const validContent = getContent("./attribute_order.valid.md");

options.fix = false;
let result = await checkFrontMatter(filePath, options);
Expand All @@ -57,7 +64,7 @@ describe("Test front-matter linter", () => {
});

it("should flag invalid values", async () => {
const filePath = fileURLToPath(new URL("./values.md", SAMPLES_DIRECTORY));
const filePath = getPath("./values.md");

options.fix = false;
let result = await checkFrontMatter(filePath, options);
Expand All @@ -74,11 +81,8 @@ describe("Test front-matter linter", () => {
});

it("should prettify the front-matter", async () => {
const filePath = fileURLToPath(new URL("./prettify.md", SAMPLES_DIRECTORY));
const validPath = fileURLToPath(
new URL("./prettify.valid.md", SAMPLES_DIRECTORY)
);
const validContent = fs.readFileSync(validPath, "utf-8");
const filePath = getPath("./prettify.md");
const validContent = getContent("./prettify.valid.md");

options.fix = true;
const result = await checkFrontMatter(filePath, options);
Expand All @@ -89,13 +93,8 @@ describe("Test front-matter linter", () => {
});

it("should flag and remove unknown attribute", async () => {
const filePath = fileURLToPath(
new URL("./unknown_attribute.md", SAMPLES_DIRECTORY)
);
const validPath = fileURLToPath(
new URL("./unknown_attribute.valid.md", SAMPLES_DIRECTORY)
);
const validContent = fs.readFileSync(validPath, "utf-8");
const filePath = getPath("./unknown_attribute.md");
const validContent = getContent("./unknown_attribute.valid.md");

options.fix = false;
let result = await checkFrontMatter(filePath, options);
Expand Down
2 changes: 1 addition & 1 deletion tests/front-matter_test_files/double_quotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: 'some api: method() is "cool"'
title: 'some api: method() is ''cool'''
short-title: "method()"
slug: Web/api/method
---
Expand Down
2 changes: 1 addition & 1 deletion tests/front-matter_test_files/double_quotes.valid.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "some api: method() is \"cool\""
title: "some api: method() is 'cool'"
short-title: method()
slug: Web/api/method
---
Expand Down
6 changes: 6 additions & 0 deletions tests/front-matter_test_files/single_quotes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: "some api: method() is \"cool\""
slug: Web/api/method
---

Content
6 changes: 6 additions & 0 deletions tests/front-matter_test_files/single_quotes.valid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: 'some api: method() is "cool"'
slug: Web/api/method
---

Content