UNPKG

4.83 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = function (d, b) {
4 extendStatics = Object.setPrototypeOf ||
5 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7 return extendStatics(d, b);
8 }
9 return function (d, b) {
10 extendStatics(d, b);
11 function __() { this.constructor = d; }
12 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13 };
14})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var AstUtils_1 = require("./utils/AstUtils");
19var FAILURE_STRING_MANIPULATION = 'Replace HTML string manipulation with jQuery API: ';
20var FAILURE_STRING_COMPLEX = 'Replace complex HTML strings with jQuery API: ';
21var Rule = (function (_super) {
22 __extends(Rule, _super);
23 function Rule() {
24 return _super !== null && _super.apply(this, arguments) || this;
25 }
26 Rule.prototype.apply = function (sourceFile) {
27 return this.applyWithWalker(new NoJqueryRawElementsRuleWalker(sourceFile, this.getOptions()));
28 };
29 Rule.metadata = {
30 ruleName: 'no-jquery-raw-elements',
31 type: 'maintainability',
32 description: 'Do not create HTML elements using JQuery and string concatenation. It is error prone and can hide subtle defects.',
33 options: null,
34 optionsDescription: '',
35 typescriptOnly: true,
36 issueClass: 'Non-SDL',
37 issueType: 'Warning',
38 severity: 'Important',
39 level: 'Opportunity for Excellence',
40 group: 'Correctness',
41 commonWeaknessEnumeration: '398, 710'
42 };
43 return Rule;
44}(Lint.Rules.AbstractRule));
45exports.Rule = Rule;
46var NoJqueryRawElementsRuleWalker = (function (_super) {
47 __extends(NoJqueryRawElementsRuleWalker, _super);
48 function NoJqueryRawElementsRuleWalker() {
49 return _super !== null && _super.apply(this, arguments) || this;
50 }
51 NoJqueryRawElementsRuleWalker.prototype.visitCallExpression = function (node) {
52 var functionName = AstUtils_1.AstUtils.getFunctionName(node);
53 if (AstUtils_1.AstUtils.isJQuery(functionName) && node.arguments.length > 0) {
54 var firstArg = node.arguments[0];
55 if (firstArg.kind === ts.SyntaxKind.StringLiteral) {
56 if (this.isComplexHtmlElement(firstArg)) {
57 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING_COMPLEX + node.getText());
58 }
59 }
60 else {
61 var finder = new HtmlLikeStringLiteralFinder(this.getSourceFile(), this.getOptions());
62 finder.walk(node.arguments[0]);
63 if (finder.isFound()) {
64 this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING_MANIPULATION + node.getText());
65 }
66 }
67 }
68 _super.prototype.visitCallExpression.call(this, node);
69 };
70 NoJqueryRawElementsRuleWalker.prototype.isComplexHtmlElement = function (literal) {
71 var text = literal.text.trim();
72 if (/^<.*>$/.test(text) === false) {
73 return false;
74 }
75 if (/^<[A-Za-z]+\s*\/?>$/.test(text) === true) {
76 return false;
77 }
78 if (/^<[A-Za-z]+\s*>\s*<\/[A-Za-z]+\s*>$/m.test(text) === true) {
79 return false;
80 }
81 var match = text.match(/^<[A-Za-z]+\s*>(.*)<\/[A-Za-z]+\s*>$/m);
82 if (match !== null && match[1] !== undefined) {
83 var enclosedContent = match[1];
84 if (enclosedContent.indexOf('<') === -1 && enclosedContent.indexOf('>') === -1) {
85 return false;
86 }
87 }
88 return true;
89 };
90 return NoJqueryRawElementsRuleWalker;
91}(Lint.RuleWalker));
92var HtmlLikeStringLiteralFinder = (function (_super) {
93 __extends(HtmlLikeStringLiteralFinder, _super);
94 function HtmlLikeStringLiteralFinder() {
95 var _this = _super !== null && _super.apply(this, arguments) || this;
96 _this.found = false;
97 return _this;
98 }
99 HtmlLikeStringLiteralFinder.prototype.isFound = function () {
100 return this.found;
101 };
102 HtmlLikeStringLiteralFinder.prototype.visitStringLiteral = function (node) {
103 if (node.text.indexOf('<') > -1 || node.text.indexOf('>') > -1) {
104 this.found = true;
105 }
106 else {
107 _super.prototype.visitStringLiteral.call(this, node);
108 }
109 };
110 return HtmlLikeStringLiteralFinder;
111}(Lint.RuleWalker));
112//# sourceMappingURL=noJqueryRawElementsRule.js.map
\No newline at end of file