1 |
|
2 |
|
3 |
|
4 | var StringChecks = (function () {
|
5 | function StringChecks() {
|
6 | }
|
7 | |
8 |
|
9 |
|
10 |
|
11 |
|
12 | StringChecks.explainIfInvalidTSDocTagName = function (tagName) {
|
13 | if (tagName[0] !== '@') {
|
14 | return 'A TSDoc tag name must start with an "@" symbol';
|
15 | }
|
16 | if (!StringChecks._tsdocTagNameRegExp.test(tagName)) {
|
17 | return 'A TSDoc tag name must start with a letter and contain only letters and numbers';
|
18 | }
|
19 | return undefined;
|
20 | };
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 | StringChecks.validateTSDocTagName = function (tagName) {
|
27 | var explanation = StringChecks.explainIfInvalidTSDocTagName(tagName);
|
28 | if (explanation) {
|
29 | throw new Error(explanation);
|
30 | }
|
31 | };
|
32 | |
33 |
|
34 |
|
35 |
|
36 | StringChecks.explainIfInvalidLinkUrl = function (url) {
|
37 | if (url.length === 0) {
|
38 | return 'The URL cannot be empty';
|
39 | }
|
40 | if (!StringChecks._urlSchemeRegExp.test(url)) {
|
41 | return ('An @link URL must begin with a scheme comprised only of letters and numbers followed by "://".' +
|
42 | ' (For general URLs, use an HTML "<a>" tag instead.)');
|
43 | }
|
44 | if (!StringChecks._urlSchemeAfterRegExp.test(url)) {
|
45 | return 'An @link URL must have at least one character after "://"';
|
46 | }
|
47 | return undefined;
|
48 | };
|
49 | |
50 |
|
51 |
|
52 | StringChecks.explainIfInvalidHtmlName = function (htmlName) {
|
53 | if (!StringChecks._htmlNameRegExp.test(htmlName)) {
|
54 | return 'An HTML name must be an ASCII letter followed by zero or more letters, digits, or hyphens';
|
55 | }
|
56 | return undefined;
|
57 | };
|
58 | |
59 |
|
60 |
|
61 | StringChecks.validateHtmlName = function (htmlName) {
|
62 | var explanation = StringChecks.explainIfInvalidHtmlName(htmlName);
|
63 | if (explanation) {
|
64 | throw new Error(explanation);
|
65 | }
|
66 | };
|
67 | |
68 |
|
69 |
|
70 | StringChecks.explainIfInvalidPackageName = function (packageName) {
|
71 | if (packageName.length === 0) {
|
72 | return 'The package name cannot be an empty string';
|
73 | }
|
74 | if (!StringChecks._validPackageNameRegExp.test(packageName)) {
|
75 | return "The package name " + JSON.stringify(packageName) + " is not a valid package name";
|
76 | }
|
77 | return undefined;
|
78 | };
|
79 | |
80 |
|
81 |
|
82 | StringChecks.explainIfInvalidImportPath = function (importPath, prefixedByPackageName) {
|
83 | if (importPath.length > 0) {
|
84 | if (importPath.indexOf('//') >= 0) {
|
85 | return 'An import path must not contain "//"';
|
86 | }
|
87 | if (importPath[importPath.length - 1] === '/') {
|
88 | return 'An import path must not end with "/"';
|
89 | }
|
90 | if (!prefixedByPackageName) {
|
91 | if (importPath[0] === '/') {
|
92 | return 'An import path must not start with "/" unless prefixed by a package name';
|
93 | }
|
94 | }
|
95 | }
|
96 | return undefined;
|
97 | };
|
98 | |
99 |
|
100 |
|
101 | StringChecks.isSystemSelector = function (selector) {
|
102 | return StringChecks._systemSelectors.has(selector);
|
103 | };
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 | StringChecks.explainIfInvalidUnquotedIdentifier = function (identifier) {
|
110 | if (identifier.length === 0) {
|
111 | return 'The identifier cannot be an empty string';
|
112 | }
|
113 | if (StringChecks._identifierBadCharRegExp.test(identifier)) {
|
114 | return 'The identifier cannot non-word characters';
|
115 | }
|
116 | if (StringChecks._identifierNumberStartRegExp.test(identifier)) {
|
117 | return 'The identifier must not start with a number';
|
118 | }
|
119 | return undefined;
|
120 | };
|
121 | |
122 |
|
123 |
|
124 |
|
125 | StringChecks.explainIfInvalidUnquotedMemberIdentifier = function (identifier) {
|
126 | var explanation = StringChecks.explainIfInvalidUnquotedIdentifier(identifier);
|
127 | if (explanation !== undefined) {
|
128 | return explanation;
|
129 | }
|
130 | if (StringChecks.isSystemSelector(identifier)) {
|
131 |
|
132 |
|
133 |
|
134 | return "The identifier \"" + identifier + "\" must be quoted because it is a TSDoc system selector name";
|
135 | }
|
136 | return undefined;
|
137 | };
|
138 | StringChecks._tsdocTagNameRegExp = /^@[a-z][a-z0-9]*$/i;
|
139 | StringChecks._urlSchemeRegExp = /^[a-z][a-z0-9]*\:\/\//i;
|
140 | StringChecks._urlSchemeAfterRegExp = /^[a-z][a-z0-9]*\:\/\/./i;
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | StringChecks._htmlNameRegExp = /^[a-z]+[a-z0-9\-]*$/i;
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | StringChecks._identifierBadCharRegExp = /[^a-z0-9_$]/i;
|
154 |
|
155 | StringChecks._identifierNumberStartRegExp = /^[0-9]/;
|
156 |
|
157 |
|
158 |
|
159 | StringChecks._validPackageNameRegExp = /^(?:@[a-z0-9\-_\.]+\/)?[a-z0-9\-_\.]+$/i;
|
160 | StringChecks._systemSelectors = new Set([
|
161 |
|
162 | 'instance',
|
163 | 'static',
|
164 | 'constructor',
|
165 |
|
166 | 'class',
|
167 | 'enum',
|
168 | 'function',
|
169 | 'interface',
|
170 | 'namespace',
|
171 | 'type',
|
172 | 'variable'
|
173 | ]);
|
174 | return StringChecks;
|
175 | }());
|
176 | export { StringChecks };
|
177 |
|
\ | No newline at end of file |