esquery.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* vim: set sw=4 sts=4 : */
  2. (function () {
  3. var estraverse = require('estraverse');
  4. var parser = require('./parser');
  5. var isArray = Array.isArray || function isArray(array) {
  6. return {}.toString.call(array) === '[object Array]';
  7. };
  8. var LEFT_SIDE = {};
  9. var RIGHT_SIDE = {};
  10. function esqueryModule() {
  11. /**
  12. * Get the value of a property which may be multiple levels down in the object.
  13. */
  14. function getPath(obj, key) {
  15. var i, keys = key.split(".");
  16. for (i = 0; i < keys.length; i++) {
  17. if (obj == null) { return obj; }
  18. obj = obj[keys[i]];
  19. }
  20. return obj;
  21. }
  22. /**
  23. * Determine whether `node` can be reached by following `path`, starting at `ancestor`.
  24. */
  25. function inPath(node, ancestor, path) {
  26. var field, remainingPath, i;
  27. if (path.length === 0) { return node === ancestor; }
  28. if (ancestor == null) { return false; }
  29. field = ancestor[path[0]];
  30. remainingPath = path.slice(1);
  31. if (isArray(field)) {
  32. for (i = 0, l = field.length; i < l; ++i) {
  33. if (inPath(node, field[i], remainingPath)) { return true; }
  34. }
  35. return false;
  36. } else {
  37. return inPath(node, field, remainingPath);
  38. }
  39. }
  40. /**
  41. * Given a `node` and its ancestors, determine if `node` is matched by `selector`.
  42. */
  43. function matches(node, selector, ancestry) {
  44. var path, ancestor, i, l, p;
  45. if (!selector) { return true; }
  46. if (!node) { return false; }
  47. if (!ancestry) { ancestry = []; }
  48. switch(selector.type) {
  49. case 'wildcard':
  50. return true;
  51. case 'identifier':
  52. return selector.value.toLowerCase() === node.type.toLowerCase();
  53. case 'field':
  54. path = selector.name.split('.');
  55. ancestor = ancestry[path.length - 1];
  56. return inPath(node, ancestor, path);
  57. case 'matches':
  58. for (i = 0, l = selector.selectors.length; i < l; ++i) {
  59. if (matches(node, selector.selectors[i], ancestry)) { return true; }
  60. }
  61. return false;
  62. case 'compound':
  63. for (i = 0, l = selector.selectors.length; i < l; ++i) {
  64. if (!matches(node, selector.selectors[i], ancestry)) { return false; }
  65. }
  66. return true;
  67. case 'not':
  68. for (i = 0, l = selector.selectors.length; i < l; ++i) {
  69. if (matches(node, selector.selectors[i], ancestry)) { return false; }
  70. }
  71. return true;
  72. case 'has':
  73. var a, collector = [];
  74. for (i = 0, l = selector.selectors.length; i < l; ++i) {
  75. a = [];
  76. estraverse.traverse(node, {
  77. enter: function (node, parent) {
  78. if (parent != null) { a.unshift(parent); }
  79. if (matches(node, selector.selectors[i], a)) {
  80. collector.push(node);
  81. }
  82. },
  83. leave: function () { a.shift(); }
  84. });
  85. }
  86. return collector.length !== 0;
  87. case 'child':
  88. if (matches(node, selector.right, ancestry)) {
  89. return matches(ancestry[0], selector.left, ancestry.slice(1));
  90. }
  91. return false;
  92. case 'descendant':
  93. if (matches(node, selector.right, ancestry)) {
  94. for (i = 0, l = ancestry.length; i < l; ++i) {
  95. if (matches(ancestry[i], selector.left, ancestry.slice(i + 1))) {
  96. return true;
  97. }
  98. }
  99. }
  100. return false;
  101. case 'attribute':
  102. p = getPath(node, selector.name);
  103. switch (selector.operator) {
  104. case null:
  105. case void 0:
  106. return p != null;
  107. case '=':
  108. switch (selector.value.type) {
  109. case 'regexp': return typeof p === 'string' && selector.value.value.test(p);
  110. case 'literal': return '' + selector.value.value === '' + p;
  111. case 'type': return selector.value.value === typeof p;
  112. }
  113. case '!=':
  114. switch (selector.value.type) {
  115. case 'regexp': return !selector.value.value.test(p);
  116. case 'literal': return '' + selector.value.value !== '' + p;
  117. case 'type': return selector.value.value !== typeof p;
  118. }
  119. case '<=': return p <= selector.value.value;
  120. case '<': return p < selector.value.value;
  121. case '>': return p > selector.value.value;
  122. case '>=': return p >= selector.value.value;
  123. }
  124. case 'sibling':
  125. return matches(node, selector.right, ancestry) &&
  126. sibling(node, selector.left, ancestry, LEFT_SIDE) ||
  127. selector.left.subject &&
  128. matches(node, selector.left, ancestry) &&
  129. sibling(node, selector.right, ancestry, RIGHT_SIDE);
  130. case 'adjacent':
  131. return matches(node, selector.right, ancestry) &&
  132. adjacent(node, selector.left, ancestry, LEFT_SIDE) ||
  133. selector.right.subject &&
  134. matches(node, selector.left, ancestry) &&
  135. adjacent(node, selector.right, ancestry, RIGHT_SIDE);
  136. case 'nth-child':
  137. return matches(node, selector.right, ancestry) &&
  138. nthChild(node, ancestry, function (length) {
  139. return selector.index.value - 1;
  140. });
  141. case 'nth-last-child':
  142. return matches(node, selector.right, ancestry) &&
  143. nthChild(node, ancestry, function (length) {
  144. return length - selector.index.value;
  145. });
  146. case 'class':
  147. if(!node.type) return false;
  148. switch(selector.name.toLowerCase()){
  149. case 'statement':
  150. if(node.type.slice(-9) === 'Statement') return true;
  151. // fallthrough: interface Declaration <: Statement { }
  152. case 'declaration':
  153. return node.type.slice(-11) === 'Declaration';
  154. case 'pattern':
  155. if(node.type.slice(-7) === 'Pattern') return true;
  156. // fallthrough: interface Expression <: Node, Pattern { }
  157. case 'expression':
  158. return node.type.slice(-10) === 'Expression' ||
  159. node.type.slice(-7) === 'Literal' ||
  160. (
  161. node.type === 'Identifier' &&
  162. (ancestry.length === 0 || ancestry[0].type !== 'MetaProperty')
  163. ) ||
  164. node.type === 'MetaProperty';
  165. case 'function':
  166. return node.type.slice(0, 8) === 'Function' ||
  167. node.type === 'ArrowFunctionExpression';
  168. }
  169. throw new Error('Unknown class name: ' + selector.name);
  170. }
  171. throw new Error('Unknown selector type: ' + selector.type);
  172. }
  173. /*
  174. * Determines if the given node has a sibling that matches the given selector.
  175. */
  176. function sibling(node, selector, ancestry, side) {
  177. var parent = ancestry[0], listProp, startIndex, keys, i, l, k, lowerBound, upperBound;
  178. if (!parent) { return false; }
  179. keys = estraverse.VisitorKeys[parent.type];
  180. for (i = 0, l = keys.length; i < l; ++i) {
  181. listProp = parent[keys[i]];
  182. if (isArray(listProp)) {
  183. startIndex = listProp.indexOf(node);
  184. if (startIndex < 0) { continue; }
  185. if (side === LEFT_SIDE) {
  186. lowerBound = 0;
  187. upperBound = startIndex;
  188. } else {
  189. lowerBound = startIndex + 1;
  190. upperBound = listProp.length;
  191. }
  192. for (k = lowerBound; k < upperBound; ++k) {
  193. if (matches(listProp[k], selector, ancestry)) {
  194. return true;
  195. }
  196. }
  197. }
  198. }
  199. return false;
  200. }
  201. /*
  202. * Determines if the given node has an asjacent sibling that matches the given selector.
  203. */
  204. function adjacent(node, selector, ancestry, side) {
  205. var parent = ancestry[0], listProp, keys, i, l, idx;
  206. if (!parent) { return false; }
  207. keys = estraverse.VisitorKeys[parent.type];
  208. for (i = 0, l = keys.length; i < l; ++i) {
  209. listProp = parent[keys[i]];
  210. if (isArray(listProp)) {
  211. idx = listProp.indexOf(node);
  212. if (idx < 0) { continue; }
  213. if (side === LEFT_SIDE && idx > 0 && matches(listProp[idx - 1], selector, ancestry)) {
  214. return true;
  215. }
  216. if (side === RIGHT_SIDE && idx < listProp.length - 1 && matches(listProp[idx + 1], selector, ancestry)) {
  217. return true;
  218. }
  219. }
  220. }
  221. return false;
  222. }
  223. /*
  224. * Determines if the given node is the nth child, determined by idxFn, which is given the containing list's length.
  225. */
  226. function nthChild(node, ancestry, idxFn) {
  227. var parent = ancestry[0], listProp, keys, i, l, idx;
  228. if (!parent) { return false; }
  229. keys = estraverse.VisitorKeys[parent.type];
  230. for (i = 0, l = keys.length; i < l; ++i) {
  231. listProp = parent[keys[i]];
  232. if (isArray(listProp)) {
  233. idx = listProp.indexOf(node);
  234. if (idx >= 0 && idx === idxFn(listProp.length)) { return true; }
  235. }
  236. }
  237. return false;
  238. }
  239. /*
  240. * For each selector node marked as a subject, find the portion of the selector that the subject must match.
  241. */
  242. function subjects(selector, ancestor) {
  243. var results, p;
  244. if (selector == null || typeof selector != 'object') { return []; }
  245. if (ancestor == null) { ancestor = selector; }
  246. results = selector.subject ? [ancestor] : [];
  247. for(p in selector) {
  248. if(!{}.hasOwnProperty.call(selector, p)) { continue; }
  249. [].push.apply(results, subjects(selector[p], p === 'left' ? selector[p] : ancestor));
  250. }
  251. return results;
  252. }
  253. /**
  254. * From a JS AST and a selector AST, collect all JS AST nodes that match the selector.
  255. */
  256. function match(ast, selector) {
  257. var ancestry = [], results = [], altSubjects, i, l, k, m;
  258. if (!selector) { return results; }
  259. altSubjects = subjects(selector);
  260. estraverse.traverse(ast, {
  261. enter: function (node, parent) {
  262. if (parent != null) { ancestry.unshift(parent); }
  263. if (matches(node, selector, ancestry)) {
  264. if (altSubjects.length) {
  265. for (i = 0, l = altSubjects.length; i < l; ++i) {
  266. if (matches(node, altSubjects[i], ancestry)) { results.push(node); }
  267. for (k = 0, m = ancestry.length; k < m; ++k) {
  268. if (matches(ancestry[k], altSubjects[i], ancestry.slice(k + 1))) {
  269. results.push(ancestry[k]);
  270. }
  271. }
  272. }
  273. } else {
  274. results.push(node);
  275. }
  276. }
  277. },
  278. leave: function () { ancestry.shift(); }
  279. });
  280. return results;
  281. }
  282. /**
  283. * Parse a selector string and return its AST.
  284. */
  285. function parse(selector) {
  286. return parser.parse(selector);
  287. }
  288. /**
  289. * Query the code AST using the selector string.
  290. */
  291. function query(ast, selector) {
  292. return match(ast, parse(selector));
  293. }
  294. query.parse = parse;
  295. query.match = match;
  296. query.matches = matches;
  297. return query.query = query;
  298. }
  299. if (typeof define === "function" && define.amd) {
  300. define(esqueryModule);
  301. } else if (typeof module !== 'undefined' && module.exports) {
  302. module.exports = esqueryModule();
  303. } else {
  304. this.esquery = esqueryModule();
  305. }
  306. })();