Skip to content

Commit 5682ae9

Browse files
authored
Align TS types, docs and implementation for this.warn and this.error (#2975)
1 parent 21b7ce4 commit 5682ae9

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

docs/05-plugin-development.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ Emitted chunks will follow the [`output.chunkFileNames`](guide/en/#outputchunkfi
343343

344344
The generated code that replaces `import.meta.ROLLUP_CHUNK_URL_chunkReferenceId` can be customized via the [`resolveFileUrl`](guide/en/#resolvefileurl) plugin hook. Once the chunk has been rendered during `generate`, you can also use [`this.getChunkFileName(chunkReferenceId)`](guide/en/#thisgetchunkfilenamechunkreferenceid-string--string) to determine the file name.
345345

346-
#### `this.error(error: string | Error, position?: number) => void`
346+
#### `this.error(error: string | Error, position?: number | { column: number; line: number }) => never`
347347

348348
Structurally equivalent to `this.warn`, except that it will also abort the bundling process.
349349

@@ -398,7 +398,7 @@ If you pass `skipSelf: true`, then the `resolveId` hook of the plugin from which
398398

399399
Set the deferred source of an asset.
400400

401-
#### `this.warn(warning: string | RollupWarning, position?: number )`
401+
#### `this.warn(warning: string | RollupWarning, position?: number | { column: number; line: number }) => void`
402402

403403
Using this method will queue warnings for a build. These warnings will be printed by the CLI just like internally-generated warnings (except with the plugin name), or captured by custom `onwarn` handlers.
404404

src/rollup/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export interface PluginContext extends MinimalPluginContext {
122122
cache: PluginCache;
123123
emitAsset: EmitAsset;
124124
emitChunk: EmitChunk;
125-
error: (err: RollupError | string, pos?: { column: number; line: number }) => never;
125+
error: (err: RollupError | string, pos?: number | { column: number; line: number }) => never;
126126
getAssetFileName: (assetReferenceId: string) => string;
127127
getChunkFileName: (chunkReferenceId: string) => string;
128128
getModuleInfo: (
@@ -146,7 +146,7 @@ export interface PluginContext extends MinimalPluginContext {
146146
/** @deprecated Use `this.resolve` instead */
147147
resolveId: (source: string, importer: string) => Promise<string | null>;
148148
setAssetSource: (assetReferenceId: string, source: string | Buffer) => void;
149-
warn: (warning: RollupWarning | string, pos?: { column: number; line: number }) => void;
149+
warn: (warning: RollupWarning | string, pos?: number | { column: number; line: number }) => void;
150150
/** @deprecated Use `this.addWatchFile` and the `watchChange` hook instead */
151151
watcher: EventEmitter;
152152
}

src/utils/error.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ export function error(base: Error | RollupError, props?: RollupError): never {
1212

1313
export function augmentCodeLocation(
1414
object: RollupError | RollupWarning,
15-
pos: { column: number; line: number },
15+
pos: number | { column: number; line: number },
1616
source: string,
1717
id: string
1818
): void {
19-
if (pos.line !== undefined && pos.column !== undefined) {
19+
if (typeof pos === 'object') {
2020
const { line, column } = pos;
2121
object.loc = { file: id, line, column };
2222
} else {
23-
object.pos = pos as any;
23+
object.pos = pos;
2424
const { line, column } = locate(source, pos, { offsetLine: 1 });
2525
object.loc = { file: id, line, column };
2626
}

src/utils/transform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,14 @@ export default function transform(
134134
return {
135135
...pluginContext,
136136
cache: trackedPluginCache ? trackedPluginCache.cache : pluginContext.cache,
137-
warn(warning: RollupWarning | string, pos?: { column: number; line: number }) {
137+
warn(warning: RollupWarning | string, pos?: number | { column: number; line: number }) {
138138
if (typeof warning === 'string') warning = { message: warning } as RollupWarning;
139139
if (pos) augmentCodeLocation(warning, pos, curSource, id);
140140
warning.id = id;
141141
warning.hook = 'transform';
142142
pluginContext.warn(warning);
143143
},
144-
error(err: RollupError | string, pos?: { column: number; line: number }): never {
144+
error(err: RollupError | string, pos?: number | { column: number; line: number }): never {
145145
if (typeof err === 'string') err = { message: err };
146146
if (pos) augmentCodeLocation(err, pos, curSource, id);
147147
err.id = id;

0 commit comments

Comments
 (0)