inject_global_value
Parameters
Name | Type | Description | Default |
---|---|---|---|
identifier |
| The name of the global variable | |
value |
| The value to inject |
|
env |
| An environment variable to read the value from (added in v0.7.0) | |
env_json |
| An environment variable to read the json-encoded value from (added in vunreleased) | |
default_value |
| The default value when using an environment variable that is not defined (added in vunreleased) |
This rule will find a global variable and replace it with a given value. The value can be defined in the rule configuration or taken from an environment variable.
To inject a static value, use the value
property.
{
rule: "inject_global_value",
identifier: "GLOBAL",
value: true,
}
If value
is not specified, the env
property can be defined to read an environment variable that will be read into a string.
{
rule: "inject_global_value",
identifier: "GLOBAL",
env: "SOME_VARIABLE",
}
Alternatively, the env_json
property allows you to read a JSON-encoded value (json5
is supported) from an environment variable. This is useful for injecting any data like booleans or structured data like arrays or objects.
{
rule: "inject_global_value",
identifier: "SETTINGS",
env_json: "APP_SETTINGS",
}
When using the env
or env_json
property, the default_value
property can be used to provide a fallback value when the environment variable is not defined. This prevents the rule from using nil
as the default value.
{
rule: "inject_global_value",
identifier: "FEATURE_FLAG",
env: "ENABLE_FEATURE",
default_value: false,
}
This rule can be used in combination with the remove_unused_if_branch
, compute_expression
, and other rules, to eliminate dead branches. In addition to making your code smaller, it should make it faster (depending on how hot the code path is) since it is eliminating branch condition evaluations at client-side runtime.
Examples
if _G.AMOUNT > 10 or _G.CONSTANT ~= nil then
--[[ ... ]]
end
if 11> 10 or 'Hello'~= nil then
--[[ ... ]]
end
if _G.DEBUG then
print('Debug information')
end
if true then
print('Debug information')
end
Input | Output |
---|---|
|
|
|
|