@hominis/fireforge 0.18.10 → 0.18.11

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.
@@ -9,80 +9,15 @@
9
9
  * how to fix it.
10
10
  *
11
11
  * Separated from `patch-lint.ts` to keep both files within the
12
- * project's per-file line budget.
12
+ * project's per-file line budget. The shim itself and the suppressed
13
+ * diagnostic code list now live in `typecheck-shim.ts` and are shared
14
+ * with the whole-project `fireforge typecheck` command — keeping a
15
+ * single source of truth for the Firefox-globals coverage.
13
16
  */
14
17
  import { resolve } from 'node:path';
15
18
  import { pathExists } from '../utils/fs.js';
16
19
  import { verbose } from '../utils/logger.js';
17
- // ---------------------------------------------------------------------------
18
- // Firefox globals shim
19
- // ---------------------------------------------------------------------------
20
- const SHIM_FILENAME = '__fireforge_firefox_globals.d.ts';
21
- /**
22
- * Minimal `.d.ts` shim for Firefox privileged-scope globals.
23
- *
24
- * Firefox source is plain JS — no TypeScript allowed. The shim lets
25
- * `checkJs` run without reporting "cannot find name" for the most
26
- * common Mozilla APIs. Types are intentionally loose (`any`) because
27
- * full Firefox type coverage is out of scope.
28
- *
29
- * Notable patterns that require shimming:
30
- * - `const lazy = {};` + `ChromeUtils.defineESModuleGetters(lazy, { ... })`
31
- * populates `lazy` at runtime; we declare it as `Record<string, any>`.
32
- * - `Services.obs`, `Services.prefs`, etc. are XPCOM service accessors.
33
- * - `Ci`, `Cc`, `Cr`, `Cu` are XPCOM component shortcuts.
34
- * - Browser chrome globals like `gBrowser`, `gURLBar` are common in
35
- * content scripts wired via `browser.js`.
36
- */
37
- const FIREFOX_GLOBALS_SHIM = `
38
- declare var Services: any;
39
- declare var ChromeUtils: {
40
- defineESModuleGetters(target: any, modules: Record<string, string>): void;
41
- importESModule(specifier: string): any;
42
- import(specifier: string): any;
43
- defineModuleGetter(target: any, name: string, specifier: string): void;
44
- generateQI(interfaces: any[]): Function;
45
- isClassInfo(obj: any): boolean;
46
- };
47
- declare var Cu: any;
48
- declare var Ci: any;
49
- declare var Cc: any;
50
- declare var Cr: any;
51
- declare var Components: any;
52
- declare var XPCOMUtils: any;
53
- declare var lazy: Record<string, any>;
54
- declare var PathUtils: any;
55
- declare var IOUtils: any;
56
- declare var FileUtils: any;
57
- declare var gBrowser: any;
58
- declare var gURLBar: any;
59
- declare var gNavigatorBundle: any;
60
- declare var AppConstants: any;
61
- `;
62
- // ---------------------------------------------------------------------------
63
- // Diagnostic filtering
64
- // ---------------------------------------------------------------------------
65
- /**
66
- * TS diagnostic codes to suppress because they are inherent to
67
- * checking Firefox JS files outside of Mozilla's own build system.
68
- *
69
- * Firefox uses `resource://` and `chrome://` URL schemes for module
70
- * imports. TypeScript's module resolver cannot follow these, so every
71
- * import from an upstream Firefox module produces a spurious
72
- * "Cannot find module" error. Filtering these out is essential to
73
- * keep the checkJs pass usable — otherwise every file with an import
74
- * would be buried in false positives.
75
- */
76
- const SUPPRESSED_DIAGNOSTIC_CODES = new Set([
77
- 2307, // Cannot find module '{0}' or its corresponding type declarations.
78
- 2306, // File '{0}' is not a module.
79
- 2305, // Module '{0}' has no exported member '{1}'.
80
- 2792, // Cannot find module '{0}'. Did you mean to set the 'moduleResolution' option...
81
- 2304, // Cannot find name '{0}'. (for globals we missed in the shim)
82
- 2552, // Cannot find name '{0}'. Did you mean '{1}'?
83
- 2580, // Cannot find name '{0}'. Do you need to install type definitions...
84
- 7016, // Could not find a declaration file for module '{0}'.
85
- ]);
20
+ import { composeShimSource, SHIM_FILENAME, SUPPRESSED_DIAGNOSTIC_CODES } from './typecheck-shim.js';
86
21
  // ---------------------------------------------------------------------------
