Update MD010/no-hard-tabs to add ignore_code_languages parameter (fixes #383).

This commit is contained in:
David Anson 2022-04-28 21:09:06 -07:00
parent 0f845e9ba1
commit b447c809bd
9 changed files with 112 additions and 3 deletions

View file

@ -2550,7 +2550,7 @@ module.exports = {
"use strict";
// @ts-check
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, forEachLine = _a.forEachLine, overlapsAnyRange = _a.overlapsAnyRange;
var _a = __webpack_require__(/*! ../helpers */ "../helpers/helpers.js"), addError = _a.addError, filterTokens = _a.filterTokens, forEachLine = _a.forEachLine, overlapsAnyRange = _a.overlapsAnyRange;
var _b = __webpack_require__(/*! ./cache */ "../lib/cache.js"), codeBlockAndSpanRanges = _b.codeBlockAndSpanRanges, lineMetadata = _b.lineMetadata;
var tabRe = /\t+/g;
module.exports = {
@ -2560,11 +2560,21 @@ module.exports = {
"function": function MD010(params, onError) {
var codeBlocks = params.config.code_blocks;
var includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
var ignoreCodeLanguages = new Set((params.config.ignore_code_languages || [])
.map(function (language) { return language.toLowerCase(); }));
var spacesPerTab = params.config.spaces_per_tab;
var spaceMultiplier = (spacesPerTab === undefined) ?
1 :
Math.max(0, Number(spacesPerTab));
var exclusions = includeCode ? [] : codeBlockAndSpanRanges();
filterTokens(params, "fence", function (token) {
var language = token.info.trim().toLowerCase();
if (ignoreCodeLanguages.has(language)) {
for (var i = token.map[0] + 1; i < token.map[1] - 1; i++) {
exclusions.push([i, 0, params.lines[i].length]);
}
}
});
forEachLine(lineMetadata(), function (line, lineIndex, inCode) {
if (includeCode || !inCode) {
var match = null;

View file

@ -396,7 +396,7 @@ Tags: whitespace, hard_tab
Aliases: no-hard-tabs
Parameters: code_blocks, spaces_per_tab (boolean; default true, number; default 1)
Parameters: code_blocks, ignore_code_languages, spaces_per_tab (boolean; default true, array of string; default empty, number; default 1)
Fixable: Most violations can be fixed by tooling
@ -429,6 +429,12 @@ set the `code_blocks` parameter to `false`. Code blocks and spans are included
by default since handling of tabs by Markdown tools can be inconsistent (e.g.,
using 4 vs. 8 spaces).
When code blocks are scanned (e.g., by default or if `code_blocks` is `true`),
the `ignore_code_languages` parameter can be set to a list of languages that
should be ignored (i.e., hard tabs will be allowed, though not required). This
makes it easier for documents to include code for languages that require hard
tabs.
By default, violations of this rule are fixed by replacing the tab with 1 space
character. To use a different number of spaces, set the `spaces_per_tab`
parameter to the desired value.

View file

@ -2,7 +2,8 @@
"use strict";
const { addError, forEachLine, overlapsAnyRange } = require("../helpers");
const { addError, filterTokens, forEachLine, overlapsAnyRange } =
require("../helpers");
const { codeBlockAndSpanRanges, lineMetadata } = require("./cache");
const tabRe = /\t+/g;
@ -14,11 +15,23 @@ module.exports = {
"function": function MD010(params, onError) {
const codeBlocks = params.config.code_blocks;
const includeCode = (codeBlocks === undefined) ? true : !!codeBlocks;
const ignoreCodeLanguages = new Set(
(params.config.ignore_code_languages || [])
.map((language) => language.toLowerCase())
);
const spacesPerTab = params.config.spaces_per_tab;
const spaceMultiplier = (spacesPerTab === undefined) ?
1 :
Math.max(0, Number(spacesPerTab));
const exclusions = includeCode ? [] : codeBlockAndSpanRanges();
filterTokens(params, "fence", (token) => {
const language = token.info.trim().toLowerCase();
if (ignoreCodeLanguages.has(language)) {
for (let i = token.map[0] + 1; i < token.map[1] - 1; i++) {
exclusions.push([ i, 0, params.lines[i].length ]);
}
}
});
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
if (includeCode || !inCode) {
let match = null;

View file

@ -58,6 +58,8 @@
"MD010": {
// Include code blocks
"code_blocks": true,
// Fenced code languages to ignore
"ignore_code_languages": [],
// Number of spaces for each hard tab
"spaces_per_tab": 1
},

View file

@ -52,6 +52,8 @@ MD009:
MD010:
# Include code blocks
code_blocks: true
# Fenced code languages to ignore
ignore_code_languages: []
# Number of spaces for each hard tab
spaces_per_tab: 1

View file

@ -147,6 +147,14 @@ rules.forEach(function forRule(rule) {
"type": "boolean",
"default": true
},
"ignore_code_languages": {
"description": "Fenced code languages to ignore",
"type": "array",
"items": {
"type": "string"
},
"default": []
},
"spaces_per_tab": {
"description": "Number of spaces for each hard tab",
"type": "integer",

View file

@ -201,6 +201,14 @@
"type": "boolean",
"default": true
},
"ignore_code_languages": {
"description": "Fenced code languages to ignore",
"type": "array",
"items": {
"type": "string"
},
"default": []
},
"spaces_per_tab": {
"description": "Number of spaces for each hard tab",
"type": "integer",

View file

@ -0,0 +1,10 @@
{
"default": true,
"MD010": {
"ignore_code_languages": [
"js",
"text"
]
},
"MD046": false
}

View file

@ -0,0 +1,50 @@
# Heading
```js
if (true) {
console.log("true");
if (false) {
console.log("false");
}
}
```
```js
if (true) {
console.log("true");
if (false) {
console.log("false");
}
}
```
if (true) {
console.log("true");
if (false) {
console.log("false");
}
}
```text
hello
world
}
```
if (true) { // {MD010}
console.log("true"); // {MD010}
if (false) { // {MD010}
console.log("false"); // {MD010}
} // {MD010}
} // {MD010}
Line with hard tab. {MD010}
```javascript
if (true) {
console.log("true"); // {MD010}
if (false) { // {MD010}
console.log("false"); // {MD010}
} // {MD010}
}
```