Implement rule MD046/code-block-style from Ruby version.

This commit is contained in:
David Anson 2019-04-17 14:42:17 -07:00
parent c45eead5d7
commit 0af999e218
24 changed files with 236 additions and 19 deletions

View file

@ -87,6 +87,7 @@ playground for learning and exploring.
* **[MD043](doc/Rules.md#md043)** *required-headings/required-headers* - Required heading structure
* **[MD044](doc/Rules.md#md044)** *proper-names* - Proper names should have the correct capitalization
* **[MD045](doc/Rules.md#md045)** *no-alt-text* - Images should have alternate text (alt text)
* **[MD046](doc/Rules.md#md046)** *code-block-style* - Code block style
See [Rules.md](doc/Rules.md) for more details.
@ -104,7 +105,7 @@ Tags group related rules and can be used to enable/disable multiple rules at onc
* **blank_lines** - MD012, MD022, MD031, MD032
* **blockquote** - MD027, MD028
* **bullet** - MD004, MD005, MD006, MD007, MD032
* **code** - MD014, MD031, MD038, MD040
* **code** - MD014, MD031, MD038, MD040, MD046
* **emphasis** - MD036, MD037
* **hard_tab** - MD010
* **headers** - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023,

View file

@ -1544,3 +1544,35 @@ Or with reference syntax as:
Guidance for writing alternate text is available from the [W3C](https://www.w3.org/WAI/alt/),
[Wikipedia](https://en.wikipedia.org/wiki/Alt_attribute), and
[other locations](https://www.phase2technology.com/blog/no-more-excuses-definitive-guide-alt-text-field).
<a name="md046"></a>
## MD046 - Code block style
Tags: code
Aliases: code-block-style
Parameters: style ("consistent", "fenced", "indented"; default "consistent")
This rule is triggered when unwanted or different code block styles are used in
the same document.
In the default configuration this rule reports a violation for the following document:
Some text.
# Indented code
More text.
```ruby
# Fenced code
```
More text.
To fix violations of this rule, use a consistent style (either indenting or code fences).
The specified style can be specific (`fenced`, `indented`) or simply require that usage
be consistent within the document (`consistent`).

32
lib/md046.js Normal file
View file

@ -0,0 +1,32 @@
// @ts-check
"use strict";
const { addErrorDetailIf } = require("../helpers");
const tokenTypeToStyle = {
"fence": "fenced",
"code_block": "indented"
};
module.exports = {
"names": [ "MD046", "code-block-style" ],
"description": "Code block style",
"tags": [ "code" ],
"function": function MD046(params, onError) {
let expectedStyle = params.config.style || "consistent";
params.tokens
.filter((token) => token.type === "code_block" || token.type === "fence")
.forEach((token) => {
const { lineNumber, type } = token;
if (expectedStyle === "consistent") {
expectedStyle = tokenTypeToStyle[type];
}
addErrorDetailIf(
onError,
lineNumber,
expectedStyle,
tokenTypeToStyle[type]);
});
}
};

View file

@ -48,7 +48,8 @@ const rules = [
require("./md042"),
require("./md043"),
require("./md044"),
require("./md045")
require("./md045"),
require("./md046")
];
rules.forEach((rule) => {
const name = rule.names[0].toLowerCase();

View file

@ -306,6 +306,20 @@ rules.forEach(function forRule(rule) {
}
};
break;
case "MD046":
scheme.properties = {
"style": {
"description": "Block style",
"type": "string",
"enum": [
"consistent",
"fenced",
"indented"
],
"default": "consistent"
}
};
break;
default:
custom = false;
break;

View file

@ -1255,6 +1255,48 @@
"type": "boolean",
"default": true
},
"MD046": {
"description": "MD046/code-block-style - Code block style",
"type": [
"boolean",
"object"
],
"default": true,
"properties": {
"style": {
"description": "Block style",
"type": "string",
"enum": [
"consistent",
"fenced",
"indented"
],
"default": "consistent"
}
},
"additionalProperties": false
},
"code-block-style": {
"description": "MD046/code-block-style - Code block style",
"type": [
"boolean",
"object"
],
"default": true,
"properties": {
"style": {
"description": "Block style",
"type": "string",
"enum": [
"consistent",
"fenced",
"indented"
],
"default": "consistent"
}
},
"additionalProperties": false
},
"headings": {
"description": "headings - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043",
"type": "boolean",
@ -1306,7 +1348,7 @@
"default": true
},
"code": {
"description": "code - MD014, MD031, MD038, MD040",
"description": "code - MD014, MD031, MD038, MD040, MD046",
"type": "boolean",
"default": true
},

View file

@ -48,7 +48,7 @@ Note: Can not break MD025 and MD002 in the same file
```js
```
* list {MD032}
* list {MD032} {MD046:49}
{MD031:50}
@ -71,7 +71,7 @@ Code `with ` space {MD038}
[link with space ](link) {MD039}
```
code fence without language {MD040:73}
code fence without language {MD040:73} {MD046:73}
```
[empty link]() {MD042}

View file

@ -0,0 +1,21 @@
# Code Block in List
Text
```js
// Code
```
Text
1. Item
```js
// Code
```
1. Item
```js
// Code
```

View file

@ -18,7 +18,7 @@ if (true) {
}
```
if (true) {
if (true) { // {MD046}
console.log("true");
if (false) {
console.log("false");

View file

@ -18,7 +18,7 @@ if (true) {
}
```
if (true) {
if (true) { // {MD046}
console.log("true");
if (false) {
console.log("false");

View file

@ -19,7 +19,7 @@ Text text text text text text text text text text text text text text text text
```
```text
Text text text text text text text text text text text text text text text text text text.
Text text text text text text text text text text text text text text text text text text. {MD046:21}
```
Text text text text text text text text text text text text text text text text text text. {MD013}

View file

@ -0,0 +1,11 @@
This is text.
This is a
code block.
And here is more text
```text
and here is a different {MD046:8}
code block
```

View file

@ -27,7 +27,7 @@ The following code block doesn't have any dollar signs, and shouldn't fire:
cat bar
The following (fenced) code block doesn't have any content at all, and
shouldn't fire:
shouldn't fire: {MD046:32}
```bash
```

View file

@ -0,0 +1,6 @@
{
"default": true,
"MD046": {
"style": "fenced"
}
}

17
test/code_block_fenced.md Normal file
View file

@ -0,0 +1,17 @@
This is text.
This is a {MD046}
code block.
And here is more text
```text
This is a code block that won't trigger.
```
But we'll do another:
And this {MD046}
will.
Final text is here

View file

@ -0,0 +1,6 @@
{
"default": true,
"MD046": {
"style": "indented"
}
}

View file

@ -0,0 +1,17 @@
This is text.
This is a
code block.
And here is more text
```text
This is {MD046:8} also a code block.
```
But we'll do another:
And this
will.
Final text is here

View file

@ -17,3 +17,9 @@ MarkDownLint.
A [normal](link) and an [empty one]() and a [fragment](#one).
An image without alternate text ![](image.jpg)
```text
Fenced code
```
Indented code

View file

@ -45,7 +45,7 @@
"errorRange": [25, 13]
},
{
"lineNumber": 20,
"lineNumber": 26,
"ruleNames": [ "MD043", "required-headings", "required-headers" ],
"ruleDescription": "Required heading structure",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md043",
@ -88,5 +88,14 @@
"errorDetail": null,
"errorContext": null,
"errorRange": null
},
{
"lineNumber": 25,
"ruleNames": [ "MD046", "code-block-style" ],
"ruleDescription": "Code block style",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md046",
"errorDetail": "Expected: fenced; Actual: indented",
"errorContext": null,
"errorRange": null
}
]

View file

@ -9,5 +9,5 @@ but this time on multiple lines
<h1>This shouldn't trigger as it's inside a code block</h1>
```text
<p>Neither should this as it's also in a code block</p>
<p>Neither should this as it's also in a code block {MD046:11}</p>
```

View file

@ -9,7 +9,7 @@ This line however, while very long, doesn't have whitespace after the 80th colum
[This long line is comprised entirely of a link](https://example.com "But is inside a code block") {MD013}
```markdown
[This long line is comprised entirely of a link](https://example.com "But is inside a code block") {MD013}
[This long line is comprised entirely of a link](https://example.com "But is inside a code block") {MD013} {MD046:11}
```
This [long line is comprised mostly of a link](https://example.com "This is the long link's title") {MD013}

View file

@ -810,7 +810,8 @@ module.exports.styleAll = function styleAll(test) {
"MD040": [ 73 ],
"MD041": [ 1 ],
"MD042": [ 77 ],
"MD045": [ 81 ]
"MD045": [ 81 ],
"MD046": [ 49, 73 ]
}
};
test.deepEqual(actualResult, expectedResult, "Undetected issues.");
@ -849,7 +850,8 @@ module.exports.styleRelaxed = function styleRelaxed(test) {
"MD035": [ 61 ],
"MD036": [ 65 ],
"MD042": [ 77 ],
"MD045": [ 81 ]
"MD045": [ 81 ],
"MD046": [ 49, 73 ]
}
};
test.deepEqual(actualResult, expectedResult, "Undetected issues.");
@ -1091,7 +1093,7 @@ module.exports.missingStringValue = function missingStringValue(test) {
};
module.exports.readme = function readme(test) {
test.expect(109);
test.expect(111);
const tagToRules = {};
rules.forEach(function forRule(rule) {
rule.tags.forEach(function forTag(tag) {
@ -1157,7 +1159,7 @@ module.exports.readme = function readme(test) {
};
module.exports.doc = function doc(test) {
test.expect(312);
test.expect(320);
fs.readFile("doc/Rules.md", helpers.utf8Encoding,
function readFile(err, contents) {
test.ifError(err);
@ -1885,7 +1887,7 @@ module.exports.configBadHybridSync = function configBadHybridSync(test) {
module.exports.allBuiltInRulesHaveValidUrl =
function allBuiltInRulesHaveValidUrl(test) {
test.expect(123);
test.expect(126);
rules.forEach(function forRule(rule) {
test.ok(rule.information);
test.ok(Object.getPrototypeOf(rule.information) === URL.prototype);

View file

@ -16,7 +16,7 @@ Execute `via the node.js engine`
node.js is runtime
```js
javascript is code
javascript is code {MD046:18}
node.js is runtime
```

View file

@ -52,7 +52,7 @@ HTML <u>javascript</u> {MD033} {MD044}
node.js is runtime {MD044}
```js
javascript is code {MD044}
javascript is code {MD044} {MD046:54}
node.js is runtime {MD044}
```