2019-07-30 23:07:42 -07:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2024-04-20 21:23:06 -07:00
|
|
|
const js = require("@eslint/js");
|
2019-07-30 23:07:42 -07:00
|
|
|
const eslint = require("eslint");
|
|
|
|
|
const linter = new eslint.Linter();
|
|
|
|
|
const languageJavaScript = /js|javascript/i;
|
|
|
|
|
|
2024-12-03 19:58:28 -08:00
|
|
|
/** @type {import("markdownlint").Rule} */
|
2019-07-30 23:07:42 -07:00
|
|
|
module.exports = {
|
|
|
|
|
"names": [ "lint-javascript" ],
|
|
|
|
|
"description": "Rule that lints JavaScript code",
|
|
|
|
|
"tags": [ "test", "lint", "javascript" ],
|
2024-03-09 16:17:50 -08:00
|
|
|
"parser": "markdownit",
|
2019-07-30 23:07:42 -07:00
|
|
|
"function": (params, onError) => {
|
2024-09-01 16:16:05 -07:00
|
|
|
const fences = params.parsers.markdownit.tokens
|
2024-10-06 21:03:07 -07:00
|
|
|
.filter((token) => token.type === "fence");
|
2024-08-18 15:34:26 -07:00
|
|
|
for (const fence of fences) {
|
2019-07-30 23:07:42 -07:00
|
|
|
if (languageJavaScript.test(fence.info)) {
|
2024-04-20 21:23:06 -07:00
|
|
|
const results = linter.verify(fence.content, js.configs.recommended);
|
|
|
|
|
for (const result of results) {
|
|
|
|
|
const lineNumber = fence.lineNumber + result.line;
|
|
|
|
|
onError({
|
|
|
|
|
"lineNumber": lineNumber,
|
|
|
|
|
"detail": result.message,
|
|
|
|
|
"context": params.lines[lineNumber - 1]
|
2019-07-30 23:07:42 -07:00
|
|
|
});
|
2024-04-20 21:23:06 -07:00
|
|
|
}
|
2019-07-30 23:07:42 -07:00
|
|
|
}
|
2024-08-18 15:34:26 -07:00
|
|
|
}
|
|
|
|
|
// Unsupported by this sample:
|
|
|
|
|
// "code_block": language unknown
|
|
|
|
|
// "code_inline": too brief
|
2019-07-30 23:07:42 -07:00
|
|
|
}
|
|
|
|
|
};
|