87
22
  // Public API
88
23
  // ---------------------------------------------------------------------------
@@ -91,9 +26,18 @@ const SUPPRESSED_DIAGNOSTIC_CODES = new Set([
91
26
  *
92
27
  * @param repoDir - Absolute path to the engine (repository) directory
93
28
  * @param patchOwnedFiles - Set of patch-owned `.sys.mjs` file paths (relative to repoDir)
29
+ * @param extraShimPath - Optional project-relative path to an additional
30
+ * `.d.ts` file whose contents are concatenated to the built-in
31
+ * Firefox-globals shim. Sourced from `patchLint.checkJsExtraShim`.
32
+ * Resolved against `projectRoot` (one level up from `repoDir` is the
33
+ * wrong root — patches sit inside `engine/` while the shim lives at
34
+ * the project root, so the caller passes both).
35
+ * @param projectRoot - Absolute project root for resolving `extraShimPath`.
36
+ * Defaults to `repoDir` for back-compat with callers that don't
37
+ * pass an extra shim (no resolution actually happens in that case).
94
38
  * @returns Array of lint issues from TS diagnostics
95
39
  */
96
- export async function runCheckJs(repoDir, patchOwnedFiles) {
40
+ export async function runCheckJs(repoDir, patchOwnedFiles, extraShimPath, projectRoot) {
97
41
  if (patchOwnedFiles.size === 0)
98
42
  return [];
99
43
  // Dynamic import — typescript stays as a dev dependency
@@ -124,6 +68,29 @@ export async function runCheckJs(repoDir, patchOwnedFiles) {
124
68
  }
125
69
  if (rootFiles.length === 0)
126
70
  return [];
71
+ // Compose the shim. `extraShimPath` is project-relative (validated
72
+ // by config-validate); resolve it against `projectRoot`. When the
73
+ // caller passes neither, fall back to `repoDir` — the only way the
74
+ // shim path is ever read in that case is when extraShimPath is
75
+ // also undefined, so the resolution target is irrelevant.
76
+ let shimSource;
77
+ try {
78
+ const composed = await composeShimSource(projectRoot ?? repoDir, extraShimPath);
79
+ shimSource = composed.source;
80
+ if (composed.extraShimAppended) {
81
+ verbose(`checkJs: extra shim ${extraShimPath ?? ''} appended to Firefox globals shim`);
82
+ }
83
+ }
84
+ catch (err) {
85
+ return [
86
+ {
87
+ file: extraShimPath ?? '(checkJs)',
88
+ check: 'checkjs-type-error',
89
+ message: err instanceof Error ? err.message : String(err),
90
+ severity: 'error',
91
+ },
92
+ ];
93
+ }
127
94
  const shimPath = resolve(repoDir, SHIM_FILENAME);
128
95
  rootFiles.push(shimPath);
129
96
  const options = {
@@ -154,7 +121,7 @@ export async function runCheckJs(repoDir, patchOwnedFiles) {
154
121
  ...defaultHost,
155
122
  getSourceFile(fileName, languageVersion, onError) {
156
123
  if (fileName === shimPath) {
157
- return ts.createSourceFile(fileName, FIREFOX_GLOBALS_SHIM, languageVersion, true);
124
+ return ts.createSourceFile(fileName, shimSource, languageVersion, true);
158
125
  }
159
126
  if (ownedAbsolute.has(fileName)) {
160
127
  return defaultHost.getSourceFile(fileName, languageVersion, onError);
@@ -177,7 +144,7 @@ export async function runCheckJs(repoDir, patchOwnedFiles) {
177
144
  },
178
145
  readFile(fileName) {
179
146
  if (fileName === shimPath)
180
- return FIREFOX_GLOBALS_SHIM;
147
+ return shimSource;
181
148
  return defaultHost.readFile(fileName);
182
149
  },
183
150
  };
@@ -1,5 +1,5 @@
1
1
  // SPDX-License-Identifier: EUPL-1.2
2
- import { join } from 'node:path';
2
+ import { dirname, join } from 'node:path';
3
3
  import { toError } from '../utils/errors.js';
4
4
  import { pathExists, readText } from '../utils/fs.js';
5
5
  import { verbose } from '../utils/logger.js';
@@ -737,10 +737,12 @@ export async function lintExportedPatch(repoDir, affectedFiles, diffContent, con
737
737
  ...jsIssues,
738
738
  ...modCommentIssues,
739
739
  ];
740
- // Optional checkJs pass — only when explicitly enabled in config
740
+ // Optional checkJs pass — only when explicitly enabled in config.
741
+ // `checkJsExtraShim` is project-relative; resolve against the
742
+ // project root (dirname(engine) by getProjectPaths convention).
741
743
  if (config.patchLint?.checkJs) {
742
- const checkJsIssues = await runCheckJs(repoDir, patchOwnedFiles);
743
- issues.push(...checkJsIssues);
744
+ const extraShim = config.patchLint.checkJsExtraShim;
745
+ issues.push(...(await runCheckJs(repoDir, patchOwnedFiles, extraShim, dirname(repoDir))));
744
746
  }
745
747
  // Filter out ignored checks last so every rule still runs (keeps the
746
748
  // implementation uniform) but suppressed rules do not surface. We do not
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Shared TypeScript-checking constants used by both the patch-lint
3
+ * `checkJs` pass (`patch-lint-checkjs.ts`) and the whole-project
4
+ * `fireforge typecheck` command (`typecheck.ts`).
5
+ *
6
+ * Centralised so both flows agree on the same Firefox-globals shim
7
+ * and the same set of suppressed diagnostic codes — drift between the
8
+ * two would mean a patch could lint clean under `fireforge lint` but
9
+ * still fail `fireforge typecheck`, or vice versa, for reasons the
10
+ * operator could not infer from the rule names.
11
+ */
12
+ /** Filename used for the synthetic Firefox-globals shim source file. */
13
+ export declare const SHIM_FILENAME = "__fireforge_firefox_globals.d.ts";
14
+ /**
15
+ * Minimal `.d.ts` shim for Firefox privileged-scope globals.
16
+ *
17
+ * Firefox source is plain JS — no TypeScript allowed. The shim lets
18
+ * TS-driven type checking run without reporting "cannot find name"
19
+ * for the most common Mozilla APIs. Types are intentionally loose
20
+ * (`any`) because full Firefox type coverage is out of scope.
21
+ *
22
+ * Notable patterns that require shimming:
23
+ * - `const lazy = {};` + `ChromeUtils.defineESModuleGetters(lazy, { ... })`
24
+ * populates `lazy` at runtime; we declare it as `Record<string, any>`.
25
+ * - `Services.obs`, `Services.prefs`, etc. are XPCOM service accessors.
26
+ * - `Ci`, `Cc`, `Cr`, `Cu` are XPCOM component shortcuts.
27
+ * - Browser chrome globals like `gBrowser`, `gURLBar` are common in
28
+ * content scripts wired via `browser.js`.
29
+ */
30
+ export declare const FIREFOX_GLOBALS_SHIM = "\ndeclare var Services: any;\ndeclare var ChromeUtils: {\n defineESModuleGetters(target: any, modules: Record<string, string>): void;\n importESModule(specifier: string): any;\n import(specifier: string): any;\n defineModuleGetter(target: any, name: string, specifier: string): void;\n generateQI(interfaces: any[]): Function;\n isClassInfo(obj: any): boolean;\n};\ndeclare var Cu: any;\ndeclare var Ci: any;\ndeclare var Cc: any;\ndeclare var Cr: any;\ndeclare var Components: any;\ndeclare var XPCOMUtils: any;\ndeclare var lazy: Record<string, any>;\ndeclare var PathUtils: any;\ndeclare var IOUtils: any;\ndeclare var FileUtils: any;\ndeclare var gBrowser: any;\ndeclare var gURLBar: any;\ndeclare var gNavigatorBundle: any;\ndeclare var AppConstants: any;\n";
31
+ /**
32
+ * TS diagnostic codes suppressed by both the patch-lint checkJs pass
33
+ * and the whole-project typecheck command. Each is a known false
34
+ * positive that arises from checking Firefox JS outside Mozilla's own
35
+ * build system: the resolver can't follow `resource://`/`chrome://`
36
+ * URLs and the global shim is intentionally narrow.
37
+ *
38
+ * Widening this set should be deliberate and per-code — silently
39
+ * suppressing more codes hides real type errors. The same set is used
40
+ * by both flows so a patch can't pass one and fail the other for a
41
+ * reason the operator couldn't infer from the docs.
42
+ */
43
+ export declare const SUPPRESSED_DIAGNOSTIC_CODES: ReadonlySet<number>;
44
+ /**
45
+ * Result of {@link composeShimSource}: the source body to feed into
46
+ * the TS host plus a flag indicating whether the user-supplied extra
47
+ * shim was actually appended (used for verbose logging).
48
+ */
49
+ export interface ComposedShim {
50
+ source: string;
51
+ extraShimAppended: boolean;
52
+ }
53
+ /**
54
+ * Composes the synthetic shim source by concatenating the built-in
55
+ * Firefox globals shim with the contents of an optional user-supplied
56
+ * `.d.ts` file. The user file is appended verbatim — the augment
57
+ * direction is intentional (declarations later in concat order
58
+ * augment earlier ones), so a project that wants to refine `Services`
59
+ * with a more specific type can do so by declaring it in the extra
60
+ * shim.
61
+ *
62
+ * Missing extra-shim files raise a clear error rather than failing
63
+ * silently with a confusing "type not found" downstream — this is the
64
+ * one config-driven path where a user typo in `fireforge.json`
65
+ * produces a runtime error, so it needs to be unmistakable.
66
+ *
67
+ * @param projectRoot - Absolute project root, used to resolve the relative shim path
68
+ * @param extraShimPath - Optional project-relative path to an extra `.d.ts`
69
+ */
70
+ export declare function composeShimSource(projectRoot: string, extraShimPath: string | undefined): Promise<ComposedShim>;
@@ -0,0 +1,112 @@
1
+ // SPDX-License-Identifier: EUPL-1.2
2
+ /**
3
+ * Shared TypeScript-checking constants used by both the patch-lint
4
+ * `checkJs` pass (`patch-lint-checkjs.ts`) and the whole-project
5
+ * `fireforge typecheck` command (`typecheck.ts`).
6
+ *
7
+ * Centralised so both flows agree on the same Firefox-globals shim
8
+ * and the same set of suppressed diagnostic codes — drift between the
9
+ * two would mean a patch could lint clean under `fireforge lint` but
10
+ * still fail `fireforge typecheck`, or vice versa, for reasons the
11
+ * operator could not infer from the rule names.
12
+ */
13
+ import { resolve } from 'node:path';
14
+ import { pathExists, readText } from '../utils/fs.js';
15
+ /** Filename used for the synthetic Firefox-globals shim source file. */
16
+ export const SHIM_FILENAME = '__fireforge_firefox_globals.d.ts';
17
+ /**
18
+ * Minimal `.d.ts` shim for Firefox privileged-scope globals.
19
+ *
20
+ * Firefox source is plain JS — no TypeScript allowed. The shim lets
21
+ * TS-driven type checking run without reporting "cannot find name"
22
+ * for the most common Mozilla APIs. Types are intentionally loose
23
+ * (`any`) because full Firefox type coverage is out of scope.
24
+ *
25
+ * Notable patterns that require shimming:
26
+ * - `const lazy = {};` + `ChromeUtils.defineESModuleGetters(lazy, { ... })`
27
+ * populates `lazy` at runtime; we declare it as `Record<string, any>`.
28
+ * - `Services.obs`, `Services.prefs`, etc. are XPCOM service accessors.
29
+ * - `Ci`, `Cc`, `Cr`, `Cu` are XPCOM component shortcuts.
30
+ * - Browser chrome globals like `gBrowser`, `gURLBar` are common in
31
+ * content scripts wired via `browser.js`.
32
+ */
33
+ export const FIREFOX_GLOBALS_SHIM = `
34
+ declare var Services: any;
35
+ declare var ChromeUtils: {
36
+ defineESModuleGetters(target: any, modules: Record<string, string>): void;
37
+ importESModule(specifier: string): any;
38
+ import(specifier: string): any;
39
+ defineModuleGetter(target: any, name: string, specifier: string): void;
40
+ generateQI(interfaces: any[]): Function;
41
+ isClassInfo(obj: any): boolean;
42
+ };
43
+ declare var Cu: any;
44
+ declare var Ci: any;
45
+ declare var Cc: any;
46
+ declare var Cr: any;
47
+ declare var Components: any;
48
+ declare var XPCOMUtils: any;
49
+ declare var lazy: Record<string, any>;
50
+ declare var PathUtils: any;
51
+ declare var IOUtils: any;
52
+ declare var FileUtils: any;
53
+ declare var gBrowser: any;
54
+ declare var gURLBar: any;
55
+ declare var gNavigatorBundle: any;
56
+ declare var AppConstants: any;
57
+ `;
58
+ /**
59
+ * TS diagnostic codes suppressed by both the patch-lint checkJs pass
60
+ * and the whole-project typecheck command. Each is a known false
61
+ * positive that arises from checking Firefox JS outside Mozilla's own
62
+ * build system: the resolver can't follow `resource://`/`chrome://`
63
+ * URLs and the global shim is intentionally narrow.
64
+ *
65
+ * Widening this set should be deliberate and per-code — silently
66
+ * suppressing more codes hides real type errors. The same set is used
67
+ * by both flows so a patch can't pass one and fail the other for a
68
+ * reason the operator couldn't infer from the docs.
69
+ */
70
+ export const SUPPRESSED_DIAGNOSTIC_CODES = new Set([
71
+ 2307, // Cannot find module '{0}' or its corresponding type declarations.
72
+ 2306, // File '{0}' is not a module.
73
+ 2305, // Module '{0}' has no exported member '{1}'.
74
+ 2792, // Cannot find module '{0}'. Did you mean to set the 'moduleResolution' option...
75
+ 2304, // Cannot find name '{0}'. (for globals we missed in the shim)
76
+ 2552, // Cannot find name '{0}'. Did you mean '{1}'?
77
+ 2580, // Cannot find name '{0}'. Do you need to install type definitions...
78
+ 7016, // Could not find a declaration file for module '{0}'.
79
+ ]);
80
+ /**
81
+ * Composes the synthetic shim source by concatenating the built-in
82
+ * Firefox globals shim with the contents of an optional user-supplied
83
+ * `.d.ts` file. The user file is appended verbatim — the augment
84
+ * direction is intentional (declarations later in concat order
85
+ * augment earlier ones), so a project that wants to refine `Services`
86
+ * with a more specific type can do so by declaring it in the extra
87
+ * shim.
88
+ *
89
+ * Missing extra-shim files raise a clear error rather than failing
90
+ * silently with a confusing "type not found" downstream — this is the
91
+ * one config-driven path where a user typo in `fireforge.json`
92
+ * produces a runtime error, so it needs to be unmistakable.
93
+ *
94
+ * @param projectRoot - Absolute project root, used to resolve the relative shim path
95
+ * @param extraShimPath - Optional project-relative path to an extra `.d.ts`
96
+ */
97
+ export async function composeShimSource(projectRoot, extraShimPath) {
98
+ if (!extraShimPath) {
99
+ return { source: FIREFOX_GLOBALS_SHIM, extraShimAppended: false };
100
+ }
101
+ const absoluteShim = resolve(projectRoot, extraShimPath);
102
+ if (!(await pathExists(absoluteShim))) {
103
+ throw new Error(`Extra TypeScript shim not found: ${extraShimPath} (resolved to ${absoluteShim}). ` +
104
+ 'Check the path in fireforge.json or create the file.');
105
+ }
106
+ const extra = await readText(absoluteShim);
107
+ return {
108
+ source: `${FIREFOX_GLOBALS_SHIM}\n// ── extraShim: ${extraShimPath} ──\n${extra}`,
109
+ extraShimAppended: true,
110
+ };
111
+ }
112
+ //# sourceMappingURL=typecheck-shim.js.map
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Whole-project TypeScript type checking driven by user-supplied
3
+ * `jsconfig.json` paths. The engine behind the `fireforge typecheck`
4
+ * command.
5
+ *
6
+ * Distinct from `patch-lint-checkjs.ts` (patch-hygiene): patchLint is
7
+ * scoped, fireforge-controlled, and runs as part of `fireforge lint`;
8
+ * this command runs whole projects with user-controlled compiler
9
+ * options and is intended as a CI gate. The two share the Firefox
10
+ * globals shim and suppressed-code list (via `typecheck-shim.ts`) so
11
+ * a file that lints clean cannot fail typecheck for a reason the
12
+ * operator could not have inferred from the docs.
13
+ *
14
+ * Loads the TypeScript compiler API via dynamic import so it is only
15
+ * required when `typecheck.projects` is configured. TypeScript
16
+ * remains a dev-dependency — if a user invokes `fireforge typecheck`
17
+ * without installing it, the function returns a clear error pointing
18
+ * at `npm install typescript`.
19
+ */
20
+ import type { TypecheckConfig } from '../types/config.js';
21
+ import type { TypecheckProjectResult } from '../types/typecheck.js';
22
+ /**
23
+ * Sentinel string surfaced as a `TypecheckIssue.message` when the
24
+ * project's jsconfig sets `checkJs: false`. Exported so tests can
25
+ * pin the contract — operators rely on it to spot opted-out projects
26
+ * in CI logs.
27
+ */
28
+ export declare const CHECK_JS_DISABLED_NOTICE = "Project sets \"checkJs: false\" \u2014 skipping (override the jsconfig to enable typecheck).";
29
+ /**
30
+ * Runs `fireforge typecheck` against every project listed in `cfg.projects`.
31
+ *
32
+ * Per-project flow:
33
+ * 1. Read the jsconfig via the TS API. JSON parse / config-spec
34
+ * errors are surfaced as issues, not thrown — a single broken
35
+ * project must not abort the rest of the run.
36
+ * 2. Compute final compiler options from the user's options. We
37
+ * only force `noEmit: true` (this is a typecheck, not a build);
38
+ * we honour `strict`, `target`, `lib`, `module`, `paths`,
39
+ * `include`, `exclude`. `allowJs` and `checkJs` default to
40
+ * `true` only when the user has not set them — if the user set
41
+ * `checkJs: false` we treat that as an explicit opt-out and
42
+ * skip the project with a single notice (see {@link CHECK_JS_DISABLED_NOTICE}).
43
+ * 3. Inject the synthetic shim (built-in + optional extraShim) as
44
+ * a virtual root file that the custom CompilerHost serves.
45
+ * 4. Build the program; collect semantic, syntactic, options, and
46
+ * global diagnostics. The patchLint flow only reads semantic +
47
+ * syntactic — fine for hygiene, but a CI gate needs the full
48
+ * set so misconfigured `lib`/`paths` entries fail loudly.
49
+ * 5. Drop diagnostics whose code is in `SUPPRESSED_DIAGNOSTIC_CODES`
50
+ * and any diagnostic originating in the synthetic shim itself.
51
+ *
52
+ * @param projectRoot - Absolute project root. All paths in `cfg` are resolved against it.
53
+ * @param cfg - Validated `typecheck` block from `fireforge.json`.
54
+ * @returns One {@link TypecheckProjectResult} per entry in `cfg.projects`,
55
+ * in declared order. The CLI is responsible for printing the issues
56
+ * and computing the exit code.
57
+ */
58
+ export declare function runTypecheck(projectRoot: string, cfg: TypecheckConfig): Promise<TypecheckProjectResult[]>;
59
+ /**
60
+ * Converts an absolute path to a path relative to the project root,
61
+ * for display in CLI output. Falls back to the absolute path when
62
+ * the path lies outside the root (e.g. a `node_modules` symlinked
63
+ * from elsewhere). Exposed for tests and the CLI.
64
+ */
65
+ export declare function relativeForDisplay(projectRoot: string, absoluteFile: string): string;