mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
Existence/Throw/Expansion AST (#5135)
* existence/throw/expansion ast * Style
This commit is contained in:
committed by
Geoffrey Booth
parent
0e37130f2e
commit
088659f893
@@ -5165,6 +5165,16 @@
|
||||
|
||||
eachName(iterator) {}
|
||||
|
||||
astType() {
|
||||
return 'RestElement';
|
||||
}
|
||||
|
||||
astProperties() {
|
||||
return {
|
||||
argument: null
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Expansion.prototype.shouldCache = NO;
|
||||
@@ -5792,6 +5802,16 @@
|
||||
return fragments;
|
||||
}
|
||||
|
||||
astType() {
|
||||
return 'ThrowStatement';
|
||||
}
|
||||
|
||||
astProperties() {
|
||||
return {
|
||||
argument: this.expression.ast()
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Throw.prototype.children = ['expression'];
|
||||
@@ -5857,6 +5877,18 @@
|
||||
return [this.makeCode(o.level <= LEVEL_COND ? code : `(${code})`)];
|
||||
}
|
||||
|
||||
astType() {
|
||||
return 'UnaryExpression';
|
||||
}
|
||||
|
||||
astProperties() {
|
||||
return {
|
||||
argument: this.expression.ast(),
|
||||
operator: '?',
|
||||
prefix: false
|
||||
};
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Existence.prototype.children = ['expression'];
|
||||
|
||||
@@ -3444,6 +3444,12 @@ exports.Expansion = class Expansion extends Base
|
||||
|
||||
eachName: (iterator) ->
|
||||
|
||||
astType: -> 'RestElement'
|
||||
|
||||
astProperties: ->
|
||||
return
|
||||
argument: null
|
||||
|
||||
#### Elision
|
||||
|
||||
# Array elision element (for example, [,a, , , b, , c, ,]).
|
||||
@@ -3869,6 +3875,12 @@ exports.Throw = class Throw extends Base
|
||||
fragments.push @makeCode ';'
|
||||
fragments
|
||||
|
||||
astType: -> 'ThrowStatement'
|
||||
|
||||
astProperties: ->
|
||||
return
|
||||
argument: @expression.ast()
|
||||
|
||||
#### Existence
|
||||
|
||||
# Checks a variable for existence -- not `null` and not `undefined`. This is
|
||||
@@ -3911,6 +3923,14 @@ exports.Existence = class Existence extends Base
|
||||
code = "#{code} #{cmp} #{@comparisonTarget}"
|
||||
[@makeCode(if o.level <= LEVEL_COND then code else "(#{code})")]
|
||||
|
||||
astType: -> 'UnaryExpression'
|
||||
|
||||
astProperties: ->
|
||||
return
|
||||
argument: @expression.ast()
|
||||
operator: '?'
|
||||
prefix: no
|
||||
|
||||
#### Parens
|
||||
|
||||
# An extra set of parentheses, specified explicitly in the source. At one time
|
||||
|
||||
@@ -1270,12 +1270,23 @@ test "AST as expected for Splat node", ->
|
||||
|
||||
# # TODO: Test object splats.
|
||||
|
||||
# test "AST as expected for Expansion node", ->
|
||||
# testExpression '(...) ->',
|
||||
# type: 'Code'
|
||||
# params: [
|
||||
# {type: 'Expansion'}
|
||||
# ]
|
||||
test "AST as expected for Expansion node", ->
|
||||
# testExpression '(...) ->',
|
||||
# type: 'Code'
|
||||
# params: [
|
||||
# {type: 'Expansion'}
|
||||
# ]
|
||||
|
||||
testExpression '[..., b] = c',
|
||||
type: 'AssignmentExpression'
|
||||
left:
|
||||
type: 'ArrayPattern'
|
||||
elements: [
|
||||
type: 'RestElement'
|
||||
argument: null
|
||||
,
|
||||
type: 'Identifier'
|
||||
]
|
||||
|
||||
test "AST as expected for Elision node", ->
|
||||
testExpression '[,,,a,,,b]',
|
||||
@@ -1546,19 +1557,19 @@ test "AST as expected for Op node", ->
|
||||
# base:
|
||||
# type: 'Call'
|
||||
|
||||
# test "AST as expected for Throw node", ->
|
||||
# testExpression 'throw new BallError "catch"',
|
||||
# type: 'Throw'
|
||||
# expression:
|
||||
# type: 'Call'
|
||||
# isNew: yes
|
||||
test "AST as expected for Throw node", ->
|
||||
testExpression 'throw new BallError "catch"',
|
||||
type: 'ThrowStatement'
|
||||
argument:
|
||||
type: 'NewExpression'
|
||||
|
||||
# test "AST as expected for Existence node", ->
|
||||
# testExpression 'Ghosts?',
|
||||
# type: 'Existence',
|
||||
# comparisonTarget: 'null'
|
||||
# expression:
|
||||
# value: 'Ghosts'
|
||||
test "AST as expected for Existence node", ->
|
||||
testExpression 'Ghosts?',
|
||||
type: 'UnaryExpression',
|
||||
argument:
|
||||
name: 'Ghosts'
|
||||
operator: '?'
|
||||
prefix: no
|
||||
|
||||
# # NOTE: Soaking is covered in `Call` and `Access` nodes.
|
||||
|
||||
|
||||
@@ -2055,3 +2055,90 @@ test "AST location data as expected for Assign node", ->
|
||||
end:
|
||||
line: 1
|
||||
column: 15
|
||||
|
||||
test "AST location data as expected for Expansion node", ->
|
||||
testAstLocationData '[..., b] = c',
|
||||
type: 'AssignmentExpression'
|
||||
left:
|
||||
elements: [
|
||||
start: 1
|
||||
end: 4
|
||||
range: [1, 4]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 1
|
||||
end:
|
||||
line: 1
|
||||
column: 4
|
||||
]
|
||||
start: 0
|
||||
end: 8
|
||||
range: [0, 8]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 0
|
||||
end:
|
||||
line: 1
|
||||
column: 8
|
||||
start: 0
|
||||
end: 12
|
||||
range: [0, 12]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 0
|
||||
end:
|
||||
line: 1
|
||||
column: 12
|
||||
|
||||
test "AST location data as expected for Throw node", ->
|
||||
testAstLocationData 'throw new BallError "catch"',
|
||||
type: 'ThrowStatement'
|
||||
argument:
|
||||
start: 6
|
||||
end: 27
|
||||
range: [6, 27]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 6
|
||||
end:
|
||||
line: 1
|
||||
column: 27
|
||||
start: 0
|
||||
end: 27
|
||||
range: [0, 27]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 0
|
||||
end:
|
||||
line: 1
|
||||
column: 27
|
||||
|
||||
test "AST location data as expected for Existence node", ->
|
||||
testAstLocationData 'Ghosts?',
|
||||
type: 'UnaryExpression'
|
||||
argument:
|
||||
start: 0
|
||||
end: 6
|
||||
range: [0, 6]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 0
|
||||
end:
|
||||
line: 1
|
||||
column: 6
|
||||
start: 0
|
||||
end: 7
|
||||
range: [0, 7]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 0
|
||||
end:
|
||||
line: 1
|
||||
column: 7
|
||||
|
||||
Reference in New Issue
Block a user