Add MD047/single-trailing-newline for enforcing new lines at the end of file (fixes #89).

This commit is contained in:
KitoW 2019-04-05 12:36:12 +02:00 committed by David Anson
parent 73511ff677
commit a977d0dcbc
40 changed files with 130 additions and 56 deletions

View file

@ -88,6 +88,7 @@ playground for learning and exploring.
* **[MD044](doc/Rules.md#md044)** *proper-names* - Proper names should have the correct capitalization * **[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) * **[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 * **[MD046](doc/Rules.md#md046)** *code-block-style* - Code block style
* **[MD047](doc/Rules.md#md047)** *single-trailing-newline* - Files should end with a single newline character
See [Rules.md](doc/Rules.md) for more details. See [Rules.md](doc/Rules.md) for more details.
@ -102,7 +103,7 @@ Tags group related rules and can be used to enable/disable multiple rules at onc
* **accessibility** - MD045 * **accessibility** - MD045
* **atx** - MD018, MD019 * **atx** - MD018, MD019
* **atx_closed** - MD020, MD021 * **atx_closed** - MD020, MD021
* **blank_lines** - MD012, MD022, MD031, MD032 * **blank_lines** - MD012, MD022, MD031, MD032, MD047
* **blockquote** - MD027, MD028 * **blockquote** - MD027, MD028
* **bullet** - MD004, MD005, MD006, MD007, MD032 * **bullet** - MD004, MD005, MD006, MD007, MD032
* **code** - MD014, MD031, MD038, MD040, MD046 * **code** - MD014, MD031, MD038, MD040, MD046

View file

@ -1576,3 +1576,30 @@ To fix violations of this rule, use a consistent style (either indenting or code
The specified style can be specific (`fenced`, `indented`) or simply require that usage The specified style can be specific (`fenced`, `indented`) or simply require that usage
be consistent within the document (`consistent`). be consistent within the document (`consistent`).
<a name="md047"></a>
## MD047 - Files should end with a single newline character
Tags: blank_lines
Aliases: single-trailing-newline
This rule is triggered when there is not a single newline character at the end of a file.
Example that triggers the rule:
```markdown
# Heading
This file ends without a newline.[EOF]
```
To fix the violation, add a newline character to the end of the file:
```markdown
# Heading
This file ends with a newline.
[EOF]
```

18
lib/md047.js Normal file
View file

@ -0,0 +1,18 @@
// @ts-check
"use strict";
const { addError, isBlankLine } = require("../helpers");
module.exports = {
"names": [ "MD047", "single-trailing-newline" ],
"description": "Files should end with a single newline character",
"tags": [ "blank_lines" ],
"function": function MD047(params, onError) {
const lastLineNumber = params.lines.length;
const lastLine = params.lines[lastLineNumber - 1];
if (!isBlankLine(lastLine)) {
addError(onError, lastLineNumber);
}
}
};

View file

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

View file

@ -1297,6 +1297,16 @@
}, },
"additionalProperties": false "additionalProperties": false
}, },
"MD047": {
"description": "MD047/single-trailing-newline - Files should end with a single newline character",
"type": "boolean",
"default": true
},
"single-trailing-newline": {
"description": "MD047/single-trailing-newline - Files should end with a single newline character",
"type": "boolean",
"default": true
},
"headings": { "headings": {
"description": "headings - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043", "description": "headings - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043",
"type": "boolean", "type": "boolean",
@ -1338,7 +1348,7 @@
"default": true "default": true
}, },
"blank_lines": { "blank_lines": {
"description": "blank_lines - MD012, MD022, MD031, MD032", "description": "blank_lines - MD012, MD022, MD031, MD032, MD047",
"type": "boolean", "type": "boolean",
"default": true "default": true
}, },

View file

@ -2,4 +2,4 @@
## Heading 2 {MD019} ## Heading 2 {MD019}
## Heading 3 {MD019} ## Heading 3 {MD019}

View file

