checker: require `params` attribute to use struct as keyword arguments in function (#11135)

pull/11489/head
Daniel Däschle 2021-09-13 23:05:30 +02:00 committed by GitHub
parent ae41d1d3c6
commit b63ec8fbcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 27 additions and 8 deletions

View File

@ -1,11 +1,12 @@
module gx module gx
// TODO: remove these and uae the enum everywhere // TODO: remove these, and use the enum everywhere
pub const ( pub const (
align_left = HorizontalAlign.left align_left = HorizontalAlign.left
align_right = HorizontalAlign.right align_right = HorizontalAlign.right
) )
[params]
pub struct TextCfg { pub struct TextCfg {
pub: pub:
color Color = black color Color = black

View File

@ -8,6 +8,7 @@ const (
retries = 10000 retries = 10000
) )
[params]
pub struct TempFileOptions { pub struct TempFileOptions {
path string = os.temp_dir() path string = os.temp_dir()
pattern string pattern string
@ -44,6 +45,7 @@ pub fn temp_file(tfo TempFileOptions) ?(os.File, string) {
' could not create temporary file in "$d". Retry limit ($util.retries) exhausted. Please ensure write permissions.') ' could not create temporary file in "$d". Retry limit ($util.retries) exhausted. Please ensure write permissions.')
} }
[params]
pub struct TempDirOptions { pub struct TempDirOptions {
path string = os.temp_dir() path string = os.temp_dir()
pattern string pattern string

View File

@ -97,6 +97,7 @@ pub fn integer_from_u64(value u64) Integer {
} }
} }
[params]
pub struct IntegerConfig { pub struct IntegerConfig {
signum int = 1 signum int = 1
} }

View File

@ -533,6 +533,7 @@ pub fn (h Header) keys() []string {
return h.data.keys() return h.data.keys()
} }
[params]
pub struct HeaderRenderConfig { pub struct HeaderRenderConfig {
version Version version Version
coerce bool coerce bool

View File

@ -7,6 +7,7 @@ import rand.seed
// generator WyRand used 64 bits, ie. 2 u32s so that is the default. In case your desired generator // generator WyRand used 64 bits, ie. 2 u32s so that is the default. In case your desired generator
// uses a different number of u32s, use the `seed.time_seed_array()` method with the correct // uses a different number of u32s, use the `seed.time_seed_array()` method with the correct
// number of u32s. // number of u32s.
[params]
pub struct PRNGConfigStruct { pub struct PRNGConfigStruct {
pub: pub:
seed_ []u32 = seed.time_seed_array(2) seed_ []u32 = seed.time_seed_array(2)

View File

@ -3,6 +3,7 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module time module time
[params]
pub struct StopWatchOptions { pub struct StopWatchOptions {
auto_start bool = true auto_start bool = true
} }

View File

@ -352,6 +352,7 @@ pub fn (t &Table) type_find_method(s &TypeSymbol, name string) ?Fn {
return none return none
} }
[params]
pub struct GetEmbedsOptions { pub struct GetEmbedsOptions {
preceding []Type preceding []Type
} }

View File

@ -2088,15 +2088,17 @@ pub fn (mut c Checker) check_expected_arg_count(mut node ast.CallExpr, f &ast.Fn
if min_required_params == nr_args + 1 { if min_required_params == nr_args + 1 {
last_typ := f.params.last().typ last_typ := f.params.last().typ
last_sym := c.table.get_type_symbol(last_typ) last_sym := c.table.get_type_symbol(last_typ)
if last_sym.kind == .struct_ { if last_sym.info is ast.Struct {
// allow empty trailing struct syntax arg (`f()` where `f` is `fn(ConfigStruct)`) is_params := last_sym.info.attrs.filter(it.name == 'params' && !it.has_arg).len > 0
node.args << ast.CallArg{ if is_params {
expr: ast.StructInit{ // allow empty trailing struct syntax arg (`f()` where `f` is `fn(ConfigStruct)`)
typ: last_typ node.args << ast.CallArg{
expr: ast.StructInit{
typ: last_typ
}
} }
typ: last_typ return
} }
return
} }
} }
c.error('expected $min_required_params arguments, but got $nr_args', node.pos) c.error('expected $min_required_params arguments, but got $nr_args', node.pos)

View File

@ -14,6 +14,7 @@ mut:
max_type_len int max_type_len int
} }
[params]
struct AddInfoConfig { struct AddInfoConfig {
use_threshold bool use_threshold bool
} }

View File

@ -18,6 +18,7 @@ pub fn (mut f Fmt) attrs(attrs []ast.Attr) {
} }
} }
[params]
pub struct AttrsOptions { pub struct AttrsOptions {
inline bool inline bool
} }

View File

@ -16,6 +16,7 @@ pub enum CommentsLevel {
// - level: either .keep (don't indent), or .indent (increment indentation) // - level: either .keep (don't indent), or .indent (increment indentation)
// - iembed: a /* ... */ block comment used inside expressions; // comments the whole line // - iembed: a /* ... */ block comment used inside expressions; // comments the whole line
// - prev_line: the line number of the previous token to save linebreaks // - prev_line: the line number of the previous token to save linebreaks
[params]
pub struct CommentsOptions { pub struct CommentsOptions {
has_nl bool = true has_nl bool = true
inline bool inline bool

View File

@ -133,6 +133,7 @@ pub fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool {
return true return true
} }
[params]
pub struct RemoveNewLineConfig { pub struct RemoveNewLineConfig {
imports_buffer bool // Work on f.out_imports instead of f.out imports_buffer bool // Work on f.out_imports instead of f.out
} }

View File

@ -642,6 +642,7 @@ pub fn (mut p Parser) comment_stmt() ast.ExprStmt {
} }
} }
[params]
struct EatCommentsConfig { struct EatCommentsConfig {
same_line bool // Only eat comments on the same line as the previous token same_line bool // Only eat comments on the same line as the previous token
follow_up bool // Comments directly below the previous token as long as there is no empty line follow_up bool // Comments directly below the previous token as long as there is no empty line

View File

@ -1,3 +1,4 @@
[params]
struct Data { struct Data {
array []int array []int
} }

View File

@ -1,3 +1,4 @@
[params]
struct Foo { struct Foo {
x int x int
} }
@ -6,6 +7,7 @@ fn foo(f Foo) int {
return f.x return f.x
} }
[params]
struct Bar { struct Bar {
x int x int
y int = 1234 y int = 1234

View File

@ -247,6 +247,7 @@ fn test_fixed_field() {
//} //}
} }
*/ */
[params]
struct Config { struct Config {
mut: mut:
n int n int