Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Allow globals to be disabled/configured with strings (fixes #9940) #11338

Merged
merged 7 commits into from Feb 1, 2019

Conversation

not-an-aardvark
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[x] Add something to the core

What changes did you make? (Give an overview)

As described in #9940, this allows globals in a config file to be specified with the strings readable and writeable rather than false and true, respectively. It also adds a new value, off, which turns off a previously-enabled global.

Is there anything you'd like reviewers to focus on?

Nothing in particular

@not-an-aardvark not-an-aardvark added enhancement This change enhances an existing feature of ESLint core Relates to ESLint's core APIs and features accepted There is consensus among the team that this change meets the criteria for inclusion labels Jan 31, 2019
As described in #9940, this allows globals in a config file to be specified with the strings `readable` and `writeable` rather than `false` and `true`, respectively. It also adds a new value, `off`, which turns off a previously-enabled global.
@@ -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 should never be written to (only read), then you can set each with a `false` 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 `writeable` flag:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This documentation was previously misleading -- it implied that "writeable" was the default for global comments like /* global foo */, but in fact the default seems to be "readable".

docs/user-guide/configuring.md Show resolved Hide resolved

// Fallback to minimize compatibility impact
default:
return "writeable";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable writability is communicated to rules via the variable.writeable boolean property in scope analysis.

Previously, a non-boolean value in a globals configuration would actually get passed directly to rules through scope analysis, so a configuration of {globals: {myGlobal: "foo"}} would end up as a variable object with variable.writeable = "foo".

Strictly speaking, we don't have any compatibility guarantees for non-boolean values here, since it's undocumented behavior. But since someone is probably relying on this by mistake (e.g. from a malformed config comment), I put in a fallback so that unexpected globals values default to "writeable" (and appear to rules as writeable: true). This fallback seems most likely to match existing behavior when users have malformed configs, since rules using this would typically be doing truthiness checks.

@aladdin-add aladdin-add self-requested a review January 31, 2019 03:04
Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direction looks good. I look forward to landing this!

Left a few inline comments.

docs/user-guide/configuring.md Show resolved Hide resolved
docs/user-guide/configuring.md Outdated Show resolved Hide resolved
```

These examples allow `var1` to be overwritten in your code, but disallow it for `var2`.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker: Do we have a way to stylistically indicate that this is an aside? (I'm assuming not and that this would be a wider information architecture question, but wanted to ask.)

Also: I wonder if this should go below the next section on disabling globals? (Edit: Or another possibility might be to use subsections for enabling globals [read/write], and disabling [off], in which case the paragraph probably shouldn't move, but everything would flow a little better because the subsection headers would serve as natural breaks.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this line below the section about disabling globals.

I think this line will be useful for users figuring out what's going on in existing configs containing booleans, so I don't want to make it too much of a footnote. I was thinking about preceding this line with "Note:", but in the new location this line is followed by another line that also starts with "Note:", which would look a bit strange. That said, I'm open to stylistic suggestions.

lib/linter.js Outdated Show resolved Hide resolved
lib/linter.js Outdated Show resolved Hide resolved
lib/linter.js Outdated Show resolved Hide resolved
tests/lib/config/config-ops.js Show resolved Hide resolved
Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@btmills btmills merged commit 0a3c3ff into master Feb 1, 2019
@not-an-aardvark not-an-aardvark deleted the string-globals branch February 1, 2019 17:12
not-an-aardvark added a commit that referenced this pull request Mar 16, 2019
Previously, invalid values for `globals` configurations would default to `writable`. This change updates those cases to throw an error instead.
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Aug 1, 2019
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Aug 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion core Relates to ESLint's core APIs and features enhancement This change enhances an existing feature of ESLint
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants