1
- import { ValueType } from '../types' ;
1
+ import { typeEquals , ValueType } from '../types' ;
2
2
import { Color , typeOf , toString as valueToString } from '../values' ;
3
3
import Formatted from '../types/formatted' ;
4
4
import ResolvedImage from '../types/resolved_image' ;
5
+ import * as isConstant from '../is_constant' ;
5
6
import Literal from './literal' ;
6
7
7
8
import type { Type } from '../types' ;
8
9
import type { Expression , SerializedExpression } from '../expression' ;
9
10
import type ParsingContext from '../parsing_context' ;
10
11
import type EvaluationContext from '../evaluation_context' ;
11
12
13
+ const FQIDSeparator = '\u001F' ;
14
+
12
15
function coerceValue ( type : string , value : any ) : any {
13
16
switch ( type ) {
14
17
case 'string' : return valueToString ( value ) ;
@@ -42,11 +45,13 @@ class Config implements Expression {
42
45
type : Type ;
43
46
key : string ;
44
47
scope : string | null | undefined ;
48
+ featureConstant : boolean ;
45
49
46
- constructor ( type : Type , key : string , scope ?: string ) {
50
+ constructor ( type : Type , key : string , scope ?: string , featureConstant : boolean = false ) {
47
51
this . type = type ;
48
52
this . key = key ;
49
53
this . scope = scope ;
54
+ this . featureConstant = featureConstant ;
50
55
}
51
56
52
57
static parse ( args : ReadonlyArray < unknown > , context : ParsingContext ) : Config | null | void {
@@ -63,19 +68,31 @@ class Config implements Expression {
63
68
return context . error ( `Key name of 'config' expression must be a string literal.` ) ;
64
69
}
65
70
71
+ let featureConstant = true ;
72
+ let configScopeValue : string | undefined ;
73
+ const configKeyValue = valueToString ( configKey . value ) ;
74
+
66
75
if ( args . length >= 3 ) {
67
76
const configScope = context . parse ( args [ 2 ] , 2 ) ;
68
77
if ( ! ( configScope instanceof Literal ) ) {
69
78
return context . error ( `Scope of 'config' expression must be a string literal.` ) ;
70
79
}
71
- return new Config ( type , valueToString ( configKey . value ) , valueToString ( configScope . value ) ) ;
80
+
81
+ configScopeValue = valueToString ( configScope . value ) ;
82
+ }
83
+
84
+ if ( context . options ) {
85
+ const key = [ configKeyValue , configScopeValue , context . _scope ] . filter ( Boolean ) . join ( FQIDSeparator ) ;
86
+ const config = context . options . get ( key ) ;
87
+ if ( config ) {
88
+ featureConstant = isConstant . isFeatureConstant ( config . value || config . default ) ;
89
+ }
72
90
}
73
91
74
- return new Config ( type , valueToString ( configKey . value ) ) ;
92
+ return new Config ( type , configKeyValue , configScopeValue , featureConstant ) ;
75
93
}
76
94
77
95
evaluate ( ctx : EvaluationContext ) : any {
78
- const FQIDSeparator = '\u001F' ;
79
96
const configKey = [ this . key , this . scope , ctx . scope ] . filter ( Boolean ) . join ( FQIDSeparator ) ;
80
97
81
98
const config = ctx . getConfig ( configKey ) ;
0 commit comments