mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
super AST (#5226)
This commit is contained in:
committed by
Geoffrey Booth
parent
82b7bd3a92
commit
0174ee082c
@@ -689,23 +689,17 @@
|
||||
o('SUPER . Property',
|
||||
function() {
|
||||
return new Super(LOC(3)(new Access($3)),
|
||||
[],
|
||||
false,
|
||||
$1);
|
||||
LOC(1)(new Literal($1)));
|
||||
}),
|
||||
o('SUPER INDEX_START Expression INDEX_END',
|
||||
function() {
|
||||
return new Super(LOC(3)(new Index($3)),
|
||||
[],
|
||||
false,
|
||||
$1);
|
||||
LOC(1)(new Literal($1)));
|
||||
}),
|
||||
o('SUPER INDEX_START INDENT Expression OUTDENT INDEX_END',
|
||||
function() {
|
||||
return new Super(LOC(4)(new Index($4)),
|
||||
[],
|
||||
false,
|
||||
$1);
|
||||
LOC(1)(new Literal($1)));
|
||||
})
|
||||
],
|
||||
// A "meta-property" access e.g. `new.target`
|
||||
|
||||
@@ -3009,9 +3009,10 @@
|
||||
|
||||
exports.Super = Super = (function() {
|
||||
class Super extends Base {
|
||||
constructor(accessor) {
|
||||
constructor(accessor, superLiteral) {
|
||||
super();
|
||||
this.accessor = accessor;
|
||||
this.superLiteral = superLiteral;
|
||||
}
|
||||
|
||||
compileNode(o) {
|
||||
@@ -3047,6 +3048,14 @@
|
||||
return fragments;
|
||||
}
|
||||
|
||||
ast(o, level) {
|
||||
var ref1;
|
||||
if (this.accessor != null) {
|
||||
return (new Value(new Super().withLocationDataFrom((ref1 = this.superLiteral) != null ? ref1 : this), [this.accessor]).withLocationDataFrom(this)).ast(o, level);
|
||||
}
|
||||
return super.ast(o, level);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
Super.prototype.children = ['accessor'];
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -394,9 +394,9 @@ grammar =
|
||||
|
||||
# A `super`-based expression that can be used as a value.
|
||||
Super: [
|
||||
o 'SUPER . Property', -> new Super LOC(3)(new Access $3), [], no, $1
|
||||
o 'SUPER INDEX_START Expression INDEX_END', -> new Super LOC(3)(new Index $3), [], no, $1
|
||||
o 'SUPER INDEX_START INDENT Expression OUTDENT INDEX_END', -> new Super LOC(4)(new Index $4), [], no, $1
|
||||
o 'SUPER . Property', -> new Super LOC(3)(new Access $3), LOC(1)(new Literal $1)
|
||||
o 'SUPER INDEX_START Expression INDEX_END', -> new Super LOC(3)(new Index $3), LOC(1)(new Literal $1)
|
||||
o 'SUPER INDEX_START INDENT Expression OUTDENT INDEX_END', -> new Super LOC(4)(new Index $4), LOC(1)(new Literal $1)
|
||||
]
|
||||
|
||||
# A "meta-property" access e.g. `new.target`
|
||||
|
||||
@@ -2030,7 +2030,7 @@ exports.SuperCall = class SuperCall extends Call
|
||||
replacement.compileToFragments o, if o.level is LEVEL_TOP then o.level else LEVEL_LIST
|
||||
|
||||
exports.Super = class Super extends Base
|
||||
constructor: (@accessor) ->
|
||||
constructor: (@accessor, @superLiteral) ->
|
||||
super()
|
||||
|
||||
children: ['accessor']
|
||||
@@ -2062,6 +2062,17 @@ exports.Super = class Super extends Base
|
||||
attachCommentsToNode salvagedComments, @accessor.name if salvagedComments
|
||||
fragments
|
||||
|
||||
ast: (o, level) ->
|
||||
if @accessor?
|
||||
return (
|
||||
new Value(
|
||||
new Super().withLocationDataFrom (@superLiteral ? @)
|
||||
[@accessor]
|
||||
).withLocationDataFrom @
|
||||
).ast o, level
|
||||
|
||||
super o, level
|
||||
|
||||
#### RegexWithInterpolations
|
||||
|
||||
# Regexes with interpolations are in fact just a variation of a `Call` (a
|
||||
|
||||
@@ -802,29 +802,65 @@ test "AST as expected for Call node", ->
|
||||
optional: yes
|
||||
optional: no
|
||||
|
||||
# test "AST as expected for SuperCall node", ->
|
||||
# testExpression 'class child extends parent then constructor: -> super()',
|
||||
# body:
|
||||
# base:
|
||||
# properties: [
|
||||
# value:
|
||||
# body:
|
||||
# base:
|
||||
# type: 'SuperCall'
|
||||
# ]
|
||||
test "AST as expected for SuperCall node", ->
|
||||
testStatement 'class child extends parent then constructor: -> super()',
|
||||
type: 'ClassDeclaration'
|
||||
body:
|
||||
type: 'ClassBody'
|
||||
body: [
|
||||
body:
|
||||
type: 'BlockStatement'
|
||||
body: [
|
||||
type: 'ExpressionStatement'
|
||||
expression:
|
||||
type: 'CallExpression'
|
||||
callee:
|
||||
type: 'Super'
|
||||
]
|
||||
]
|
||||
|
||||
# test "AST as expected for Super node", ->
|
||||
# testExpression 'class child extends parent then func: -> super.prop',
|
||||
# body:
|
||||
# base:
|
||||
# properties: [
|
||||
# value:
|
||||
# body:
|
||||
# base:
|
||||
# type: 'Super'
|
||||
# accessor:
|
||||
# type: 'Access'
|
||||
# ]
|
||||
test "AST as expected for Super node", ->
|
||||
testStatement 'class child extends parent then func: -> super.prop',
|
||||
type: 'ClassDeclaration'
|
||||
body:
|
||||
type: 'ClassBody'
|
||||
body: [
|
||||
body:
|
||||
type: 'BlockStatement'
|
||||
body: [
|
||||
type: 'ExpressionStatement'
|
||||
expression:
|
||||
type: 'MemberExpression'
|
||||
object:
|
||||
type: 'Super'
|
||||
property: ID 'prop'
|
||||
computed: no
|
||||
]
|
||||
]
|
||||
|
||||
testStatement '''
|
||||
class child extends parent
|
||||
func: ->
|
||||
super[prop]()
|
||||
''',
|
||||
type: 'ClassDeclaration'
|
||||
body:
|
||||
type: 'ClassBody'
|
||||
body: [
|
||||
body:
|
||||
type: 'BlockStatement'
|
||||
body: [
|
||||
type: 'ExpressionStatement'
|
||||
expression:
|
||||
type: 'CallExpression'
|
||||
callee:
|
||||
type: 'MemberExpression'
|
||||
object:
|
||||
type: 'Super'
|
||||
property: ID 'prop'
|
||||
computed: yes
|
||||
]
|
||||
]
|
||||
|
||||
test "AST as expected for RegexWithInterpolations node", ->
|
||||
testExpression '///^#{flavor}script$///',
|
||||
|
||||
@@ -867,6 +867,116 @@ test "AST location data as expected for Call node", ->
|
||||
line: 1
|
||||
column: 7
|
||||
|
||||
test "AST location data as expected for SuperCall node", ->
|
||||
testAstLocationData 'class child then constructor: -> super()',
|
||||
type: 'ClassDeclaration'
|
||||
body:
|
||||
body: [
|
||||
body:
|
||||
body: [
|
||||
expression:
|
||||
callee:
|
||||
start: 33
|
||||
end: 38
|
||||
range: [33, 38]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 33
|
||||
end:
|
||||
line: 1
|
||||
column: 38
|
||||
start: 33
|
||||
end: 40
|
||||
range: [33, 40]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 33
|
||||
end:
|
||||
line: 1
|
||||
column: 40
|
||||
]
|
||||
]
|
||||
start: 0
|
||||
end: 40
|
||||
range: [0, 40]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 0
|
||||
end:
|
||||
line: 1
|
||||
column: 40
|
||||
|
||||
test "AST location data as expected for Super node", ->
|
||||
testAstLocationData '''
|
||||
class child extends parent
|
||||
func: ->
|
||||
super[prop]()
|
||||
''',
|
||||
type: 'ClassDeclaration'
|
||||
body:
|
||||
body: [
|
||||
body:
|
||||
body: [
|
||||
expression:
|
||||
callee:
|
||||
object:
|
||||
start: 42
|
||||
end: 47
|
||||
range: [42, 47]
|
||||
loc:
|
||||
start:
|
||||
line: 3
|
||||
column: 4
|
||||
end:
|
||||
line: 3
|
||||
column: 9
|
||||
property:
|
||||
start: 48
|
||||
end: 52
|
||||
range: [48, 52]
|
||||
loc:
|
||||
start:
|
||||
line: 3
|
||||
column: 10
|
||||
end:
|
||||
line: 3
|
||||
column: 14
|
||||
start: 42
|
||||
end: 53
|
||||
range: [42, 53]
|
||||
loc:
|
||||
start:
|
||||
line: 3
|
||||
column: 4
|
||||
end:
|
||||
line: 3
|
||||
column: 15
|
||||
start: 42
|
||||
end: 55
|
||||
range: [42, 55]
|
||||
loc:
|
||||
start:
|
||||
line: 3
|
||||
column: 4
|
||||
end:
|
||||
line: 3
|
||||
column: 17
|
||||
]
|
||||
]
|
||||
start: 0
|
||||
end: 55
|
||||
range: [0, 55]
|
||||
loc:
|
||||
start:
|
||||
line: 1
|
||||
column: 0
|
||||
end:
|
||||
line: 3
|
||||
column: 17
|
||||
|
||||
test "AST location data as expected for Range node", ->
|
||||
testAstLocationData '[x..y]',
|
||||
type: 'Range'
|
||||
|
||||
Reference in New Issue
Block a user