• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nightlycommit / rollup-plugin-typescript / 30

27 Dec 2023 09:56AM CUT coverage: 100.0%. Remained the same
30

push

gitlab-ci

Eric MORAND
Merge branch 'bootstrapping' into 'main'

Use the plugin to build itself

See merge request nightlycommit/rollup-plugin-typescript!2

53 of 53 branches covered (100.0%)

Branch coverage included in aggregate %.

96 of 96 relevant lines covered (100.0%)

479.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/lib/compiler.ts
1
import ts from "typescript";
2
import {Volume} from "memfs";
3
import {dirname} from "path";
4
import {isAMapOutputFile, isACodeOutputFile} from "./helpers";
5
import {System} from "typescript";
6

7
type Error = {
8
    column: number;
9
    file: string;
10
    line: number;
11
    message: string;
12
} | string;
13

14
export type CompilationResult = Array<Error>;
15

16
export interface Compiler {
17
    compile(fileName: string): CompilationResult;
18

19
    getOutputFileNames(fileName: string): {
20
        code: string;
21
        map: string;
22
    };
23
}
24

25
const createErrorFromDiagnostic = (diagnostic: ts.Diagnostic): Error => {
1✔
26
    const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, ts.sys.newLine);
8✔
27

28
    if (diagnostic.file) {
8✔
29
        const {line, character} = ts.getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start!);
6✔
30

31
        return {
6✔
32
            column: character,
33
            file: diagnostic.file.fileName,
34
            message,
35
            line: line + 1
36
        };
37
    }
38
    else {
39
        return message;
2✔
40
    }
41
};
42

43
export const createCompiler = (
1✔
44
    options: ts.CompilerOptions,
45
    fileSystem: InstanceType<typeof Volume>
46
): Compiler => {
47
    const tsSystem = ts.sys;
46✔
48

49
    const system: System = {
46✔
50
        ...tsSystem,
51
        fileExists: (path) => {
52
            return fileSystem.existsSync(path) || tsSystem.fileExists(path);
15,651✔
53
        },
54
        readFile: (path, encoding?) => {
55
            if (fileSystem.existsSync(path)) {
5,121✔
56
                return fileSystem.readFileSync(path, encoding).toString();
7✔
57
            }
58

59
            return tsSystem.readFile(path, encoding);
5,114✔
60
        },
61
        writeFile: (path, data) => {
62
            const parentPath = dirname(path);
296✔
63

64
            if (!fileSystem.existsSync(parentPath)) {
296✔
65
                fileSystem.mkdirSync(parentPath, {
40✔
66
                    recursive: true
67
                });
68
            }
69

70
            return fileSystem.writeFileSync(path, data);
296✔
71
        }
72
    };
73

74
    const host = ts.createIncrementalCompilerHost(
46✔
75
        options,
76
        system
77
    );
78

79
    const createProgram = (
46✔
80
        fileName: string,
81
        options: ts.CompilerOptions,
82
        host: ts.CompilerHost
83
    ) => {
84
        return ts.createIncrementalProgram({
43✔
85
            rootNames: [
86
                fileName
87
            ],
88
            options,
89
            host
90
        });
91
    };
92

93
    return {
46✔
94
        compile: (fileName) => {
95
            const program = createProgram(fileName, options, host);
43✔
96
            
97
            // options diagnostics
98
            const optionsDiagnostics = program.getOptionsDiagnostics();
43✔
99

100
            if (optionsDiagnostics.length > 0) {
43✔
101
                return optionsDiagnostics.map(createErrorFromDiagnostic);
2✔
102
            }
103
            
104
            const errors: Array<Error> = [];
41✔
105
            
106
            let done: boolean = false;
41✔
107

108
            while (!done) {
41✔
109
                const emitResult = program.emitNextAffectedFile();
2,816✔
110

111
                const diagnostics = emitResult?.result.diagnostics;
2,816✔
112

113
                if (diagnostics) {
2,816✔
114
                    errors.push(...diagnostics.map(createErrorFromDiagnostic));
2,775✔
115
                }
116

117
                done = (emitResult === undefined);
2,816✔
118
            }
119
            
120
            return errors;
41✔
121
        },
122
        getOutputFileNames: (fileName) => {
123
            const outputFileNames = ts.getOutputFileNames({
146✔
124
                options,
125
                fileNames: [
126
                    fileName
127
                ],
128
                errors: []
129
            }, fileName, !ts.sys.useCaseSensitiveFileNames);
130

131
            return {
146✔
132
                code: outputFileNames.find(isACodeOutputFile)!,
133
                map: outputFileNames.find(isAMapOutputFile)!
134
            };
135
        }
136
    }
137
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc