Skip to content

Commit

Permalink
fix: Add estree.Program type to rollup.d.ts (#5323)
Browse files Browse the repository at this point in the history
* Includes Program type from estree

* Add @types/estree as a dependency

* Remove annotation type from the official type file

* Fix JSDoc types for internal scripts

* Change ProgramAst to ProgramNode

ProgramAst is a tautology as a Program IS the AST.
In the future, we may need names for other AST nodes,
so a naming scheme XXXNode should work well.

---------

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
TrickyPi and lukastaegert committed Jan 4, 2024
1 parent 347a347 commit 8520c55
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 28 deletions.
3 changes: 3 additions & 0 deletions browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"url": "https://github.com/rollup/rollup/issues"
},
"homepage": "https://rollupjs.org/",
"dependencies": {
"@types/estree": "1.0.5"
},
"files": [
"dist/**/*.wasm",
"dist/**/*.js",
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@
"optionalDependencies": {
"fsevents": "~2.3.2"
},
"dependencies": {
"@types/estree": "1.0.5"
},
"devDependenciesComments": {
"@rollup/plugin-typescript": "It appears that 11.1.3 breaks sourcemaps"
},
Expand All @@ -124,7 +127,6 @@
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "11.1.5",
"@rollup/pluginutils": "^5.1.0",
"@types/estree": "1.0.5",
"@types/inquirer": "^9.0.7",
"@types/mocha": "^10.0.6",
"@types/node": "18.0.0",
Expand Down
6 changes: 3 additions & 3 deletions scripts/perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function getAverage(accumulatedMeasurements, runs, discarded) {
}

/**
* @param {import('rollup').MergedRollupOptions} config
* @param {import('../dist/rollup.js').MergedRollupOptions} config
* @param {PersistedTimings} existingTimings
* @return {Promise<void>}
*/
Expand Down Expand Up @@ -148,8 +148,8 @@ async function calculatePrintAndPersistTimings(config, existingTimings) {
}

/**
* @param {import('rollup').MergedRollupOptions} config
* @return {Promise<import('rollup').SerializedTimings>}
* @param {import('../dist/rollup.js').MergedRollupOptions} config
* @return {Promise<import('../dist/rollup.js').SerializedTimings>}
*/
async function buildAndGetTimings(config) {
config.perf = true;
Expand Down
4 changes: 2 additions & 2 deletions src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { ModuleLoader, type UnresolvedModule } from './ModuleLoader';
import GlobalScope from './ast/scopes/GlobalScope';
import { PathTracker } from './ast/utils/PathTracker';
import type {
AstNode,
ModuleInfo,
ModuleJSON,
NormalizedInputOptions,
ProgramNode,
RollupCache,
RollupWatcher,
SerializablePluginCache,
Expand Down Expand Up @@ -52,7 +52,7 @@ function normalizeEntryModules(
}

export default class Graph {
readonly astLru = flru<AstNode>(5);
readonly astLru = flru<ProgramNode>(5);
readonly cachedModules = new Map<string, ModuleJSON>();
readonly deoptimizationTracker = new PathTracker();
entryModules: Module[] = [];
Expand Down
9 changes: 4 additions & 5 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import type {
} from './rollup/types';
import { EMPTY_OBJECT } from './utils/blank';
import { BuildPhase } from './utils/buildPhase';
import type { ProgramAst } from './utils/convert-ast';
import { decodedSourcemap, resetSourcemapCache } from './utils/decodedSourcemap';
import { getId } from './utils/getId';
import { getNewSet, getOrCreate } from './utils/getOrCreate';
Expand Down Expand Up @@ -1347,17 +1346,17 @@ export default class Module {
this.exports.set(name, MISSING_EXPORT_SHIM_DESCRIPTION);
}

private tryParse(): ProgramAst {
private tryParse() {
try {
return parseAst(this.info.code!) as ProgramAst;
return parseAst(this.info.code!);
} catch (error_: any) {
return this.error(logModuleParseError(error_, this.id), error_.pos);
}
}

private async tryParseAsync(): Promise<ProgramAst> {
private async tryParseAsync() {
try {
return (await parseAstAsync(this.info.code!)) as ProgramAst;
return await parseAstAsync(this.info.code!);
} catch (error_: any) {
return this.error(logModuleParseError(error_, this.id), error_.pos);
}
Expand Down
18 changes: 11 additions & 7 deletions src/rollup/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Program } from 'estree';

export const VERSION: string;

// utils
Expand Down Expand Up @@ -98,13 +100,13 @@ interface ModuleOptions {
}

export interface SourceDescription extends Partial<PartialNull<ModuleOptions>> {
ast?: AstNode;
ast?: ProgramNode;
code: string;
map?: SourceMapInput;
}

export interface TransformModuleJSON {
ast?: AstNode;
ast?: ProgramNode;
code: string;
// note if plugins use new this.cache to opt-out auto transform cache
customTransformCache: boolean;
Expand All @@ -115,7 +117,7 @@ export interface TransformModuleJSON {
}

export interface ModuleJSON extends TransformModuleJSON, ModuleOptions {
ast: AstNode;
ast: ProgramNode;
dependencies: string[];
id: string;
resolvedIds: ResolvedIdMap;
Expand Down Expand Up @@ -171,7 +173,7 @@ export type EmittedFile = EmittedAsset | EmittedChunk | EmittedPrebuiltChunk;
export type EmitFile = (emittedFile: EmittedFile) => string;

interface ModuleInfo extends ModuleOptions {
ast: AstNode | null;
ast: ProgramNode | null;
code: string | null;
dynamicImporters: readonly string[];
dynamicallyImportedIdResolutions: readonly ResolvedId[];
Expand Down Expand Up @@ -204,7 +206,7 @@ type LoggingFunctionWithPosition = (
export type ParseAst = (
input: string,
options?: { allowReturnOutsideFunction?: boolean }
) => AstNode;
) => ProgramNode;

// declare AbortSignal here for environments without DOM lib or @types/node
declare global {
Expand All @@ -214,7 +216,7 @@ declare global {
export type ParseAstAsync = (
input: string,
options?: { allowReturnOutsideFunction?: boolean; signal?: AbortSignal }
) => Promise<AstNode>;
) => Promise<ProgramNode>;

export interface PluginContext extends MinimalPluginContext {
addWatchFile: (id: string) => void;
Expand Down Expand Up @@ -280,7 +282,7 @@ export type ResolveIdHook = (
export type ShouldTransformCachedModuleHook = (
this: PluginContext,
options: {
ast: AstNode;
ast: ProgramNode;
code: string;
id: string;
meta: CustomPluginOptions;
Expand Down Expand Up @@ -977,6 +979,8 @@ interface AstNode {
type: string;
}

type ProgramNode = Program & AstNode;

export function defineConfig(options: RollupOptions): RollupOptions;
export function defineConfig(options: RollupOptions[]): RollupOptions[];
export function defineConfig(optionsFunction: RollupOptionsFunction): RollupOptionsFunction;
Expand Down
13 changes: 6 additions & 7 deletions src/utils/convert-ast.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type * as estree from 'estree';
import type { AstNode } from '../rollup/types';
import type { AstNode, ProgramNode } from '../rollup/types';
import { FIXED_STRINGS } from './convert-ast-strings';
import { error, logParseError } from './logs';

type ReadString = (start: number, length: number) => string;

export const convertProgram = (buffer: ArrayBuffer, readString: ReadString): ProgramAst =>
export const convertProgram = (buffer: ArrayBuffer, readString: ReadString): InternalProgramNode =>
convertNode(0, new Uint32Array(buffer), readString);

const convertNode = (position: number, buffer: Uint32Array, readString: ReadString): any => {
Expand Down Expand Up @@ -826,7 +826,7 @@ const nodeConverters: ((position: number, buffer: Uint32Array, readString: ReadS
};
},
// index:53; Program
(position, buffer, readString): ProgramAst => {
(position, buffer, readString): InternalProgramNode => {
const start = buffer[position++];
const end = buffer[position++];
const annotations = convertAnnotationList(buffer[position++], buffer);
Expand Down Expand Up @@ -1259,7 +1259,6 @@ interface ArrowFunctionExpression extends estree.ArrowFunctionExpression {
[ANNOTATION_KEY]?: RollupAnnotation[];
}

export type ProgramAst = estree.Program &
AstNode & {
[INVALID_ANNOTATION_KEY]?: RollupAnnotation[];
};
type InternalProgramNode = ProgramNode & {
[INVALID_ANNOTATION_KEY]?: RollupAnnotation[];
};

0 comments on commit 8520c55

Please sign in to comment.