UNPKG

2.45 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2016 Palantir Technologies, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17"use strict";
18var __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};
23var Lint = require("tslint");
24var ts = require("typescript");
25var 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));
36Rule.FAILURE_STRING = "JSX elements with no children must be self-closing";
37exports.Rule = Rule;
38var 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 // The last part of the textIsEmpty assignment is to check whether the tag is completely empty or
46 // only consists of spaces/new lines.
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));