Skip to content

Commit 578caa6

Browse files
committed
Update OutputFormatterOptions to include an FsPromisesLike parameter so output formatters can read from/write to the same file system object used for linting.
1 parent 018d1eb commit 578caa6

8 files changed

Lines changed: 66 additions & 18 deletions

‎.github/dictionary.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CommonMark
88
config
99
dotfiles
1010
formatter
11+
formatter's
1112
formatters
1213
Formatters
1314
GitLab

‎doc/OutputFormatters.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ Where `options` is an object that looks like:
2828
standard output
2929
- `logError`: `Function` that takes a single `String` argument and logs it to
3030
standard error
31+
- `fsPromises`: `Object` that presents a subset of the `node:fs/promises` file
32+
system module
3133

32-
And `params` is an object containing formatter parameters from configuration.
34+
And `params` is an object containing the formatter's custom parameters from the
35+
provided configuration array.
3336

3437
For a `.markdownlint-cli2.jsonc` like:
3538

‎formatter-codequality/markdownlint-cli2-formatter-codequality.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
"use strict";
44

5-
const fs = require("node:fs").promises;
65
const path = require("node:path");
76
const { createHash } = require("node:crypto");
87

@@ -29,7 +28,7 @@ const createFingerprint = function createFingerprint(violation) {
2928
// Writes markdownlint-cli2 results to a GitLab Code Quality report JSON file.
3029
// See: https://docs.gitlab.com/ci/testing/code_quality/#code-quality-report-format
3130
const outputFormatter = (/** @type {OutputFormatterOptions} */ options, /** @type {Parameters} */ params) => {
32-
const { directory, results } = options;
31+
const { directory, fsPromises, results } = options;
3332
const { name, severity, severityError, severityWarning } = (params || {});
3433
const issues = [];
3534

@@ -67,7 +66,7 @@ const outputFormatter = (/** @type {OutputFormatterOptions} */ options, /** @typ
6766
}
6867

6968
const content = JSON.stringify(issues, null, 2);
70-
return fs.writeFile(
69+
return fsPromises.writeFile(
7170
path.resolve(
7271
directory,
7372
name || "markdownlint-cli2-codequality.json"

‎formatter-json/markdownlint-cli2-formatter-json.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
"use strict";
44

5-
const fs = require("node:fs").promises;
65
const path = require("node:path");
76

87
/** @typedef {import("../markdownlint-cli2.mjs").OutputFormatterOptions} OutputFormatterOptions */
@@ -15,10 +14,10 @@ const path = require("node:path");
1514

1615
// Writes markdownlint-cli2 results to a file in JSON format
1716
const outputFormatter = (/** @type {OutputFormatterOptions} */ options, /** @type {Parameters} */ params) => {
18-
const { directory, results } = options;
17+
const { directory, fsPromises, results } = options;
1918
const { name, spaces } = (params || {});
2019
const content = JSON.stringify(results, null, spaces || 2);
21-
return fs.writeFile(
20+
return fsPromises.writeFile(
2221
path.resolve(
2322
directory,
2423
name || "markdownlint-cli2-results.json"

‎formatter-junit/markdownlint-cli2-formatter-junit.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
"use strict";
44

5-
const fs = require("node:fs").promises;
65
const path = require("node:path");
76
const junitReportBuilder = require("junit-report-builder");
87

@@ -15,7 +14,7 @@ const junitReportBuilder = require("junit-report-builder");
1514

1615
// Writes markdownlint-cli2 results to a file in JUnit XML format
1716
const outputFormatter = (/** @type {OutputFormatterOptions} */ options, /** @type {Parameters} */ params) => {
18-
const { directory, results } = options;
17+
const { directory, fsPromises, results } = options;
1918
const { name } = (params || {});
2019
// Get a new builder instance because the default builder is shared
2120
// @ts-ignore
@@ -50,7 +49,7 @@ const outputFormatter = (/** @type {OutputFormatterOptions} */ options, /** @typ
5049
time(0);
5150
}
5251
const content = builder.build();
53-
return fs.writeFile(
52+
return fsPromises.writeFile(
5453
path.resolve(
5554
directory,
5655
name || "markdownlint-cli2-junit.xml"

‎formatter-sarif/markdownlint-cli2-formatter-sarif.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
"use strict";
44

5-
const fs = require("node:fs").promises;
65
const path = require("node:path");
76
const packageJson = require("./package.json");
87

@@ -54,7 +53,7 @@ const toUpper = (/** @type {string} */ s) => s.toUpperCase();
5453
// Writes markdownlint-cli2 results to a file in Static Analysis Results
5554
// Interchange Format/SARIF
5655
const outputFormatter = (/** @type {OutputFormatterOptions} */ options, /** @type {Parameters} */ params) => {
57-
const { directory, results } = options;
56+
const { directory, fsPromises, results } = options;
5857
const { name } = (params || {});
5958

6059
// Create SARIF object
@@ -154,7 +153,7 @@ const outputFormatter = (/** @type {OutputFormatterOptions} */ options, /** @typ
154153

155154
// Write SARIF object
156155
const content = JSON.stringify(sarif, null, 2);
157-
return fs.writeFile(
156+
return fsPromises.writeFile(
158157
path.resolve(
159158
directory,
160159
name || "markdownlint-cli2-sarif.sarif"

‎markdownlint-cli2.mjs‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ const flattenTaskResults = (/** @type {string} */ baseDir, /** @type {LintTaskRe
892892

893893
// Output summary via formatters
894894
const outputResults = async (
895-
/** @type {string} */ baseDir,
895+
/** @type {ExecutionContext} */ context,
896896
/** @type {string | null} */ relativeDir,
897897
/** @type {LintResult[]} */ results,
898898
/** @type {OutputFormatterConfiguration[] | undefined} */ outputFormatters,
@@ -903,12 +903,19 @@ const outputResults = async (
903903
) => {
904904
// eslint-disable-next-line unicorn/prefer-early-return
905905
if (outputFormatters || (results.length > 0)) {
906+
const { baseDir, fs } = context;
907+
/** @type {FsPromisesLike} */
908+
const fsPromises = {
909+
"readFile": fs.promises.readFile,
910+
"writeFile": fs.promises.writeFile
911+
};
906912
/** @type {OutputFormatterOptions} */
907913
const formatterOptions = {
908914
"directory": baseDir,
909915
results,
910916
logMessage,
911-
logError
917+
logError,
918+
fsPromises
912919
};
913920
const dir = relativeDir || baseDir;
914921
const dirs = [ dir, ...modulePaths ];
@@ -1110,7 +1117,7 @@ export const main = async (/** @type {Parameters} */ params) => {
11101117
baseMarkdownlintOptions.modulePaths || []
11111118
);
11121119
await outputResults(
1113-
baseDir,
1120+
context,
11141121
relativeDir,
11151122
lintResults,
11161123
outputFormatters,
@@ -1256,4 +1263,15 @@ export const main = async (/** @type {Parameters} */ params) => {
12561263
* @property {LintResult[]} results Lint results.
12571264
* @property {Logger} logMessage Message logger.
12581265
* @property {Logger} logError Error logger.
1266+
* @property {FsPromisesLike} fsPromises File system promises object.
1267+
*/
1268+
1269+
/** @typedef {(file: string, options: "utf8") => Promise<string>} FsPromisesReadFileLike */
1270+
1271+
/** @typedef {(file: string, data: string, options: "utf8") => Promise<void>} FsPromisesWriteFileLike */
1272+
1273+
/**
1274+
* @typedef {object} FsPromisesLike
1275+
* @property {FsPromisesReadFileLike} readFile Implementation of node:fs/promises.readFile.
1276+
* @property {FsPromisesWriteFileLike} writeFile Implementation of node:fs/promises.writeFile.
12591277
*/

‎test/markdownlint-cli2-test-formatters.mjs‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// @ts-check
22

3-
import { mkdir, readFile, rm } from "node:fs/promises";
3+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
44
import path from "node:path";
55
import test from "node:test";
6+
import { main as markdownlintCli2 } from "../markdownlint-cli2.mjs";
7+
import FsVirtual from "../webworker/fs-virtual.cjs";
68

9+
/** @typedef {import("../markdownlint-cli2.mjs").FsPromisesLike} FsPromisesLike */
710
/** @typedef {import("../markdownlint-cli2.mjs").OutputFormatterOptions} OutputFormatterOptions */
811
/** @typedef {import("../markdownlint-cli2.mjs").LintResult} LintResult */
912

@@ -82,12 +85,15 @@ test.suite(import.meta.url.replace(/^.*?\/(?<name>[^/]*)$/u, "$<name>"), () => {
8285
// @ts-ignore
8386
transform((res) => delete res.severity)
8487
];
88+
/** @type {FsPromisesLike} */
89+
const fsPromises = { readFile, writeFile };
8590
/** @type {OutputFormatterOptions} */
8691
const options = {
8792
"directory": testDirectory,
8893
results,
8994
logMessage,
90-
logError
95+
logError,
96+
fsPromises
9197
};
9298
// eslint-disable-next-line unicorn/no-await-expression-member
9399
const instance = (await import(`../formatter-${formatter}/markdownlint-cli2-formatter-${formatter}.js`)).default;
@@ -100,4 +106,28 @@ test.suite(import.meta.url.replace(/^.*?\/(?<name>[^/]*)$/u, "$<name>"), () => {
100106
});
101107
}
102108

109+
test("fsPromises", async (t) => {
110+
t.plan(3);
111+
const fs = new FsVirtual([]);
112+
/** @type {import("../markdownlint-cli2.mjs").OutputFormatter} */
113+
const formatter = async (options) => {
114+
const { fsPromises } = options;
115+
const { "readFile": fspReadFile, "writeFile": fspWriteFile } = fsPromises;
116+
const file = "/file.txt";
117+
const data = "Hello world.";
118+
await fspWriteFile(file, data, "utf8");
119+
t.assert.equal(await fspReadFile(file, "utf8"), data);
120+
t.assert.equal(await fs.promises.readFile(file, "utf8"), data);
121+
};
122+
await markdownlintCli2({
123+
"argv": [ "../README.md" ],
124+
"optionsOverride": {
125+
"outputFormatters": [ [ formatter ] ]
126+
},
127+
fs
128+
}).then((exitCode) => {
129+
t.assert.equal(exitCode, 0);
130+
});
131+
});
132+
103133
});

0 commit comments

Comments
 (0)