From 0e3a2e2cf8afdbf9a3302959f2cc4cb3d1262297 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20B=C5=82a=C5=BCejewicz=20=28Peter=20Blazejewicz=29?= Date: Sun, 22 Mar 2020 21:47:38 +0100 Subject: [PATCH] feat(pngjs): update 'this' scope binding in events (#43299) - modify core events defintiioin to handle 'this' scope - test updated for non-arrow syntax usage with events scoped to PNG https://github.com/lukeapage/pngjs#async-api Thanks! --- types/pngjs/index.d.ts | 11 ++++++----- types/pngjs/pngjs-tests.ts | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/types/pngjs/index.d.ts b/types/pngjs/index.d.ts index a2f1832d88..b41451f084 100644 --- a/types/pngjs/index.d.ts +++ b/types/pngjs/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/lukeapage/pngjs // Definitions by: Jason Cheatham // Florian Keller +// Piotr Błażejewicz // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// @@ -47,11 +48,11 @@ export class PNG extends Duplex { deltaY?: number, ): PNG; - on(event: 'metadata', callback: (metadata: Metadata) => void): this; - on(event: 'parsed', callback: (data: Buffer) => void): this; - on(event: 'error', callback: (error: Error) => void): this; - on(event: 'close', callback: () => void): this; - on(event: string, callback: (...args: any[]) => void): this; + on(event: 'metadata', callback: (this: PNG, metadata: Metadata) => void): this; + on(event: 'parsed', callback: (this: PNG, data: Buffer) => void): this; + on(event: 'error', callback: (this: PNG, error: Error) => void): this; + on(event: 'close', callback: (this: PNG) => void): this; + on(event: string, callback: (this: PNG, ...args: any[]) => void): this; pack(): PNG; diff --git a/types/pngjs/pngjs-tests.ts b/types/pngjs/pngjs-tests.ts index bfa6b198a8..0c1d3b2f95 100644 --- a/types/pngjs/pngjs-tests.ts +++ b/types/pngjs/pngjs-tests.ts @@ -1,5 +1,6 @@ import { PNG } from 'pngjs'; import { createDeflate } from 'zlib'; +import fs = require('fs'); const pngs = [ new PNG(), @@ -45,16 +46,35 @@ png.bitblt(pngs[1], 1, 1, 1, 1, 1, 1); png.on('metadata', metadata => { metadata.bpp === 1; }); +png.on('metadata', function(metadata) { + this; // $ExpectType PNG + this.width === metadata.width; + this.height === metadata.height; +}); png.on('parsed', data => { data.byteLength === 1; }); +png.on('parsed', function(data) { + this; // $ExpectType PNG + this.adjustGamma(); + this.pack().pipe(fs.createWriteStream('out.png')); +}); png.on('error', error => { error === new Error('testing'); }); +png.on('error', function(error) { + this; // $ExpectType PNG +}); png.on('closed', () => { // closed }); +png.on('closed', function() { + this; // $ExpectType PNG +}); png.on('foo', () => {}); +png.on('foo', function() { + this; // $ExpectType PNG +}); png.pack().adjustGamma();