diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 3da18fd1..ea310f00 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -23,8 +23,8 @@ jobs: - name: ESLint run: yarn lint - - name: Typecheck baseline - run: yarn typecheck:baseline + - name: Typecheck + run: yarn typecheck - name: Page render test run: yarn react-test diff --git a/e2e/helpers/newick.js b/e2e/helpers/newick.js index e264dabb..d643af6d 100644 --- a/e2e/helpers/newick.js +++ b/e2e/helpers/newick.js @@ -76,7 +76,10 @@ function edges(root) { const first = getTaxa(root)[0]; /** @type {Map} */ const out = new Map(); - /** @param {Node} node */ + /** + * @param {Node} node + * @param {boolean} isRoot + */ const walk = (node, isRoot) => { const leaves = leafNames(node).sort(); if (!isRoot) { diff --git a/package.json b/package.json index ff5a4cd2..a328b60e 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,6 @@ "postinstall": "run-s postinstall-deps postinstall-patch postinstall-electron", "docs-serve": "cd docs && bundle exec jekyll serve --config _config.yml,_config_dev.yml", "typecheck": "tsc --noEmit", - "typecheck:baseline": "node scripts/typecheck-baseline.js", "lint": "eslint \"src/**/*.{js,jsx,ts,tsx}\" electron.vite.config.js vitest.config.mjs --max-warnings 0", "lint:fix": "eslint \"src/**/*.{js,jsx,ts,tsx}\" electron.vite.config.js vitest.config.mjs --fix" }, diff --git a/scripts/typecheck-baseline.js b/scripts/typecheck-baseline.js deleted file mode 100644 index 11013c58..00000000 --- a/scripts/typecheck-baseline.js +++ /dev/null @@ -1,155 +0,0 @@ -const { spawnSync } = require('child_process'); -const fs = require('fs'); -const path = require('path'); - -const root = path.join(__dirname, '..'); -const baselinePath = path.join(__dirname, 'typecheck-baseline.txt'); - -function diagnosticLines(text) { - return text - .split('\n') - .map((line) => line.trimEnd()) - .filter((line) => line.trim() !== ''); -} - -function fingerprint(line) { - return line.replace(/\(\d+,\d+\): /, ': '); -} - -/** Pathless tsc continuations belong to the preceding file header. */ -function ownedFingerprints(lines) { - let owner = ''; - return lines.map((line) => { - const match = /^(\S+?)\(\d+,\d+\): /.exec(line); - if (match) { - owner = match[1]; - return fingerprint(line); - } - return owner - ? `${owner}|${line}` - : line; - }); -} - -function freshLines(actual, known) { - const seen = new Set(ownedFingerprints(known)); - const actualOwned = ownedFingerprints(actual); - return actual.filter( (line, i) => !seen.has(actualOwned[i]) ); -} - -function unusedLines(known, actual) { - const seen = new Set(ownedFingerprints(actual)); - const knownOwned = ownedFingerprints(known); - return known.filter( (line, i) => !seen.has(knownOwned[i]) ); -} - -/** - * Repoint baseline entries from one file path to another after a pure move. - * An entry is rewritten only when tsc reports the same code and message at the - * new path, so the rename can never shelter a new diagnostic. - */ -function renameLines(known, actual, from, to) { - const unclaimed = actual.filter( (line) => line.startsWith(`${to}(`) ); - const missing = []; - const renamed = known.map((line) => { - if (!line.startsWith(`${from}(`)) { - return line; - } - const want = fingerprint(`${to}${line.slice(from.length)}`); - const i = unclaimed.findIndex( (candidate) => fingerprint(candidate) === want ); - if (i === -1) { - missing.push(line); - return line; - } - return unclaimed.splice(i, 1)[0]; - }); - return { renamed, missing }; -} - -/** Keep tsc diagnostics (and their continuation lines) reported in a .ts or .tsx file. */ -function typeScriptLines(lines) { - let inTypeScript = false; - return lines.filter((line) => { - const match = /^(\S+?)\(\d+,\d+\): /.exec(line); - if (match) { - inTypeScript = /\.tsx?$/.test(match[1]); - } - return inTypeScript; - }); -} - -function runTsc(args) { - const tsc = path.join(root, 'node_modules', 'typescript', 'bin', 'tsc'); - const result = spawnSync(process.execPath, [tsc, '--noEmit', '--pretty', 'false', ...args], { - cwd: root, - encoding: 'utf8', - }); - if (result.error || (result.status !== 0 && result.status !== 2)) { - console.error(result.error || result.stderr || result.stdout); - process.exit(result.status || 1); - } - return diagnosticLines(`${result.stdout || ''}\n${result.stderr || ''}`); -} - -function main() { - if (!fs.existsSync(baselinePath)) { - console.error('typecheck baseline is missing: ' + baselinePath); - process.exit(1); - } - const baseline = diagnosticLines(fs.readFileSync(baselinePath, 'utf8')); - const actual = runTsc([]); - const renameAt = process.argv.indexOf('--rename'); - if (renameAt !== -1) { - const [from, to] = process.argv.slice(renameAt + 1, renameAt + 3); - if (!from || !to) { - console.error('usage: typecheck-baseline.js --rename '); - process.exit(1); - } - if (!baseline.some( (line) => line.startsWith(`${from}(`) )) { - console.error('no baseline entries at ' + from); - process.exit(1); - } - const { renamed, missing } = renameLines(baseline, actual, from, to); - if (missing.length) { - console.error('no matching diagnostic at ' + to + ' for:\n' + missing.join('\n')); - process.exit(1); - } - fs.writeFileSync(baselinePath, renamed.join('\n') + '\n'); - return; - } - if (process.argv.includes('--write')) { - const typed = typeScriptLines(actual); - if (typed.length) { - console.error('a .ts/.tsx file has diagnostics:\n' + typed.join('\n')); - process.exit(1); - } - fs.writeFileSync(baselinePath, actual.join('\n') + '\n'); - return; - } - // Second pass: no allowlist. A .ts/.tsx file may not have any diagnostic under --noImplicitAny. - const implicitAny = typeScriptLines(runTsc(['--noImplicitAny'])); - if (implicitAny.length) { - console.error('noImplicitAny:\n' + implicitAny.join('\n')); - process.exitCode = 1; - } - const fresh = freshLines(actual, baseline); - const unused = unusedLines(baseline, actual); - if (fresh.length || unused.length) { - if (fresh.length) { - console.error(fresh.join('\n')); - } - if (unused.length) { - console.error('unused baseline:\n' + unused.join('\n')); - } - process.exit(1); - } - if (process.exitCode) { - process.exit(process.exitCode); - } -} - -if (require.main === module) { - main(); -} - -module.exports = { typeScriptLines, diagnosticLines, freshLines, unusedLines, ownedFingerprints, renameLines }; diff --git a/scripts/typecheck-baseline.txt b/scripts/typecheck-baseline.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/typecheckBaseline.test.ts b/src/typecheckBaseline.test.ts deleted file mode 100644 index 1dc31886..00000000 --- a/src/typecheckBaseline.test.ts +++ /dev/null @@ -1,82 +0,0 @@ -import { expect, it } from 'vitest'; -import { spawnSync } from 'child_process'; -import fs from 'fs'; -import path from 'path'; - -import { freshLines, unusedLines, typeScriptLines } from '../scripts/typecheck-baseline'; - -it( 'reports a diagnostic line that the baseline does not list', () => { - const known = ['src/a.js(1,1): error TS1: known']; - expect( freshLines( known, known ) ).toEqual( [] ); - expect( freshLines( [ - 'src/a.js(1,1): error TS1: known', - 'src/b.js(2,1): error TS2: new', - ], known ) ).toEqual( ['src/b.js(2,1): error TS2: new'] ); -} ); - -it( 'treats the same diagnostic at another line as known', () => { - const known = ['src/a.js(1,1): error TS1: known']; - expect( freshLines( ['src/a.js(9,4): error TS1: known'], known ) ).toEqual( [] ); -} ); - -it( 'reports a src/ baseline line whose diagnostic is gone', () => { - const known = [ - 'src/a.js(1,1): error TS1: known', - 'src/gone.js(2,1): error TS2: gone', - ' Overload 1 of 4, leftover continuation', - ]; - expect( unusedLines( known, ['src/a.js(9,4): error TS1: known'] ) ).toEqual( [ - 'src/gone.js(2,1): error TS2: gone', - ' Overload 1 of 4, leftover continuation', - ] ); -} ); - -it( 'does not let one file\'s continuation shelter another file\'s', () => { - const known = [ - 'src/a.js(1,1): error TS1: known', - ' Overload 1 of 4, same text', - ]; - const actual = [ - 'src/b.js(2,1): error TS1: known', - ' Overload 1 of 4, same text', - ]; - expect( freshLines( actual, known ) ).toEqual( actual ); -} ); - -it( 'keeps the noImplicitAny diagnostics of .ts and .tsx files and their continuations', () => { - expect( typeScriptLines( [ - 'src/a.js(1,1): error TS7006: Parameter implicitly has an any type.', - ' js continuation', - 'src/b.ts(2,1): error TS7016: Could not find a declaration file.', - ' ts continuation', - 'src/c.tsx(3,1): error TS7006: Parameter implicitly has an any type.', - 'e2e/helpers/d.js(4,1): error TS7006: Parameter implicitly has an any type.', - 'e2e/helpers/d.test.ts(5,1): error TS7006: Parameter implicitly has an any type.', - 'scripts/e.js(6,1): error TS7006: Parameter implicitly has an any type.', - ] ) ).toEqual( [ - 'src/b.ts(2,1): error TS7016: Could not find a declaration file.', - ' ts continuation', - 'src/c.tsx(3,1): error TS7006: Parameter implicitly has an any type.', - 'e2e/helpers/d.test.ts(5,1): error TS7006: Parameter implicitly has an any type.', - ] ); - expect( typeScriptLines( ['src/a.js(1,1): error TS7006: x'] ) ).toEqual( [] ); -} ); - -it( 'passes on an empty baseline when tsc reports nothing', () => { - expect( freshLines( [], [] ) ).toEqual( [] ); - expect( unusedLines( [], [] ) ).toEqual( [] ); -} ); - -it( 'exits non-zero when a .ts file has an implicit any, even if the first pass is clean', () => { - const file = path.join( __dirname, 'implicitAnyProbe.ts' ); - fs.writeFileSync( file, 'export function probe( x ) { return x; }\n' ); - try { - const result = spawnSync( process.execPath, [ - path.join( __dirname, '..', 'scripts', 'typecheck-baseline.js' ), - ], { encoding: 'utf8' } ); - expect( result.status ).toBe( 1 ); - expect( result.stderr ).toContain( 'src/implicitAnyProbe.ts' ); - } finally { - fs.unlinkSync( file ); - } -}, 60000 ); diff --git a/tsconfig.json b/tsconfig.json index b0fabd4e..012427b4 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,7 @@ "esModuleInterop": true, "allowSyntheticDefaultImports": true, "strict": false, + "noImplicitAny": true, "skipLibCheck": true }, "include": ["src", "e2e/helpers/**/*.test.ts"]