Change default value of options.resultVersion to 1 (breaking change).

This commit is contained in:
David Anson 2017-07-05 21:53:21 -07:00
parent 5bea80f5cd
commit 8c34383f80
6 changed files with 116 additions and 142 deletions

View file

@ -337,11 +337,11 @@ Specifies which version of the `result` object to return (see the "Usage" sectio
below for examples). below for examples).
Passing a `resultVersion` of `0` corresponds to the original, simple format where Passing a `resultVersion` of `0` corresponds to the original, simple format where
each error is identified by rule name and line number. This is the default. each error is identified by rule name and line number. This is deprecated.
Passing a `resultVersion` of `1` corresponds to a more detailed format where each Passing a `resultVersion` of `1` corresponds to a detailed format where each error
error includes information about the line number, rule name, alias, description, includes information about the line number, rule name, alias, description, as well
as well as any additional detail or context that is available. as any additional detail or context that is available. This is the default.
#### callback #### callback
@ -437,31 +437,21 @@ markdownlint(options, function callback(err, result) {
Output: Output:
```text ```text
bad.string: 3: MD010 Hard tabs bad.string: 3: MD010/no-hard-tabs Hard tabs [Column: 19]
bad.string: 1: MD018 No space after hash on atx style header bad.string: 1: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#bad.string"]
bad.string: 3: MD018 No space after hash on atx style header bad.string: 3: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#This string fails some rules."]
bad.md: 3: MD010 Hard tabs bad.string: 1: MD041/first-line-h1 First line in file should be a top level header [Context: "#bad.string"]
bad.md: 1: MD018 No space after hash on atx style header bad.md: 3: MD010/no-hard-tabs Hard tabs [Column: 17]
bad.md: 3: MD018 No space after hash on atx style header bad.md: 1: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#bad.md"]
bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#This file fails some rules."]
bad.md: 1: MD041/first-line-h1 First line in file should be a top level header [Context: "#bad.md"]
``` ```
Or invoke `markdownlint.sync` for a synchronous call and/or pass `true` to `toString` Or invoke `markdownlint.sync` for a synchronous call:
to use rule aliases instead of names:
```js ```js
var result = markdownlint.sync(options); var result = markdownlint.sync(options);
console.log(result.toString(true)); console.log(result.toString());
```
Output:
```text
bad.string: 3: no-hard-tabs Hard tabs
bad.string: 1: no-missing-space-atx No space after hash on atx style header
bad.string: 3: no-missing-space-atx No space after hash on atx style header
bad.md: 3: no-hard-tabs Hard tabs
bad.md: 1: no-missing-space-atx No space after hash on atx style header
bad.md: 3: no-missing-space-atx No space after hash on atx style header
``` ```
To examine the `result` object directly: To examine the `result` object directly:
@ -469,45 +459,13 @@ To examine the `result` object directly:
```js ```js
markdownlint(options, function callback(err, result) { markdownlint(options, function callback(err, result) {
if (!err) { if (!err) {
console.dir(result, { "colors": true }); console.dir(result, { "colors": true, "depth": null });
} }
}); });
``` ```
Output: Output:
```json
{
"good.md": {},
"bad.md": {
"MD010": [ 3 ],
"MD018": [ 1, 3 ]
}
}
```
For more detailed error reporting, set `options.resultVersion` to `1`:
```js
var options = {
"files": [ "good.md", "bad.md" ],
"resultVersion": 1
};
```
With that, the output of `result.toString` looks like:
```text
bad.string: 3: MD010/no-hard-tabs Hard tabs [Column: 19]
bad.string: 1: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#bad.string"]
bad.string: 3: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#This string fails some rules."]
bad.md: 3: MD010/no-hard-tabs Hard tabs [Column: 17]
bad.md: 1: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#bad.md"]
bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#This file fails some rules."]
```
And the `result` object becomes:
```json ```json
{ {
"good.md": [], "good.md": [],
@ -532,7 +490,14 @@ And the `result` object becomes:
"ruleDescription": "No space after hash on atx style header", "ruleDescription": "No space after hash on atx style header",
"errorDetail": null, "errorDetail": null,
"errorContext": "#This file fails\tsome rules.", "errorContext": "#This file fails\tsome rules.",
"errorRange": [ 1, 2 ] } "errorRange": [ 1, 2 ] },
{ "lineNumber": 1,
"ruleName": "MD041",
"ruleAlias": "first-line-h1",
"ruleDescription": "First line in file should be a top level header",
"errorDetail": null,
"errorContext": "#bad.md",
"errorRange": null }
] ]
} }
``` ```
@ -564,9 +529,10 @@ Output:
```text ```text
[00:00:00] Starting 'markdownlint'... [00:00:00] Starting 'markdownlint'...
bad.md: 3: MD010 Hard tabs bad.md: 3: MD010/no-hard-tabs Hard tabs [Column: 17]
bad.md: 1: MD018 No space after hash on atx style header bad.md: 1: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#bad.md"]
bad.md: 3: MD018 No space after hash on atx style header bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#This file fails some rules."]
bad.md: 1: MD041/first-line-h1 First line in file should be a top level header [Context: "#bad.md"]
[00:00:00] Finished 'markdownlint' after 10 ms [00:00:00] Finished 'markdownlint' after 10 ms
``` ```
@ -604,9 +570,10 @@ Output:
```text ```text
Running "markdownlint:example" (markdownlint) task Running "markdownlint:example" (markdownlint) task
Warning: Warning:
bad.md: 3: MD010 Hard tabs bad.md: 3: MD010/no-hard-tabs Hard tabs [Column: 17]
bad.md: 1: MD018 No space after hash on atx style header bad.md: 1: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#bad.md"]
bad.md: 3: MD018 No space after hash on atx style header bad.md: 3: MD018/no-missing-space-atx No space after hash on atx style header [Context: "#This file fails some rules."]
bad.md: 1: MD041/first-line-h1 First line in file should be a top level header [Context: "#bad.md"]
Use --force to continue. Use --force to continue.
``` ```

