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