1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getPatternFromSpec = exports.createTsInternals = void 0;
|
4 | const path_1 = require("path");
|
5 | const util_1 = require("./util");
|
6 |
|
7 | exports.createTsInternals = (0, util_1.cachedLookup)(createTsInternalsUncached);
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function createTsInternalsUncached(_ts) {
|
18 | const ts = _ts;
|
19 | |
20 |
|
21 |
|
22 |
|
23 | function getExtendsConfigPath(extendedConfig, host, basePath, errors, createDiagnostic) {
|
24 | extendedConfig = (0, util_1.normalizeSlashes)(extendedConfig);
|
25 | if (isRootedDiskPath(extendedConfig) ||
|
26 | startsWith(extendedConfig, './') ||
|
27 | startsWith(extendedConfig, '../')) {
|
28 | let extendedConfigPath = getNormalizedAbsolutePath(extendedConfig, basePath);
|
29 | if (!host.fileExists(extendedConfigPath) &&
|
30 | !endsWith(extendedConfigPath, ts.Extension.Json)) {
|
31 | extendedConfigPath = `${extendedConfigPath}.json`;
|
32 | if (!host.fileExists(extendedConfigPath)) {
|
33 | errors.push(createDiagnostic(ts.Diagnostics.File_0_not_found, extendedConfig));
|
34 | return undefined;
|
35 | }
|
36 | }
|
37 | return extendedConfigPath;
|
38 | }
|
39 |
|
40 | const resolved = ts.nodeModuleNameResolver(extendedConfig, combinePaths(basePath, 'tsconfig.json'), { moduleResolution: ts.ModuleResolutionKind.NodeJs }, host,
|
41 | undefined,
|
42 | undefined,
|
43 | true);
|
44 | if (resolved.resolvedModule) {
|
45 | return resolved.resolvedModule.resolvedFileName;
|
46 | }
|
47 | errors.push(createDiagnostic(ts.Diagnostics.File_0_not_found, extendedConfig));
|
48 | return undefined;
|
49 | }
|
50 | return { getExtendsConfigPath };
|
51 | }
|
52 |
|
53 | function isRootedDiskPath(path) {
|
54 | return (0, path_1.isAbsolute)(path);
|
55 | }
|
56 | function combinePaths(path, ...paths) {
|
57 | return (0, util_1.normalizeSlashes)((0, path_1.resolve)(path, ...paths.filter((path) => path)));
|
58 | }
|
59 | function getNormalizedAbsolutePath(fileName, currentDirectory) {
|
60 | return (0, util_1.normalizeSlashes)(currentDirectory != null
|
61 | ? (0, path_1.resolve)(currentDirectory, fileName)
|
62 | : (0, path_1.resolve)(fileName));
|
63 | }
|
64 | function startsWith(str, prefix) {
|
65 | return str.lastIndexOf(prefix, 0) === 0;
|
66 | }
|
67 | function endsWith(str, suffix) {
|
68 | const expectedPos = str.length - suffix.length;
|
69 | return expectedPos >= 0 && str.indexOf(suffix, expectedPos) === expectedPos;
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 | const reservedCharacterPattern = /[^\w\s\/]/g;
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function getPatternFromSpec(spec, basePath) {
|
80 | const pattern = spec && getSubPatternFromSpec(spec, basePath, excludeMatcher);
|
81 | return pattern && `^(${pattern})${'($|/)'}`;
|
82 | }
|
83 | exports.getPatternFromSpec = getPatternFromSpec;
|
84 | function getSubPatternFromSpec(spec, basePath, { singleAsteriskRegexFragment, doubleAsteriskRegexFragment, replaceWildcardCharacter, }) {
|
85 | let subpattern = '';
|
86 | let hasWrittenComponent = false;
|
87 | const components = getNormalizedPathComponents(spec, basePath);
|
88 | const lastComponent = last(components);
|
89 |
|
90 |
|
91 | components[0] = removeTrailingDirectorySeparator(components[0]);
|
92 | if (isImplicitGlob(lastComponent)) {
|
93 | components.push('**', '*');
|
94 | }
|
95 | let optionalCount = 0;
|
96 | for (let component of components) {
|
97 | if (component === '**') {
|
98 | subpattern += doubleAsteriskRegexFragment;
|
99 | }
|
100 | else {
|
101 | if (hasWrittenComponent) {
|
102 | subpattern += directorySeparator;
|
103 | }
|
104 | subpattern += component.replace(reservedCharacterPattern, replaceWildcardCharacter);
|
105 | }
|
106 | hasWrittenComponent = true;
|
107 | }
|
108 | while (optionalCount > 0) {
|
109 | subpattern += ')?';
|
110 | optionalCount--;
|
111 | }
|
112 | return subpattern;
|
113 | }
|
114 | const directoriesMatcher = {
|
115 | singleAsteriskRegexFragment: '[^/]*',
|
116 | |
117 |
|
118 |
|
119 |
|
120 | doubleAsteriskRegexFragment: `(/[^/.][^/]*)*?`,
|
121 | replaceWildcardCharacter: (match) => replaceWildcardCharacter(match, directoriesMatcher.singleAsteriskRegexFragment),
|
122 | };
|
123 | const excludeMatcher = {
|
124 | singleAsteriskRegexFragment: '[^/]*',
|
125 | doubleAsteriskRegexFragment: '(/.+?)?',
|
126 | replaceWildcardCharacter: (match) => replaceWildcardCharacter(match, excludeMatcher.singleAsteriskRegexFragment),
|
127 | };
|
128 | function getNormalizedPathComponents(path, currentDirectory) {
|
129 | return reducePathComponents(getPathComponents(path, currentDirectory));
|
130 | }
|
131 | function getPathComponents(path, currentDirectory = '') {
|
132 | path = combinePaths(currentDirectory, path);
|
133 | return pathComponents(path, getRootLength(path));
|
134 | }
|
135 | function reducePathComponents(components) {
|
136 | if (!some(components))
|
137 | return [];
|
138 | const reduced = [components[0]];
|
139 | for (let i = 1; i < components.length; i++) {
|
140 | const component = components[i];
|
141 | if (!component)
|
142 | continue;
|
143 | if (component === '.')
|
144 | continue;
|
145 | if (component === '..') {
|
146 | if (reduced.length > 1) {
|
147 | if (reduced[reduced.length - 1] !== '..') {
|
148 | reduced.pop();
|
149 | continue;
|
150 | }
|
151 | }
|
152 | else if (reduced[0])
|
153 | continue;
|
154 | }
|
155 | reduced.push(component);
|
156 | }
|
157 | return reduced;
|
158 | }
|
159 | function getRootLength(path) {
|
160 | const rootLength = getEncodedRootLength(path);
|
161 | return rootLength < 0 ? ~rootLength : rootLength;
|
162 | }
|
163 | function getEncodedRootLength(path) {
|
164 | if (!path)
|
165 | return 0;
|
166 | const ch0 = path.charCodeAt(0);
|
167 |
|
168 | if (ch0 === 47 || ch0 === 92 ) {
|
169 | if (path.charCodeAt(1) !== ch0)
|
170 | return 1;
|
171 | const p1 = path.indexOf(ch0 === 47 ? directorySeparator : altDirectorySeparator, 2);
|
172 | if (p1 < 0)
|
173 | return path.length;
|
174 | return p1 + 1;
|
175 | }
|
176 |
|
177 | if (isVolumeCharacter(ch0) && path.charCodeAt(1) === 58 ) {
|
178 | const ch2 = path.charCodeAt(2);
|
179 | if (ch2 === 47 || ch2 === 92 )
|
180 | return 3;
|
181 | if (path.length === 2)
|
182 | return 2;
|
183 | }
|
184 |
|
185 | const schemeEnd = path.indexOf(urlSchemeSeparator);
|
186 | if (schemeEnd !== -1) {
|
187 | const authorityStart = schemeEnd + urlSchemeSeparator.length;
|
188 | const authorityEnd = path.indexOf(directorySeparator, authorityStart);
|
189 | if (authorityEnd !== -1) {
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | const scheme = path.slice(0, schemeEnd);
|
195 | const authority = path.slice(authorityStart, authorityEnd);
|
196 | if (scheme === 'file' &&
|
197 | (authority === '' || authority === 'localhost') &&
|
198 | isVolumeCharacter(path.charCodeAt(authorityEnd + 1))) {
|
199 | const volumeSeparatorEnd = getFileUrlVolumeSeparatorEnd(path, authorityEnd + 2);
|
200 | if (volumeSeparatorEnd !== -1) {
|
201 | if (path.charCodeAt(volumeSeparatorEnd) === 47 ) {
|
202 |
|
203 | return ~(volumeSeparatorEnd + 1);
|
204 | }
|
205 | if (volumeSeparatorEnd === path.length) {
|
206 |
|
207 |
|
208 | return ~volumeSeparatorEnd;
|
209 | }
|
210 | }
|
211 | }
|
212 | return ~(authorityEnd + 1);
|
213 | }
|
214 | return ~path.length;
|
215 | }
|
216 |
|
217 | return 0;
|
218 | }
|
219 | function ensureTrailingDirectorySeparator(path) {
|
220 | if (!hasTrailingDirectorySeparator(path)) {
|
221 | return path + directorySeparator;
|
222 | }
|
223 | return path;
|
224 | }
|
225 | function hasTrailingDirectorySeparator(path) {
|
226 | return (path.length > 0 && isAnyDirectorySeparator(path.charCodeAt(path.length - 1)));
|
227 | }
|
228 | function isAnyDirectorySeparator(charCode) {
|
229 | return (charCode === 47 || charCode === 92 );
|
230 | }
|
231 | function removeTrailingDirectorySeparator(path) {
|
232 | if (hasTrailingDirectorySeparator(path)) {
|
233 | return path.substr(0, path.length - 1);
|
234 | }
|
235 | return path;
|
236 | }
|
237 | const directorySeparator = '/';
|
238 | const altDirectorySeparator = '\\';
|
239 | const urlSchemeSeparator = '://';
|
240 | function isVolumeCharacter(charCode) {
|
241 | return ((charCode >= 97 && charCode <= 122 ) ||
|
242 | (charCode >= 65 && charCode <= 90 ));
|
243 | }
|
244 | function getFileUrlVolumeSeparatorEnd(url, start) {
|
245 | const ch0 = url.charCodeAt(start);
|
246 | if (ch0 === 58 )
|
247 | return start + 1;
|
248 | if (ch0 === 37 &&
|
249 | url.charCodeAt(start + 1) === 51 ) {
|
250 | const ch2 = url.charCodeAt(start + 2);
|
251 | if (ch2 === 97 || ch2 === 65 )
|
252 | return start + 3;
|
253 | }
|
254 | return -1;
|
255 | }
|
256 | function some(array, predicate) {
|
257 | if (array) {
|
258 | if (predicate) {
|
259 | for (const v of array) {
|
260 | if (predicate(v)) {
|
261 | return true;
|
262 | }
|
263 | }
|
264 | }
|
265 | else {
|
266 | return array.length > 0;
|
267 | }
|
268 | }
|
269 | return false;
|
270 | }
|
271 | function pathComponents(path, rootLength) {
|
272 | const root = path.substring(0, rootLength);
|
273 | const rest = path.substring(rootLength).split(directorySeparator);
|
274 | if (rest.length && !lastOrUndefined(rest))
|
275 | rest.pop();
|
276 | return [root, ...rest];
|
277 | }
|
278 | function lastOrUndefined(array) {
|
279 | return array.length === 0 ? undefined : array[array.length - 1];
|
280 | }
|
281 | function last(array) {
|
282 |
|
283 | return array[array.length - 1];
|
284 | }
|
285 | function replaceWildcardCharacter(match, singleAsteriskRegexFragment) {
|
286 | return match === '*'
|
287 | ? singleAsteriskRegexFragment
|
288 | : match === '?'
|
289 | ? '[^/]'
|
290 | : '\\' + match;
|
291 | }
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | function isImplicitGlob(lastPathComponent) {
|
297 | return !/[.*?]/.test(lastPathComponent);
|
298 | }
|
299 |
|
\ | No newline at end of file |