View file

@ -47,8 +47,7 @@
}, },
"config": { "config": {
"MD013": false "MD013": false
}, }
"resultVersion": 1
}; };
var results = window.markdownlint.sync(options); var results = window.markdownlint.sync(options);
violations.innerHTML = results.content.map(function mapResult(result) { violations.innerHTML = results.content.map(function mapResult(result) {

View file

@ -10,28 +10,20 @@ var options = {
} }
}; };
// Uses result.toString for pretty formatting // Makes a synchronous call, using result.toString for pretty formatting
var result = markdownlint.sync(options);
console.log(result.toString());
// Makes an asynchronous call
markdownlint(options, function callback(err, result) { markdownlint(options, function callback(err, result) {
if (!err) { if (!err) {
console.log(result.toString()); console.log(result.toString());
} }
}); });
// Examines the result object directly // Displays the result object directly
markdownlint(options, function callback(err, result) { markdownlint(options, function callback(err, result) {
if (!err) { if (!err) {
console.dir(result, { "colors": true, "depth": null }); console.dir(result, { "colors": true, "depth": null });
} }
}); });
// Again, using resultVersion 1 for more detail
options.resultVersion = 1;
markdownlint(options, function callback(err, result) {
if (!err) {
console.dir(result, { "colors": true, "depth": null });
}
});
// Make a synchronous call, passing true to toString()
var result = markdownlint.sync(options);
console.log(result.toString(true));

View file

@ -213,7 +213,7 @@ function lintContent(
"frontMatterLines": frontMatterLines "frontMatterLines": frontMatterLines
}; };
// Run each rule // Run each rule
var result = (resultVersion === 1) ? [] : {}; var result = (resultVersion === 0) ? {} : [];
rules.forEach(function forRule(rule) { rules.forEach(function forRule(rule) {
// Configure rule // Configure rule
params.options = mergedRules[rule.name]; params.options = mergedRules[rule.name];
@ -258,42 +258,42 @@ function lintContent(
return enabledRulesPerLineNumber[error.lineNumber][rule.name]; return enabledRulesPerLineNumber[error.lineNumber][rule.name];
}) })
.map(function formatResults(error) { .map(function formatResults(error) {
if (resultVersion === 1) { if (resultVersion === 0) {
var range = null; return error.lineNumber;
var regexp = rule.regexp;
if (regexp) {
if (typeof regexp === "function") {
regexp = regexp(params.options);
}
var lineIndex = error.lineNumber - frontMatterLines.length - 1;
var match = lines[lineIndex].match(regexp);
if (match) {
var column = match.index + 1;
var length = match[0].length;
if (match[2]) {
column += match[1].length;
length -= match[1].length;
}
range = [ column, length ];
}
}
return {
"lineNumber": error.lineNumber,
"ruleName": rule.name,
"ruleAlias": rule.aliases[0],
"ruleDescription": rule.desc,
"errorDetail": error.detail,
"errorContext": error.context,
"errorRange": range
};
} }
return error.lineNumber; var range = null;
var regexp = rule.regexp;
if (regexp) {
if (typeof regexp === "function") {
regexp = regexp(params.options);
}
var lineIndex = error.lineNumber - frontMatterLines.length - 1;
var match = lines[lineIndex].match(regexp);
if (match) {
var column = match.index + 1;
var length = match[0].length;
if (match[2]) {
column += match[1].length;
length -= match[1].length;
}
range = [ column, length ];
}
}
return {
"lineNumber": error.lineNumber,
"ruleName": rule.name,
"ruleAlias": rule.aliases[0],
"ruleDescription": rule.desc,
"errorDetail": error.detail,
"errorContext": error.context,
"errorRange": range
};
}); });
if (filteredErrors.length) { if (filteredErrors.length) {
if (resultVersion === 1) { if (resultVersion === 0) {
result.push.apply(result, filteredErrors);
} else {
result[rule.name] = filteredErrors; result[rule.name] = filteredErrors;
} else {
result.push.apply(result, filteredErrors);
} }
} }
} }
@ -356,7 +356,8 @@ function markdownlint(options, callback) {
var frontMatter = (options.frontMatter === undefined) ? var frontMatter = (options.frontMatter === undefined) ?
shared.frontMatterRe : options.frontMatter; shared.frontMatterRe : options.frontMatter;
var noInlineConfig = !!options.noInlineConfig; var noInlineConfig = !!options.noInlineConfig;
var resultVersion = options.resultVersion || 0; var resultVersion = (options.resultVersion === undefined) ?
1 : options.resultVersion;
var synchronous = (callback === markdownlintSynchronousCallback); var synchronous = (callback === markdownlintSynchronousCallback);
var results = new Results(); var results = new Results();
// Helper to lint the next file in the array // Helper to lint the next file in the array

View file

@ -12,7 +12,8 @@ module.exports.typeTestFiles = function typeTestFiles(test) {
var results = markdownlint.sync({ var results = markdownlint.sync({
"strings": { "strings": {
"content": content "content": content
} },
"resultVersion": 0
}); });
var contentLineCount = content.split(shared.newLineRe).length; var contentLineCount = content.split(shared.newLineRe).length;
Object.keys(results.content).forEach(function forKey(ruleName) { Object.keys(results.content).forEach(function forKey(ruleName) {

View file

@ -91,7 +91,7 @@ module.exports.projectFiles = function projectFiles(test) {
}; };
markdownlint(options, function callback(err, actual) { markdownlint(options, function callback(err, actual) {
test.ifError(err); test.ifError(err);
var expected = { "README.md": {} }; var expected = { "README.md": [] };
test.deepEqual(actual, expected, "Issue(s) with project files."); test.deepEqual(actual, expected, "Issue(s) with project files.");
test.done(); test.done();
}); });
@ -104,7 +104,8 @@ module.exports.resultFormattingV0 = function resultFormattingV0(test) {
"./test/atx_header_spacing.md", "./test/atx_header_spacing.md",
"./test/first_header_bad_atx.md" "./test/first_header_bad_atx.md"
], ],
"config": defaultConfig "config": defaultConfig,
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -211,8 +212,7 @@ module.exports.resultFormattingV1 = function resultFormattingV1(test) {
"./test/atx_header_spacing.md", "./test/atx_header_spacing.md",
"./test/first_header_bad_atx.md" "./test/first_header_bad_atx.md"
], ],
"config": defaultConfig, "config": defaultConfig
"resultVersion": 1
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -334,7 +334,8 @@ module.exports.stringInputLineEndings = function stringInputLineEndings(test) {
"crlf": "One\r\nTwo\r\n#Three", "crlf": "One\r\nTwo\r\n#Three",
"mixed": "One\rTwo\n#Three" "mixed": "One\rTwo\n#Three"
}, },
"config": defaultConfig "config": defaultConfig,
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -364,9 +365,9 @@ module.exports.inputOnlyNewline = function inputOnlyNewline(test) {
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
var expectedResult = { var expectedResult = {
"cr": {}, "cr": [],
"lf": {}, "lf": [],
"crlf": {} "crlf": []
}; };
test.deepEqual(actualResult, expectedResult, "Undetected issues."); test.deepEqual(actualResult, expectedResult, "Undetected issues.");
test.done(); test.done();
@ -382,7 +383,8 @@ module.exports.defaultTrue = function defaultTrue(test) {
], ],
"config": { "config": {
"default": true "default": true
} },
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -412,7 +414,8 @@ module.exports.defaultFalse = function defaultFalse(test) {
], ],
"config": { "config": {
"default": false "default": false
} },
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -432,7 +435,8 @@ module.exports.defaultUndefined = function defaultUndefined(test) {
"./test/atx_header_spacing.md", "./test/atx_header_spacing.md",
"./test/first_header_bad_atx.md" "./test/first_header_bad_atx.md"
], ],
"config": {} "config": {},
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -465,7 +469,8 @@ module.exports.disableRules = function disableRules(test) {
"default": true, "default": true,
"MD019": false, "MD019": false,
"first-line-h1": false "first-line-h1": false
} },
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -491,7 +496,8 @@ module.exports.enableRules = function enableRules(test) {
"MD002": true, "MD002": true,
"default": false, "default": false,
"no-multiple-space-atx": true "no-multiple-space-atx": true
} },
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -520,7 +526,8 @@ module.exports.enableRulesMixedCase = function enableRulesMixedCase(test) {
"Md002": true, "Md002": true,
"DeFaUlT": false, "DeFaUlT": false,
"nO-mUlTiPlE-sPaCe-AtX": true "nO-mUlTiPlE-sPaCe-AtX": true
} },
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -548,7 +555,8 @@ module.exports.disableTag = function disableTag(test) {
"config": { "config": {
"default": true, "default": true,
"spaces": false "spaces": false
} },
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -578,7 +586,8 @@ module.exports.enableTag = function enableTag(test) {
"default": false, "default": false,
"spaces": true, "spaces": true,
"notatag": true "notatag": true
} },
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -605,7 +614,8 @@ module.exports.enableTagMixedCase = function enableTagMixedCase(test) {
"DeFaUlT": false, "DeFaUlT": false,
"SpAcEs": true, "SpAcEs": true,
"NoTaTaG": true "NoTaTaG": true
} },
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -636,7 +646,8 @@ module.exports.styleAll = function styleAll(test) {
test.expect(2); test.expect(2);
var options = { var options = {
"files": [ "./test/break-all-the-rules.md" ], "files": [ "./test/break-all-the-rules.md" ],
"config": require("../style/all.json") "config": require("../style/all.json"),
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -690,7 +701,8 @@ module.exports.styleRelaxed = function styleRelaxed(test) {
test.expect(2); test.expect(2);
var options = { var options = {
"files": [ "./test/break-all-the-rules.md" ], "files": [ "./test/break-all-the-rules.md" ],
"config": require("../style/relaxed.json") "config": require("../style/relaxed.json"),
"resultVersion": 0
}; };
markdownlint(options, function callback(err, actualResult) { markdownlint(options, function callback(err, actualResult) {
test.ifError(err); test.ifError(err);
@ -734,7 +746,8 @@ module.exports.nullFrontMatter = function nullFrontMatter(test) {
"config": { "config": {
"default": false, "default": false,
"MD010": true "MD010": true
} },
"resultVersion": 0
}, function callback(err, result) { }, function callback(err, result) {
test.ifError(err); test.ifError(err);
var expectedResult = { var expectedResult = {
@ -759,7 +772,7 @@ module.exports.customFrontMatter = function customFrontMatter(test) {
}, function callback(err, result) { }, function callback(err, result) {
test.ifError(err); test.ifError(err);
var expectedResult = { var expectedResult = {
"content": {} "content": []
}; };
test.deepEqual(result, expectedResult, "Did not get empty results."); test.deepEqual(result, expectedResult, "Did not get empty results.");
test.done(); test.done();
@ -784,7 +797,8 @@ module.exports.noInlineConfig = function noInlineConfig(test) {
"\tTab" "\tTab"
].join("\n") ].join("\n")
}, },
"noInlineConfig": true "noInlineConfig": true,
"resultVersion": 0
}, function callback(err, result) { }, function callback(err, result) {
test.ifError(err); test.ifError(err);
var expectedResult = { var expectedResult = {
@ -840,7 +854,7 @@ module.exports.readmeHeaders = function readmeHeaders(test) {
} }
}, function callback(err, result) { }, function callback(err, result) {
test.ifError(err); test.ifError(err);
var expected = { "README.md": {} }; var expected = { "README.md": [] };
test.deepEqual(result, expected, "Unexpected issues."); test.deepEqual(result, expected, "Unexpected issues.");
test.done(); test.done();
}); });
@ -871,7 +885,7 @@ module.exports.filesArrayAsString = function filesArrayAsString(test) {
} }
}, function callback(err, actual) { }, function callback(err, actual) {
test.ifError(err); test.ifError(err);
var expected = { "README.md": {} }; var expected = { "README.md": [] };
test.deepEqual(actual, expected, "Unexpected issues."); test.deepEqual(actual, expected, "Unexpected issues.");
test.done(); test.done();
}); });
@ -941,9 +955,9 @@ module.exports.missingStringValue = function missingStringValue(test) {
}, function callback(err, result) { }, function callback(err, result) {
test.ifError(err); test.ifError(err);
var expectedResult = { var expectedResult = {
"undefined": {}, "undefined": [],
"null": {}, "null": [],
"empty": {} "empty": []
}; };
test.deepEqual(result, expectedResult, "Did not get empty results."); test.deepEqual(result, expectedResult, "Did not get empty results.");
test.done(); test.done();