From f5ee02c8be3d21a6aa7cbde192731678fc52dbbb Mon Sep 17 00:00:00 2001 From: Suchan Lee Date: Mon, 23 Apr 2018 02:21:39 -0700 Subject: [PATCH] add fix for jsx-boolean-value rule (#154) --- src/rules/jsxBooleanValueRule.ts | 12 ++++++++++-- test/rules/jsx-boolean-value/always/test.tsx.fix | 7 +++++++ test/rules/jsx-boolean-value/always/test.tsx.lint | 3 +++ test/rules/jsx-boolean-value/never/test.tsx.fix | 7 +++++++ test/rules/jsx-boolean-value/never/test.tsx.lint | 2 ++ test/rules/jsx-boolean-value/true/test.tsx.fix | 7 +++++++ 6 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 test/rules/jsx-boolean-value/always/test.tsx.fix create mode 100644 test/rules/jsx-boolean-value/never/test.tsx.fix create mode 100644 test/rules/jsx-boolean-value/true/test.tsx.fix diff --git a/src/rules/jsxBooleanValueRule.ts b/src/rules/jsxBooleanValueRule.ts index ac8dee1..1d2c998 100644 --- a/src/rules/jsxBooleanValueRule.ts +++ b/src/rules/jsxBooleanValueRule.ts @@ -66,14 +66,22 @@ function walk(ctx: Lint.WalkContext): 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); } } } diff --git a/test/rules/jsx-boolean-value/always/test.tsx.fix b/test/rules/jsx-boolean-value/always/test.tsx.fix new file mode 100644 index 0000000..8510aeb --- /dev/null +++ b/test/rules/jsx-boolean-value/always/test.tsx.fix @@ -0,0 +1,7 @@ +function render(){ + return( + + + + ); +} diff --git a/test/rules/jsx-boolean-value/always/test.tsx.lint b/test/rules/jsx-boolean-value/always/test.tsx.lint index bd53af0..6ef2434 100644 --- a/test/rules/jsx-boolean-value/always/test.tsx.lint +++ b/test/rules/jsx-boolean-value/always/test.tsx.lint @@ -3,5 +3,8 @@ function render(){ ~~~~~~~~ [Value must be set for boolean attributes] + + ~~~~~~~~ [Value must be set for boolean attributes] + ~~~ [Value must be set for boolean attributes] ); } diff --git a/test/rules/jsx-boolean-value/never/test.tsx.fix b/test/rules/jsx-boolean-value/never/test.tsx.fix new file mode 100644 index 0000000..bb06849 --- /dev/null +++ b/test/rules/jsx-boolean-value/never/test.tsx.fix @@ -0,0 +1,7 @@ +function render(){ + return( + + + + ); +} diff --git a/test/rules/jsx-boolean-value/never/test.tsx.lint b/test/rules/jsx-boolean-value/never/test.tsx.lint index e92e892..c284295 100644 --- a/test/rules/jsx-boolean-value/never/test.tsx.lint +++ b/test/rules/jsx-boolean-value/never/test.tsx.lint @@ -1,5 +1,7 @@ function render(){ return( + + ~~~~~~~~~~~~~~~~~~~ [Value must be omitted for boolean attributes] ~~~~~~~~~~~~~~~ [Value must be omitted for boolean attributes] diff --git a/test/rules/jsx-boolean-value/true/test.tsx.fix b/test/rules/jsx-boolean-value/true/test.tsx.fix new file mode 100644 index 0000000..5869abe --- /dev/null +++ b/test/rules/jsx-boolean-value/true/test.tsx.fix @@ -0,0 +1,7 @@ +function render(){ + return( + + + + ); +}