@ -78,4 +78,4 @@ code fence without language {MD040:73} {MD046:73}
markdownLint {MD044} markdownLint {MD044}
![](image.jpg) {MD045} ![](image.jpg) {MD045} {MD047}

View file

@ -1,3 +1,3 @@
* Test X * Test X
* Test Y {MD007} * Test Y {MD007}
* Test Z {MD007} * Test Z {MD007}

View file

@ -11,4 +11,4 @@ Some text
Some more text Some more text
* Item {MD006} * Item {MD006}
* Item * Item

View file

@ -8,4 +8,4 @@ Some text {MD012:3}
with two blank lines in it with two blank lines in it
Some more text Some more text

View file

@ -1,3 +1,3 @@
* Item * Item
* Item * Item
* Item * Item

View file

@ -1,3 +1,3 @@
- Item - Item
- Item - Item
- Item - Item

View file

@ -1,3 +1,3 @@
+ Item + Item
+ Item + Item
+ Item + Item

View file

@ -10,4 +10,4 @@
- a - a
1. a 1. a

View file

@ -23,3 +23,5 @@ Fenced code
``` ```
Indented code Indented code
Missing newline character

View file

@ -45,7 +45,7 @@
"errorRange": [25, 13] "errorRange": [25, 13]
}, },
{ {
"lineNumber": 26, "lineNumber": 27,
"ruleNames": [ "MD043", "required-headings", "required-headers" ], "ruleNames": [ "MD043", "required-headings", "required-headers" ],
"ruleDescription": "Required heading structure", "ruleDescription": "Required heading structure",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md043", "ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md043",
@ -97,5 +97,14 @@
"errorDetail": "Expected: fenced; Actual: indented", "errorDetail": "Expected: fenced; Actual: indented",
"errorContext": null, "errorContext": null,
"errorRange": null "errorRange": null
},
{
"lineNumber": 27,
"ruleNames": [ "MD047", "single-trailing-newline" ],
"ruleDescription": "Files should end with a single newline character",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md047",
"errorDetail": null,
"errorContext": null,
"errorRange": null
} }
] ]

View file

@ -38,5 +38,5 @@ code
text text
``` ```
code at end of file without newline code at end of file without newline {MD047:42}
``` ```

View file

@ -1 +1 @@
## Heading ## Heading

View file

@ -1,2 +1,2 @@
Heading Heading
------- -------

View file

@ -1 +1 @@
# Heading # Heading

View file

@ -1,2 +1,2 @@
Heading Heading
======= =======

View file

@ -8,4 +8,4 @@
## Heading 3 ## Heading 3
{MD024:5} {MD024:7} {MD024:5} {MD024:7}

View file

@ -1,3 +1,3 @@
# Heading 1 # Heading 1
# Heading 2 {MD025} # Heading 2 {MD025}

View file

@ -2,4 +2,4 @@ Some introductory text
# Heading 1 # Heading 1
# Heading 2 # Heading 2

View file

@ -8,4 +8,4 @@
## Heading 5 {MD026}; ## Heading 5 {MD026};
## Heading 6 {MD026}? ## Heading 6 {MD026}?

View file

@ -4,4 +4,4 @@
## Heading 2 ## Heading 2
#### Heading 4 {MD001} #### Heading 4 {MD001}

View file

@ -2,4 +2,4 @@
## Heading 2 ## Heading 2
## Heading 3 ## Heading 3

View file

@ -12,4 +12,4 @@ Heading 4
Some text Some text
Heading 5 Heading 5
--------- ---------

View file

@ -1,4 +1,4 @@
* Item * Item
* Item {MD007} * Item {MD007}
* Item {MD005} * Item {MD005}
* Item {MD007} * Item {MD007}

View file

@ -1,3 +1,3 @@
* Item * Item
+ Item {MD004} + Item {MD004}
- Item {MD004} - Item {MD004}

View file

@ -1,3 +1,3 @@
- Item - Item
* Item {MD004} * Item {MD004}
+ Item {MD004} + Item {MD004}

View file

@ -1,3 +1,3 @@
+ Item + Item
* Item {MD004} * Item {MD004}
- Item {MD004} - Item {MD004}

