remark-lint-list-item-spacing
Advanced tools
Comparing version 1.1.1 to 1.1.2
139
index.js
@@ -110,101 +110,88 @@ /** | ||
'use strict'; | ||
'use strict' | ||
var rule = require('unified-lint-rule'); | ||
var visit = require('unist-util-visit'); | ||
var position = require('unist-util-position'); | ||
var generated = require('unist-util-generated'); | ||
var rule = require('unified-lint-rule') | ||
var visit = require('unist-util-visit') | ||
var position = require('unist-util-position') | ||
var generated = require('unist-util-generated') | ||
module.exports = rule('remark-lint:list-item-spacing', listItemSpacing); | ||
module.exports = rule('remark-lint:list-item-spacing', listItemSpacing) | ||
var start = position.start; | ||
var end = position.end; | ||
var start = position.start | ||
var end = position.end | ||
function listItemSpacing(ast, file, preferred) { | ||
var blanks = Boolean( | ||
preferred && | ||
typeof preferred === 'object' && | ||
preferred.checkBlanks | ||
); | ||
var reasonLoose = 'Missing new line after list item' | ||
var reasonTight = 'Extraneous new line after list item' | ||
visit(ast, 'list', visitor); | ||
function listItemSpacing(tree, file, pref) { | ||
var blanks = pref && typeof pref === 'object' && Boolean(pref.checkBlanks) | ||
var fn = blanks ? inferBlankLine : inferMultiline | ||
visit(tree, 'list', visitor) | ||
function visitor(node) { | ||
var items = node.children; | ||
var isTightList = true; | ||
var indent = start(node).column; | ||
var type; | ||
var tight = true | ||
var indent | ||
var children | ||
var length | ||
var index | ||
var child | ||
var next | ||
if (generated(node)) { | ||
return; | ||
} | ||
if (!generated(node)) { | ||
children = node.children | ||
length = children.length | ||
index = -1 | ||
items.forEach(infer); | ||
type = isTightList ? 'tight' : 'loose'; | ||
items.forEach(warn); | ||
function infer(item) { | ||
var fn = blanks ? inferBlankLine : inferMultiline; | ||
if (fn(item)) { | ||
isTightList = false; | ||
while (++index < length) { | ||
if (fn(children[index])) { | ||
tight = false | ||
break | ||
} | ||
} | ||
} | ||
function inferBlankLine(item) { | ||
var children = item.children; | ||
var length = children.length; | ||
var index = 0; | ||
var child = children[index]; | ||
var next; | ||
indent = start(node).column | ||
child = children[0] | ||
index = 0 | ||
while (++index < length) { | ||
next = children[index]; | ||
next = children[index] | ||
/* All children in `listItem`s are block. */ | ||
if ((start(next).line - end(child).line) > 1) { | ||
return true; | ||
if (end(child).column > indent !== tight) { | ||
file.message(tight ? reasonTight : reasonLoose, { | ||
start: end(child), | ||
end: start(next) | ||
}) | ||
} | ||
child = next; | ||
child = next | ||
} | ||
return false; | ||
} | ||
} | ||
} | ||
function inferMultiline(item) { | ||
var content = item.children; | ||
var head = content[0]; | ||
var tail = content[content.length - 1]; | ||
return (end(tail).line - start(head).line) > 0; | ||
} | ||
function inferBlankLine(node) { | ||
var children = node.children | ||
var child = children[0] | ||
var length = children.length | ||
var index = 0 | ||
var next | ||
function warn(item, index) { | ||
var next = items[index + 1]; | ||
var isTight = end(item).column > indent; | ||
while (++index < length) { | ||
next = children[index] | ||
/* Ignore last. */ | ||
if (!next) { | ||
return; | ||
} | ||
/* All children in `listItem`s are block. */ | ||
if (start(next).line - end(child).line > 1) { | ||
return true | ||
} | ||
/* Check if the list item's state does (not) | ||
* match the list's state. */ | ||
if (isTight !== isTightList) { | ||
if (type === 'loose') { | ||
file.message('Missing new line after list item', { | ||
start: end(item), | ||
end: start(next) | ||
}); | ||
} else { | ||
file.message('Extraneous new line after list item', { | ||
start: end(item), | ||
end: start(next) | ||
}); | ||
} | ||
} | ||
} | ||
child = next | ||
} | ||
return false | ||
} | ||
function inferMultiline(node) { | ||
var children = node.children | ||
return end(children[children.length - 1]).line - start(children[0]).line > 0 | ||
} |
{ | ||
"name": "remark-lint-list-item-spacing", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "remark-lint rule to warn when list looseness is incorrect", | ||
@@ -15,4 +15,4 @@ "license": "MIT", | ||
], | ||
"repository": "https://github.com/wooorm/remark-lint/tree/master/packages/remark-lint-list-item-spacing", | ||
"bugs": "https://github.com/wooorm/remark-lint/issues", | ||
"repository": "https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-list-item-spacing", | ||
"bugs": "https://github.com/remarkjs/remark-lint/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
@@ -19,0 +19,0 @@ "contributors": [ |
@@ -27,3 +27,3 @@ <!--This file is generated--> | ||
| ------ | ------- | | ||
| [`remark-preset-lint-markdown-style-guide`](https://github.com/wooorm/remark-lint/tree/master/packages/remark-preset-lint-markdown-style-guide) | | | ||
| [`remark-preset-lint-markdown-style-guide`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-preset-lint-markdown-style-guide) | | | ||
@@ -195,2 +195,2 @@ ## Example | ||
[MIT](https://github.com/wooorm/remark-lint/blob/master/LICENSE) © [Titus Wormer](http://wooorm.com) | ||
[MIT](https://github.com/remarkjs/remark-lint/blob/master/LICENSE) © [Titus Wormer](http://wooorm.com) |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
8238
176