Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changelog

* improve `inject_global_value` to support structured data. Add the `env_json` property to inject JSON encoded data and the `default_value` property to inject data when the provided environment variable is not defined ([#277](https://github.com/seaofvoices/darklua/pull/277))
* add rule to remove method call syntax (`remove_method_call`) ([#276](https://github.com/seaofvoices/darklua/pull/276))
* fix `remove_unused_variable` rule to correctly handle trailing unassigned (but used!) variables ([#275](https://github.com/seaofvoices/darklua/pull/275))
* add rule to convert Luau numbers (`convert_luau_number`) ([#274](https://github.com/seaofvoices/darklua/pull/274))
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ full_moon = { version = "1.0.0", features = ["roblox"] }
indexmap = "2.7.0"
json5 = "0.4.1"
log = "0.4.22"
num-traits = "0.2.19"
pathdiff = "0.2.3"
petgraph = "0.6.5"
regex = "1.11.1"
Expand Down
46 changes: 45 additions & 1 deletion site/content/rules/inject_global_value.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,46 @@ parameters:
type: string
description: The name of the global variable
- name: value
type: boolean, number or string
type: any
description: The value to inject
default: nil
- name: env
added_in: "0.7.0"
type: string
description: An environment variable to read the value from
- name: env_json
added_in: "unreleased"
type: string
description: An environment variable to read the json-encoded value from
- name: default_value
added_in: "unreleased"
type: any
description: The default value when using an environment variable that is not defined
examples:
- rules: "[{ rule: 'inject_global_value', identifier: 'CONSTANT', value: 'Hello' }, { rule: 'inject_global_value', identifier: 'AMOUNT', value: 11 }]"
content: |
if _G.AMOUNT > 10 or _G.CONSTANT ~= nil then
--[[ ... ]]
end
- rules: "[{ rule: 'inject_global_value', identifier: 'DEBUG', value: true }]"
content: |
if _G.DEBUG then
print('Debug information')
end
---

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.

```json5
{
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.

```json5
Expand All @@ -34,4 +57,25 @@ If `value` is not specified, the `env` property can be defined to read an enviro
}
```

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.

```json5
{
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.

```json5
{
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.
Loading
Loading