保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 26a09f08cf1ed43c640879f23fdad56c5c9282f7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
/**
 * It handles most of the logic required to process embedded TypeScript code (like in Vue components or MDX)
 *
 * @param embeddedExtensions List of file extensions that should be treated as an embedded TypeScript source
 *                           (for example ['.vue'])
 * @param getEmbeddedSource  Function that returns embedded TypeScript source text and extension that this file
 *                           would have if it would be a regular TypeScript file
 */
function createTypeScriptEmbeddedExtension({ embeddedExtensions, getEmbeddedSource, }) {
    const embeddedSourceCache = new Map();
    function getCachedEmbeddedSource(fileName) {
        if (!embeddedSourceCache.has(fileName)) {
            embeddedSourceCache.set(fileName, getEmbeddedSource(fileName));
        }
        return embeddedSourceCache.get(fileName);
    }
    function parsePotentiallyEmbeddedFileName(fileName) {
        const extension = path_1.extname(fileName);
        const embeddedFileName = fileName.slice(0, fileName.length - extension.length);
        const embeddedExtension = path_1.extname(embeddedFileName);
        return {
            extension,
            embeddedFileName,
            embeddedExtension,
        };
    }
    function createEmbeddedFileExists(fileExists) {
        return function embeddedFileExists(fileName) {
            const { embeddedExtension, embeddedFileName, extension } = parsePotentiallyEmbeddedFileName(fileName);
            if (embeddedExtensions.includes(embeddedExtension) && fileExists(embeddedFileName)) {
                const embeddedSource = getCachedEmbeddedSource(embeddedFileName);
                return !!(embeddedSource && embeddedSource.extension === extension);
            }
            return fileExists(fileName);
        };
    }
    function createEmbeddedReadFile(readFile) {
        return function embeddedReadFile(fileName, encoding) {
            const { embeddedExtension, embeddedFileName, extension } = parsePotentiallyEmbeddedFileName(fileName);
            if (embeddedExtensions.includes(embeddedExtension)) {
                const embeddedSource = getCachedEmbeddedSource(embeddedFileName);
                if (embeddedSource && embeddedSource.extension === extension) {
                    return embeddedSource.sourceText;
                }
            }
            return readFile(fileName, encoding);
        };
    }
    return {
        extendIssues(issues) {
            return issues.map((issue) => {
                if (issue.file) {
                    const { embeddedExtension, embeddedFileName } = parsePotentiallyEmbeddedFileName(issue.file);
                    if (embeddedExtensions.includes(embeddedExtension)) {
                        return Object.assign(Object.assign({}, issue), { file: embeddedFileName });
                    }
                }
                return issue;
            });
        },
        extendWatchCompilerHost(host) {
            return Object.assign(Object.assign({}, host), { watchFile(fileName, callback, poolingInterval) {
                    const { embeddedExtension, embeddedFileName } = parsePotentiallyEmbeddedFileName(fileName);
                    if (embeddedExtensions.includes(embeddedExtension)) {
                        return host.watchFile(embeddedFileName, (innerFileName, eventKind) => {
                            embeddedSourceCache.delete(embeddedFileName);
                            return callback(fileName, eventKind);
                        }, poolingInterval);
                    }
                    else {
                        return host.watchFile(fileName, callback, poolingInterval);
                    }
                }, readFile: createEmbeddedReadFile(host.readFile), fileExists: createEmbeddedFileExists(host.fileExists) });
        },
        extendCompilerHost(host) {
            return Object.assign(Object.assign({}, host), { readFile: createEmbeddedReadFile(host.readFile), fileExists: createEmbeddedFileExists(host.fileExists) });
        },
        extendParseConfigFileHost(host) {
            return Object.assign(Object.assign({}, host), { readDirectory(rootDir, extensions, excludes, includes, depth) {
                    return host
                        .readDirectory(rootDir, [...extensions, ...embeddedExtensions], excludes, includes, depth)
                        .map((fileName) => {
                        const isEmbeddedFile = embeddedExtensions.some((embeddedExtension) => fileName.endsWith(embeddedExtension));
                        if (isEmbeddedFile) {
                            const embeddedSource = getCachedEmbeddedSource(fileName);
                            return embeddedSource ? `${fileName}${embeddedSource.extension}` : fileName;
                        }
                        else {
                            return fileName;
                        }
                    });
                } });
        },
        extendDependencies(dependencies) {
            return Object.assign(Object.assign({}, dependencies), { files: dependencies.files.map((fileName) => {
                    const { embeddedExtension, embeddedFileName, extension, } = parsePotentiallyEmbeddedFileName(fileName);
                    if (embeddedExtensions.includes(embeddedExtension)) {
                        const embeddedSource = getCachedEmbeddedSource(embeddedFileName);
                        if (embeddedSource && embeddedSource.extension === extension) {
                            return embeddedFileName;
                        }
                    }
                    return fileName;
                }), extensions: [...dependencies.extensions, ...embeddedExtensions] });
        },
    };
}
exports.createTypeScriptEmbeddedExtension = createTypeScriptEmbeddedExtension;