Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 20, 2018
1 parent 0ecce3b commit de18d7d
Show file tree
Hide file tree
Showing 20 changed files with 473 additions and 476 deletions.
3 changes: 3 additions & 0 deletions .prettierignore
@@ -0,0 +1,3 @@
coverage/
mdast-util-assert.js
mdast-util-assert.min.js
177 changes: 106 additions & 71 deletions index.js
@@ -1,28 +1,27 @@
'use strict';
'use strict'

/* Dependencies. */
var assert = require('assert');
var array = require('x-is-array');
var zwitch = require('zwitch');
var mapz = require('mapz');
var unist = require('unist-util-assert');
var assert = require('assert')
var array = require('x-is-array')
var zwitch = require('zwitch')
var mapz = require('mapz')
var unist = require('unist-util-assert')

/* Construct. */
var mdast = zwitch('type');
var mdast = zwitch('type')

/* Expose. */
exports = unist.wrap(mdast);
module.exports = exports;
exports = unist.wrap(mdast)
module.exports = exports

exports.parent = unist.wrap(parent);
exports.text = unist.text;
exports.void = unist.void;
exports.wrap = unist.wrap;
exports.all = mapz(exports, {key: 'children', indices: false});
exports.parent = unist.wrap(parent)
exports.text = unist.text
exports.void = unist.void
exports.wrap = unist.wrap
exports.all = mapz(exports, {key: 'children', indices: false})

/* Core interface. */
mdast.unknown = unknown;
mdast.invalid = unknown;
mdast.unknown = unknown
mdast.invalid = unknown

/* Per-type handling. */
mdast.handlers = {
Expand Down Expand Up @@ -53,184 +52,220 @@ mdast.handlers = {
footnoteReference: unist.wrap(footnoteReference),
table: unist.wrap(table),
html: exports.text
};
}

function unknown(node, ancestor) {
unist(node, ancestor);
unist(node, ancestor)
}

function parent(node) {
unist.parent(node);
exports.all(node);
unist.parent(node)
exports.all(node)
}

function root(node, ancestor) {
parent(node);
parent(node)

assert.strictEqual(ancestor, undefined, '`root` should not have a parent');
assert.strictEqual(ancestor, undefined, '`root` should not have a parent')
}

function list(node) {
parent(node);
parent(node)

if (node.loose != null) {
assert.strictEqual(typeof node.loose, 'boolean', '`loose` must be `boolean`');
assert.strictEqual(
typeof node.loose,
'boolean',
'`loose` must be `boolean`'
)
}

if (node.ordered != null) {
assert.strictEqual(typeof node.ordered, 'boolean', '`ordered` must be `boolean`');
assert.strictEqual(
typeof node.ordered,
'boolean',
'`ordered` must be `boolean`'
)
}

if (!node.ordered) {
assert.ok(node.start == null, 'unordered lists must not have `start`');
assert.ok(node.start == null, 'unordered lists must not have `start`')
} else if (node.start != null) {
assert.strictEqual(typeof node.start, 'number', 'ordered lists must have `start`');
assert.ok(node.start >= 0, '`start` must be gte `0`');
assert.strictEqual(
typeof node.start,
'number',
'ordered lists must have `start`'
)
assert.ok(node.start >= 0, '`start` must be gte `0`')
}
}

function listItem(node) {
parent(node);
parent(node)

if (node.loose != null) {
assert.strictEqual(typeof node.loose, 'boolean', '`loose` must be `boolean`');
assert.strictEqual(
typeof node.loose,
'boolean',
'`loose` must be `boolean`'
)
}

if (node.checked != null) {
assert.strictEqual(typeof node.checked, 'boolean', '`checked` must be `boolean`');
assert.strictEqual(
typeof node.checked,
'boolean',
'`checked` must be `boolean`'
)
}
}

function heading(node) {
parent(node);
parent(node)

assert.ok(node.depth > 0, '`depth` should be gte `1`');
assert.ok(node.depth <= 6, '`depth` should be lte `6`');
assert.ok(node.depth > 0, '`depth` should be gte `1`')
assert.ok(node.depth <= 6, '`depth` should be lte `6`')
}

function code(node) {
unist.text(node);
unist.text(node)

if (node.lang != null) {
assert.strictEqual(typeof node.lang, 'string', '`lang` must be `string`');
assert.strictEqual(typeof node.lang, 'string', '`lang` must be `string`')
}
}

function footnoteDefinition(node) {
parent(node);
parent(node)

assert.strictEqual(typeof node.identifier, 'string', '`footnoteDefinition` must have `identifier`');
assert.strictEqual(
typeof node.identifier,
'string',
'`footnoteDefinition` must have `identifier`'
)
}

function definition(node) {
unist.void(node);
unist.void(node)

assert.strictEqual(typeof node.identifier, 'string', '`identifier` must be `string`');
assert.strictEqual(
typeof node.identifier,
'string',
'`identifier` must be `string`'
)

if (node.url != null) {
assert.strictEqual(typeof node.url, 'string', '`url` must be `string`');
assert.strictEqual(typeof node.url, 'string', '`url` must be `string`')
}

if (node.title != null) {
assert.strictEqual(typeof node.title, 'string', '`title` must be `string`');
assert.strictEqual(typeof node.title, 'string', '`title` must be `string`')
}
}

