Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add estree.Program type to rollup.d.ts #5323

Merged
merged 5 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
ProgramAst,
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<ProgramAst>(5);
readonly cachedModules = new Map<string, ModuleJSON>();
readonly deoptimizationTracker = new PathTracker();
entryModules: Module[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type {
NormalizedInputOptions,
PartialNull,
PreserveEntrySignaturesOption,
ProgramAst,
ResolvedId,
ResolvedIdMap,
RollupError,
Expand All @@ -47,7 +48,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
7 changes: 5 additions & 2 deletions src/ast/nodes/shared/Node.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { locate, type Location } from 'locate-character';
import type MagicString from 'magic-string';
import type { AstContext } from '../../../Module';
import type { AstNode, NormalizedTreeshakingOptions } from '../../../rollup/types';
import type { RollupAnnotation } from '../../../utils/convert-ast';
import type {
AstNode,
NormalizedTreeshakingOptions,
RollupAnnotation
} from '../../../rollup/types';
import { ANNOTATION_KEY, INVALID_ANNOTATION_KEY } from '../../../utils/convert-ast';
import { LOGLEVEL_WARN } from '../../../utils/logging';
import { logInvalidAnnotation } from '../../../utils/logs';
Expand Down
30 changes: 23 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?: ProgramAst;
code: string;
map?: SourceMapInput;
}

export interface TransformModuleJSON {
ast?: AstNode;
ast?: ProgramAst;
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: ProgramAst;
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: ProgramAst | 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;
) => ProgramAst;

// 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<ProgramAst>;

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: ProgramAst;
code: string;
id: string;
meta: CustomPluginOptions;
Expand Down Expand Up @@ -977,6 +979,20 @@ interface AstNode {
type: string;
}

type AnnotationType = 'pure' | 'noSideEffects';

interface RollupAnnotation {
start: number;
end: number;
type: AnnotationType;
}

type INVALID_ANNOTATION_KEY_TYPE = '_rollupRemoved';

type ProgramAst = Program &
AstNode &
Partial<Record<INVALID_ANNOTATION_KEY_TYPE, RollupAnnotation[]>>;
TrickyPi marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down Expand Up @@ -1229,15 +1235,7 @@ interface ImportExpression extends estree.ImportExpression {
}

export const ANNOTATION_KEY = '_rollupAnnotations';
export const INVALID_ANNOTATION_KEY = '_rollupRemoved';

export type AnnotationType = 'pure' | 'noSideEffects';

export interface RollupAnnotation {
start: number;
end: number;
type: AnnotationType;
}
export const INVALID_ANNOTATION_KEY: INVALID_ANNOTATION_KEY_TYPE = '_rollupRemoved';

interface CallExpression extends estree.SimpleCallExpression {
[ANNOTATION_KEY]?: RollupAnnotation[];
Expand All @@ -1258,8 +1256,3 @@ interface FunctionDeclaration extends estree.FunctionDeclaration {
interface ArrowFunctionExpression extends estree.ArrowFunctionExpression {
[ANNOTATION_KEY]?: RollupAnnotation[];
}

export type ProgramAst = estree.Program &
AstNode & {
[INVALID_ANNOTATION_KEY]?: RollupAnnotation[];
};
2 changes: 1 addition & 1 deletion src/utils/logs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { locate } from 'locate-character';
import type Module from '../Module';
import type {
AnnotationType,
InternalModuleFormat,
LogHandler,
NormalizedInputOptions,
RollupLog
} from '../rollup/types';
import type { AnnotationType } from './convert-ast';
import getCodeFrame from './getCodeFrame';
import { LOGLEVEL_WARN } from './logging';
import { extname } from './path';
Expand Down