|
| 1 | +/** |
| 2 | + * @author Titus Wormer |
| 3 | + * @copyright 2016 Titus Wormer |
| 4 | + * @license MIT |
| 5 | + * @module unist-util-assert |
| 6 | + * @fileoverview Assert `unist` nodes. |
| 7 | + */ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +/* eslint-env commonjs */ |
| 12 | +/* eslint-disable no-useless-concat */ |
| 13 | + |
| 14 | +/* Dependencies. */ |
| 15 | +var assert = require('assert'); |
| 16 | +var array = require('x-is-array'); |
| 17 | +var object = require('x-is-object'); |
| 18 | + |
| 19 | +var inspect; |
| 20 | + |
| 21 | +try { |
| 22 | + inspect = require('ut' + 'il').inspect; |
| 23 | +} catch (err) { /* Empty. */ } |
| 24 | + |
| 25 | +/* Expose. */ |
| 26 | +module.exports = exports = wrap(unist); |
| 27 | + |
| 28 | +exports.parent = wrap(parent); |
| 29 | +exports.text = wrap(text); |
| 30 | +exports.void = wrap(empty); |
| 31 | +exports.wrap = wrap; |
| 32 | + |
| 33 | +/* Identifier to check if a value is seen. */ |
| 34 | +var ID = '__unist__'; |
| 35 | + |
| 36 | +/* List of specced properties. */ |
| 37 | +var defined = ['type', 'value', 'children', 'position']; |
| 38 | + |
| 39 | +/** |
| 40 | + * Wrapper around `Node` which adds the current node |
| 41 | + * (and parent, if available), to the message. |
| 42 | + * |
| 43 | + * @param {Node} node - Node to checl. |
| 44 | + * @param {Node?} [parent] - Parent of `node`. |
| 45 | + * @throws {Error} - Prettified error. |
| 46 | + */ |
| 47 | +function wrap(fn) { |
| 48 | + return wrapped; |
| 49 | + |
| 50 | + function wrapped(node, parent) { |
| 51 | + try { |
| 52 | + fn(node, parent); |
| 53 | + } catch (err) { |
| 54 | + if (!err[ID]) { |
| 55 | + err[ID] = true; |
| 56 | + |
| 57 | + err.message += ': `' + view(node) + '`'; |
| 58 | + |
| 59 | + if (parent) { |
| 60 | + err.message += ' in `' + view(parent) + '`'; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + throw err; |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Assert. |
| 71 | + * |
| 72 | + * @param {Node} node - Value to assert. |
| 73 | + * @throws {Error} - If the given value doesn’t adhere to |
| 74 | + * unist. |
| 75 | + */ |
| 76 | +function unist(node) { |
| 77 | + var type; |
| 78 | + var children; |
| 79 | + var value; |
| 80 | + var key; |
| 81 | + var index; |
| 82 | + var length; |
| 83 | + |
| 84 | + assert.ok(object(node), 'node should be an object'); |
| 85 | + |
| 86 | + type = node.type; |
| 87 | + children = node.children; |
| 88 | + value = node.value; |
| 89 | + |
| 90 | + assert.ok('type' in node, 'node should have a type'); |
| 91 | + assert.equal(typeof type, 'string', '`type` should be a string'); |
| 92 | + assert.notEqual(type, '', '`type` should not be empty'); |
| 93 | + |
| 94 | + if (value != null) { |
| 95 | + assert.equal(typeof value, 'string', '`value` should be a string'); |
| 96 | + } |
| 97 | + |
| 98 | + location(node.position); |
| 99 | + |
| 100 | + for (key in node) { |
| 101 | + if (defined.indexOf(key) === -1) { |
| 102 | + vanilla(key, node[key]); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if (children != null) { |
| 107 | + assert.ok(array(children), '`children` should be an array'); |
| 108 | + index = -1; |
| 109 | + length = children.length; |
| 110 | + |
| 111 | + while (++index < length) { |
| 112 | + exports(children[index], node); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Assert `value` (which lives at `key`) can be stringified |
| 119 | + * and re-parsed to the same (deep) value. |
| 120 | + * |
| 121 | + * @param {*} value - Value to assert. |
| 122 | + * @param {string} key - Property at which `value` lives. |
| 123 | + * @throws {Error} - If the given value doesn’t adhere to |
| 124 | + * `Parent`. |
| 125 | + */ |
| 126 | +function vanilla(key, value) { |
| 127 | + try { |
| 128 | + assert.deepEqual(value, JSON.parse(JSON.stringify(value))); |
| 129 | + } catch (err) { |
| 130 | + assert.fail('', '', 'non-specced property `' + key + '` should be JSON'); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Stringify a value to inspect it. Tries `JSON.stringify()`, |
| 136 | + * and if that fails uses `String()` instead. If `stringify()` |
| 137 | + * works, `` |
| 138 | + * |
| 139 | + * @param {*} value - Value to assert. |
| 140 | + * @param {string} key - Property at which `value` lives. |
| 141 | + * @throws {Error} - If the given value doesn’t adhere to |
| 142 | + * `Parent`. |
| 143 | + */ |
| 144 | +function view(value) { |
| 145 | + try { |
| 146 | + /* eslint-disable no-else-return */ |
| 147 | + /* istanbul ignore else - Browser. */ |
| 148 | + if (inspect) { |
| 149 | + return inspect(value, {colors: false}); |
| 150 | + } else { |
| 151 | + return JSON.stringify(value); |
| 152 | + } |
| 153 | + } catch (err) { |
| 154 | + /* istanbul ignore next - Cyclical. */ |
| 155 | + return String(value); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +/** |
| 160 | + * Assert `node` is a parent node. |
| 161 | + * |
| 162 | + * @param {Node} node - Value to assert. |
| 163 | + * @throws {Error} - If the given value doesn’t adhere to |
| 164 | + * `Parent`. |
| 165 | + */ |
| 166 | +function parent(node) { |
| 167 | + unist(node); |
| 168 | + |
| 169 | + assert.equal('value' in node, false, 'parent should not have `value`'); |
| 170 | + assert.ok('children' in node, 'parent should have `children`'); |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * Assert `node` is a text node. |
| 175 | + * |
| 176 | + * @param {Node} node - Value to assert. |
| 177 | + * @throws {Error} - If the given value doesn’t adhere to |
| 178 | + * `Text`. |
| 179 | + */ |
| 180 | +function text(node) { |
| 181 | + unist(node); |
| 182 | + |
| 183 | + assert.equal('children' in node, false, 'text should not have `children`'); |
| 184 | + assert.ok('value' in node, 'text should have `value`'); |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Assert `node` is a Unist node, but neither parent nor |
| 189 | + * text. |
| 190 | + * |
| 191 | + * @param {Node} node - Value to assert. |
| 192 | + * @throws {Error} - If the given value doesn’t adhere to |
| 193 | + * Unist, is a `Parent`, or a `Text`. |
| 194 | + */ |
| 195 | +function empty(node) { |
| 196 | + unist(node); |
| 197 | + |
| 198 | + assert.equal('value' in node, false, 'void should not have `value`'); |
| 199 | + assert.equal('children' in node, false, 'void should not have `children`'); |
| 200 | +} |
| 201 | + |
| 202 | +/** |
| 203 | + * Assert `location` is a Unist Location. |
| 204 | + * |
| 205 | + * @param {Location} location - Location to assert. |
| 206 | + * @throws {Error} - If the given value doesn’t adhere to |
| 207 | + * Unist Location. |
| 208 | + */ |
| 209 | +function location(location) { |
| 210 | + if (location != null) { |
| 211 | + assert.ok(object(location), '`position` should be an object'); |
| 212 | + |
| 213 | + position(location.start, 'position.start'); |
| 214 | + position(location.end, 'position.end'); |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +/** |
| 219 | + * Assert `location` is a Unist Location. |
| 220 | + * |
| 221 | + * @param {Location} location - Location to assert. |
| 222 | + * @throws {Error} - If the given value doesn’t adhere to |
| 223 | + * Unist Location. |
| 224 | + */ |
| 225 | +function position(position, name) { |
| 226 | + if (position != null) { |
| 227 | + assert.ok(object(position), '`' + name + '` should be an object'); |
| 228 | + |
| 229 | + if (position.line != null) { |
| 230 | + assert.ok('line' in position, '`' + name + '` should have numeric `line`'); |
| 231 | + assert.ok(position.line >= 1, '`' + name + '.line` should be gte `1`'); |
| 232 | + } |
| 233 | + |
| 234 | + if (position.column != null) { |
| 235 | + assert.ok('column' in position, '`' + name + '` should have numeric `column`'); |
| 236 | + assert.ok(position.column >= 1, '`' + name + '.column` should be gte `1`'); |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments