@inflexa-ai/typecheck 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/run.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ export type TypecheckRun = {
2
+ /** The working folder: the base of `projects`, of the globs, and of each printed path. */
3
+ cwd: string;
4
+ /** The tsconfig files or folders of `-p`, in order. None checks `tsconfig.json` of `cwd`. */
5
+ projects: string[];
6
+ /** The configuration file of `--config`, relative to `cwd`. */
7
+ config?: string;
8
+ };
9
+ export type TypecheckResult = {
10
+ output: string;
11
+ exitCode: 0 | 1;
12
+ };
13
+ /**
14
+ * One run of the command. It prints the diagnostics of `tsc --noEmit` for each
15
+ * project, in the order of the projects, and then the reports of the rules of
16
+ * all projects, sorted by path, line and column. All projects open in one
17
+ * snapshot of one API, thus one TypeScript process serves the run. A rule runs
18
+ * once on each file, in the first project of the order that holds the file.
19
+ */
20
+ export declare function runTypecheck({ cwd, projects, config: configFile }: TypecheckRun): Promise<TypecheckResult>;
package/dist/run.js ADDED
@@ -0,0 +1,79 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import path from 'node:path';
3
+ import { API } from 'typescript/unstable/sync';
4
+ import { loadConfig, resolveRules } from './config.js';
5
+ import { applyDirectives, DIRECTIVE } from './directives.js';
6
+ import { computeLineStarts } from 'typescript/unstable/ast';
7
+ import { collectDiagnostics, displayPath, formatDiagnostic, sortAndDeduplicate, withRealConfig } from './diagnostics.js';
8
+ import { runRules } from './engine.js';
9
+ import { TypecheckLoadError } from './errors.js';
10
+ import { projectFiles, resolveProject, withoutByteOrderMark } from './projects.js';
11
+ import { rules as builtinRules } from './rules/index.js';
12
+ const DECLARATION_FILE = /\.d\.[cm]?ts$/;
13
+ function isRuleTarget(relative) {
14
+ return !DECLARATION_FILE.test(relative) && !relative.startsWith('../') && !path.isAbsolute(relative) && !relative.split('/').includes('node_modules');
15
+ }
16
+ function compareReports(a, b) {
17
+ if (a.path !== b.path)
18
+ return a.path < b.path ? -1 : 1;
19
+ return a.line - b.line || a.column - b.column;
20
+ }
21
+ export async function runTypecheck({ cwd, projects, config: configFile }) {
22
+ const files = (projects.length > 0 ? projects : ['.']).map((project) => projectFiles(resolveProject(cwd, project)));
23
+ const resolved = resolveRules(await loadConfig(cwd, configFile), builtinRules);
24
+ const virtualTexts = new Map(files.map(({ virtual, virtualText }) => [virtual, virtualText]));
25
+ const lines = new Map();
26
+ function linesOf(fileName) {
27
+ let starts = lines.get(fileName);
28
+ if (starts === undefined) {
29
+ starts = computeLineStarts(withoutByteOrderMark(virtualTexts.get(fileName) ?? readFileSync(fileName, 'utf8')));
30
+ lines.set(fileName, starts);
31
+ }
32
+ return starts;
33
+ }
34
+ const api = new API({
35
+ cwd,
36
+ fs: {
37
+ readFile: (fileName) => virtualTexts.get(fileName),
38
+ fileExists: (fileName) => (virtualTexts.has(fileName) ? true : undefined),
39
+ },
40
+ });
41
+ try {
42
+ const snapshot = api.updateSnapshot({ openProjects: files.map(({ virtual }) => virtual) });
43
+ let output = '';
44
+ const reports = [];
45
+ const visited = new Set();
46
+ for (const projectFile of files) {
47
+ const { tsconfig, virtual } = projectFile;
48
+ const project = snapshot.getProject(virtual);
49
+ if (project === undefined)
50
+ throw new TypecheckLoadError(`Cannot open the project ${path.relative(cwd, tsconfig)}.`);
51
+ const diagnostics = sortAndDeduplicate(collectDiagnostics(project.program).map((diagnostic) => withRealConfig(diagnostic, projectFile)));
52
+ for (const diagnostic of diagnostics)
53
+ output += formatDiagnostic(diagnostic, cwd, linesOf);
54
+ for (const fileName of project.program.getSourceFileNames()) {
55
+ if (visited.has(fileName))
56
+ continue;
57
+ visited.add(fileName);
58
+ const relative = displayPath(cwd, fileName);
59
+ if (!isRuleTarget(relative))
60
+ continue;
61
+ const active = resolved.rulesFor(relative);
62
+ if (active.length === 0 && !readFileSync(fileName, 'utf8').includes(DIRECTIVE))
63
+ continue;
64
+ const sourceFile = project.program.getSourceFile(fileName);
65
+ if (sourceFile === undefined || sourceFile.isDeclarationFile)
66
+ continue;
67
+ const found = active.length === 0 ? [] : runRules(sourceFile, project.program, project.checker, active);
68
+ for (const report of applyDirectives(sourceFile, found))
69
+ reports.push({ ...report, path: relative });
70
+ }
71
+ }
72
+ for (const { path: file, line, column, id, message } of reports.toSorted(compareReports))
73
+ output += `${file}(${String(line)},${String(column)}): error ${id}: ${message}\n`;
74
+ return { output, exitCode: output === '' ? 0 : 1 };
75
+ }
76
+ finally {
77
+ api.close();
78
+ }
79
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@inflexa-ai/typecheck",
3
+ "version": "0.5.0",
4
+ "description": "The type check of Inflexa on TypeScript 7: the diagnostics of tsc --noEmit and the typed rules in one command.",
5
+ "keywords": [
6
+ "typescript",
7
+ "typecheck",
8
+ "lint",
9
+ "neverthrow"
10
+ ],
11
+ "license": "Apache-2.0",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/inflexa-ai/lint.git",
15
+ "directory": "oxlint/typecheck"
16
+ },
17
+ "type": "module",
18
+ "exports": {
19
+ ".": {
20
+ "types": "./dist/index.d.ts",
21
+ "default": "./dist/index.js"
22
+ },
23
+ "./rule-tester": {
24
+ "types": "./dist/rule-tester.d.ts",
25
+ "default": "./dist/rule-tester.js"
26
+ },
27
+ "./helpers/*": {
28
+ "types": "./dist/helpers/*.d.ts",
29
+ "default": "./dist/helpers/*.js"
30
+ },
31
+ "./package.json": "./package.json"
32
+ },
33
+ "bin": {
34
+ "inflexa-typecheck": "dist/cli.js"
35
+ },
36
+ "files": [
37
+ "dist",
38
+ "NOTICE"
39
+ ],
40
+ "engines": {
41
+ "node": ">=22.18.0"
42
+ },
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "peerDependencies": {
47
+ "typescript": "7.0.2"
48
+ }
49
+ }