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

Commit 7a547b2

Browse files
committed
requireSpacesInGenerator: port improved logic, fix spelling
1 parent 2b6a93c commit 7a547b2

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed

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

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

9191
var parent = node.parentNode;
92-
var shortland = false;
92+
var shorthand = false;
9393

9494
// shorthand or constructor methods
9595
if (parent.method || parent.type === 'MethodDefinition') {
96-
shortland = true;
96+
shorthand = true;
9797
node = parent.key;
9898
}
9999

@@ -103,7 +103,7 @@ module.exports.prototype = {
103103
currentToken = file.getNextToken(currentToken);
104104
}
105105

106-
if (beforeStar && !shortland) {
106+
if (beforeStar && !shorthand) {
107107
// currentToken assigned outside of function
108108
errors.assert.noWhitespaceBetween({
109109
token: currentToken,
@@ -113,22 +113,18 @@ module.exports.prototype = {
113113
}
114114

115115
if (afterStar) {
116-
if (shortland) {
117-
errors.assert.noWhitespaceBetween({
118-
token: file.getPrevToken(currentToken),
119-
nextToken: currentToken,
120-
message: 'Illegal space after star'
121-
});
116+
if (shorthand) {
117+
currentToken = file.getPrevToken(currentToken);
122118
} else {
123-
124119
// currentToken reassigned for star token
125120
currentToken = file.getNextToken(currentToken);
126-
errors.assert.noWhitespaceBetween({
127-
token: currentToken,
128-
nextToken: file.getNextToken(currentToken),
129-
message: 'Illegal space after star'
130-
});
131121
}
122+
123+
errors.assert.noWhitespaceBetween({
124+
token: currentToken,
125+
nextToken: file.getNextToken(currentToken),
126+
message: 'Illegal space after star'
127+
});
132128
}
133129
});
134130
}

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,11 @@ module.exports.prototype = {
7979
}
8080

8181
var parent = node.parentNode;
82-
83-
// Ignore syntactic sugar for getters and setters.
84-
if (parent.type === 'Property' && (parent.kind === 'get' || parent.kind === 'set')) {
85-
return;
86-
}
82+
var shorthand = false;
8783

8884
// shorthand or constructor methods
8985
if (parent.method || parent.type === 'MethodDefinition') {
86+
shorthand = true;
9087
node = parent.key;
9188
}
9289

@@ -96,7 +93,7 @@ module.exports.prototype = {
9693
currentToken = file.getNextToken(currentToken);
9794
}
9895

99-
if (beforeStar) {
96+
if (beforeStar && !shorthand) {
10097
// currentToken assigned outside of function
10198
errors.assert.whitespaceBetween({
10299
token: currentToken,
@@ -106,8 +103,13 @@ module.exports.prototype = {
106103
}
107104

108105
if (afterStar) {
109-
// currentToken reassigned for star token
110-
currentToken = file.getNextToken(currentToken);
106+
if (shorthand) {
107+
currentToken = file.getPrevToken(currentToken);
108+
} else {
109+
// currentToken reassigned for star token
110+
currentToken = file.getNextToken(currentToken);
111+
}
112+
111113
errors.assert.whitespaceBetween({
112114
token: currentToken,
113115
nextToken: file.getNextToken(currentToken),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ 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() {
70+
it('should not report shorthand method', function() {
7171
expect(checker.checkString('({ * foo() {} });')).to.have.no.errors();
7272
});
7373

@@ -163,7 +163,7 @@ describe('rules/disallow-spaces-in-generator', function() {
163163
});
164164

165165
reportAndFix({
166-
name: 'should report illegal space after the star for the shortland',
166+
name: 'should report illegal space after the star for the shorthand',
167167
rules: rules,
168168
errors: 1,
169169
input: '({ * foo() {} });',

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,31 @@ describe('rules/require-spaces-in-generator', function() {
9191
output: 'var x = function * asdf(){}'
9292
});
9393

94+
reportAndFix({
95+
name: 'should report missing space after the star for the shorthand',
96+
rules: rules,
97+
errors: 1,
98+
input: '({ *foo() {} });',
99+
output: '({ * foo() {} });'
100+
});
101+
102+
it('should not report shorthand method', function() {
103+
expect(checker.checkString('({ * foo() {} })')).to.have.no.errors();
104+
});
105+
94106
it('should not report missing spaces error', function() {
95107
expect(checker.checkString('var x = function * (){}')).to.have.no.errors();
96108
});
97109

110+
it('should bail out if this is not a generator', function() {
111+
expect(checker.checkString('var x = function (){}')).to.have.no.errors();
112+
});
113+
98114
it('should not report missing spaces error with named function', function() {
99115
expect(checker.checkString('var x = function * asdf(){}')).to.have.no.errors();
100116
});
101117

102-
it('should not report missing spaces error with function delcaration', function() {
118+
it('should not report missing spaces error with function declaration', function() {
103119
expect(checker.checkString('function * asdf(){}')).to.have.no.errors();
104120
});
105121

0 commit comments

Comments
 (0)