forked from DavidAnson/markdownlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-javascript.js
58 lines (54 loc) · 1.76 KB
/
lint-javascript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// @ts-check
"use strict";
const { filterTokens } = require("../../helpers");
const eslint = require("eslint");
const eslintInstance = new eslint.ESLint();
const linter = new eslint.Linter();
const languageJavaScript = /js|javascript/i;
/**
* Remove references to rules from eslint-plugin-jsdoc.
*
* @param {Object} config ESLint configuration object.
* @returns {Object} ESLint configuration object.
*/
function cleanJsdocRulesFromEslintConfig(config) {
const cleanedConfig = { ...config };
for (const rule in config.rules) {
if (/^(?:jsdoc|n|regexp|unicorn)\//.test(rule)) {
delete cleanedConfig.rules[rule];
}
}
return cleanedConfig;
}
/** @type import("../../lib/markdownlint").Rule */
module.exports = {
"names": [ "lint-javascript" ],
"description": "Rule that lints JavaScript code",
"tags": [ "test", "lint", "javascript" ],
"parser": "markdownit",
"asynchronous": true,
"function": (params, onError) => {
filterTokens(params, "fence", (fence) => {
if (languageJavaScript.test(fence.info)) {
return eslintInstance.calculateConfigForFile(params.name)
.then((config) => {
config = cleanJsdocRulesFromEslintConfig(config);
const results = linter.verify(fence.content, config);
for (const result of results) {
// @ts-ignore
const lineNumber = fence.lineNumber + result.line;
onError({
"lineNumber": lineNumber,
"detail": result.message,
"context": params.lines[lineNumber - 1]
});
}
});
}
return Promise.resolve();
});
// Unsupported:
// filterTokens("code_block"), language unknown
// filterTokens("code_inline"), too brief
}
};