1 | {difference} = require './functional-helpers'
|
2 | exports = module?.exports ? this
|
3 |
|
4 | createNode = (type, props) ->
|
5 | class extends Nodes
|
6 | constructor: ->
|
7 | @[prop] = arguments[i] for prop, i in props
|
8 | type: type
|
9 | childNodes: props
|
10 |
|
11 | @Nodes = class Nodes
|
12 | listMembers: []
|
13 | instanceof: (ctors...) ->
|
14 |
|
15 | for ctor in ctors when @type is ctor::type
|
16 | return yes
|
17 | no
|
18 | toJSON: ->
|
19 | json = {@type}
|
20 | json.leadingComments = @leadingComments if @leadingComments?
|
21 | for child in @childNodes
|
22 | if child in @listMembers
|
23 | json[child] = (p?.toJSON() for p in @[child])
|
24 | else
|
25 | json[child] = @[child]?.toJSON()
|
26 | if @line? and @column?
|
27 | json.loc = start: {@line, @column}
|
28 | if @offset?
|
29 | json.range = [
|
30 | @offset
|
31 | if @raw? then @offset + @raw.length else undefined
|
32 | ]
|
33 | if @raw? then json.raw = @raw
|
34 | json
|
35 |
|
36 | nodeData = [
|
37 |
|
38 | ['ArrayExpression' , no , ['elements']]
|
39 | ['AssignmentExpression' , no , ['operator', 'left', 'right']]
|
40 | ['BinaryExpression' , no , ['operator', 'left', 'right']]
|
41 | ['BlockStatement' , yes, ['body']]
|
42 | ['BreakStatement' , yes, ['label']]
|
43 | ['CallExpression' , no , ['callee', 'arguments']]
|
44 | ['CatchClause' , yes, ['param', 'body']]
|
45 | ['ConditionalExpression', no , ['test', 'consequent', 'alternate']]
|
46 | ['ContinueStatement' , yes, ['label']]
|
47 | ['DebuggerStatement' , yes, []]
|
48 | ['DoWhileStatement' , yes, ['body', 'test']]
|
49 | ['EmptyStatement' , yes, []]
|
50 | ['ExpressionStatement' , yes, ['expression']]
|
51 | ['ForInStatement' , yes, ['left', 'right', 'body']]
|
52 | ['ForStatement' , yes, ['init', 'test', 'update', 'body']]
|
53 | ['FunctionDeclaration' , yes, ['id', 'params', 'body']]
|
54 | ['FunctionExpression' , no , ['id', 'params', 'body']]
|
55 | ['GenSym' , no , ['ns', 'uniqueId']]
|
56 | ['Identifier' , no , ['name']]
|
57 | ['IfStatement' , yes, ['test', 'consequent', 'alternate']]
|
58 | ['LabeledStatement' , yes, ['label', 'body']]
|
59 | ['Literal' , no , ['value']]
|
60 | ['LogicalExpression' , no , ['left', 'right']]
|
61 | ['MemberExpression' , no , ['computed', 'object', 'property']]
|
62 | ['NewExpression' , no , ['callee', 'arguments']]
|
63 | ['ObjectExpression' , no , ['properties']]
|
64 | ['Program' , yes, ['body']]
|
65 | ['Property' , yes, ['key', 'value']]
|
66 | ['ReturnStatement' , yes, ['argument']]
|
67 | ['SequenceExpression' , no , ['expressions']]
|
68 | ['SwitchCase' , yes, ['test', 'consequent']]
|
69 | ['SwitchStatement' , yes, ['discriminant', 'cases']]
|
70 | ['ThisExpression' , no , []]
|
71 | ['ThrowStatement' , yes, ['argument']]
|
72 | ['TryStatement' , yes, ['block', 'handlers', 'finalizer']]
|
73 | ['UnaryExpression' , no , ['operator', 'argument']]
|
74 | ['UpdateExpression' , no , ['operator', 'prefix', 'argument']]
|
75 | ['VariableDeclaration' , yes, ['kind', 'declarations']]
|
76 | ['VariableDeclarator' , yes, ['id', 'init']]
|
77 | ['WhileStatement' , yes, ['test', 'body']]
|
78 | ['WithStatement' , yes, ['object', 'body']]
|
79 | ]
|
80 |
|
81 | for [node, isStatement, params] in nodeData
|
82 | exports[node] = ctor = createNode node, params
|
83 | ctor::isStatement = isStatement
|
84 | ctor::isExpression = not isStatement
|
85 |
|
86 |
|
87 | {
|
88 | Program, BlockStatement, Literal, Identifier, FunctionExpression,
|
89 | CallExpression, SequenceExpression, ArrayExpression, BinaryExpression,
|
90 | UnaryExpression, NewExpression, VariableDeclaration, ObjectExpression,
|
91 | MemberExpression, UpdateExpression, AssignmentExpression, GenSym,
|
92 | FunctionDeclaration, VariableDeclaration, SwitchStatement, SwitchCase,
|
93 | TryStatement
|
94 | } = exports
|
95 |
|
96 |
|
97 |
|
98 | handlePrimitives = (ctor, primitives) ->
|
99 | ctor::childNodes = difference ctor::childNodes, primitives
|
100 | ctor::toJSON = ->
|
101 | json = Nodes::toJSON.call this
|
102 | for primitive in primitives
|
103 | json[primitive] = @[primitive]
|
104 | json
|
105 |
|
106 | handlePrimitives AssignmentExpression, ['operator']
|
107 | handlePrimitives BinaryExpression, ['operator']
|
108 | handlePrimitives GenSym, ['ns', 'uniqueId']
|
109 | handlePrimitives Identifier, ['name']
|
110 | handlePrimitives Literal, ['value']
|
111 | handlePrimitives MemberExpression, ['computed']
|
112 | handlePrimitives UnaryExpression, ['operator']
|
113 | handlePrimitives UpdateExpression, ['operator', 'prefix']
|
114 | handlePrimitives VariableDeclaration, ['kind']
|
115 |
|
116 |
|
117 |
|
118 | handleLists = (ctor, listProps) -> ctor::listMembers = listProps
|
119 |
|
120 | handleLists ArrayExpression, ['elements']
|
121 | handleLists BlockStatement, ['body']
|
122 | handleLists CallExpression, ['arguments']
|
123 | handleLists FunctionDeclaration, ['params']
|
124 | handleLists FunctionExpression, ['params']
|
125 | handleLists NewExpression, ['arguments']
|
126 | handleLists ObjectExpression, ['properties']
|
127 | handleLists Program, ['body']
|
128 | handleLists SequenceExpression, ['expressions']
|
129 | handleLists SwitchCase, ['consequent']
|
130 | handleLists SwitchStatement, ['cases']
|
131 | handleLists TryStatement, ['handlers']
|
132 | handleLists VariableDeclaration, ['declarations']
|