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

Commit 3bd9e7c

Browse files
committed
ObjectExpression Rules: take into account shorthand syntax.
None of the rules were currently aware of `var a = { b };`. This updates any rule that concerned itself with object keys, values, or the colon between. Fixes #772 Closes gh-992
1 parent 5c4565e commit 3bd9e7c

13 files changed

+86
-47
lines changed

lib/rules/disallow-space-after-object-keys.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ module.exports = function() {};
2828
module.exports.prototype = {
2929

3030
configure: function(disallow) {
31-
assert(
32-
typeof disallow === 'boolean',
33-
this.getOptionName() + ' option requires boolean value'
34-
);
3531
assert(
3632
disallow === true,
3733
this.getOptionName() + ' option requires true value or should be removed'
@@ -43,9 +39,12 @@ module.exports.prototype = {
4339
},
4440

4541
check: function(file, errors) {
46-
var tokens = file.getTokens();
4742
file.iterateNodesByType('ObjectExpression', function(node) {
4843
node.properties.forEach(function(property) {
44+
if (property.shorthand) {
45+
return;
46+
}
47+
4948
var token = file.getFirstNodeToken(property.key);
5049
errors.assert.noWhitespaceBetween({
5150
token: token,

lib/rules/disallow-space-before-object-values.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ module.exports.prototype = {
4141
check: function(file, errors) {
4242
file.iterateNodesByType('ObjectExpression', function(node) {
4343
node.properties.forEach(function(property) {
44+
if (property.shorthand) {
45+
return;
46+
}
47+
4448
var keyToken = file.getFirstNodeToken(property.key);
4549
var colon = file.findNextToken(keyToken, 'Punctuator', ':');
4650

lib/rules/require-aligned-object-values.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ module.exports.prototype = {
6767

6868
var maxKeyEndPos = 0;
6969
var skip = node.properties.some(function(property, index) {
70+
if (property.shorthand) {
71+
return true;
72+
}
73+
7074
maxKeyEndPos = Math.max(maxKeyEndPos, property.key.loc.end.column);
7175

7276
if (mode === 'ignoreFunction' && property.value.type === 'FunctionExpression') {

lib/rules/require-quoted-keys-in-objects.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,10 @@ module.exports = function() { };
3232
module.exports.prototype = {
3333

3434
configure: function(requireQuotedKeysInObjects) {
35-
assert(
36-
typeof requireQuotedKeysInObjects === 'boolean',
37-
this.getOptionName() + ' option requires boolean value'
38-
);
3935
assert(
4036
requireQuotedKeysInObjects === true,
4137
this.getOptionName() + ' option requires true value or should be removed'
4238
);
43-
44-
this._mode = requireQuotedKeysInObjects;
4539
},
4640

4741
getOptionName: function() {
@@ -51,6 +45,10 @@ module.exports.prototype = {
5145
check: function(file, errors) {
5246
file.iterateNodesByType('ObjectExpression', function(node) {
5347
node.properties.forEach(function(prop) {
48+
if (prop.shorthand) {
49+
return;
50+
}
51+
5452
var key = prop.key;
5553
if (!(typeof key.value === 'string' && key.type === 'Literal')) {
5654
errors.add('Object key without surrounding quotes', prop.loc.start);

lib/rules/require-space-after-object-keys.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ module.exports = function() {};
2828
module.exports.prototype = {
2929

3030
configure: function(requireSpaceAfterObjectKeys) {
31-
assert(
32-
typeof requireSpaceAfterObjectKeys === 'boolean',
33-
this.getOptionName() + ' option requires boolean value'
34-
);
3531
assert(
3632
requireSpaceAfterObjectKeys === true,
3733
this.getOptionName() + ' option requires true value or should be removed'
@@ -43,9 +39,12 @@ module.exports.prototype = {
4339
},
4440

4541
check: function(file, errors) {
46-
var tokens = file.getTokens();
4742
file.iterateNodesByType('ObjectExpression', function(node) {
4843
node.properties.forEach(function(property) {
44+
if (property.shorthand) {
45+
return;
46+
}
47+
4948
var token = file.getFirstNodeToken(property.key);
5049
errors.assert.whitespaceBetween({
5150
token: token,

lib/rules/require-space-before-object-values.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ module.exports.prototype = {
3939
},
4040

4141
check: function(file, errors) {
42-
var tokens = file.getTokens();
4342
file.iterateNodesByType('ObjectExpression', function(node) {
4443
node.properties.forEach(function(property) {
44+
if (property.shorthand) {
45+
return;
46+
}
47+
4548
var keyToken = file.getFirstNodeToken(property.key);
4649
var colon = file.findNextToken(keyToken, 'Punctuator', ':');
4750

test/rules/disallow-space-after-object-keys.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,15 @@ describe('rules/disallow-space-after-object-keys', function() {
2828
it('should not report without space after keys', function() {
2929
assert(checker.checkString('var x = { a: 1, bcd: 2 };').isEmpty());
3030
});
31+
32+
it('should not report shorthand object properties', function() {
33+
checker.configure({ esnext: true });
34+
assert(checker.checkString('var x = { a, b };').isEmpty());
35+
assert(checker.checkString('var x = {a, b};').isEmpty());
36+
});
37+
38+
it('should report mixed shorthand and normal object propertis', function() {
39+
checker.configure({ esnext: true });
40+
assert.equal(checker.checkString('var x = { a : 1, b };').getErrorCount(), 1);
41+
});
3142
});

test/rules/disallow-space-before-object-values.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,14 @@ describe('rules/disallow-space-before-object-values', function() {
4141
assert(checker.checkString('var x = { a:1, bcd :2 };').isEmpty());
4242
});
4343

44+
it('should not report shorthand object properties', function() {
45+
checker.configure({ esnext: true });
46+
assert(checker.checkString('var x = { a, b };').isEmpty());
47+
assert(checker.checkString('var x = {a, b};').isEmpty());
48+
});
49+
50+
it('should report mixed shorthand and normal object propertis', function() {
51+
checker.configure({ esnext: true });
52+
assert.equal(checker.checkString('var x = { a : 1, b };').getErrorCount(), 1);
53+
});
4454
});

test/rules/require-aligned-object-values.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ describe('rules/require-aligned-object-values', function() {
3333
);
3434
});
3535

36+
it('should not report shorthand properties', function() {
37+
checker.configure({ esnext: true });
38+
assert(
39+
checker.checkString(
40+
'var x = {\n' +
41+
'bcd : 2,\n' +
42+
'a,\n' +
43+
'efg : 2\n' +
44+
'};'
45+
).isEmpty()
46+
);
47+
});
48+
3649
it('should report invalid alignment', function() {
3750
assert(
3851
checker.checkString(

test/rules/require-quoted-keys-in-objects.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var assert = require('assert');
33

44
describe('rules/require-quoted-keys-in-objects', function() {
55
var checker;
6+
67
beforeEach(function() {
78
checker = new Checker();
89
checker.registerDefaultRules();
@@ -61,4 +62,9 @@ describe('rules/require-quoted-keys-in-objects', function() {
6162
assert(error.line === 1);
6263
assert(error.column === 10);
6364
});
65+
66+
it('should not report shorthand object properties', function() {
67+
checker.configure({ esnext: true });
68+
assert(checker.checkString('var x = { a, b };').isEmpty());
69+
});
6470
});

0 commit comments

Comments
 (0)