remark-lint-checkbox-character-style
Advanced tools
Comparing version 1.0.1 to 1.0.2
143
index.js
@@ -20,2 +20,10 @@ /** | ||
* | ||
* ## Fix | ||
* | ||
* [`remark-stringify`](https://github.com/remarkjs/remark/tree/master/packages/remark-stringify) | ||
* formats checked checkboxes using `x` and unchecked checkboxes as ` `. | ||
* | ||
* See [Using remark to fix your markdown](https://github.com/remarkjs/remark-lint#using-remark-to-fix-your-markdown) | ||
* on how to automatically fix warnings for this rule. | ||
* | ||
* @example {"name": "valid.md", "setting": {"checked": "x"}} | ||
@@ -64,101 +72,92 @@ * | ||
'use strict'; | ||
'use strict' | ||
var rule = require('unified-lint-rule'); | ||
var vfileLocation = require('vfile-location'); | ||
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 vfileLocation = require('vfile-location') | ||
var visit = require('unist-util-visit') | ||
var position = require('unist-util-position') | ||
var generated = require('unist-util-generated') | ||
module.exports = rule('remark-lint:checkbox-character-style', checkboxCharacterStyle); | ||
module.exports = rule( | ||
'remark-lint:checkbox-character-style', | ||
checkboxCharacterStyle | ||
) | ||
var start = position.start; | ||
var end = position.end; | ||
var start = position.start | ||
var end = position.end | ||
var CHECKED = {x: true, X: true}; | ||
var UNCHECKED = {' ': true, '\t': true}; | ||
var checked = {x: true, X: true} | ||
var unchecked = {' ': true, '\t': true} | ||
var types = {true: 'checked', false: 'unchecked'} | ||
function checkboxCharacterStyle(tree, file, preferred) { | ||
var contents = file.toString(); | ||
var location = vfileLocation(file); | ||
function checkboxCharacterStyle(tree, file, pref) { | ||
var contents = String(file) | ||
var location = vfileLocation(file) | ||
if (preferred === 'consistent' || typeof preferred !== 'object') { | ||
preferred = {}; | ||
} | ||
pref = typeof pref === 'object' ? pref : {} | ||
if (!preferred.unchecked) { | ||
preferred.unchecked = null; | ||
} | ||
if (!preferred.checked) { | ||
preferred.checked = null; | ||
} | ||
if ( | ||
preferred.unchecked !== null && | ||
UNCHECKED[preferred.unchecked] !== true | ||
) { | ||
if (pref.unchecked && unchecked[pref.unchecked] !== true) { | ||
file.fail( | ||
'Invalid unchecked checkbox marker `' + | ||
preferred.unchecked + | ||
'`: use either `\'\\t\'`, or `\' \'`' | ||
); | ||
pref.unchecked + | ||
"`: use either `'\\t'`, or `' '`" | ||
) | ||
} | ||
if ( | ||
preferred.checked !== null && | ||
CHECKED[preferred.checked] !== true | ||
) { | ||
if (pref.checked && checked[pref.checked] !== true) { | ||
file.fail( | ||
'Invalid checked checkbox marker `' + | ||
preferred.checked + | ||
'`: use either `\'x\'`, or `\'X\'`' | ||
); | ||
pref.checked + | ||
"`: use either `'x'`, or `'X'`" | ||
) | ||
} | ||
visit(tree, 'listItem', visitor); | ||
visit(tree, 'listItem', visitor) | ||
function visitor(node) { | ||
var type; | ||
var initial; | ||
var final; | ||
var stop; | ||
var value; | ||
var style; | ||
var character; | ||
var type | ||
var initial | ||
var final | ||
var value | ||
var style | ||
var character | ||
var reason | ||
/* Exit early for items without checkbox. */ | ||
if (node.checked !== Boolean(node.checked) || generated(node)) { | ||
return; | ||
if (typeof node.checked !== 'boolean' || generated(node)) { | ||
return | ||
} | ||
type = node.checked ? 'checked' : 'unchecked'; | ||
type = types[node.checked] | ||
initial = start(node).offset | ||
final = (node.children.length ? start(node.children[0]) : end(node)).offset | ||
initial = start(node).offset; | ||
final = (node.children.length ? start(node.children[0]) : end(node)).offset; | ||
/* For a checkbox to be parsed, it must be followed by a white space. */ | ||
value = contents | ||
.slice(initial, final) | ||
.trimRight() | ||
.slice(0, -1) | ||
/* For a checkbox to be parsed, it must be followed | ||
* by a white space. */ | ||
value = contents.slice(initial, final).trimRight().slice(0, -1); | ||
/* The checkbox character is behind a square bracket. */ | ||
character = value.charAt(value.length - 1) | ||
style = pref[type] | ||
/* The checkbox character is behind a square | ||
* bracket. */ | ||
character = value.charAt(value.length - 1); | ||
style = preferred[type]; | ||
if (style) { | ||
if (character !== style) { | ||
reason = | ||
type.charAt(0).toUpperCase() + | ||
type.slice(1) + | ||
' checkboxes should use `' + | ||
style + | ||
'` as a marker' | ||
if (style === null) { | ||
preferred[type] = character; | ||
} else if (character !== style) { | ||
stop = initial + value.length; | ||
file.message( | ||
type.charAt(0).toUpperCase() + type.slice(1) + | ||
' checkboxes should use `' + style + '` as a marker', | ||
{ | ||
start: location.toPosition(stop - 1), | ||
end: location.toPosition(stop) | ||
} | ||
); | ||
file.message(reason, { | ||
start: location.toPosition(initial + value.length - 1), | ||
end: location.toPosition(initial + value.length) | ||
}) | ||
} | ||
} else { | ||
pref[type] = character | ||
} | ||
} | ||
} |
{ | ||
"name": "remark-lint-checkbox-character-style", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "remark-lint rule to warn when list item checkboxes violate a given style", | ||
@@ -15,4 +15,4 @@ "license": "MIT", | ||
], | ||
"repository": "https://github.com/wooorm/remark-lint/tree/master/packages/remark-lint-checkbox-character-style", | ||
"bugs": "https://github.com/wooorm/remark-lint/issues", | ||
"repository": "https://github.com/remarkjs/remark-lint/tree/master/packages/remark-lint-checkbox-character-style", | ||
"bugs": "https://github.com/remarkjs/remark-lint/issues", | ||
"author": "Titus Wormer <tituswormer@gmail.com> (http://wooorm.com)", | ||
@@ -19,0 +19,0 @@ "contributors": [ |
@@ -18,2 +18,10 @@ <!--This file is generated--> | ||
## Fix | ||
[`remark-stringify`](https://github.com/remarkjs/remark/tree/master/packages/remark-stringify) | ||
formats checked checkboxes using `x` and unchecked checkboxes as ``. | ||
See [Using remark to fix your markdown](https://github.com/remarkjs/remark-lint#using-remark-to-fix-your-markdown) | ||
on how to automatically fix warnings for this rule. | ||
## Presets | ||
@@ -25,3 +33,3 @@ | ||
| ------ | ------- | | ||
| [`remark-preset-lint-consistent`](https://github.com/wooorm/remark-lint/tree/master/packages/remark-preset-lint-consistent) | | | ||
| [`remark-preset-lint-consistent`](https://github.com/remarkjs/remark-lint/tree/master/packages/remark-preset-lint-consistent) | `'consistent'` | | ||
@@ -181,2 +189,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) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
8327
144
188