function link(node) {
parent(node);
parent(node)

if (node.url != null) {
assert.strictEqual(typeof node.url, 'string', '`url` must be `string`');
assert.strictEqual(typeof node.url, 'string', '`url` must be `string`')
}

if (node.title != null) {
assert.strictEqual(typeof node.title, 'string', '`title` must be `string`');
assert.strictEqual(typeof node.title, 'string', '`title` must be `string`')
}
}

function image(node) {
unist.void(node);
unist.void(node)

if (node.url != null) {
assert.strictEqual(typeof node.url, 'string', '`url` must be `string`');
assert.strictEqual(typeof node.url, 'string', '`url` must be `string`')
}

if (node.alt != null) {
assert.strictEqual(typeof node.alt, 'string', '`alt` must be `string`');
assert.strictEqual(typeof node.alt, 'string', '`alt` must be `string`')
}

if (node.title != null) {
assert.strictEqual(typeof node.title, 'string', '`title` must be `string`');
assert.strictEqual(typeof node.title, 'string', '`title` must be `string`')
}
}

function linkReference(node) {
parent(node);
parent(node)

assert.strictEqual(typeof node.identifier, 'string', '`identifier` must be `string`');
assert.strictEqual(
typeof node.identifier,
'string',
'`identifier` must be `string`'
)

if (node.referenceType != null) {
assert.notStrictEqual(
['shortcut', 'collapsed', 'full'].indexOf(node.referenceType),
-1,
'`referenceType` must be `shortcut`, `collapsed`, or `full`'
);
)
}
}

function imageReference(node) {
unist.void(node);
unist.void(node)

assert.strictEqual(
typeof node.identifier,
'string',
'`identifier` must be `string`'
);
)

if (node.alt != null) {
assert.strictEqual(typeof node.alt, 'string', '`alt` must be `string`');
assert.strictEqual(typeof node.alt, 'string', '`alt` must be `string`')
}

if (node.referenceType != null) {
assert.notStrictEqual(
['shortcut', 'collapsed', 'full'].indexOf(node.referenceType),
-1,
'`referenceType` must be `shortcut`, `collapsed`, or `full`'
);
)
}
}

function footnoteReference(node) {
unist.void(node);
unist.void(node)

assert.strictEqual(typeof node.identifier, 'string', '`identifier` must be `string`');
assert.strictEqual(
typeof node.identifier,
'string',
'`identifier` must be `string`'
)
}

function table(node) {
var align;
var val;
var length;
var index;
var align
var val
var length
var index

parent(node);
parent(node)

align = node.align;
align = node.align

if (align != null) {
assert.ok(array(align), '`align` must be `array`');
assert.ok(array(align), '`align` must be `array`')

length = align.length;
index = -1;
length = align.length
index = -1

while (++index < length) {
val = align[index];
val = align[index]

if (val != null) {
assert.notStrictEqual(
['left', 'right', 'center'].indexOf(val),
-1,
'each align in table must be `null, \'left\', \'right\', \'center\'`'
);
"each align in table must be `null, 'left', 'right', 'center'`"
)
}
}
}
Expand Down
21 changes: 14 additions & 7 deletions package.json
Expand Up @@ -28,28 +28,35 @@
"browserify": "^16.0.0",
"esmangle": "^1.0.1",
"nyc": "^12.0.0",
"prettier": "^1.14.2",
"remark-cli": "^5.0.0",
"remark-preset-wooorm": "^4.0.0",
"tape": "^4.0.0",
"xo": "^0.22.0"
},
"scripts": {
"build-md": "remark . --quiet --frail --output",
"format": "remark . -qfo && prettier --write '**/*.js' && xo --fix",
"build-bundle": "browserify index.js -s mdastUtilAssert > mdast-util-assert.js",
"build-mangle": "esmangle < mdast-util-assert.js > mdast-util-assert.min.js",
"build": "npm run build-md && npm run build-bundle && npm run build-mangle",
"lint": "xo",
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test",
"test": "npm run build && npm run lint && npm run test-coverage"
"test": "npm run format && npm run build && npm run test-coverage"
},
"prettier": {
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false,
"semi": false,
"trailingComma": "none"
},
"xo": {
"space": true,
"prettier": true,
"esnext": false,
"rules": {
"no-eq-null": "off",
"eqeqeq": "off",
"guard-for-in": "off"
"eqeqeq": "off"
},
"ignores": [
"mdast-util-assert.js"
Expand Down
12 changes: 6 additions & 6 deletions readme.md
Expand Up @@ -13,17 +13,17 @@ npm install mdast-util-assert
## Usage

```javascript
var assert = require('mdast-util-assert');
var assert = require('mdast-util-assert')

assert({type: 'root', children: []});
assert({type: 'break'});
assert({type: 'listItem', checked: true, children: []});
assert({type: 'root', children: []})
assert({type: 'break'})
assert({type: 'listItem', checked: true, children: []})
// All OK.

assert({children: []});
assert({children: []})
// AssertionError: node should have a type: `{ children: [] }`

assert({type: 'paragraph', value: 'foo'});
assert({type: 'paragraph', value: 'foo'})
// AssertionError: parent should have children: `{ type: 'paragraph', value: 'foo' }`
```

Expand Down

0 comments on commit de18d7d

Please sign in to comment.