Skip to content

Commit

Permalink
Update: Use 'readonly' and 'writable' for globals (fixes #11359) (#11384
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nzakas committed Feb 13, 2019
1 parent f1d3a7e commit 983c520
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
16 changes: 8 additions & 8 deletions docs/user-guide/configuring.md
Expand Up @@ -208,19 +208,19 @@ To specify globals using a comment inside of your JavaScript file, use the follo
/* global var1, var2 */
```

This defines two global variables, `var1` and `var2`. If you want to optionally specify that these global variables can be written to (rather than only being read), then you can set each with a `writeable` flag:
This defines two global variables, `var1` and `var2`. If you want to optionally specify that these global variables can be written to (rather than only being read), then you can set each with a `"writable"` flag:

```js
/* global var1:writeable, var2:writeable */
/* global var1:writable, var2:writable */
```

To configure global variables inside of a configuration file, set the `globals` configuration property to an object containing keys named for each of the global variables you want to use. For each global variable key, set the corresponding value equal to `"writeable"` to allow the variable to be overwritten or `"readable"` to disallow overwriting. For example:
To configure global variables inside of a configuration file, set the `globals` configuration property to an object containing keys named for each of the global variables you want to use. For each global variable key, set the corresponding value equal to `"writable"` to allow the variable to be overwritten or `"readonly"` to disallow overwriting. For example:

```json
{
"globals": {
"var1": "writeable",
"var2": "readable"
"var1": "writable",
"var2": "readonly"
}
}
```
Expand All @@ -230,8 +230,8 @@ And in YAML:
```yaml
---
globals:
var1: writeable
var2: readable
var1: writable
var2: readonly
```

These examples allow `var1` to be overwritten in your code, but disallow it for `var2`.
Expand All @@ -249,7 +249,7 @@ Globals can be disabled with the string `"off"`. For example, in an environment
}
```

For historical reasons, the boolean values `false` and `true` can also be used to configure globals, and are equivalent to `"readable"` and `"writeable"`, respectively. However, this usage of booleans is deprecated.
For historical reasons, the boolean value `false` and the string value `"readable"` are equivalent to `"readonly"`. Similarly, the boolean value `true` and the string value `"writeable"` are equivalent to `"writable"`. However, the use of older values is deprecated.

**Note:** Enable the [no-global-assign](../rules/no-global-assign.md) rule to disallow modifications to read-only global variables in your code.

Expand Down
2 changes: 2 additions & 0 deletions lib/config/config-ops.js
Expand Up @@ -386,12 +386,14 @@ module.exports = {
case true:
case "true":
case "writeable":
case "writable":
return "writeable";

case null:
case false:
case "false":
case "readable":
case "readonly":
return "readable";

// Fallback to minimize compatibility impact
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/config/config-ops.js
Expand Up @@ -908,7 +908,9 @@ describe("ConfigOps", () => {
["false", "readable"],
[null, "readable"],
["writeable", "writeable"],
["writable", "writeable"],
["readable", "readable"],
["readonly", "readable"],
["writable", "writeable"],
["something else", "writeable"]
].forEach(([input, output]) => {
Expand Down

0 comments on commit 983c520

Please sign in to comment.