UNPKG

5.08 kBtext/coffeescriptView Raw
1{difference} = require './functional-helpers'
2exports = module?.exports ? this
3
4createNode = (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 # not a fold for efficiency's sake
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
36nodeData = [
37 # constructor name, isStatement, construction parameters
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
81for [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## Nodes that contain primitive properties
97
98handlePrimitives = (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
106handlePrimitives AssignmentExpression, ['operator']
107handlePrimitives BinaryExpression, ['operator']
108handlePrimitives GenSym, ['ns', 'uniqueId']
109handlePrimitives Identifier, ['name']
110handlePrimitives Literal, ['value']
111handlePrimitives MemberExpression, ['computed']
112handlePrimitives UnaryExpression, ['operator']
113handlePrimitives UpdateExpression, ['operator', 'prefix']
114handlePrimitives VariableDeclaration, ['kind']
115
116## Nodes that contain list properties
117
118handleLists = (ctor, listProps) -> ctor::listMembers = listProps
119
120handleLists ArrayExpression, ['elements']
121handleLists BlockStatement, ['body']
122handleLists CallExpression, ['arguments']
123handleLists FunctionDeclaration, ['params']
124handleLists FunctionExpression, ['params']
125handleLists NewExpression, ['arguments']
126handleLists ObjectExpression, ['properties']
127handleLists Program, ['body']
128handleLists SequenceExpression, ['expressions']
129handleLists SwitchCase, ['consequent']
130handleLists SwitchStatement, ['cases']
131handleLists TryStatement, ['handlers']
132handleLists VariableDeclaration, ['declarations']