BVB Source Codes

reactide Show index.js Source code

Return Download reactide: download index.js Source code - Download reactide Source code - Type:.js
  1. // AST walker module for Mozilla Parser API compatible trees
  2.  
  3. // A simple walk is one where you simply specify callbacks to be
  4. // called on specific nodes. The last two arguments are optional. A
  5. // simple use would be
  6. //
  7. //     walk.simple(myTree, {
  8. //         Expression: function(node) { ... }
  9. //     });
  10. //
  11. // to do something with all expressions. All Parser API node types
  12. // can be used to identify node types, as well as Expression,
  13. // Statement, and ScopeBody, which denote categories of nodes.
  14. //
  15. // The base argument can be used to pass a custom (recursive)
  16. // walker, and state can be used to give this walked an initial
  17. // state.
  18.  
  19. export function simple(node, visitors, base, state, override) {
  20.   if (!base) base = exports.base
  21.   ;(function c(node, st, override) {
  22.     let type = override || node.type, found = visitors[type]
  23.     base[type](node, st, c)
  24.     if (found) found(node, st)
  25.   })(node, state, override)
  26. }
  27.  
  28. // An ancestor walk keeps an array of ancestor nodes (including the
  29. // current node) and passes them to the callback as third parameter
  30. // (and also as state parameter when no other state is present).
  31. export function ancestor(node, visitors, base, state) {
  32.   if (!base) base = exports.base
  33.   let ancestors = []
  34.   ;(function c(node, st, override) {
  35.     let type = override || node.type, found = visitors[type]
  36.     let isNew = node != ancestors[ancestors.length - 1]
  37.     if (isNew) ancestors.push(node)
  38.     base[type](node, st, c)
  39.     if (found) found(node, st || ancestors, ancestors)
  40.     if (isNew) ancestors.pop()
  41.   })(node, state)
  42. }
  43.  
  44. // A recursive walk is one where your functions override the default
  45. // walkers. They can modify and replace the state parameter that's
  46. // threaded through the walk, and can opt how and whether to walk
  47. // their child nodes (by calling their third argument on these
  48. // nodes).
  49. export function recursive(node, state, funcs, base, override) {
  50.   let visitor = funcs ? exports.make(funcs, base) : base
  51.   ;(function c(node, st, override) {
  52.     visitor[override || node.type](node, st, c)
  53.   })(node, state, override)
  54. }
  55.  
  56. function makeTest(test) {
  57.   if (typeof test == "string")
  58.     return type => type == test
  59.   else if (!test)
  60.     return () => true
  61.   else
  62.     return test
  63. }
  64.  
  65. class Found {
  66.   constructor(node, state) { this.node = node; this.state = state }
  67. }
  68.  
  69. // Find a node with a given start, end, and type (all are optional,
  70. // null can be used as wildcard). Returns a {node, state} object, or
  71. // undefined when it doesn't find a matching node.
  72. export function findNodeAt(node, start, end, test, base, state) {
  73.   test = makeTest(test)
  74.   if (!base) base = exports.base
  75.   try {
  76.     ;(function c(node, st, override) {
  77.       let type = override || node.type
  78.       if ((start == null || node.start <= start) &&
  79.           (end == null || node.end >= end))
  80.         base[type](node, st, c)
  81.       if ((start == null || node.start == start) &&
  82.           (end == null || node.end == end) &&
  83.           test(type, node))
  84.         throw new Found(node, st)
  85.     })(node, state)
  86.   } catch (e) {
  87.     if (e instanceof Found) return e
  88.     throw e
  89.   }
  90. }
  91.  
  92. // Find the innermost node of a given type that contains the given
  93. // position. Interface similar to findNodeAt.
  94. export function findNodeAround(node, pos, test, base, state) {
  95.   test = makeTest(test)
  96.   if (!base) base = exports.base
  97.   try {
  98.     ;(function c(node, st, override) {
  99.       let type = override || node.type
  100.       if (node.start > pos || node.end < pos) return
  101.       base[type](node, st, c)
  102.       if (test(type, node)) throw new Found(node, st)
  103.     })(node, state)
  104.   } catch (e) {
  105.     if (e instanceof Found) return e
  106.     throw e
  107.   }
  108. }
  109.  
  110. // Find the outermost matching node after a given position.
  111. export function findNodeAfter(node, pos, test, base, state) {
  112.   test = makeTest(test)
  113.   if (!base) base = exports.base
  114.   try {
  115.     ;(function c(node, st, override) {
  116.       if (node.end < pos) return
  117.       let type = override || node.type
  118.       if (node.start >= pos && test(type, node)) throw new Found(node, st)
  119.       base[type](node, st, c)
  120.     })(node, state)
  121.   } catch (e) {
  122.     if (e instanceof Found) return e
  123.     throw e
  124.   }
  125. }
  126.  
  127. // Find the outermost matching node before a given position.
  128. export function findNodeBefore(node, pos, test, base, state) {
  129.   test = makeTest(test)
  130.   if (!base) base = exports.base
  131.   let max
  132.   ;(function c(node, st, override) {
  133.     if (node.start > pos) return
  134.     let type = override || node.type
  135.     if (node.end <= pos && (!max || max.node.end < node.end) && test(type, node))
  136.       max = new Found(node, st)
  137.     base[type](node, st, c)
  138.   })(node, state)
  139.   return max
  140. }
  141.  
  142. // Fallback to an Object.create polyfill for older environments.
  143. const create = Object.create || function(proto) {
  144.   function Ctor() {}
  145.   Ctor.prototype = proto
  146.   return new Ctor
  147. }
  148.  
  149. // Used to create a custom walker. Will fill in all missing node
  150. // type properties with the defaults.
  151. export function make(funcs, base) {
  152.   if (!base) base = exports.base
  153.   let visitor = create(base)
  154.   for (var type in funcs) visitor[type] = funcs[type]
  155.   return visitor
  156. }
  157.  
  158. function skipThrough(node, st, c) { c(node, st) }
  159. function ignore(_node, _st, _c) {}
  160.  
  161. // Node walkers.
  162.  
  163. export const base = {}
  164.  
  165. base.Program = base.BlockStatement = (node, st, c) => {
  166.   for (let i = 0; i < node.body.length; ++i)
  167.     c(node.body[i], st, "Statement")
  168. }
  169. base.Statement = skipThrough
  170. base.EmptyStatement = ignore
  171. base.ExpressionStatement = base.ParenthesizedExpression =
  172.   (node, st, c) => c(node.expression, st, "Expression")
  173. base.IfStatement = (node, st, c) => {
  174.   c(node.test, st, "Expression")
  175.   c(node.consequent, st, "Statement")
  176.   if (node.alternate) c(node.alternate, st, "Statement")
  177. }
  178. base.LabeledStatement = (node, st, c) => c(node.body, st, "Statement")
  179. base.BreakStatement = base.ContinueStatement = ignore
  180. base.WithStatement = (node, st, c) => {
  181.   c(node.object, st, "Expression")
  182.   c(node.body, st, "Statement")
  183. }
  184. base.SwitchStatement = (node, st, c) => {
  185.   c(node.discriminant, st, "Expression")
  186.   for (let i = 0; i < node.cases.length; ++i) {
  187.     let cs = node.cases[i]
  188.     if (cs.test) c(cs.test, st, "Expression")
  189.     for (let j = 0; j < cs.consequent.length; ++j)
  190.       c(cs.consequent[j], st, "Statement")
  191.   }
  192. }
  193. base.ReturnStatement = base.YieldExpression = (node, st, c) => {
  194.   if (node.argument) c(node.argument, st, "Expression")
  195. }
  196. base.ThrowStatement = base.SpreadElement =
  197.   (node, st, c) => c(node.argument, st, "Expression")
  198. base.TryStatement = (node, st, c) => {
  199.   c(node.block, st, "Statement")
  200.   if (node.handler) c(node.handler, st)
  201.   if (node.finalizer) c(node.finalizer, st, "Statement")
  202. }
  203. base.CatchClause = (node, st, c) => {
  204.   c(node.param, st, "Pattern")
  205.   c(node.body, st, "ScopeBody")
  206. }
  207. base.WhileStatement = base.DoWhileStatement = (node, st, c) => {
  208.   c(node.test, st, "Expression")
  209.   c(node.body, st, "Statement")
  210. }
  211. base.ForStatement = (node, st, c) => {
  212.   if (node.init) c(node.init, st, "ForInit")
  213.   if (node.test) c(node.test, st, "Expression")
  214.   if (node.update) c(node.update, st, "Expression")
  215.   c(node.body, st, "Statement")
  216. }
  217. base.ForInStatement = base.ForOfStatement = (node, st, c) => {
  218.   c(node.left, st, "ForInit")
  219.   c(node.right, st, "Expression")
  220.   c(node.body, st, "Statement")
  221. }
  222. base.ForInit = (node, st, c) => {
  223.   if (node.type == "VariableDeclaration") c(node, st)
  224.   else c(node, st, "Expression")
  225. }
  226. base.DebuggerStatement = ignore
  227.  
  228. base.FunctionDeclaration = (node, st, c) => c(node, st, "Function")
  229. base.VariableDeclaration = (node, st, c) => {
  230.   for (let i = 0; i < node.declarations.length; ++i)
  231.     c(node.declarations[i], st)
  232. }
  233. base.VariableDeclarator = (node, st, c) => {
  234.   c(node.id, st, "Pattern")
  235.   if (node.init) c(node.init, st, "Expression")
  236. }
  237.  
  238. base.Function = (node, st, c) => {
  239.   if (node.id) c(node.id, st, "Pattern")
  240.   for (let i = 0; i < node.params.length; i++)
  241.     c(node.params[i], st, "Pattern")
  242.   c(node.body, st, node.expression ? "ScopeExpression" : "ScopeBody")
  243. }
  244. // FIXME drop these node types in next major version
  245. // (They are awkward, and in ES6 every block can be a scope.)
  246. base.ScopeBody = (node, st, c) => c(node, st, "Statement")
  247. base.ScopeExpression = (node, st, c) => c(node, st, "Expression")
  248.  
  249. base.Pattern = (node, st, c) => {
  250.   if (node.type == "Identifier")
  251.     c(node, st, "VariablePattern")
  252.   else if (node.type == "MemberExpression")
  253.     c(node, st, "MemberPattern")
  254.   else
  255.     c(node, st)
  256. }
  257. base.VariablePattern = ignore
  258. base.MemberPattern = skipThrough
  259. base.RestElement = (node, st, c) => c(node.argument, st, "Pattern")
  260. base.ArrayPattern =  (node, st, c) => {
  261.   for (let i = 0; i < node.elements.length; ++i) {
  262.     let elt = node.elements[i]
  263.     if (elt) c(elt, st, "Pattern")
  264.   }
  265. }
  266. base.ObjectPattern = (node, st, c) => {
  267.   for (let i = 0; i < node.properties.length; ++i)
  268.     c(node.properties[i].value, st, "Pattern")
  269. }
  270.  
  271. base.Expression = skipThrough
  272. base.ThisExpression = base.Super = base.MetaProperty = ignore
  273. base.ArrayExpression = (node, st, c) => {
  274.   for (let i = 0; i < node.elements.length; ++i) {
  275.     let elt = node.elements[i]
  276.     if (elt) c(elt, st, "Expression")
  277.   }
  278. }
  279. base.ObjectExpression = (node, st, c) => {
  280.   for (let i = 0; i < node.properties.length; ++i)
  281.     c(node.properties[i], st)
  282. }
  283. base.FunctionExpression = base.ArrowFunctionExpression = base.FunctionDeclaration
  284. base.SequenceExpression = base.TemplateLiteral = (node, st, c) => {
  285.   for (let i = 0; i < node.expressions.length; ++i)
  286.     c(node.expressions[i], st, "Expression")
  287. }
  288. base.UnaryExpression = base.UpdateExpression = (node, st, c) => {
  289.   c(node.argument, st, "Expression")
  290. }
  291. base.BinaryExpression = base.LogicalExpression = (node, st, c) => {
  292.   c(node.left, st, "Expression")
  293.   c(node.right, st, "Expression")
  294. }
  295. base.AssignmentExpression = base.AssignmentPattern = (node, st, c) => {
  296.   c(node.left, st, "Pattern")
  297.   c(node.right, st, "Expression")
  298. }
  299. base.ConditionalExpression = (node, st, c) => {
  300.   c(node.test, st, "Expression")
  301.   c(node.consequent, st, "Expression")
  302.   c(node.alternate, st, "Expression")
  303. }
  304. base.NewExpression = base.CallExpression = (node, st, c) => {
  305.   c(node.callee, st, "Expression")
  306.   if (node.arguments) for (let i = 0; i < node.arguments.length; ++i)
  307.     c(node.arguments[i], st, "Expression")
  308. }
  309. base.MemberExpression = (node, st, c) => {
  310.   c(node.object, st, "Expression")
  311.   if (node.computed) c(node.property, st, "Expression")
  312. }
  313. base.ExportNamedDeclaration = base.ExportDefaultDeclaration = (node, st, c) => {
  314.   if (node.declaration)
  315.     c(node.declaration, st, node.type == "ExportNamedDeclaration" || node.declaration.id ? "Statement" : "Expression")
  316.   if (node.source) c(node.source, st, "Expression")
  317. }
  318. base.ExportAllDeclaration = (node, st, c) => {
  319.   c(node.source, st, "Expression")
  320. }
  321. base.ImportDeclaration = (node, st, c) => {
  322.   for (let i = 0; i < node.specifiers.length; i++)
  323.     c(node.specifiers[i], st)
  324.   c(node.source, st, "Expression")
  325. }
  326. base.ImportSpecifier = base.ImportDefaultSpecifier = base.ImportNamespaceSpecifier = base.Identifier = base.Literal = ignore
  327.  
  328. base.TaggedTemplateExpression = (node, st, c) => {
  329.   c(node.tag, st, "Expression")
  330.   c(node.quasi, st)
  331. }
  332. base.ClassDeclaration = base.ClassExpression = (node, st, c) => c(node, st, "Class")
  333. base.Class = (node, st, c) => {
  334.   if (node.id) c(node.id, st, "Pattern")
  335.   if (node.superClass) c(node.superClass, st, "Expression")
  336.   for (let i = 0; i < node.body.body.length; i++)
  337.     c(node.body.body[i], st)
  338. }
  339. base.MethodDefinition = base.Property = (node, st, c) => {
  340.   if (node.computed) c(node.key, st, "Expression")
  341.   c(node.value, st, "Expression")
  342. }
  343.  
downloadindex.js Source code - Download reactide Source code
Related Source Codes/Software:
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
postal - 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top