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

Commit 4f65c05

Browse files
committed
Fix: requireSpacesInGenerator - account for named functions
Fixes #1955 Closes gh-1963
1 parent d4408f4 commit 4f65c05

File tree

4 files changed

+129
-65
lines changed

4 files changed

+129
-65
lines changed

lib/config/configuration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ Configuration.prototype.registerDefaultRules = function() {
947947
this.registerRule(require('../rules/require-shorthand-arrow-functions'));
948948
this.registerRule(require('../rules/disallow-shorthand-arrow-functions'));
949949
this.registerRule(require('../rules/disallow-identical-destructuring-names'));
950-
this.registerRule(require('../rules/require-spaces-around-generators'));
950+
this.registerRule(require('../rules/require-spaces-in-generator'));
951951

952952
/* ES6 only (end) */
953953

lib/rules/require-spaces-around-generators.js renamed to lib/rules/require-spaces-in-generator.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ module.exports.prototype = {
7878
return;
7979
}
8080

81-
// for a named function, use node.id
82-
var functionNode = node.id || node;
8381
var parent = node.parentNode;
8482

8583
// Ignore syntactic sugar for getters and setters.
@@ -89,10 +87,10 @@ module.exports.prototype = {
8987

9088
// shorthand or constructor methods
9189
if (parent.method || parent.type === 'MethodDefinition') {
92-
functionNode = parent.key;
90+
node = parent.key;
9391
}
9492

95-
var currentToken = file.getFirstNodeToken(functionNode);
93+
var currentToken = file.getFirstNodeToken(node);
9694

9795
if (node.async && currentToken.value === 'async') {
9896
currentToken = file.getNextToken(currentToken);

test/specs/rules/require-space-around-generators.js

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
var Checker = require('../../../lib/checker');
2+
var expect = require('chai').expect;
3+
var reportAndFix = require('../../lib/assertHelpers').reportAndFix;
4+
5+
describe('rules/require-spaces-in-generator', function() {
6+
var checker;
7+
beforeEach(function() {
8+
checker = new Checker();
9+
checker.registerDefaultRules();
10+
});
11+
12+
describe('both true', function() {
13+
var rules = {
14+
requireSpacesInGenerator: { beforeStar: true, afterStar: true },
15+
esnext: true
16+
};
17+
18+
beforeEach(function() {
19+
checker.configure(rules);
20+
});
21+
22+
reportAndFix({
23+
name: 'should report missing space before and after the star with function declaration',
24+
rules: rules,
25+
errors: 2,
26+
input: 'function*asdf(){}',
27+
output: 'function * asdf(){}'
28+
});
29+
30+
reportAndFix({
31+
name: 'should report missing space after the star with function declaration',
32+
rules: rules,
33+
errors: 1,
34+
input: 'function *asdf(){}',
35+
output: 'function * asdf(){}'
36+
});
37+
38+
reportAndFix({
39+
name: 'should report missing space before the star with function declaration',
40+
rules: rules,
41+
errors: 1,
42+
input: 'function* asdf(){}',
43+
output: 'function * asdf(){}'
44+
});
45+
46+
reportAndFix({
47+
name: 'should report missing space before and after the star',
48+
rules: rules,
49+
errors: 2,
50+
input: 'var x = function*(){}',
51+
output: 'var x = function * (){}'
52+
});
53+
54+
reportAndFix({
55+
name: 'should report missing space after the star',
56+
rules: rules,
57+
errors: 1,
58+
input: 'var x = function *(){}',
59+
output: 'var x = function * (){}'
60+
});
61+
62+
reportAndFix({
63+
name: 'should report missing space before the star',
64+
rules: rules,
65+
errors: 1,
66+
input: 'var x = function* (){}',
67+
output: 'var x = function * (){}'
68+
});
69+
70+
reportAndFix({
71+
name: 'should report missing space before and after the star with named function',
72+
rules: rules,
73+
errors: 2,
74+
input: 'var x = function*asdf(){}',
75+
output: 'var x = function * asdf(){}'
76+
});
77+
78+
reportAndFix({
79+
name: 'should report missing space after the star with named function',
80+
rules: rules,
81+
errors: 1,
82+
input: 'var x = function *asdf(){}',
83+
output: 'var x = function * asdf(){}'
84+
});
85+
86+
reportAndFix({
87+
name: 'should report missing space before the star with named function',
88+
rules: rules,
89+
errors: 1,
90+
input: 'var x = function* asdf(){}',
91+
output: 'var x = function * asdf(){}'
92+
});
93+
94+
it('should not report missing spaces error', function() {
95+
expect(checker.checkString('var x = function * (){}')).to.have.no.errors();
96+
});
97+
98+
it('should not report missing spaces error with named function', function() {
99+
expect(checker.checkString('var x = function * asdf(){}')).to.have.no.errors();
100+
});
101+
102+
it('should not report missing spaces error with function delcaration', function() {
103+
expect(checker.checkString('function * asdf(){}')).to.have.no.errors();
104+
});
105+
106+
it('should not report missing spaces error with async function', function() {
107+
expect(checker.checkString('var x = async function * (){}')).to.have.no.errors();
108+
});
109+
110+
it('should skip async functions with named function', function() {
111+
expect(checker.checkString('var x = async function * asdf(){}')).to.have.no.errors();
112+
});
113+
114+
it('should report missing space after star for async function', function() {
115+
var testExp = checker.checkString('var x = async function *(){}');
116+
var reqTest = 'requireSpacesInGenerator';
117+
expect(testExp).to.have.one.validation.error.from(reqTest);
118+
});
119+
120+
it('should report missing space after star for named async function', function() {
121+
var testExp = checker.checkString('var x = async function *asdf(){}');
122+
var reqTest = 'requireSpacesInGenerator';
123+
expect(testExp).to.have.one.validation.error.from(reqTest);
124+
});
125+
});
126+
});

0 commit comments

Comments
 (0)