1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.djb2 = exports.getOutputFileNamesOfProjectReference = exports.iterateProjectReferences = exports.invertChangeRange = exports.hasParseErrors = exports.addUnique = exports.flatMap = exports.mapDefined = exports.hasSupportedExtension = exports.calculateChangeRange = exports.assertNever = exports.format = exports.unixifyPath = exports.resolveCachedResult = exports.arrayify = exports.emptyArray = exports.OFFSET_TO_NODE_MODULES = void 0;
|
4 | const json5 = require("json5");
|
5 | const yaml = require("js-yaml");
|
6 | const path = require("path");
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | exports.OFFSET_TO_NODE_MODULES = 3;
|
15 | exports.emptyArray = [];
|
16 | function arrayify(maybeArr) {
|
17 | return Array.isArray(maybeArr)
|
18 | ? maybeArr
|
19 | : maybeArr === undefined
|
20 | ? exports.emptyArray
|
21 | : [maybeArr];
|
22 | }
|
23 | exports.arrayify = arrayify;
|
24 | function resolveCachedResult(cache, key, cb) {
|
25 | let result = cache.get(key);
|
26 | if (result === undefined && !cache.has(key)) {
|
27 | result = cb(key);
|
28 | cache.set(key, result);
|
29 | }
|
30 | return result;
|
31 | }
|
32 | exports.resolveCachedResult = resolveCachedResult;
|
33 | function unixifyPath(p) {
|
34 | return p.replace(/\\/g, '/');
|
35 | }
|
36 | exports.unixifyPath = unixifyPath;
|
37 | function format(value, fmt = "yaml" /* Yaml */) {
|
38 | value = convertToPrintable(value) || {};
|
39 | switch (fmt) {
|
40 | case "json" :
|
41 | return JSON.stringify(value, undefined, 2);
|
42 | case "json5" :
|
43 | return json5.stringify(value, undefined, 2);
|
44 | case "yaml" :
|
45 | return yaml.dump(value, {
|
46 | indent: 2,
|
47 | schema: yaml.JSON_SCHEMA,
|
48 | sortKeys: true,
|
49 | });
|
50 | default:
|
51 | return assertNever(fmt);
|
52 | }
|
53 | }
|
54 | exports.format = format;
|
55 | function convertToPrintable(value) {
|
56 | if (value == undefined || typeof value !== 'object')
|
57 | return value;
|
58 | if (value instanceof Map) {
|
59 | const obj = {};
|
60 | for (const [k, v] of value)
|
61 | if (v !== undefined)
|
62 | obj[k] = v;
|
63 | value = obj;
|
64 | }
|
65 | if (Array.isArray(value)) {
|
66 | const result = mapDefined(value, convertToPrintable);
|
67 | return result.length === 0 ? undefined : result;
|
68 | }
|
69 | const keys = Object.keys(value);
|
70 | if (keys.length === 0)
|
71 | return;
|
72 | let added = false;
|
73 | const newValue = {};
|
74 | for (const key of keys) {
|
75 | const converted = convertToPrintable(value[key]);
|
76 | if (converted !== undefined) {
|
77 | newValue[key] = converted;
|
78 | added = true;
|
79 | }
|
80 | }
|
81 | return added ? newValue : undefined;
|
82 | }
|
83 | function assertNever(v) {
|
84 | throw new Error(`unexpected value '${v}'`);
|
85 | }
|
86 | exports.assertNever = assertNever;
|
87 | function calculateChangeRange(original, changed) {
|
88 | const diff = changed.length - original.length;
|
89 | let start = 0;
|
90 | let end = original.length;
|
91 | const minEnd = diff >= 0 ? end : changed.length;
|
92 | for (; start < minEnd && original[start] === changed[start]; ++start)
|
93 | ;
|
94 | if (start !== minEnd) {
|
95 | const maxStart = end - minEnd + start;
|
96 | for (let changedEnd = changed.length; maxStart < end && original[end - 1] === changed[changedEnd - 1]; --end, --changedEnd)
|
97 | ;
|
98 | }
|
99 | const length = end - start;
|
100 | return {
|
101 | span: {
|
102 | start,
|
103 | length,
|
104 | },
|
105 | newLength: length + diff,
|
106 | };
|
107 | }
|
108 | exports.calculateChangeRange = calculateChangeRange;
|
109 | function hasSupportedExtension(fileName, extensions) {
|
110 | const ext = path.extname(fileName);
|
111 | return /^\.[jt]sx?$/.test(ext) || extensions !== undefined && extensions.includes(ext);
|
112 | }
|
113 | exports.hasSupportedExtension = hasSupportedExtension;
|
114 | function mapDefined(input, cb) {
|
115 | const result = [];
|
116 | for (const item of input) {
|
117 | const current = cb(item);
|
118 | if (current !== undefined)
|
119 | result.push(current);
|
120 | }
|
121 | return result;
|
122 | }
|
123 | exports.mapDefined = mapDefined;
|
124 | function flatMap(input, cb) {
|
125 | const result = [];
|
126 | for (const item of input)
|
127 | result.push(...cb(item));
|
128 | return result;
|
129 | }
|
130 | exports.flatMap = flatMap;
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | function addUnique(arr, item) {
|
136 | if (arr.includes(item))
|
137 | return false;
|
138 | arr.push(item);
|
139 | return true;
|
140 | }
|
141 | exports.addUnique = addUnique;
|
142 | function hasParseErrors(sourceFile) {
|
143 | return sourceFile.parseDiagnostics.length !== 0;
|
144 | }
|
145 | exports.hasParseErrors = hasParseErrors;
|
146 | function invertChangeRange(range) {
|
147 | return {
|
148 | span: {
|
149 | start: range.span.start,
|
150 | length: range.newLength,
|
151 | },
|
152 | newLength: range.span.length,
|
153 | };
|
154 | }
|
155 | exports.invertChangeRange = invertChangeRange;
|
156 | function iterateProjectReferences(references) {
|
157 | return iterateProjectReferencesWorker(references, []);
|
158 | }
|
159 | exports.iterateProjectReferences = iterateProjectReferences;
|
160 | function* iterateProjectReferencesWorker(references, seen) {
|
161 | if (references === undefined)
|
162 | return;
|
163 | for (const ref of references) {
|
164 | if (ref === undefined || !addUnique(seen, ref.sourceFile.fileName))
|
165 | continue;
|
166 | yield ref;
|
167 | yield* iterateProjectReferencesWorker(ref.references, seen);
|
168 | }
|
169 | }
|
170 | function getOutputFileNamesOfProjectReference(ref) {
|
171 | const options = ref.commandLine.options;
|
172 | const outFile = options.outFile || options.out;
|
173 | if (outFile)
|
174 | return [getOutFileDeclarationName(outFile)];
|
175 | const projectDirectory = path.dirname(ref.sourceFile.fileName);
|
176 | return mapDefined(ref.commandLine.fileNames, (fileName) => getDeclarationOutputName(fileName, options, projectDirectory));
|
177 | }
|
178 | exports.getOutputFileNamesOfProjectReference = getOutputFileNamesOfProjectReference;
|
179 |
|
180 | function getDeclarationOutputName(fileName, options, projectDirectory) {
|
181 | const extension = path.extname(fileName);
|
182 | switch (extension) {
|
183 | case '.tsx':
|
184 | case '.js':
|
185 | case '.jsx':
|
186 | break;
|
187 | case '.ts':
|
188 | if (path.extname(fileName.slice(0, -extension.length)) !== '.d')
|
189 | break;
|
190 |
|
191 | default:
|
192 | return;
|
193 | }
|
194 | fileName = fileName.slice(0, -extension.length) + '.d.ts';
|
195 | return unixifyPath(path.resolve(options.declarationDir || options.outDir || projectDirectory, path.relative(options.rootDir || projectDirectory, fileName)));
|
196 | }
|
197 | function getOutFileDeclarationName(outFile) {
|
198 |
|
199 | return outFile.slice(0, -path.extname(outFile).length) + '.d.ts';
|
200 | }
|
201 | function djb2(str) {
|
202 | let hash = 5381;
|
203 | for (let i = 0; i < str.length; ++i)
|
204 | hash = ((hash << 5) + hash) + str.charCodeAt(i);
|
205 | return hash;
|
206 | }
|
207 | exports.djb2 = djb2;
|
208 |
|
\ | No newline at end of file |