Skip to content

Commit bf7cc72

Browse files
committed
fix(module-source): address review comments on type definitions
- Remove `extends SourceMapHookDetails` from `TransformSourceParams`; `source` is only available at the `sourceMapHook` call site, not in the options/state bag. Inline the optional `sourceUrl`/`sourceMapUrl` fields directly. - Add `allowHidden?: boolean` to `TransformSourceParams` to match runtime usage in `babel-plugin.js`, removing the `@ts-expect-error`. - Make `SourceMapObject.file` optional per Source Map v3 spec. - Use full tuple `[name, false, undefined]` for `hoistedDecls.push()` instead of short `[name]`, removing the `@ts-expect-error`. - Remove stale `@ts-expect-error` directives for `@babel/generator` options now recognized by `@types/babel__generator`. - Enable `exactOptionalPropertyTypes: true`. - Add missing `typedoc.json`.
1 parent cd09316 commit bf7cc72

6 files changed

Lines changed: 32 additions & 21 deletions

File tree

packages/module-source/src/babel-plugin.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ function makeModulePlugins(options) {
219219
} else {
220220
// Rewrite to be just name = value.
221221
soften(id);
222-
// @ts-expect-error - needs types
223-
options.hoistedDecls.push([name]);
222+
options.hoistedDecls.push([name, false, undefined]);
224223
replacements.push(
225224
t.expressionStatement(
226225
t.assignmentExpression(
@@ -280,7 +279,6 @@ function makeModulePlugins(options) {
280279

281280
const visitor = {
282281
Identifier(path) {
283-
// @ts-expect-error - needs types
284282
if (options.allowHidden || allowedHiddens.has(path.node)) {
285283
return;
286284
}

packages/module-source/src/transform-analyze.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import * as h from './hidden.js';
66
import { createSourceOptions } from './source-options.js';
77
import { buildFunctorSource, buildModuleRecord } from './functor.js';
88

9-
/** @import {Options} from './module-source.js' */
9+
/** @import {ModuleSourceOptions} from './types/module-source.js' */
1010

1111
const makeCreateStaticRecord = transformSource =>
1212
/**
1313
*
1414
* @param {string} moduleSource
15-
* @param {Options} options
15+
* @param {ModuleSourceOptions} options
1616
*/
1717
function createStaticRecord(
1818
moduleSource,

packages/module-source/src/transform-source.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const makeTransformSource = (makeModulePlugins, babel = null) => {
4343
options;
4444

4545
const ast = parseBabel(source, {
46-
sourceType,
46+
...(sourceType !== undefined ? { sourceType } : {}),
4747
tokens: true,
4848
createParenthesizedExpressions: true,
4949
});
@@ -57,7 +57,7 @@ export const makeTransformSource = (makeModulePlugins, babel = null) => {
5757
{
5858
sourceFileName: sourceMapUrl,
5959
sourceMaps: !!sourceMapHook,
60-
// @ts-expect-error - undocumented??
60+
// @ts-expect-error undocumented option
6161
inputSourceMap: sourceMap,
6262
experimental_preserveFormat: true,
6363
preserveFormat: true,

packages/module-source/src/types/module-source.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export type ModuleSourceRecord = Readonly<
2828
* **Do not confuse with `SourceMapHookDetails` type from `@endo/compartment-mapper`.**
2929
*/
3030
export interface SourceMapHookDetails {
31-
sourceUrl?: string;
32-
sourceMapUrl?: string;
31+
sourceUrl?: string | undefined;
32+
sourceMapUrl?: string | undefined;
3333
source: string;
3434
}
3535

@@ -43,7 +43,7 @@ export type SourceMapObject = {
4343
sourceRoot?: string | undefined;
4444
sourcesContent?: string[] | undefined;
4545
mappings: string;
46-
file: string;
46+
file?: string | undefined;
4747
};
4848

4949
/**
@@ -60,11 +60,16 @@ export type SourceMapHook = (
6060

6161
/**
6262
* A bulging bucket of options for `transformSource`.
63+
*
64+
* The `source` string is intentionally excluded: it belongs in
65+
* {@link SourceMapHookDetails} and is only available inside
66+
* `transformSource()` when invoking `sourceMapHook`, not when constructing
67+
* the options/state bag.
6368
*/
64-
export interface TransformSourceParams
65-
extends GeneratorOptions,
66-
SourceMapHookDetails {
67-
sourceType: ParserOptions['sourceType'];
69+
export interface TransformSourceParams extends GeneratorOptions {
70+
sourceType?: ParserOptions['sourceType'] | undefined;
71+
sourceUrl?: string | undefined;
72+
sourceMapUrl?: string | undefined;
6873
fixedExportMap: Record<string, any>;
6974
imports: Record<string, any>;
7075
exportAlls: string[];
@@ -75,14 +80,15 @@ export interface TransformSourceParams
7580
importDecls: string[];
7681
dynamicImport: { present: boolean };
7782
importMeta: { present: boolean };
78-
sourceMapHook?: SourceMapHook;
83+
sourceMapHook?: SourceMapHook | undefined;
84+
allowHidden?: boolean | undefined;
7985

8086
/**
8187
* This is either a string or a SourceMapV3 object, but it's used with an
8288
* undocumented option (`inputSourceMap` of `@babel/generator`), so might not
8389
* do anything at all.
8490
*/
85-
sourceMap?: unknown;
91+
sourceMap?: unknown | undefined;
8692
}
8793

8894
/**
@@ -97,8 +103,8 @@ export type PluginFactory = (params: { types: typeof babelTypes }) => {
97103
* Options for the `ModuleSource` constructor.
98104
*/
99105
export interface ModuleSourceOptions {
100-
sourceUrl?: string;
101-
sourceMap?: string;
102-
sourceMapUrl?: string;
103-
sourceMapHook?: SourceMapHook;
106+
sourceUrl?: string | undefined;
107+
sourceMap?: string | undefined;
108+
sourceMapUrl?: string | undefined;
109+
sourceMapHook?: SourceMapHook | undefined;
104110
}

packages/module-source/tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@
1212
],
1313
"exclude": [
1414
"**/*.d.ts"
15-
]
15+
],
16+
"compilerOptions": {
17+
"exactOptionalPropertyTypes": true
18+
}
1619
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": ["../../typedoc.base.json"],
3+
"entryPoints": ["index.js", "shim.js"]
4+
}

0 commit comments

Comments
 (0)