|
| 1 | +import pathUtil from "node:path"; |
| 2 | +import ts from "typescript"; |
| 3 | +import { createAnnotation } from "./ci-commands.js"; |
| 4 | + |
| 5 | +/** |
| 6 | + * @fileoverview |
| 7 | + * tsc command output will be parsed by GitHub Actions as errors. |
| 8 | + * We want them to be just warnings. |
| 9 | + */ |
| 10 | + |
| 11 | +const check = () => { |
| 12 | + const rootDir = pathUtil.join(import.meta.dirname, ".."); |
| 13 | + const tsconfigPath = pathUtil.join(rootDir, "tsconfig.json"); |
| 14 | + const commandLine = ts.getParsedCommandLineOfConfigFile( |
| 15 | + tsconfigPath, |
| 16 | + {}, |
| 17 | + ts.sys |
| 18 | + ); |
| 19 | + |
| 20 | + const program = ts.createProgram({ |
| 21 | + rootNames: commandLine.fileNames, |
| 22 | + options: commandLine.options, |
| 23 | + }); |
| 24 | + const emitted = program.emit(); |
| 25 | + |
| 26 | + const diagnostics = [ |
| 27 | + ...ts.getPreEmitDiagnostics(program), |
| 28 | + ...emitted.diagnostics, |
| 29 | + ]; |
| 30 | + |
| 31 | + let numWarnings = 0; |
| 32 | + |
| 33 | + for (const diagnostic of diagnostics) { |
| 34 | + numWarnings++; |
| 35 | + |
| 36 | + const startPosition = ts.getLineAndCharacterOfPosition( |
| 37 | + diagnostic.file, |
| 38 | + diagnostic.start |
| 39 | + ); |
| 40 | + const endPosition = ts.getLineAndCharacterOfPosition( |
| 41 | + diagnostic.file, |
| 42 | + diagnostic.start |
| 43 | + ); |
| 44 | + const flattened = ts.flattenDiagnosticMessageText( |
| 45 | + diagnostic.messageText, |
| 46 | + " ", |
| 47 | + 0 |
| 48 | + ); |
| 49 | + |
| 50 | + createAnnotation({ |
| 51 | + type: "warning", |
| 52 | + file: diagnostic.file.fileName, |
| 53 | + title: "Type warning - may indicate a bug - ignore if no bug", |
| 54 | + message: flattened, |
| 55 | + line: startPosition.line + 1, |
| 56 | + col: startPosition.character + 1, |
| 57 | + endLine: endPosition.line + 1, |
| 58 | + endCol: endPosition.character + 1, |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + console.log(`${numWarnings} warnings.`); |
| 63 | +}; |
| 64 | + |
| 65 | +check(); |
0 commit comments