mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-09-22 05:40:48 +02:00
Add custom rule example to lint JavaScript code blocks using ESLint (fixes #197).
This commit is contained in:
parent
c3e8eab87b
commit
4d11e60cfe
4 changed files with 99 additions and 2 deletions
23
test/lint-javascript.md
Normal file
23
test/lint-javascript.md
Normal file
|
@ -0,0 +1,23 @@
|
|||
# Lint JavaScript
|
||||
|
||||
<!-- markdownlint-disable MD046 -->
|
||||
|
||||
Text
|
||||
|
||||
```js
|
||||
"use strict";
|
||||
|
||||
var x = 0;
|
||||
|
||||
console.log(x);
|
||||
```
|
||||
|
||||
Text
|
||||
|
||||
var y = 0;
|
||||
|
||||
Text
|
||||
|
||||
Text `undefined` text
|
||||
|
||||
Text
|
|
@ -1952,7 +1952,7 @@ module.exports.allBuiltInRulesHaveValidUrl =
|
|||
|
||||
module.exports.someCustomRulesHaveValidUrl =
|
||||
function someCustomRulesHaveValidUrl(test) {
|
||||
test.expect(6);
|
||||
test.expect(7);
|
||||
customRules.all.forEach(function forRule(rule) {
|
||||
test.ok(!rule.information ||
|
||||
(Object.getPrototypeOf(rule.information) === URL.prototype));
|
||||
|
@ -2886,6 +2886,42 @@ module.exports.customRulesDoc = function customRulesDoc(test) {
|
|||
});
|
||||
};
|
||||
|
||||
module.exports.customRulesLintJavaScript =
|
||||
function customRulesLintJavaScript(test) {
|
||||
test.expect(2);
|
||||
const options = {
|
||||
"customRules": customRules.lintJavaScript,
|
||||
"files": "test/lint-javascript.md"
|
||||
};
|
||||
markdownlint(options, (err, actual) => {
|
||||
test.ifError(err);
|
||||
const expected = {
|
||||
"test/lint-javascript.md": [
|
||||
{
|
||||
"lineNumber": 10,
|
||||
"ruleNames": [ "lint-javascript" ],
|
||||
"ruleDescription": "Rule that lints JavaScript code",
|
||||
"ruleInformation": null,
|
||||
"errorDetail": "Unexpected var, use let or const instead.",
|
||||
"errorContext": "var x = 0;",
|
||||
"errorRange": null
|
||||
},
|
||||
{
|
||||
"lineNumber": 12,
|
||||
"ruleNames": [ "lint-javascript" ],
|
||||
"ruleDescription": "Rule that lints JavaScript code",
|
||||
"ruleInformation": null,
|
||||
"errorDetail": "Unexpected console statement.",
|
||||
"errorContext": "console.log(x);",
|
||||
"errorRange": null
|
||||
}
|
||||
]
|
||||
};
|
||||
test.deepEqual(actual, expected, "Unexpected issues.");
|
||||
test.done();
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.markdownItPluginsSingle =
|
||||
function markdownItPluginsSingle(test) {
|
||||
test.expect(2);
|
||||
|
|
34
test/rules/lint-javascript.js
Normal file
34
test/rules/lint-javascript.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
// @ts-check
|
||||
|
||||
"use strict";
|
||||
|
||||
const { filterTokens } = require("markdownlint-rule-helpers");
|
||||
const eslint = require("eslint");
|
||||
const cliEngine = new eslint.CLIEngine({});
|
||||
const linter = new eslint.Linter();
|
||||
const languageJavaScript = /js|javascript/i;
|
||||
|
||||
module.exports = {
|
||||
"names": [ "lint-javascript" ],
|
||||
"description": "Rule that lints JavaScript code",
|
||||
"tags": [ "test", "lint", "javascript" ],
|
||||
"function": (params, onError) => {
|
||||
filterTokens(params, "fence", (fence) => {
|
||||
if (languageJavaScript.test(fence.info)) {
|
||||
const config = cliEngine.getConfigForFile(params.name);
|
||||
const results = linter.verify(fence.content, config);
|
||||
results.forEach((result) => {
|
||||
const lineNumber = fence.lineNumber + result.line;
|
||||
onError({
|
||||
"lineNumber": lineNumber,
|
||||
"detail": result.message,
|
||||
"context": params.lines[lineNumber - 1]
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
// Unused:
|
||||
// filterTokens("code_block"), language unknown
|
||||
// filterTokens("code_inline"), too brief
|
||||
}
|
||||
};
|
|
@ -14,9 +14,13 @@ module.exports.firstLine = firstLine;
|
|||
const lettersEX = require("./letters-E-X");
|
||||
module.exports.lettersEX = lettersEX;
|
||||
|
||||
const lintJavaScript = require("./lint-javascript");
|
||||
module.exports.lintJavaScript = lintJavaScript;
|
||||
|
||||
module.exports.all = [
|
||||
anyBlockquote,
|
||||
everyNLines,
|
||||
firstLine,
|
||||
lettersEX
|
||||
lettersEX,
|
||||
lintJavaScript
|
||||
];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue