UNPKG

canonical

Version:

Canonical code style linter and formatter for JavaScript, SCSS, CSS and JSON.

1 lines 1.96 kB
'use strict';function _classCallCheck(instance, Constructor){if(!(instance instanceof Constructor)){throw new TypeError('Cannot call a class as a function');}}function _inherits(subClass, superClass){if(typeof superClass !== 'function' && superClass !== null){throw new TypeError('Super expression must either be null or a function, not ' + typeof superClass);}subClass.prototype = Object.create(superClass && superClass.prototype, {constructor:{value:subClass, enumerable:false, writable:true, configurable:true}});if(superClass)subClass.__proto__ = superClass;}var Node=require('./basic-node');var RootNode=(function(_Node){function RootNode(){_classCallCheck(this, RootNode);if(_Node != null){_Node.apply(this, arguments);}}_inherits(RootNode, _Node);RootNode.prototype.buildIndex = function buildIndex(ast, index, indexHasChanged){if(!Array.isArray(ast.content))return;for(var i=0, l=ast.content.length; i < l; i++) {var node=ast.content[i];if(!index[node.type])index[node.type] = [];node.indexHasChanged = indexHasChanged;index[node.type].push({node:node, parent:ast, i:i});this.buildIndex(node, index, indexHasChanged);}};RootNode.prototype.traverseByType = function traverseByType(type, callback){if(!this.index){this.index = {stylesheet:[this]};this.indexHasChanged = [0];this.buildIndex(this, this.index, this.indexHasChanged);}var nodes=this.index[type];var breakLoop;if(!nodes)return;for(var i=0, l=nodes.length; i < l; i++) {if(this.indexHasChanged[0]){this.index = {stylesheet:[this]};this.indexHasChanged = [0];this.buildIndex(this, this.index, this.indexHasChanged);nodes = this.index[type];i += nodes.length - l;l = nodes.length;}var node=nodes[i];breakLoop = callback(node.node, node.i, node.parent);if(breakLoop === null)break;}};RootNode.prototype.traverseByTypes = function traverseByTypes(types, callback){for(var i=0, l=types.length; i < l; i++) {this.traverseByType(types[i], callback);}};return RootNode;})(Node);;module.exports = RootNode;