-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: jsxSpaceBeforeTrailingSlashRule
- Loading branch information
Showing
7 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
test/rules/jsx-space-before-trailing-slash/default/test.tsx.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
7 changes: 7 additions & 0 deletions
7
test/rules/jsx-space-before-trailing-slash/default/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
8
test/rules/jsx-space-before-trailing-slash/default/tslint.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
test/rules/jsx-space-before-trailing-slash/never/test.tsx.lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
7 changes: 7 additions & 0 deletions
7
test/rules/jsx-space-before-trailing-slash/never/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es5", | ||
"lib": ["es6"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"rulesDirectory": [ | ||
"../../../../lib" | ||
], | ||
"rules": { | ||
"jsx-space-before-trailing-slash": [true, "never"] | ||
} | ||
} |