UNPKG

5.53 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 tsutils_1 = require("tsutils");
19var UNSPECIFIED_BROWSER_VERSION = 'unspecified version';
20var JSDOC_BROWSERSPECIFIC = '@browserspecific';
21var COMMENT_BROWSERSPECIFIC = 'Browser Specific:';
22var FAILURE_BROWSER_STRING = 'Unsupported browser';
23var FAILURE_VERSION_STRING = 'Unsupported browser version';
24var Rule = (function (_super) {
25 __extends(Rule, _super);
26 function Rule() {
27 return _super !== null && _super.apply(this, arguments) || this;
28 }
29 Rule.prototype.apply = function (sourceFile) {
30 return this.applyWithFunction(sourceFile, walk, this.parseSupportedBrowsers(this.getOptions()));
31 };
32 Rule.prototype.parseSupportedBrowsers = function (options) {
33 var result = {};
34 var ruleArguments = options.ruleArguments;
35 (ruleArguments || []).forEach(function (option) {
36 if (option instanceof Array) {
37 option.forEach(function (browserString) {
38 var browser = parseBrowserString(browserString);
39 if (browser !== undefined) {
40 result[browser.name.toLowerCase()] = browser;
41 }
42 });
43 }
44 });
45 return {
46 supportedBrowsers: result
47 };
48 };
49 Rule.metadata = {
50 ruleName: 'no-unsupported-browser-code',
51 type: 'maintainability',
52 description: 'Avoid writing browser-specific code for unsupported browser versions',
53 options: null,
54 optionsDescription: '',
55 typescriptOnly: true,
56 issueClass: 'Non-SDL',
57 issueType: 'Warning',
58 severity: 'Low',
59 level: 'Opportunity for Excellence',
60 group: 'Clarity'
61 };
62 return Rule;
63}(Lint.Rules.AbstractRule));
64exports.Rule = Rule;
65function parseBrowserString(browser) {
66 var regex = /([a-zA-Z ]*)(>=|<=|<|>)?\s*(\d*)/i;
67 var match = browser.match(regex);
68 if (match === null) {
69 return undefined;
70 }
71 return {
72 name: match[1].trim(),
73 comparison: match[2] || '=',
74 version: parseInt(match[3], 10) || UNSPECIFIED_BROWSER_VERSION
75 };
76}
77function walk(ctx) {
78 var supportedBrowsers = ctx.options.supportedBrowsers;
79 function findUnsupportedBrowserFailures(targetBrowser, startPos, length) {
80 if (!isSupportedBrowser(targetBrowser)) {
81 ctx.addFailureAt(startPos, length, FAILURE_BROWSER_STRING + ": " + targetBrowser.name);
82 }
83 else if (!isSupportedBrowserVersion(targetBrowser)) {
84 ctx.addFailureAt(startPos, length, FAILURE_VERSION_STRING + ": " + targetBrowser.name + " " + targetBrowser.version);
85 }
86 }
87 function isSupportedBrowser(targetBrowser) {
88 return targetBrowser.name.toLowerCase() in supportedBrowsers;
89 }
90 function isSupportedBrowserVersion(targetBrowser) {
91 var supportedBrowser = supportedBrowsers[targetBrowser.name.toLowerCase()];
92 if (supportedBrowser.version === UNSPECIFIED_BROWSER_VERSION) {
93 return true;
94 }
95 switch (supportedBrowser.comparison) {
96 case '>':
97 return targetBrowser.version > supportedBrowser.version;
98 case '>=':
99 return targetBrowser.version >= supportedBrowser.version;
100 case '<':
101 return targetBrowser.version < supportedBrowser.version;
102 case '<=':
103 return targetBrowser.version <= supportedBrowser.version;
104 case '=':
105 return targetBrowser.version === supportedBrowser.version;
106 default:
107 return false;
108 }
109 }
110 function cb(node) {
111 tsutils_1.forEachTokenWithTrivia(node, function (text, tokenSyntaxKind, range) {
112 var regex;
113 if (tokenSyntaxKind === ts.SyntaxKind.MultiLineCommentTrivia) {
114 regex = new RegExp(JSDOC_BROWSERSPECIFIC + "\\s*(.*)", 'gi');
115 }
116 else if (tokenSyntaxKind === ts.SyntaxKind.SingleLineCommentTrivia) {
117 regex = new RegExp(COMMENT_BROWSERSPECIFIC + "\\s*(.*)", 'gi');
118 }
119 else {
120 return;
121 }
122 var match;
123 var tokenText = text.substring(range.pos, range.end);
124 while ((match = regex.exec(tokenText))) {
125 var browser = parseBrowserString(match[1]);
126 if (browser === undefined) {
127 break;
128 }
129 findUnsupportedBrowserFailures(browser, range.pos, range.end - range.pos);
130 }
131 });
132 }
133 return ts.forEachChild(ctx.sourceFile, cb);
134}
135//# sourceMappingURL=noUnsupportedBrowserCodeRule.js.map
\No newline at end of file