1
0
forked from aniani/vim

patch 9.1.1116: Vim9: super not supported in lambda expressions

Problem:  Vim9: super not supported in lambda expressions
          (Aliaksei Budavei)
Solution: Support using the super keyword in a closure in an instance
          method (Yegappan Lakshmanan)

fixes: #16586
closes: #16647

Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Yegappan Lakshmanan
2025-02-16 16:25:24 +01:00
committed by Christian Brabandt
parent 44831e4bea
commit b5f463ce4f
4 changed files with 190 additions and 104 deletions

View File

@@ -3059,27 +3059,6 @@ def Test_class_extends()
END
v9.CheckSourceFailure(lines, 'E1354: Cannot extend SomeVar', 5)
lines =<< trim END
vim9script
class Base
var name: string
def ToString(): string
return this.name
enddef
endclass
class Child extends Base
var age: number
def ToString(): string
return super.ToString() .. ': ' .. this.age
enddef
endclass
var o = Child.new('John', 42)
assert_equal('John: 42', o.ToString())
END
v9.CheckSourceSuccess(lines)
lines =<< trim END
vim9script
class Child
@@ -3094,49 +3073,6 @@ def Test_class_extends()
END
v9.CheckSourceFailure(lines, 'E1355: Duplicate function: ToString', 9)
lines =<< trim END
vim9script
class Child
var age: number
def ToString(): string
return super .ToString() .. ': ' .. this.age
enddef
endclass
var o = Child.new(42)
echo o.ToString()
END
v9.CheckSourceFailure(lines, 'E1356: "super" must be followed by a dot', 1)
lines =<< trim END
vim9script
class Base
var name: string
def ToString(): string
return this.name
enddef
endclass
var age = 42
def ToString(): string
return super.ToString() .. ': ' .. age
enddef
echo ToString()
END
v9.CheckSourceFailure(lines, 'E1357: Using "super" not in a class method', 1)
lines =<< trim END
vim9script
class Child
var age: number
def ToString(): string
return super.ToString() .. ': ' .. this.age
enddef
endclass
var o = Child.new(42)
echo o.ToString()
END
v9.CheckSourceFailure(lines, 'E1358: Using "super" not in a child class', 1)
lines =<< trim END
vim9script
class Base
@@ -3244,28 +3180,6 @@ def Test_using_base_class()
END
v9.CheckSourceSuccess(lines)
unlet g:result
# Using super, Child invokes Base method which has optional arg. #12471
lines =<< trim END
vim9script
class Base
var success: bool = false
def Method(arg = 0)
this.success = true
enddef
endclass
class Child extends Base
def new()
super.Method()
enddef
endclass
var obj = Child.new()
assert_equal(true, obj.success)
END
v9.CheckSourceSuccess(lines)
enddef
" Test for using a method from the super class
@@ -12409,4 +12323,162 @@ def Test_protected_new_method()
v9.CheckSourceSuccess(lines)
enddef
" Test for using 'super' in a closure function inside an object method
def Test_super_in_closure()
var lines =<< trim END
vim9script
class A
const _value: number
def Fn(): func(any): number
return (_: any) => this._value
enddef
endclass
class B extends A
def Fn(): func(any): number
return (_: any) => super._value
enddef
endclass
assert_equal(100, A.new(100).Fn()(null))
assert_equal(200, B.new(200).Fn()(null))
END
v9.CheckSourceSuccess(lines)
enddef
" Test for using 'super' to access methods and variables
def Test_super_keyword()
var lines =<< trim END
vim9script
class Base
var name: string
def ToString(): string
return this.name
enddef
endclass
class Child extends Base
var age: number
def ToString(): string
return super.ToString() .. ': ' .. this.age
enddef
endclass
var o = Child.new('John', 42)
assert_equal('John: 42', o.ToString())
END
v9.CheckSourceSuccess(lines)
lines =<< trim END
vim9script
class Child
var age: number
def ToString(): string
return super .ToString() .. ': ' .. this.age
enddef
endclass
var o = Child.new(42)
echo o.ToString()
END
v9.CheckSourceFailure(lines, 'E1356: "super" must be followed by a dot', 1)
lines =<< trim END
vim9script
class Base
var name: string
def ToString(): string
return this.name
enddef
endclass
var age = 42
def ToString(): string
return super.ToString() .. ': ' .. age
enddef
echo ToString()
END
v9.CheckSourceFailure(lines, 'E1357: Using "super" not in a class method', 1)
lines =<< trim END
vim9script
class Child
var age: number
def ToString(): string
return super.ToString() .. ': ' .. this.age
enddef
endclass
var o = Child.new(42)
echo o.ToString()
END
v9.CheckSourceFailure(lines, 'E1358: Using "super" not in a child class', 1)
# Using super, Child invokes Base method which has optional arg. #12471
lines =<< trim END
vim9script
class Base
var success: bool = false
def Method(arg = 0)
this.success = true
enddef
endclass
class Child extends Base
def new()
super.Method()
enddef
endclass
var obj = Child.new()
assert_equal(true, obj.success)
END
v9.CheckSourceSuccess(lines)
# Using 'super' to access an object variable in the parent
lines =<< trim END
vim9script
class A
var foo: string = 'xxx'
endclass
class B extends A
def GetString(): string
return super.foo
enddef
endclass
var b: B = B.new()
echo b.GetString()
END
v9.CheckSourceSuccess(lines)
# Using super to access an overriden method in the parent class
lines =<< trim END
vim9script
class A
def Foo(): string
return 'A.Foo'
enddef
endclass
class B extends A
def Foo(): string
return 'B.Foo'
enddef
def Bar(): string
return $'{super.Foo()} {this.Foo()}'
enddef
endclass
var b = B.new()
assert_equal('A.Foo B.Foo', b.Bar())
END
v9.CheckSourceSuccess(lines)
enddef
" vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker