You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. 'use strict';
  2. var isObject = require('isobject');
  3. var define = require('define-property');
  4. var utils = require('snapdragon-util');
  5. var ownNames;
  6. /**
  7. * Create a new AST `Node` with the given `val` and `type`.
  8. *
  9. * ```js
  10. * var node = new Node('*', 'Star');
  11. * var node = new Node({type: 'star', val: '*'});
  12. * ```
  13. * @name Node
  14. * @param {String|Object} `val` Pass a matched substring, or an object to merge onto the node.
  15. * @param {String} `type` The node type to use when `val` is a string.
  16. * @return {Object} node instance
  17. * @api public
  18. */
  19. function Node(val, type, parent) {
  20. if (typeof type !== 'string') {
  21. parent = type;
  22. type = null;
  23. }
  24. define(this, 'parent', parent);
  25. define(this, 'isNode', true);
  26. define(this, 'expect', null);
  27. if (typeof type !== 'string' && isObject(val)) {
  28. lazyKeys();
  29. var keys = Object.keys(val);
  30. for (var i = 0; i < keys.length; i++) {
  31. var key = keys[i];
  32. if (ownNames.indexOf(key) === -1) {
  33. this[key] = val[key];
  34. }
  35. }
  36. } else {
  37. this.type = type;
  38. this.val = val;
  39. }
  40. }
  41. /**
  42. * Returns true if the given value is a node.
  43. *
  44. * ```js
  45. * var Node = require('snapdragon-node');
  46. * var node = new Node({type: 'foo'});
  47. * console.log(Node.isNode(node)); //=> true
  48. * console.log(Node.isNode({})); //=> false
  49. * ```
  50. * @param {Object} `node`
  51. * @returns {Boolean}
  52. * @api public
  53. */
  54. Node.isNode = function(node) {
  55. return utils.isNode(node);
  56. };
  57. /**
  58. * Define a non-enumberable property on the node instance.
  59. * Useful for adding properties that shouldn't be extended
  60. * or visible during debugging.
  61. *
  62. * ```js
  63. * var node = new Node();
  64. * node.define('foo', 'something non-enumerable');
  65. * ```
  66. * @param {String} `name`
  67. * @param {any} `val`
  68. * @return {Object} returns the node instance
  69. * @api public
  70. */
  71. Node.prototype.define = function(name, val) {
  72. define(this, name, val);
  73. return this;
  74. };
  75. /**
  76. * Returns true if `node.val` is an empty string, or `node.nodes` does
  77. * not contain any non-empty text nodes.
  78. *
  79. * ```js
  80. * var node = new Node({type: 'text'});
  81. * node.isEmpty(); //=> true
  82. * node.val = 'foo';
  83. * node.isEmpty(); //=> false
  84. * ```
  85. * @param {Function} `fn` (optional) Filter function that is called on `node` and/or child nodes. `isEmpty` will return false immediately when the filter function returns false on any nodes.
  86. * @return {Boolean}
  87. * @api public
  88. */
  89. Node.prototype.isEmpty = function(fn) {
  90. return utils.isEmpty(this, fn);
  91. };
  92. /**
  93. * Given node `foo` and node `bar`, push node `bar` onto `foo.nodes`, and
  94. * set `foo` as `bar.parent`.
  95. *
  96. * ```js
  97. * var foo = new Node({type: 'foo'});
  98. * var bar = new Node({type: 'bar'});
  99. * foo.push(bar);
  100. * ```
  101. * @param {Object} `node`
  102. * @return {Number} Returns the length of `node.nodes`
  103. * @api public
  104. */
  105. Node.prototype.push = function(node) {
  106. assert(Node.isNode(node), 'expected node to be an instance of Node');
  107. define(node, 'parent', this);
  108. this.nodes = this.nodes || [];
  109. return this.nodes.push(node);
  110. };
  111. /**
  112. * Given node `foo` and node `bar`, unshift node `bar` onto `foo.nodes`, and
  113. * set `foo` as `bar.parent`.
  114. *
  115. * ```js
  116. * var foo = new Node({type: 'foo'});
  117. * var bar = new Node({type: 'bar'});
  118. * foo.unshift(bar);
  119. * ```
  120. * @param {Object} `node`
  121. * @return {Number} Returns the length of `node.nodes`
  122. * @api public
  123. */
  124. Node.prototype.unshift = function(node) {
  125. assert(Node.isNode(node), 'expected node to be an instance of Node');
  126. define(node, 'parent', this);
  127. this.nodes = this.nodes || [];
  128. return this.nodes.unshift(node);
  129. };
  130. /**
  131. * Pop a node from `node.nodes`.
  132. *
  133. * ```js
  134. * var node = new Node({type: 'foo'});
  135. * node.push(new Node({type: 'a'}));
  136. * node.push(new Node({type: 'b'}));
  137. * node.push(new Node({type: 'c'}));
  138. * node.push(new Node({type: 'd'}));
  139. * console.log(node.nodes.length);
  140. * //=> 4
  141. * node.pop();
  142. * console.log(node.nodes.length);
  143. * //=> 3
  144. * ```
  145. * @return {Number} Returns the popped `node`
  146. * @api public
  147. */
  148. Node.prototype.pop = function() {
  149. return this.nodes && this.nodes.pop();
  150. };
  151. /**
  152. * Shift a node from `node.nodes`.
  153. *
  154. * ```js
  155. * var node = new Node({type: 'foo'});
  156. * node.push(new Node({type: 'a'}));
  157. * node.push(new Node({type: 'b'}));
  158. * node.push(new Node({type: 'c'}));
  159. * node.push(new Node({type: 'd'}));
  160. * console.log(node.nodes.length);
  161. * //=> 4
  162. * node.shift();
  163. * console.log(node.nodes.length);
  164. * //=> 3
  165. * ```
  166. * @return {Object} Returns the shifted `node`
  167. * @api public
  168. */
  169. Node.prototype.shift = function() {
  170. return this.nodes && this.nodes.shift();
  171. };
  172. /**
  173. * Remove `node` from `node.nodes`.
  174. *
  175. * ```js
  176. * node.remove(childNode);
  177. * ```
  178. * @param {Object} `node`
  179. * @return {Object} Returns the removed node.
  180. * @api public
  181. */
  182. Node.prototype.remove = function(node) {
  183. assert(Node.isNode(node), 'expected node to be an instance of Node');
  184. this.nodes = this.nodes || [];
  185. var idx = node.index;
  186. if (idx !== -1) {
  187. node.index = -1;
  188. return this.nodes.splice(idx, 1);
  189. }
  190. return null;
  191. };
  192. /**
  193. * Get the first child node from `node.nodes` that matches the given `type`.
  194. * If `type` is a number, the child node at that index is returned.
  195. *
  196. * ```js
  197. * var child = node.find(1); //<= index of the node to get
  198. * var child = node.find('foo'); //<= node.type of a child node
  199. * var child = node.find(/^(foo|bar)$/); //<= regex to match node.type
  200. * var child = node.find(['foo', 'bar']); //<= array of node.type(s)
  201. * ```
  202. * @param {String} `type`
  203. * @return {Object} Returns a child node or undefined.
  204. * @api public
  205. */
  206. Node.prototype.find = function(type) {
  207. return utils.findNode(this.nodes, type);
  208. };
  209. /**
  210. * Return true if the node is the given `type`.
  211. *
  212. * ```js
  213. * var node = new Node({type: 'bar'});
  214. * cosole.log(node.isType('foo')); // false
  215. * cosole.log(node.isType(/^(foo|bar)$/)); // true
  216. * cosole.log(node.isType(['foo', 'bar'])); // true
  217. * ```
  218. * @param {String} `type`
  219. * @return {Boolean}
  220. * @api public
  221. */
  222. Node.prototype.isType = function(type) {
  223. return utils.isType(this, type);
  224. };
  225. /**
  226. * Return true if the `node.nodes` has the given `type`.
  227. *
  228. * ```js
  229. * var foo = new Node({type: 'foo'});
  230. * var bar = new Node({type: 'bar'});
  231. * foo.push(bar);
  232. *
  233. * cosole.log(foo.hasType('qux')); // false
  234. * cosole.log(foo.hasType(/^(qux|bar)$/)); // true
  235. * cosole.log(foo.hasType(['qux', 'bar'])); // true
  236. * ```
  237. * @param {String} `type`
  238. * @return {Boolean}
  239. * @api public
  240. */
  241. Node.prototype.hasType = function(type) {
  242. return utils.hasType(this, type);
  243. };
  244. /**
  245. * Get the siblings array, or `null` if it doesn't exist.
  246. *
  247. * ```js
  248. * var foo = new Node({type: 'foo'});
  249. * var bar = new Node({type: 'bar'});
  250. * var baz = new Node({type: 'baz'});
  251. * foo.push(bar);
  252. * foo.push(baz);
  253. *
  254. * console.log(bar.siblings.length) // 2
  255. * console.log(baz.siblings.length) // 2
  256. * ```
  257. * @return {Array}
  258. * @api public
  259. */
  260. Object.defineProperty(Node.prototype, 'siblings', {
  261. set: function() {
  262. throw new Error('node.siblings is a getter and cannot be defined');
  263. },
  264. get: function() {
  265. return this.parent ? this.parent.nodes : null;
  266. }
  267. });
  268. /**
  269. * Get the node's current index from `node.parent.nodes`.
  270. * This should always be correct, even when the parent adds nodes.
  271. *
  272. * ```js
  273. * var foo = new Node({type: 'foo'});
  274. * var bar = new Node({type: 'bar'});
  275. * var baz = new Node({type: 'baz'});
  276. * var qux = new Node({type: 'qux'});
  277. * foo.push(bar);
  278. * foo.push(baz);
  279. * foo.unshift(qux);
  280. *
  281. * console.log(bar.index) // 1
  282. * console.log(baz.index) // 2
  283. * console.log(qux.index) // 0
  284. * ```
  285. * @return {Number}
  286. * @api public
  287. */
  288. Object.defineProperty(Node.prototype, 'index', {
  289. set: function(index) {
  290. define(this, 'idx', index);
  291. },
  292. get: function() {
  293. if (!Array.isArray(this.siblings)) {
  294. return -1;
  295. }
  296. var tok = this.idx !== -1 ? this.siblings[this.idx] : null;
  297. if (tok !== this) {
  298. this.idx = this.siblings.indexOf(this);
  299. }
  300. return this.idx;
  301. }
  302. });
  303. /**
  304. * Get the previous node from the siblings array or `null`.
  305. *
  306. * ```js
  307. * var foo = new Node({type: 'foo'});
  308. * var bar = new Node({type: 'bar'});
  309. * var baz = new Node({type: 'baz'});
  310. * foo.push(bar);
  311. * foo.push(baz);
  312. *
  313. * console.log(baz.prev.type) // 'bar'
  314. * ```
  315. * @return {Object}
  316. * @api public
  317. */
  318. Object.defineProperty(Node.prototype, 'prev', {
  319. set: function() {
  320. throw new Error('node.prev is a getter and cannot be defined');
  321. },
  322. get: function() {
  323. if (Array.isArray(this.siblings)) {
  324. return this.siblings[this.index - 1] || this.parent.prev;
  325. }
  326. return null;
  327. }
  328. });
  329. /**
  330. * Get the siblings array, or `null` if it doesn't exist.
  331. *
  332. * ```js
  333. * var foo = new Node({type: 'foo'});
  334. * var bar = new Node({type: 'bar'});
  335. * var baz = new Node({type: 'baz'});
  336. * foo.push(bar);
  337. * foo.push(baz);
  338. *
  339. * console.log(bar.siblings.length) // 2
  340. * console.log(baz.siblings.length) // 2
  341. * ```
  342. * @return {Object}
  343. * @api public
  344. */
  345. Object.defineProperty(Node.prototype, 'next', {
  346. set: function() {
  347. throw new Error('node.next is a getter and cannot be defined');
  348. },
  349. get: function() {
  350. if (Array.isArray(this.siblings)) {
  351. return this.siblings[this.index + 1] || this.parent.next;
  352. }
  353. return null;
  354. }
  355. });
  356. /**
  357. * Get the first node from `node.nodes`.
  358. *
  359. * ```js
  360. * var foo = new Node({type: 'foo'});
  361. * var bar = new Node({type: 'bar'});
  362. * var baz = new Node({type: 'baz'});
  363. * var qux = new Node({type: 'qux'});
  364. * foo.push(bar);
  365. * foo.push(baz);
  366. * foo.push(qux);
  367. *
  368. * console.log(foo.first.type) // 'bar'
  369. * ```
  370. * @return {Object} The first node, or undefiend
  371. * @api public
  372. */
  373. Object.defineProperty(Node.prototype, 'first', {
  374. get: function() {
  375. return this.nodes ? this.nodes[0] : null;
  376. }
  377. });
  378. /**
  379. * Get the last node from `node.nodes`.
  380. *
  381. * ```js
  382. * var foo = new Node({type: 'foo'});
  383. * var bar = new Node({type: 'bar'});
  384. * var baz = new Node({type: 'baz'});
  385. * var qux = new Node({type: 'qux'});
  386. * foo.push(bar);
  387. * foo.push(baz);
  388. * foo.push(qux);
  389. *
  390. * console.log(foo.last.type) // 'qux'
  391. * ```
  392. * @return {Object} The last node, or undefiend
  393. * @api public
  394. */
  395. Object.defineProperty(Node.prototype, 'last', {
  396. get: function() {
  397. return this.nodes ? utils.last(this.nodes) : null;
  398. }
  399. });
  400. /**
  401. * Get the last node from `node.nodes`.
  402. *
  403. * ```js
  404. * var foo = new Node({type: 'foo'});
  405. * var bar = new Node({type: 'bar'});
  406. * var baz = new Node({type: 'baz'});
  407. * var qux = new Node({type: 'qux'});
  408. * foo.push(bar);
  409. * foo.push(baz);
  410. * foo.push(qux);
  411. *
  412. * console.log(foo.last.type) // 'qux'
  413. * ```
  414. * @return {Object} The last node, or undefiend
  415. * @api public
  416. */
  417. Object.defineProperty(Node.prototype, 'scope', {
  418. get: function() {
  419. if (this.isScope !== true) {
  420. return this.parent ? this.parent.scope : this;
  421. }
  422. return this;
  423. }
  424. });
  425. /**
  426. * Get own property names from Node prototype, but only the
  427. * first time `Node` is instantiated
  428. */
  429. function lazyKeys() {
  430. if (!ownNames) {
  431. ownNames = Object.getOwnPropertyNames(Node.prototype);
  432. }
  433. }
  434. /**
  435. * Simplified assertion. Throws an error is `val` is falsey.
  436. */
  437. function assert(val, message) {
  438. if (!val) throw new Error(message);
  439. }
  440. /**
  441. * Expose `Node`
  442. */
  443. exports = module.exports = Node;