Skip to content

Commit

Permalink
Align TS types, docs and implementation for this.warn and this.error (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jul 2, 2019
1 parent 21b7ce4 commit 5682ae9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/05-plugin-development.md
Expand Up @@ -343,7 +343,7 @@ Emitted chunks will follow the [`output.chunkFileNames`](guide/en/#outputchunkfi

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.

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

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

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

Set the deferred source of an asset.

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

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.

Expand Down
4 changes: 2 additions & 2 deletions src/rollup/types.d.ts
Expand Up @@ -122,7 +122,7 @@ export interface PluginContext extends MinimalPluginContext {
cache: PluginCache;
emitAsset: EmitAsset;
emitChunk: EmitChunk;
error: (err: RollupError | string, pos?: { column: number; line: number }) => never;
error: (err: RollupError | string, pos?: number | { column: number; line: number }) => never;
getAssetFileName: (assetReferenceId: string) => string;
getChunkFileName: (chunkReferenceId: string) => string;
getModuleInfo: (
Expand All @@ -146,7 +146,7 @@ export interface PluginContext extends MinimalPluginContext {
/** @deprecated Use `this.resolve` instead */
resolveId: (source: string, importer: string) => Promise<string | null>;
setAssetSource: (assetReferenceId: string, source: string | Buffer) => void;
warn: (warning: RollupWarning | string, pos?: { column: number; line: number }) => void;
warn: (warning: RollupWarning | string, pos?: number | { column: number; line: number }) => void;
/** @deprecated Use `this.addWatchFile` and the `watchChange` hook instead */
watcher: EventEmitter;
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/error.ts
Expand Up @@ -12,15 +12,15 @@ export function error(base: Error | RollupError, props?: RollupError): never {

export function augmentCodeLocation(
object: RollupError | RollupWarning,
pos: { column: number; line: number },
pos: number | { column: number; line: number },
source: string,
id: string
): void {
if (pos.line !== undefined && pos.column !== undefined) {
if (typeof pos === 'object') {
const { line, column } = pos;
object.loc = { file: id, line, column };
} else {
object.pos = pos as any;
object.pos = pos;
const { line, column } = locate(source, pos, { offsetLine: 1 });
object.loc = { file: id, line, column };
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/transform.ts
Expand Up @@ -134,14 +134,14 @@ export default function transform(
return {
...pluginContext,
cache: trackedPluginCache ? trackedPluginCache.cache : pluginContext.cache,
warn(warning: RollupWarning | string, pos?: { column: number; line: number }) {
warn(warning: RollupWarning | string, pos?: number | { column: number; line: number }) {
if (typeof warning === 'string') warning = { message: warning } as RollupWarning;
if (pos) augmentCodeLocation(warning, pos, curSource, id);
warning.id = id;
warning.hook = 'transform';
pluginContext.warn(warning);
},
error(err: RollupError | string, pos?: { column: number; line: number }): never {
error(err: RollupError | string, pos?: number | { column: number; line: number }): never {
if (typeof err === 'string') err = { message: err };
if (pos) augmentCodeLocation(err, pos, curSource, id);
err.id = id;
Expand Down

0 comments on commit 5682ae9

Please sign in to comment.