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
* **[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
* **[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.
@ -102,7 +103,7 @@ Tags group related rules and can be used to enable/disable multiple rules at onc
* **accessibility** - MD045
* **atx** - MD018, MD019
* **atx_closed** - MD020, MD021
* **blank_lines** - MD012, MD022, MD031, MD032
* **blank_lines** - MD012, MD022, MD031, MD032, MD047
* **blockquote** - MD027, MD028
* **bullet** - MD004, MD005, MD006, MD007, MD032
* **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
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("./md044"),
require("./md045"),
require("./md046")
require("./md046"),
require("./md047")
];
rules.forEach((rule) => {
const name = rule.names[0].toLowerCase();

View file

@ -1297,6 +1297,16 @@
},
"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": {
"description": "headings - MD001, MD002, MD003, MD018, MD019, MD020, MD021, MD022, MD023, MD024, MD025, MD026, MD036, MD041, MD043",
"type": "boolean",
@ -1338,7 +1348,7 @@
"default": true
},
"blank_lines": {
"description": "blank_lines - MD012, MD022, MD031, MD032",
"description": "blank_lines - MD012, MD022, MD031, MD032, MD047",
"type": "boolean",
"default": true
},

View file

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

View file

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

View file

@ -45,7 +45,7 @@
"errorRange": [25, 13]
},
{
"lineNumber": 26,
"lineNumber": 27,
"ruleNames": [ "MD043", "required-headings", "required-headers" ],
"ruleDescription": "Required heading structure",
"ruleInformation": "https://github.com/DavidAnson/markdownlint/blob/v0.0.0/doc/Rules.md#md043",
@ -97,5 +97,14 @@
"errorDetail": "Expected: fenced; Actual: indented",
"errorContext": 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
```
code at end of file without newline
code at end of file without newline {MD047:42}
```

View file

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

View file

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

View file

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