From 474d72b72904e6ed80833bdf47f9d9a324367370 Mon Sep 17 00:00:00 2001 From: Heather Booker Date: Thu, 8 Nov 2018 18:00:34 -0500 Subject: [PATCH] Add auto fixer for jsx-space-before-trailing-slash --- README.md | 1 + src/rules/jsxSpaceBeforeTrailingSlashRule.ts | 3 ++- .../jsx-space-before-trailing-slash/test.tsx.fix | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/rules/jsx-space-before-trailing-slash/test.tsx.fix diff --git a/README.md b/README.md index c7ab243..6a1c982 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ The built-in configuration preset you get with `"extends": "tslint-react"` is se - `jsx-space-before-trailing-slash` - Checks that self-closing JSX elements have a space before the '/>' part. - Rule options: _none_ + - _Includes automatic code fix_ - `jsx-wrap-multiline` (since v2.1) - Enforces that multiline JSX expressions are wrapped with parentheses. - Opening parenthesis must be followed by a newline. diff --git a/src/rules/jsxSpaceBeforeTrailingSlashRule.ts b/src/rules/jsxSpaceBeforeTrailingSlashRule.ts index 24f812f..f99522b 100644 --- a/src/rules/jsxSpaceBeforeTrailingSlashRule.ts +++ b/src/rules/jsxSpaceBeforeTrailingSlashRule.ts @@ -48,7 +48,8 @@ function walk(ctx: Lint.WalkContext): void { return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void { if (isJsxSelfClosingElement(node)) { if (!hasWhitespaceBeforeClosing(node.getText(ctx.sourceFile))) { - ctx.addFailureAtNode(node, Rule.FAILURE_STRING); + const fix = Lint.Replacement.appendText(node.getEnd() - closingLength, " "); + ctx.addFailureAtNode(node, Rule.FAILURE_STRING, fix); } } return ts.forEachChild(node, cb); diff --git a/test/rules/jsx-space-before-trailing-slash/test.tsx.fix b/test/rules/jsx-space-before-trailing-slash/test.tsx.fix new file mode 100644 index 0000000..1a306b8 --- /dev/null +++ b/test/rules/jsx-space-before-trailing-slash/test.tsx.fix @@ -0,0 +1,15 @@ +
+ Contents +
+ + + +