Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Fix #353: react-tsx-curly-spacing bug with comments (#533)
Browse files Browse the repository at this point in the history
* Add single comment jsx expression test case

* Add node children count check to avoid empty code block validation
  • Loading branch information
br1anchen authored and Josh Goldberg committed Oct 8, 2018
1 parent 3d5036d commit 1706244
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/reactTsxCurlySpacingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,15 @@ class TsxCurlySpacingWalker extends Lint.RuleWalker {

public visitJsxExpression(node: ts.JsxExpression): void {
const childrenCount: number = node.getChildCount();
const first = node.getFirstToken(); // '{' sign
const last = node.getLastToken(); // '}' sign
const second: ts.Node = node.getChildAt(1); // after '{' sign
const penultimate: ts.Node = node.getChildAt(childrenCount - 2); // before '}' sign
this.validateBraceSpacing(node, first, second, first);
this.validateBraceSpacing(node, penultimate, last, last);

if (childrenCount > 2) {// not empty code block (eg. only comments)
const first = node.getFirstToken(); // '{' sign
const last = node.getLastToken(); // '}' sign
const second: ts.Node = node.getChildAt(1); // after '{' sign
const penultimate: ts.Node = node.getChildAt(childrenCount - 2); // before '}' sign
this.validateBraceSpacing(node, first, second, first);
this.validateBraceSpacing(node, penultimate, last, last);
}
}

protected visitNode(node: ts.Node): void {
Expand Down
1 change: 1 addition & 0 deletions src/tests/ReactTsxCurlySpacingRuleTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('reactTsxCurlySpacing', () => {
const script: string = `
import React = require('react');
const a = <Hello name={firstname} />;
const b = <div>{/* comment */}</div>;
`;
TestHelper.assertViolationsWithOptions(ruleName, [ 'never' ], script, []);
});
Expand Down

0 comments on commit 1706244

Please sign in to comment.