Skip to content

Commit

Permalink
Update: merge no-reserved-keys into quote-props (fixes eslint#1539)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrvidal committed Jul 24, 2015
1 parent 83db585 commit fa3e59d
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 260 deletions.
1 change: 0 additions & 1 deletion conf/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"no-proto": 0,
"no-redeclare": 2,
"no-regex-spaces": 2,
"no-reserved-keys": 0,
"no-restricted-modules": 0,
"no-return-assign": 0,
"no-script-url": 0,
Expand Down
1 change: 1 addition & 0 deletions conf/replacements.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"no-comma-dangle": ["comma-dangle"],
"no-empty-class": ["no-empty-character-class"],
"no-extra-strict": ["strict"],
"no-reserved-keys": ["quote-props"],
"no-space-before-semi": ["semi-spacing"],
"no-wrap-func": ["no-extra-parens"],
"space-after-function-name": ["space-before-function-paren"],
Expand Down
1 change: 0 additions & 1 deletion docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ The following rules point out areas where you might have made mistakes.
* [no-negated-in-lhs](no-negated-in-lhs.md) - disallow negation of the left operand of an `in` expression (recommended)
* [no-obj-calls](no-obj-calls.md) - disallow the use of object properties of the global object (`Math` and `JSON`) as functions (recommended)
* [no-regex-spaces](no-regex-spaces.md) - disallow multiple spaces in a regular expression literal (recommended)
* [no-reserved-keys](no-reserved-keys.md) - disallow reserved words being used as object literal keys
* [no-sparse-arrays](no-sparse-arrays.md) - disallow sparse arrays (recommended)
* [no-unreachable](no-unreachable.md) - disallow unreachable statements after a return, throw, continue, or break statement (recommended)
* [use-isnan](use-isnan.md) - disallow comparisons with the value `NaN` (recommended)
Expand Down
51 changes: 0 additions & 51 deletions docs/rules/no-reserved-keys.md

This file was deleted.

71 changes: 34 additions & 37 deletions docs/rules/quote-props.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ var object2 = {
};
```

In many cases, it doesn't matter if you choose to use an identifier instead of a string or vice-versa. There are, however, some occasions when you must use quotes:
In many cases, it doesn't matter if you choose to use an identifier instead of a string or vice-versa. Even so, you might decide to enforce a consistent style in your code.

There are, however, some occasions when you must use quotes:

1. If you are using an ECMAScript 3 JavaScript engine (such as IE8) and you want to use a keyword (such as `if`) as a property name. This restriction was removed in ECMAScript 5.
2. You want to use a non-identifier character in your property name, such as having a property with a space like `"one two"`.

Sometimes the choice to use quotes is purely stylistic and sometimes it's functional. Here's another example:
Another example where quotes do matter is when using numeric literals as property keys:

```js
var object = {
Expand All @@ -32,8 +34,6 @@ This may look alright at first sight, but this code in fact throws a syntax erro

This rule aims to enforce use of quotes in property names and as such will flag any properties that don't use quotes.

### Options

There are two behaviors for this rule: `"always"` (default) and `"as-needed"`. You can define these options in your configuration as:

```json
Expand All @@ -42,37 +42,7 @@ There are two behaviors for this rule: `"always"` (default) and `"as-needed"`. Y
}
```

When configured with `"always"` as the first option (the default), quoting for all properties will be enforced. Some believe that ensuring property names in object literals are always wrapped in quotes is generally a good idea, since [depending on the property name you may need to quote them anyway](https://mathiasbynens.be/notes/javascript-properties). Consider this example:

```js
var object = {
foo: "bar",
baz: 42,
"qux-lorem": true
};
```

Here, the properties `foo` and `baz` are not wrapped in quotes, but `qux-lorem` is, because it doesn’t work without the quotes. This is rather inconsistent. Instead, you may prefer to quote property names consistently:

```js
var object = {
"foo": "bar",
"baz": 42,
"qux-lorem": true
};
```

or, if you prefer single quotes:

```js
var object = {
'foo': 'bar',
'baz': 42,
'qux-lorem': true
};
```

When configured with `"always"` (the default), the following patterns are considered warnings:
When configured with `"always"` as the first option (the default), quoting for all properties will be enforced. The following patterns are considered warnings:

```js
var object = {
Expand Down Expand Up @@ -104,7 +74,7 @@ var object3 = {
};
```

When configured with `"as-needed"` as the first option (the default), the following patterns are considered warnings:
When configured with `"as-needed"` as the first option, quotes will be enforced when they are strictly required, and unnecessary quotes will cause warnings. The following patterns are considered warnings:

```js
var object = {
Expand All @@ -126,6 +96,7 @@ var object1 = {
var object2 = {
foo: 'bar',
baz: 42,
true: 0,
'qux-lorem': true
};

Expand All @@ -136,6 +107,32 @@ var object3 = {
};
```


### Options

When the `"as-needed"` mode is selected, an additional `keywords` option can be provided. This flag indicates whether language keywords can be used unquoted as properties. By default it is set to `false`.

```json
{
"quote-props": [2, "as-needed", {"keywords": true}]
}
```

When `keywords` is set to `true`, the following patterns become warnings:

```
var x = {
while: 1,
volatile: "foo"
};
```

## When Not To Use It

If you don't care if property names are consistently wrapped in quotes or not, turn this rule off.
If you don't care if property names are consistently wrapped in quotes or not, and you don't target legacy ES3 environments, turn this rule off.

## Further Reading

* [Reserved words as property names](http://kangax.github.io/compat-table/es5/#Reserved_words_as_property_names)
* [Unquoted property names / object keys in JavaScript](https://mathiasbynens.be/notes/javascript-properties)
1 change: 1 addition & 0 deletions docs/user-guide/migrating-to-1.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Over the past several releases, we have been deprecating rules and introducing n
* [no-comma-dangle](http://eslint.org/docs/rules/no-comma-dangle) is replaced by [comma-dangle](http://eslint.org/docs/rules/comma-dangle)
* [no-empty-class](http://eslint.org/docs/rules/no-empty-class) is replaced by [no-empty-character-class](http://eslint.org/docs/rules/no-empty-character-class)
* [no-extra-strict](http://eslint.org/docs/rules/no-extra-strict) is replaced by [strict](http://eslint.org/docs/rules/strict)
* [no-reserved-keys](http://eslint.org/docs/rules/no-reserved-keys) is replaced by [quote-props](http://eslint.org/docs/rules/quote-props)
* [no-space-before-semi](http://eslint.org/docs/rules/no-space-before-semi) is replaced by [semi-spacing](http://eslint.org/docs/rules/semi-spacing)
* [no-wrap-func](http://eslint.org/docs/rules/no-wrap-func) is replaced by [no-extra-parens](http://eslint.org/docs/rules/no-extra-parens)
* [space-after-function-name](http://eslint.org/docs/rules/space-after-function-name) is replaced by [space-before-function-paren](http://eslint.org/docs/rules/space-before-function-paren)
Expand Down
62 changes: 1 addition & 61 deletions lib/rules/dot-notation.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,7 @@
//------------------------------------------------------------------------------

var validIdentifier = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
var keywords = [
"this",
"function",
"if",
"return",
"var",
"else",
"for",
"new",
"in",
"typeof",
"while",
"case",
"break",
"try",
"catch",
"delete",
"throw",
"switch",
"continue",
"default",
"instanceof",
"do",
"void",
"finally",
"with",
"debugger",
"implements",
"interface",
"package",
"private",
"protected",
"public",
"static",
"class",
"enum",
"export",
"extends",
"import",
"super",
"true",
"false",
"null",
"abstract",
"boolean",
"byte",
"char",
"const",
"double",
"final",
"float",
"goto",
"int",
"long",
"native",
"short",
"synchronized",
"throws",
"transient",
"volatile"
];
var keywords = require("../util/keywords");

module.exports = function(context) {
var options = context.options[0] || {};
Expand Down
56 changes: 0 additions & 56 deletions lib/rules/no-reserved-keys.js

This file was deleted.

0 comments on commit fa3e59d

Please sign in to comment.