Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
requireDotNotation: Require dots for es3 keywords when not in es3 mode.
Browse files Browse the repository at this point in the history
Fixes gh-161
  • Loading branch information
mikesherov committed Dec 8, 2014
1 parent 375402d commit cab9f35
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 34 deletions.
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2913,18 +2913,19 @@ var d = new e();

### requireDotNotation

Requires member expressions to use dot notation when possible
Requires member expressions to use dot notation when possible. Note, if you specify the --es3 option to JSCS, ES3 keywords and future reserved words MUST remain quoted.

Type: `Boolean`

Values: `true`

JSHint: [`sub`](http://www.jshint.com/docs/options/#sub)

#### Example
#### Example for `"es3": true`

```js
"requireDotNotation": true
"requireDotNotation": true,
"es3": true
```

##### Valid
Expand All @@ -2943,6 +2944,30 @@ var a = b['while']; //reserved word
var a = b['c'];
```

#### Example for `"es3": false` or `"es3": null`

```js
"requireDotNotation": true,
"es3": false
```

##### Valid

```js
var a = b[c];
var a = b.c;
var a = b[c.d];
var a = b[1];
var a = b.while;
```

##### Invalid

```js
var a = b['c'];
var a = b['while']; //reserved words can be property names in ES5
```

### requireYodaConditions

Requires the variable to be the right hand operator when doing a boolean comparison
Expand Down
7 changes: 5 additions & 2 deletions lib/rules/require-dot-notation.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module.exports.prototype = {
},

check: function(file, errors) {
function isES3Allowed(value) {
return file.isES3Enabled() && (utils.isEs3Keyword(value) || utils.isEs3FutureReservedWord(value));
}

file.iterateNodesByType('MemberExpression', function(node) {
if (!node.computed || node.property.type !== 'Literal') {
return;
Expand All @@ -36,8 +40,7 @@ module.exports.prototype = {
value === 'null' ||
value === 'true' ||
value === 'false' ||
utils.isEs3Keyword(value) ||
utils.isEs3FutureReservedWord(value)
isES3Allowed(value)
) {
return;
}
Expand Down
85 changes: 56 additions & 29 deletions test/rules/require-dot-notation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,71 @@ describe('rules/require-dot-notation', function() {
beforeEach(function() {
checker = new Checker();
checker.registerDefaultRules();
checker.configure({ requireDotNotation: true });
});

it('should report literal subscription', function() {
assert(checker.checkString('var x = a[\'b\']').getErrorCount() === 1);
});
describe('true value', function() {
beforeEach(function() {
checker.configure({ requireDotNotation: true });
});

it('should not report literal subscription for reserved words', function() {
assert(checker.checkString('var x = a[\'while\']').isEmpty());
assert(checker.checkString('var x = a[null]').isEmpty());
assert(checker.checkString('var x = a[true]').isEmpty());
assert(checker.checkString('var x = a[false]').isEmpty());
assert(checker.checkString('var x = a["null"]').isEmpty());
assert(checker.checkString('var x = a["true"]').isEmpty());
assert(checker.checkString('var x = a["false"]').isEmpty());
});
it('should report literal subscription', function() {
assert.equal(checker.checkString('var x = a[\'b\']').getErrorCount(), 1);
});

it('should not report number subscription', function() {
assert(checker.checkString('var x = a[1]').isEmpty());
});
it('should not report literal subscription for reserved words', function() {
assert(checker.checkString('var x = a[\'while\']').isEmpty());
assert(checker.checkString('var x = a[null]').isEmpty());
assert(checker.checkString('var x = a[true]').isEmpty());
assert(checker.checkString('var x = a[false]').isEmpty());
assert(checker.checkString('var x = a["null"]').isEmpty());
assert(checker.checkString('var x = a["true"]').isEmpty());
assert(checker.checkString('var x = a["false"]').isEmpty());
});

it('should not report variable subscription', function() {
assert(checker.checkString('var x = a[c]').isEmpty());
});
it('should not report number subscription', function() {
assert(checker.checkString('var x = a[1]').isEmpty());
});

it('should not report variable subscription', function() {
assert(checker.checkString('var x = a[c]').isEmpty());
});

it('should not report object property subscription', function() {
assert(checker.checkString('var x = a[b.c]').isEmpty());
it('should not report object property subscription', function() {
assert(checker.checkString('var x = a[b.c]').isEmpty());
});

it('should not report dot notation', function() {
assert(checker.checkString('var x = a.b').isEmpty());
});

it('should not report for string that can\'t be identifier', function() {
assert(checker.checkString('x["a-b"]').isEmpty());
assert(checker.checkString('x["a.b"]').isEmpty());
assert(checker.checkString('x["a b"]').isEmpty());
assert(checker.checkString('x["1a"]').isEmpty());
assert(checker.checkString('x["*"]').isEmpty());
});
});

it('should not report dot notation', function() {
assert(checker.checkString('var x = a.b').isEmpty());
describe('true value with es3 explicitly enabled', function() {
beforeEach(function() {
checker.configure({ es3: true, requireDotNotation: true });
});

it('should not report literal subscription for es3 keywords or future reserved words', function() {
assert(checker.checkString('var x = a[\'while\']').isEmpty());
assert(checker.checkString('var x = a[\'abstract\']').isEmpty());
});
});

it('should not report for string that can\'t be identifier', function() {
assert(checker.checkString('x["a-b"]').isEmpty());
assert(checker.checkString('x["a.b"]').isEmpty());
assert(checker.checkString('x["a b"]').isEmpty());
assert(checker.checkString('x["1a"]').isEmpty());
assert(checker.checkString('x["*"]').isEmpty());
describe('true value with es3 explicitly disabled', function() {
beforeEach(function() {
checker.configure({ es3: false, requireDotNotation: true });
});

it('should not report literal subscription for es3 keywords or future reserved words', function() {
assert.equal(checker.checkString('var x = a[\'while\']').getErrorCount(), 1);
assert.equal(checker.checkString('var x = a[\'abstract\']').getErrorCount(), 1);
});
});
});

0 comments on commit cab9f35

Please sign in to comment.