Skip to content

Commit

Permalink
feat: jsxSpaceBeforeTrailingSlashRule
Browse files Browse the repository at this point in the history
  • Loading branch information
paibamboo committed Mar 14, 2019
1 parent ceed2ee commit b933c36
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/jsxSpaceBeforeTrailingSlashRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as Lint from "tslint";
import * as ts from "typescript";

class JsxSpaceBeforeTrailingSlashRule extends Lint.RuleWalker {
private readonly enforceWhiteSpace: boolean = true;
constructor(
sourceFile: ts.SourceFile,
options: Lint.IOptions
) {
super(sourceFile, options);
if (options.ruleArguments[0] === "never") {
this.enforceWhiteSpace = false;
}
}

private static hasWhitespaceBeforeClosing(nodeText: string): boolean {
return /\s/.test(nodeText.charAt(nodeText.length - "/>".length - 1));
}

public visitJsxSelfClosingElement(node: ts.JsxSelfClosingElement): void {
super.visitJsxSelfClosingElement(node);
if (this.enforceWhiteSpace) {
if (!JsxSpaceBeforeTrailingSlashRule.hasWhitespaceBeforeClosing(node.getText())) {
this.addFailureAtNode(node, "Self-closing JSX elements must have a space before the '/>' part");
return;
}
} else {
if (!/[\r\n]/.test(node.getText()) && JsxSpaceBeforeTrailingSlashRule.hasWhitespaceBeforeClosing(node.getText())) {
this.addFailureAtNode(node, "One-line self-closing JSX elements must not have a space(s) before the '/>' part");
return;
}
}
}
}

// tslint:disable-next-line:export-name max-classes-per-file
export class Rule extends Lint.Rules.TypedRule {
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithWalker(new JsxSpaceBeforeTrailingSlashRule(sourceFile, this.getOptions()));
}
}
23 changes: 23 additions & 0 deletions test/rules/jsx-space-before-trailing-slash/default/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div>
Contents
</div>

<span/>
~~~~~~~ [Self-closing JSX elements must have a space before the '/>' part]

<button />

<h2 class="colouring" contents="B"/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Self-closing JSX elements must have a space before the '/>' part]

<button onClick="run()" />

<img
src="./foo/bar.png"
/>

<div>
<img
src="./foo/bar.png"
/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": ["es6"]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rulesDirectory": [
"../../../../lib"
],
"rules": {
"jsx-space-before-trailing-slash": true
}
}
23 changes: 23 additions & 0 deletions test/rules/jsx-space-before-trailing-slash/never/test.tsx.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div>
Contents
</div>

<span/>

<button />
~~~~~~~~~~ [One-line self-closing JSX elements must not have a space(s) before the '/>' part]

<h2 class="colouring" contents="B"/>

<button onClick="run()" />
~~~~~~~~~~~~~~~~~~~~~~~~~~ [One-line self-closing JSX elements must not have a space(s) before the '/>' part]

<img
src="./foo/bar.png"
/>

<div>
<img
src="./foo/bar.png"
/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"lib": ["es6"]
}
}
8 changes: 8 additions & 0 deletions test/rules/jsx-space-before-trailing-slash/never/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rulesDirectory": [
"../../../../lib"
],
"rules": {
"jsx-space-before-trailing-slash": [true, "never"]
}
}

0 comments on commit b933c36

Please sign in to comment.