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

Commit 2b6a93c

Browse files
committed
disallowSpacesInGenerator: improve logic and fix bug for shortland
* Actually check shortland generators, but only for the `afterStar` option, omit check for `beforeStar` since it doesn't makes since to check it * Remove check for getter & setters, since they can't be generators * Increase coverage
1 parent 747c3b4 commit 2b6a93c

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

lib/rules/disallow-spaces-in-generator.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,11 @@ module.exports.prototype = {
8989
}
9090

9191
var parent = node.parentNode;
92-
93-
// Ignore syntactic sugar for getters and setters.
94-
if (parent.type === 'Property' && (parent.kind === 'get' || parent.kind === 'set')) {
95-
return;
96-
}
92+
var shortland = false;
9793

9894
// shorthand or constructor methods
9995
if (parent.method || parent.type === 'MethodDefinition') {
96+
shortland = true;
10097
node = parent.key;
10198
}
10299

@@ -106,7 +103,7 @@ module.exports.prototype = {
106103
currentToken = file.getNextToken(currentToken);
107104
}
108105

109-
if (beforeStar) {
106+
if (beforeStar && !shortland) {
110107
// currentToken assigned outside of function
111108
errors.assert.noWhitespaceBetween({
112109
token: currentToken,
@@ -116,13 +113,22 @@ module.exports.prototype = {
116113
}
117114

118115
if (afterStar) {
119-
// currentToken reassigned for star token
120-
currentToken = file.getNextToken(currentToken);
121-
errors.assert.noWhitespaceBetween({
122-
token: currentToken,
123-
nextToken: file.getNextToken(currentToken),
124-
message: 'Illegal space after star'
125-
});
116+
if (shortland) {
117+
errors.assert.noWhitespaceBetween({
118+
token: file.getPrevToken(currentToken),
119+
nextToken: currentToken,
120+
message: 'Illegal space after star'
121+
});
122+
} else {
123+
124+
// currentToken reassigned for star token
125+
currentToken = file.getNextToken(currentToken);
126+
errors.assert.noWhitespaceBetween({
127+
token: currentToken,
128+
nextToken: file.getNextToken(currentToken),
129+
message: 'Illegal space after star'
130+
});
131+
}
126132
}
127133
});
128134
}

test/specs/rules/disallow-spaces-in-generator.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,20 @@ describe('rules/disallow-spaces-in-generator', function() {
6767
output: 'var x = function*asdf(){}'
6868
});
6969

70+
it('should not report shortland method (since it doesn\' makes sense)', function() {
71+
expect(checker.checkString('({ * foo() {} });')).to.have.no.errors();
72+
});
73+
7074
it('should not report illegal space error', function() {
7175
expect(checker.checkString('var x = function* (){}')).to.have.no.errors();
7276
});
7377

74-
it('should not report illegal space error with named function expression', function() {
75-
expect(checker.checkString('var x = function* asdf(){}')).to.have.no.errors();
78+
it('should bail out if this is not a generator', function() {
79+
expect(checker.checkString('var x = function (){}')).to.have.no.errors();
7680
});
7781

78-
it('should not report illegal space error with function delcaration', function() {
79-
expect(checker.checkString('function* asdf(){}')).to.have.no.errors();
82+
it('should not report illegal space error with named function expression', function() {
83+
expect(checker.checkString('var x = function* asdf(){}')).to.have.no.errors();
8084
});
8185

8286
it('should not report illegal space error with async function', function() {
@@ -158,6 +162,14 @@ describe('rules/disallow-spaces-in-generator', function() {
158162
output: 'var x = function*asdf(){}'
159163
});
160164

165+
reportAndFix({
166+
name: 'should report illegal space after the star for the shortland',
167+
rules: rules,
168+
errors: 1,
169+
input: '({ * foo() {} });',
170+
output: '({ *foo() {} });'
171+
});
172+
161173
it('should not report illegal space error', function() {
162174
expect(checker.checkString('var x = function *(){}')).to.have.no.errors();
163175
});

0 commit comments

Comments
 (0)