Add spaces_per_tab parameter to MD010/no-hard-tabs (fixes #384). (#385)

This commit is contained in:
Yash Singh 2021-04-09 16:33:01 -07:00 committed by GitHub
parent 6302b26c99
commit 47ff95e8d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 157 additions and 4 deletions

View file

@ -2143,6 +2143,10 @@ module.exports = {
"function": function MD010(params, onError) {
var codeBlocks = params.config.code_blocks;
var includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
var spacesPerTab = params.config.spaces_per_tab;
var spaceMultiplier = (spacesPerTab === undefined) ?
1 :
Math.max(0, Number(spacesPerTab));
forEachLine(lineMetadata(), function (line, lineIndex, inCode) {
if (!inCode || includeCodeBlocks) {
var match = null;
@ -2152,7 +2156,7 @@ module.exports = {
addError(onError, lineIndex + 1, "Column: " + column, null, [column, length_1], {
"editColumn": column,
"deleteCount": length_1,
"insertText": "".padEnd(length_1)
"insertText": "".padEnd(length_1 * spaceMultiplier)
});
}
}

View file

@ -388,7 +388,7 @@ Tags: whitespace, hard_tab
Aliases: no-hard-tabs
Parameters: code_blocks (boolean; default true)
Parameters: code_blocks, spaces_per_tab (boolean; default true, number; default 1)
Fixable: Most violations can be fixed by tooling
@ -420,6 +420,9 @@ You have the option to exclude this rule for code blocks. To do so, set the
`code_blocks` parameter to `false`. Code blocks are included by default since
handling of tabs by tools is often inconsistent (ex: using 4 vs. 8 spaces).
If you would like the fixer to change tabs to x spaces, then configure the `spaces_per_tab`
parameter to the number x. The default value would be 1.
Rationale: Hard tabs are often rendered inconsistently by different editors and
can be harder to work with than spaces.

View file

@ -14,6 +14,10 @@ module.exports = {
"function": function MD010(params, onError) {
const codeBlocks = params.config.code_blocks;
const includeCodeBlocks = (codeBlocks === undefined) ? true : !!codeBlocks;
const spacesPerTab = params.config.spaces_per_tab;
const spaceMultiplier = (spacesPerTab === undefined) ?
1 :
Math.max(0, Number(spacesPerTab));
forEachLine(lineMetadata(), (line, lineIndex, inCode) => {
if (!inCode || includeCodeBlocks) {
let match = null;
@ -29,7 +33,7 @@ module.exports = {
{
"editColumn": column,
"deleteCount": length,
"insertText": "".padEnd(length)
"insertText": "".padEnd(length * spaceMultiplier)
});
}
}

View file

@ -55,7 +55,9 @@
// MD010/no-hard-tabs - Hard tabs
"MD010": {
// Include code blocks
"code_blocks": true
"code_blocks": true,
// Number of spaces for each hard tab
"spaces_per_tab": 1
},
// MD011/no-reversed-links - Reversed link syntax

View file

@ -50,6 +50,8 @@ MD009:
MD010:
# Include code blocks
code_blocks: true
# Number of spaces for each hard tab
spaces_per_tab: 1
# MD011/no-reversed-links - Reversed link syntax
MD011: true

View file

@ -135,6 +135,11 @@ rules.forEach(function forRule(rule) {
"description": "Include code blocks",
"type": "boolean",
"default": true
},
"spaces_per_tab": {
"description": "Number of spaces for each hard tab",
"type": "number",
"default": 1
}
};
break;

View file

@ -327,6 +327,11 @@
"description": "Include code blocks",
"type": "boolean",
"default": true
},
"spaces_per_tab": {
"description": "Number of spaces for each hard tab",
"type": "number",
"default": 1
}
},
"additionalProperties": false
@ -343,6 +348,11 @@
"description": "Include code blocks",
"type": "boolean",
"default": true
},
"spaces_per_tab": {
"description": "Number of spaces for each hard tab",
"type": "number",
"default": 1
}
},
"additionalProperties": false

View file

@ -0,0 +1,7 @@
{
"MD009": false,
"MD010": {
"spaces_per_tab": 4
},
"MD041": false
}

View file

@ -0,0 +1,3 @@
text
text and text 2
texts with trailing spaces

View file

@ -0,0 +1,3 @@
text
text and text 2
texts with trailing spaces

View file

@ -0,0 +1,29 @@
[
{
"errorContext": null,
"errorDetail": "Column: 1",
"errorRange": [1, 13],
"lineNumber": 1,
"ruleDescription": "Hard tabs",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md010",
"ruleNames": ["MD010", "no-hard-tabs"]
},
{
"errorContext": null,
"errorDetail": "Column: 5",
"errorRange": [5, 2],
"lineNumber": 2,
"ruleDescription": "Hard tabs",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md010",
"ruleNames": ["MD010", "no-hard-tabs"]
},
{
"errorContext": null,
"errorDetail": "Column: 27",
"errorRange": [27, 3],
"lineNumber": 3,
"ruleDescription": "Hard tabs",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md010",
"ruleNames": ["MD010", "no-hard-tabs"]
}
]

View file

@ -0,0 +1,4 @@
{
"MD009": false,
"MD041": false
}

View file

@ -0,0 +1,3 @@
text
text and text 2
texts with trailing spaces

View file

@ -0,0 +1,3 @@
text
text and text 2
texts with trailing spaces

View file

@ -0,0 +1,29 @@
[
{
"errorContext": null,
"errorDetail": "Column: 1",
"errorRange": [1, 13],
"lineNumber": 1,
"ruleDescription": "Hard tabs",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md010",
"ruleNames": ["MD010", "no-hard-tabs"]
},
{
"errorContext": null,
"errorDetail": "Column: 5",
"errorRange": [5, 2],
"lineNumber": 2,
"ruleDescription": "Hard tabs",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md010",
"ruleNames": ["MD010", "no-hard-tabs"]
},
{
"errorContext": null,
"errorDetail": "Column: 27",
"errorRange": [27, 3],
"lineNumber": 3,
"ruleDescription": "Hard tabs",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md010",
"ruleNames": ["MD010", "no-hard-tabs"]
}
]

View file

@ -0,0 +1,7 @@
{
"MD009": false,
"MD010": {
"spaces_per_tab": 0
},
"MD041": false
}

View file

@ -0,0 +1,3 @@
text
text and text 2
texts with trailing spaces

View file

@ -0,0 +1,3 @@
text
text and text 2
texts with trailing spaces

View file

@ -0,0 +1,29 @@
[
{
"errorContext": null,
"errorDetail": "Column: 1",
"errorRange": [1, 13],
"lineNumber": 1,
"ruleDescription": "Hard tabs",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md010",
"ruleNames": ["MD010", "no-hard-tabs"]
},
{
"errorContext": null,
"errorDetail": "Column: 5",
"errorRange": [5, 2],
"lineNumber": 2,
"ruleDescription": "Hard tabs",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md010",
"ruleNames": ["MD010", "no-hard-tabs"]
},
{
"errorContext": null,
"errorDetail": "Column: 27",
"errorRange": [27, 3],
"lineNumber": 3,
"ruleDescription": "Hard tabs",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md010",
"ruleNames": ["MD010", "no-hard-tabs"]
}
]