Skip to content

Commit

Permalink
add fix for jsx-boolean-value rule (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
suchanlee authored Apr 23, 2018
1 parent daeb290 commit f5ee02c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/rules/jsxBooleanValueRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,22 @@ function walk(ctx: Lint.WalkContext<string | undefined>): void {
if (initializer === undefined) {
// if no option set, or explicitly set to "always"
if (ctx.options === undefined || ctx.options === OPTION_ALWAYS) {
ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.ALWAYS_MESSAGE);
const text = node.name.text;
const width = text.length;
const start = node.end - width;
const fix = Lint.Replacement.replaceFromTo(start, node.end, `${text}={true}`);
ctx.addFailureAt(start, width, Rule.ALWAYS_MESSAGE, fix);
}
} else if (initializer.kind === ts.SyntaxKind.JsxExpression) {
const isValueTrue = initializer.expression !== undefined
&& initializer.expression.kind === ts.SyntaxKind.TrueKeyword;

if (isValueTrue && ctx.options === OPTION_NEVER) {
ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.NEVER_MESSAGE);
const width = node.getWidth(ctx.sourceFile);
const start = node.end - width;
const fix = Lint.Replacement.replaceFromTo(
start, node.end, node.getFirstToken(ctx.sourceFile).getText(ctx.sourceFile));
ctx.addFailureAt(start, width, Rule.NEVER_MESSAGE, fix);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions test/rules/jsx-boolean-value/always/test.tsx.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function render(){
return(
<Foo readOnly={true} />
<Foo readOnly={true} />
<Foo readOnly={true} bar={true} />
);
}
3 changes: 3 additions & 0 deletions test/rules/jsx-boolean-value/always/test.tsx.lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ function render(){
<Foo readOnly={true} />
<Foo readOnly />
~~~~~~~~ [Value must be set for boolean attributes]
<Foo readOnly bar />
~~~~~~~~ [Value must be set for boolean attributes]
~~~ [Value must be set for boolean attributes]
);
}
7 changes: 7 additions & 0 deletions test/rules/jsx-boolean-value/never/test.tsx.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function render(){
return(
<Foo readOnly />
<Foo readOnly />
<Foo readOnly />
);
}
2 changes: 2 additions & 0 deletions test/rules/jsx-boolean-value/never/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function render(){
return(
<Foo readOnly = { true } />
~~~~~~~~~~~~~~~~~~~ [Value must be omitted for boolean attributes]
<Foo readOnly={true} />
~~~~~~~~~~~~~~~ [Value must be omitted for boolean attributes]
<Foo readOnly />
Expand Down
7 changes: 7 additions & 0 deletions test/rules/jsx-boolean-value/true/test.tsx.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function render(){
return(
<Foo readOnly={true} />
<Foo readOnly={true} />
<Foo readOnly={false} />
);
}

0 comments on commit f5ee02c

Please sign in to comment.