3
3
import { Base } from "./base.js" ;
4
4
import { Default } from "./default.js" ;
5
5
import { ExtendedAttributes } from "./extended-attributes.js" ;
6
- import { unescape , type_with_extended_attributes , autoParenter , getFirstToken } from "./helpers.js" ;
6
+ import {
7
+ unescape ,
8
+ type_with_extended_attributes ,
9
+ autoParenter ,
10
+ getFirstToken ,
11
+ } from "./helpers.js" ;
7
12
import { argumentNameKeywords , Tokeniser } from "../tokeniser.js" ;
8
13
import { validationError } from "../error.js" ;
9
- import { idlTypeIncludesDictionary , dictionaryIncludesRequiredField } from "../validators/helpers.js" ;
14
+ import {
15
+ idlTypeIncludesDictionary ,
16
+ dictionaryIncludesRequiredField ,
17
+ } from "../validators/helpers.js" ;
10
18
11
19
export class Argument extends Base {
12
20
/**
@@ -16,7 +24,9 @@ export class Argument extends Base {
16
24
const start_position = tokeniser . position ;
17
25
/** @type {Base["tokens"] } */
18
26
const tokens = { } ;
19
- const ret = autoParenter ( new Argument ( { source : tokeniser . source , tokens } ) ) ;
27
+ const ret = autoParenter (
28
+ new Argument ( { source : tokeniser . source , tokens } )
29
+ ) ;
20
30
ret . extAttrs = ExtendedAttributes . parse ( tokeniser ) ;
21
31
tokens . optional = tokeniser . consume ( "optional" ) ;
22
32
ret . idlType = type_with_extended_attributes ( tokeniser , "argument-type" ) ;
@@ -52,23 +62,46 @@ export class Argument extends Base {
52
62
*/
53
63
* validate ( defs ) {
54
64
yield * this . idlType . validate ( defs ) ;
55
- const result = idlTypeIncludesDictionary ( this . idlType , defs , { useNullableInner : true } ) ;
65
+ const result = idlTypeIncludesDictionary ( this . idlType , defs , {
66
+ useNullableInner : true ,
67
+ } ) ;
56
68
if ( result ) {
57
69
if ( this . idlType . nullable ) {
58
70
const message = `Dictionary arguments cannot be nullable.` ;
59
- yield validationError ( this . tokens . name , this , "no-nullable-dict-arg" , message ) ;
71
+ yield validationError (
72
+ this . tokens . name ,
73
+ this ,
74
+ "no-nullable-dict-arg" ,
75
+ message
76
+ ) ;
60
77
} else if ( ! this . optional ) {
61
- if ( this . parent && ! dictionaryIncludesRequiredField ( result . dictionary , defs ) && isLastRequiredArgument ( this ) ) {
78
+ if (
79
+ this . parent &&
80
+ ! dictionaryIncludesRequiredField ( result . dictionary , defs ) &&
81
+ isLastRequiredArgument ( this )
82
+ ) {
62
83
const message = `Dictionary argument must be optional if it has no required fields` ;
63
- yield validationError ( this . tokens . name , this , "dict-arg-optional" , message , {
64
- autofix : autofixDictionaryArgumentOptionality ( this )
65
- } ) ;
84
+ yield validationError (
85
+ this . tokens . name ,
86
+ this ,
87
+ "dict-arg-optional" ,
88
+ message ,
89
+ {
90
+ autofix : autofixDictionaryArgumentOptionality ( this ) ,
91
+ }
92
+ ) ;
66
93
}
67
94
} else if ( ! this . default ) {
68
95
const message = `Optional dictionary arguments must have a default value of \`{}\`.` ;
69
- yield validationError ( this . tokens . name , this , "dict-arg-default" , message , {
70
- autofix : autofixOptionalDictionaryDefaultValue ( this )
71
- } ) ;
96
+ yield validationError (
97
+ this . tokens . name ,
98
+ this ,
99
+ "dict-arg-default" ,
100
+ message ,
101
+ {
102
+ autofix : autofixOptionalDictionaryDefaultValue ( this ) ,
103
+ }
104
+ ) ;
72
105
}
73
106
}
74
107
}
@@ -80,7 +113,7 @@ export class Argument extends Base {
80
113
function isLastRequiredArgument ( arg ) {
81
114
const list = arg . parent . arguments || arg . parent . list ;
82
115
const index = list . indexOf ( arg ) ;
83
- const requiredExists = list . slice ( index + 1 ) . some ( a => ! a . optional ) ;
116
+ const requiredExists = list . slice ( index + 1 ) . some ( ( a ) => ! a . optional ) ;
84
117
return ! requiredExists ;
85
118
}
86
119
@@ -90,7 +123,11 @@ function isLastRequiredArgument(arg) {
90
123
function autofixDictionaryArgumentOptionality ( arg ) {
91
124
return ( ) => {
92
125
const firstToken = getFirstToken ( arg . idlType ) ;
93
- arg . tokens . optional = { type : "optional" , value : "optional" , trivia : firstToken . trivia } ;
126
+ arg . tokens . optional = {
127
+ type : "optional" ,
128
+ value : "optional" ,
129
+ trivia : firstToken . trivia ,
130
+ } ;
94
131
firstToken . trivia = " " ;
95
132
autofixOptionalDictionaryDefaultValue ( arg ) ( ) ;
96
133
} ;
0 commit comments