Skip to content

Commit 55d0c9d

Browse files
committed
Merge branch 'gitim-fix-jsx-curly-spacing'
2 parents cfecd9c + 1770719 commit 55d0c9d

File tree

2 files changed

+327
-17
lines changed

2 files changed

+327
-17
lines changed

lib/rules/jsx-curly-spacing.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,16 @@ module.exports = function(context) {
144144
/**
145145
* Determines if spacing in curly braces is valid.
146146
* @param {ASTNode} node The AST node to check.
147-
* @param {Token} first The first token to check (should be the opening brace)
148-
* @param {Token} second The second token to check (should be first after the opening brace)
149-
* @param {Token} penultimate The penultimate token to check (should be last before closing brace)
150-
* @param {Token} last The last token to check (should be closing brace)
151147
* @returns {void}
152148
*/
153-
function validateBraceSpacing(node, first, second, third, antepenultimate, penultimate, last) {
149+
function validateBraceSpacing(node) {
150+
var first = context.getFirstToken(node);
151+
var last = sourceCode.getLastToken(node);
152+
var second = context.getTokenAfter(first);
153+
var penultimate = sourceCode.getTokenBefore(last);
154+
var third = context.getTokenAfter(second);
155+
var antepenultimate = sourceCode.getTokenBefore(penultimate);
156+
154157
if (spaced) {
155158
if (!alternative) {
156159
if (!sourceCode.isSpaceBetweenTokens(first, second)) {
@@ -207,16 +210,8 @@ module.exports = function(context) {
207210
// --------------------------------------------------------------------------
208211

209212
return {
210-
JSXExpressionContainer: function(node) {
211-
var first = context.getFirstToken(node);
212-
var last = sourceCode.getLastToken(node);
213-
var second = context.getTokenAfter(first);
214-
var penultimate = sourceCode.getTokenBefore(last);
215-
var third = context.getTokenAfter(second);
216-
var antepenultimate = sourceCode.getTokenBefore(penultimate);
217-
218-
validateBraceSpacing(node, first, second, third, antepenultimate, penultimate, last);
219-
}
213+
JSXExpressionContainer: validateBraceSpacing,
214+
JSXSpreadAttribute: validateBraceSpacing
220215
};
221216
};
222217

tests/lib/rules/jsx-curly-spacing.js

+317-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ ruleTester.run('jsx-curly-spacing', rule, {
8181
}, {
8282
code: '<App foo={bar/* comment */} />;',
8383
options: ['never'],
84-
parserOptions: parserOptions,
85-
parser: 'babel-eslint'
84+
parserOptions: parserOptions
8685
}, {
8786
code: '<App foo={ bar } />;',
8887
options: ['always', {alternative: true}],
@@ -99,6 +98,86 @@ ruleTester.run('jsx-curly-spacing', rule, {
9998
].join('\n'),
10099
options: ['always', {alternative: true}],
101100
parserOptions: parserOptions
101+
}, {
102+
code: '<App {...bar} />;',
103+
parserOptions: parserOptions
104+
}, {
105+
code: '<App {...bar} />;',
106+
options: ['never'],
107+
parserOptions: parserOptions
108+
}, {
109+
code: '<App { ...bar } />;',
110+
options: ['always'],
111+
parserOptions: parserOptions
112+
}, {
113+
code: '<App { ...bar } />;',
114+
options: ['always', {allowMultiline: false}],
115+
parserOptions: parserOptions
116+
}, {
117+
code: [
118+
'<App {',
119+
'...bar',
120+
'} />;'
121+
].join('\n'),
122+
options: ['always'],
123+
parserOptions: parserOptions
124+
}, {
125+
code: [
126+
'<App {',
127+
'...bar',
128+
'} />;'
129+
].join('\n'),
130+
options: ['always'],
131+
parserOptions: parserOptions
132+
}, {
133+
code: [
134+
'<App {',
135+
'...bar',
136+
'} />;'
137+
].join('\n'),
138+
options: ['never'],
139+
parserOptions: parserOptions
140+
}, {
141+
code: '<App {...bar/* comment */} />;',
142+
options: ['never'],
143+
parserOptions: parserOptions
144+
}, {
145+
code: '<App foo={bar} {...baz} />;',
146+
parserOptions: parserOptions
147+
}, {
148+
code: '<App foo={bar} {...baz} />;',
149+
options: ['never'],
150+
parserOptions: parserOptions
151+
}, {
152+
code: '<App foo={ bar } { ...baz } />;',
153+
options: ['always'],
154+
parserOptions: parserOptions
155+
}, {
156+
code: '<App foo={ bar } { ...baz } />;',
157+
options: ['always', {allowMultiline: false}],
158+
parserOptions: parserOptions
159+
}, {
160+
code: '<App foo={{ bar:baz }} {...baz} />;',
161+
options: ['never'],
162+
parserOptions: parserOptions
163+
}, {
164+
code: '<App foo={ {bar:baz} } { ...baz } />;',
165+
options: ['always'],
166+
parserOptions: parserOptions
167+
}, {
168+
code: [
169+
'<App foo={',
170+
'bar',
171+
'} {',
172+
'...bar',
173+
'}/>;'
174+
].join('\n'),
175+
options: ['always'],
176+
parserOptions: parserOptions
177+
}, {
178+
code: '<App foo={bar/* comment */} {...baz/* comment */} />;',
179+
options: ['never'],
180+
parserOptions: parserOptions
102181
}],
103182

104183
invalid: [{
@@ -241,5 +320,241 @@ ruleTester.run('jsx-curly-spacing', rule, {
241320
message: 'There should be no space before \'}\''
242321
}],
243322
parserOptions: parserOptions
323+
}, {
324+
code: '<App { ...bar } />;',
325+
output: '<App {...bar} />;',
326+
options: ['never'],
327+
errors: [{
328+
message: 'There should be no space after \'{\''
329+
}, {
330+
message: 'There should be no space before \'}\''
331+
}],
332+
parserOptions: parserOptions
333+
}, {
334+
code: '<App { ...bar } />;',
335+
output: '<App {...bar} />;',
336+
options: ['never', {allowMultiline: false}],
337+
errors: [{
338+
message: 'There should be no space after \'{\''
339+
}, {
340+
message: 'There should be no space before \'}\''
341+
}],
342+
parserOptions: parserOptions
343+
}, {
344+
code: '<App {...bar} />;',
345+
output: '<App { ...bar } />;',
346+
options: ['always'],
347+
errors: [{
348+
message: 'A space is required after \'{\''
349+
}, {
350+
message: 'A space is required before \'}\''
351+
}],
352+
parserOptions: parserOptions
353+
}, {
354+
code: '<App {...bar} />;',
355+
output: '<App { ...bar } />;',
356+
options: ['always', {allowMultiline: false}],
357+
errors: [{
358+
message: 'A space is required after \'{\''
359+
}, {
360+
message: 'A space is required before \'}\''
361+
}],
362+
parserOptions: parserOptions
363+
}, {
364+
code: '<App { ...bar} />;',
365+
output: '<App { ...bar } />;',
366+
options: ['always'],
367+
errors: [{
368+
message: 'A space is required before \'}\''
369+
}],
370+
parserOptions: parserOptions
371+
}, {
372+
code: '<App {...bar } />;',
373+
output: '<App { ...bar } />;',
374+
options: ['always'],
375+
errors: [{
376+
message: 'A space is required after \'{\''
377+
}],
378+
parserOptions: parserOptions
379+
}, {
380+
code: '<App { ...bar} />;',
381+
output: '<App {...bar} />;',
382+
options: ['never'],
383+
errors: [{
384+
message: 'There should be no space after \'{\''
385+
}],
386+
parserOptions: parserOptions
387+
}, {
388+
code: '<App {...bar } />;',
389+
output: '<App {...bar} />;',
390+
options: ['never'],
391+
errors: [{
392+
message: 'There should be no space before \'}\''
393+
}],
394+
parserOptions: parserOptions
395+
}, {
396+
code: [
397+
'<App {',
398+
'...bar',
399+
'} />;'
400+
].join('\n'),
401+
output: '<App {...bar} />;',
402+
options: ['never', {allowMultiline: false}],
403+
errors: [{
404+
message: 'There should be no space after \'{\''
405+
}, {
406+
message: 'There should be no space before \'}\''
407+
}],
408+
parserOptions: parserOptions
409+
}, {
410+
code: [
411+
'<App {',
412+
'...bar',
413+
'} />;'
414+
].join('\n'),
415+
output: '<App { ...bar } />;',
416+
options: ['always', {allowMultiline: false}],
417+
errors: [{
418+
message: 'There should be no newline after \'{\''
419+
}, {
420+
message: 'There should be no newline before \'}\''
421+
}],
422+
parserOptions: parserOptions
423+
}, {
424+
code: '<App foo={ bar } { ...baz } />;',
425+
output: '<App foo={bar} {...baz} />;',
426+
options: ['never'],
427+
errors: [{
428+
message: 'There should be no space after \'{\''
429+
}, {
430+
message: 'There should be no space before \'}\''
431+
}, {
432+
message: 'There should be no space after \'{\''
433+
}, {
434+
message: 'There should be no space before \'}\''
435+
}],
436+
parserOptions: parserOptions
437+
}, {
438+
code: '<App foo={ bar } { ...baz } />;',
439+
output: '<App foo={bar} {...baz} />;',
440+
options: ['never', {allowMultiline: false}],
441+
errors: [{
442+
message: 'There should be no space after \'{\''
443+
}, {
444+
message: 'There should be no space before \'}\''
445+
}, {
446+
message: 'There should be no space after \'{\''
447+
}, {
448+
message: 'There should be no space before \'}\''
449+
}],
450+
parserOptions: parserOptions
451+
}, {
452+
code: '<App foo={bar} {...baz} />;',
453+
output: '<App foo={ bar } { ...baz } />;',
454+
options: ['always'],
455+
errors: [{
456+
message: 'A space is required after \'{\''
457+
}, {
458+
message: 'A space is required before \'}\''
459+
}, {
460+
message: 'A space is required after \'{\''
461+
}, {
462+
message: 'A space is required before \'}\''
463+
}],
464+
parserOptions: parserOptions
465+
}, {
466+
code: '<App foo={bar} {...baz} />;',
467+
output: '<App foo={ bar } { ...baz } />;',
468+
options: ['always', {allowMultiline: false}],
469+
errors: [{
470+
message: 'A space is required after \'{\''
471+
}, {
472+
message: 'A space is required before \'}\''
473+
}, {
474+
message: 'A space is required after \'{\''
475+
}, {
476+
message: 'A space is required before \'}\''
477+
}],
478+
parserOptions: parserOptions
479+
}, {
480+
code: '<App foo={ bar} { ...baz} />;',
481+
output: '<App foo={ bar } { ...baz } />;',
482+
options: ['always'],
483+
errors: [{
484+
message: 'A space is required before \'}\''
485+
}, {
486+
message: 'A space is required before \'}\''
487+
}],
488+
parserOptions: parserOptions
489+
}, {
490+
code: '<App foo={bar } {...baz } />;',
491+
output: '<App foo={ bar } { ...baz } />;',
492+
options: ['always'],
493+
errors: [{
494+
message: 'A space is required after \'{\''
495+
}, {
496+
message: 'A space is required after \'{\''
497+
}],
498+
parserOptions: parserOptions
499+
}, {
500+
code: '<App foo={ bar} { ...baz} />;',
501+
output: '<App foo={bar} {...baz} />;',
502+
options: ['never'],
503+
errors: [{
504+
message: 'There should be no space after \'{\''
505+
}, {
506+
message: 'There should be no space after \'{\''
507+
}],
508+
parserOptions: parserOptions
509+
}, {
510+
code: '<App foo={bar } {...baz } />;',
511+
output: '<App foo={bar} {...baz} />;',
512+
options: ['never'],
513+
errors: [{
514+
message: 'There should be no space before \'}\''
515+
}, {
516+
message: 'There should be no space before \'}\''
517+
}],
518+
parserOptions: parserOptions
519+
}, {
520+
code: [
521+
'<App foo={',
522+
'bar',
523+
'} {',
524+
'...baz',
525+
'} />;'
526+
].join('\n'),
527+
output: '<App foo={bar} {...baz} />;',
528+
options: ['never', {allowMultiline: false}],
529+
errors: [{
530+
message: 'There should be no space after \'{\''
531+
}, {
532+
message: 'There should be no space before \'}\''
533+
}, {
534+
message: 'There should be no space after \'{\''
535+
}, {
536+
message: 'There should be no space before \'}\''
537+
}],
538+
parserOptions: parserOptions
539+
}, {
540+
code: [
541+
'<App foo={',
542+
'bar',
543+
'} {',
544+
'...baz',
545+
'} />;'
546+
].join('\n'),
547+
output: '<App foo={ bar } { ...baz } />;',
548+
options: ['always', {allowMultiline: false}],
549+
errors: [{
550+
message: 'There should be no newline after \'{\''
551+
}, {
552+
message: 'There should be no newline before \'}\''
553+
}, {
554+
message: 'There should be no newline after \'{\''
555+
}, {
556+
message: 'There should be no newline before \'}\''
557+
}],
558+
parserOptions: parserOptions
244559
}]
245560
});

0 commit comments

Comments
 (0)