1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | "use strict";
|
18 | var __extends = (this && this.__extends) || function (d, b) {
|
19 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
20 | function __() { this.constructor = d; }
|
21 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
22 | };
|
23 | var Lint = require("tslint");
|
24 | var ts = require("typescript");
|
25 | var Rule = (function (_super) {
|
26 | __extends(Rule, _super);
|
27 | function Rule() {
|
28 | return _super.apply(this, arguments) || this;
|
29 | }
|
30 | Rule.prototype.apply = function (sourceFile) {
|
31 | var walker = new JsxSelfCloseWalker(sourceFile, this.getOptions());
|
32 | return this.applyWithWalker(walker);
|
33 | };
|
34 | return Rule;
|
35 | }(Lint.Rules.AbstractRule));
|
36 | Rule.FAILURE_STRING = "JSX elements with no children must be self-closing";
|
37 | exports.Rule = Rule;
|
38 | var JsxSelfCloseWalker = (function (_super) {
|
39 | __extends(JsxSelfCloseWalker, _super);
|
40 | function JsxSelfCloseWalker() {
|
41 | return _super.apply(this, arguments) || this;
|
42 | }
|
43 | JsxSelfCloseWalker.prototype.visitJsxElement = function (node) {
|
44 | var missingOpeningOrClosingTag = node.openingElement == null || node.closingElement == null;
|
45 |
|
46 |
|
47 | var textIsEmpty = node.children.length === 1
|
48 | && node.children[0].kind === ts.SyntaxKind.JsxText
|
49 | && node.children[0].getText() === "";
|
50 | var noChildren = node.children.length === 0 || textIsEmpty;
|
51 | if (missingOpeningOrClosingTag || noChildren) {
|
52 | this.addFailure(this.createFailure(node.getStart(), node.getWidth(), Rule.FAILURE_STRING));
|
53 | }
|
54 | _super.prototype.visitJsxElement.call(this, node);
|
55 | };
|
56 | return JsxSelfCloseWalker;
|
57 | }(Lint.RuleWalker));
|