View file

@ -72,4 +72,4 @@ code
text text
* list (on last line without newline) * list (on last line without newline) {MD047}

View file

@ -250,7 +250,7 @@ module.exports.resultFormattingV1 = function resultFormattingV1(test) {
const options = { const options = {
"strings": { "strings": {
"truncate": "truncate":
"# Multiple spaces inside hashes on closed atx style heading #" "# Multiple spaces inside hashes on closed atx style heading #\n"
}, },
"files": [ "files": [
"./test/atx_heading_spacing.md", "./test/atx_heading_spacing.md",
@ -352,7 +352,7 @@ module.exports.resultFormattingV2 = function resultFormattingV2(test) {
const options = { const options = {
"strings": { "strings": {
"truncate": "truncate":
"# Multiple spaces inside hashes on closed atx style heading #" "# Multiple spaces inside hashes on closed atx style heading #\n"
}, },
"files": [ "files": [
"./test/atx_heading_spacing.md", "./test/atx_heading_spacing.md",
@ -448,14 +448,14 @@ module.exports.stringInputLineEndings = function stringInputLineEndings(test) {
test.expect(2); test.expect(2);
const options = { const options = {
"strings": { "strings": {
"cr": "One\rTwo\r#Three", "cr": "One\rTwo\r#Three\n",
"lf": "One\nTwo\n#Three", "lf": "One\nTwo\n#Three\n",
"crlf": "One\r\nTwo\r\n#Three", "crlf": "One\r\nTwo\r\n#Three\n",
"mixed": "One\rTwo\n#Three", "mixed": "One\rTwo\n#Three\n",
"crnel": "One\r\u0085Two\r\u0085#Three", "crnel": "One\r\u0085Two\r\u0085#Three\n",
"snl": "One\u2424Two\u2424#Three", "snl": "One\u2424Two\u2424#Three\n",
"lsep": "One\u2028Two\u2028#Three", "lsep": "One\u2028Two\u2028#Three\n",
"nel": "One\u0085Two\u0085#Three" "nel": "One\u0085Two\u0085#Three\n"
}, },
"config": defaultConfig, "config": defaultConfig,
"resultVersion": 0 "resultVersion": 0
@ -811,7 +811,8 @@ module.exports.styleAll = function styleAll(test) {
"MD041": [ 1 ], "MD041": [ 1 ],
"MD042": [ 77 ], "MD042": [ 77 ],
"MD045": [ 81 ], "MD045": [ 81 ],
"MD046": [ 49, 73 ] "MD046": [ 49, 73 ],
"MD047": [ 81 ]
} }
}; };
test.deepEqual(actualResult, expectedResult, "Undetected issues."); test.deepEqual(actualResult, expectedResult, "Undetected issues.");
@ -851,7 +852,8 @@ module.exports.styleRelaxed = function styleRelaxed(test) {
"MD036": [ 65 ], "MD036": [ 65 ],
"MD042": [ 77 ], "MD042": [ 77 ],
"MD045": [ 81 ], "MD045": [ 81 ],
"MD046": [ 49, 73 ] "MD046": [ 49, 73 ],
"MD047": [ 81 ]
} }
}; };
test.deepEqual(actualResult, expectedResult, "Undetected issues."); test.deepEqual(actualResult, expectedResult, "Undetected issues.");
@ -917,7 +919,7 @@ module.exports.noInlineConfig = function noInlineConfig(test) {
"", "",
"<!-- markdownlint-enable-->", "<!-- markdownlint-enable-->",
"", "",
"\tTab" "\tTab\n"
].join("\n") ].join("\n")
}, },
"noInlineConfig": true, "noInlineConfig": true,
@ -1093,7 +1095,7 @@ module.exports.missingStringValue = function missingStringValue(test) {
}; };
module.exports.readme = function readme(test) { module.exports.readme = function readme(test) {
test.expect(111); test.expect(113);
const tagToRules = {}; const tagToRules = {};
rules.forEach(function forRule(rule) { rules.forEach(function forRule(rule) {
rule.tags.forEach(function forTag(tag) { rule.tags.forEach(function forTag(tag) {
@ -1159,7 +1161,7 @@ module.exports.readme = function readme(test) {
}; };
module.exports.doc = function doc(test) { module.exports.doc = function doc(test) {
test.expect(320); test.expect(327);
fs.readFile("doc/Rules.md", helpers.utf8Encoding, fs.readFile("doc/Rules.md", helpers.utf8Encoding,
function readFile(err, contents) { function readFile(err, contents) {
test.ifError(err); test.ifError(err);
@ -1859,7 +1861,7 @@ module.exports.configBadHybridSync = function configBadHybridSync(test) {
module.exports.allBuiltInRulesHaveValidUrl = module.exports.allBuiltInRulesHaveValidUrl =
function allBuiltInRulesHaveValidUrl(test) { function allBuiltInRulesHaveValidUrl(test) {
test.expect(126); test.expect(129);
rules.forEach(function forRule(rule) { rules.forEach(function forRule(rule) {
test.ok(rule.information); test.ok(rule.information);
test.ok(Object.getPrototypeOf(rule.information) === URL.prototype); test.ok(Object.getPrototypeOf(rule.information) === URL.prototype);
@ -2216,7 +2218,7 @@ module.exports.customRulesNpmPackage = function customRulesNpmPackage(test) {
const options = { const options = {
"customRules": [ require("./rules/npm") ], "customRules": [ require("./rules/npm") ],
"strings": { "strings": {
"string": "# Text\n\n---\n\nText" "string": "# Text\n\n---\n\nText\n"
}, },
"resultVersion": 0 "resultVersion": 0
}; };
@ -2515,7 +2517,7 @@ module.exports.customRulesOnErrorLazy = function customRulesOnErrorLazy(test) {
} }
], ],
"strings": { "strings": {
"string": "# Heading" "string": "# Heading\n"
} }
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
@ -2602,7 +2604,7 @@ module.exports.markdownItPluginsSingle =
test.expect(2); test.expect(2);
markdownlint({ markdownlint({
"strings": { "strings": {
"string": "# Heading\n\nText [ link ](https://example.com)" "string": "# Heading\n\nText [ link ](https://example.com)\n"
}, },
"markdownItPlugins": [ "markdownItPlugins": [
[ [
@ -2627,7 +2629,7 @@ module.exports.markdownItPluginsMultiple =
test.expect(4); test.expect(4);
markdownlint({ markdownlint({
"strings": { "strings": {
"string": "# Heading\n\nText H~2~0 text 29^th^ text" "string": "# Heading\n\nText H~2~0 text 29^th^ text\n"
}, },
"markdownItPlugins": [ "markdownItPlugins": [
[ pluginSub ], [ pluginSub ],
@ -2680,7 +2682,7 @@ $$
1 1
$$$$ $$$$
2 2
$$` $$\n`
}, },
"markdownItPlugins": [ [ pluginKatex ] ], "markdownItPlugins": [ [ pluginKatex ] ],
"resultVersion": 0 "resultVersion": 0

View file

@ -0,0 +1,4 @@
{
"default": true,
"MD047": false
}

View file

@ -3,4 +3,4 @@
## Heading 2 {MD003} ## ## Heading 2 {MD003} ##
Heading 3 {MD003} Heading 3 {MD003}
----------------- -----------------

View file

@ -3,4 +3,4 @@
## Heading 2 {MD003} ## Heading 2 {MD003}
Heading 3 {MD003} Heading 3 {MD003}
----------------- -----------------

View file

@ -3,4 +3,4 @@ Heading 1
## Heading 2 {MD003} ## Heading 2 {MD003}
## Heading 3 {MD003} ## ## Heading 3 {MD003} ##

View file

@ -4,4 +4,4 @@ One
Two Two
--- ---
{MD043} {MD043} {MD047}

View file

@ -1,3 +1,3 @@
Some text {MD009} Some text {MD009}
Some more text {MD010} Some more text {MD010}
Some more text Some more text