Skip to content

Commit

Permalink
Chore: nullish coalescing and optional chaining (pixijs#8419)
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtimebuddy committed Jun 21, 2022
1 parent 9feac0e commit b841857
Show file tree
Hide file tree
Showing 23 changed files with 46 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/canvas-graphics/src/CanvasGraphicsRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ export class CanvasGraphicsRenderer
{
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');

if (svg && svg.createSVGMatrix)
if (svg?.createSVGMatrix)
{
this._svgMatrix = svg.createSVGMatrix();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-renderer/src/CanvasObjectRendererSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class CanvasObjectRendererSystem implements ISystem
_context._outerBlend = false;
context2D.globalCompositeOperation = _context.blendModes[BLEND_MODES.NORMAL];

if (clear !== undefined ? clear : renderer.background.clearBeforeRender)
if (clear ?? renderer.background.clearBeforeRender)
{
if (this.renderingToScreen)
{
Expand Down
2 changes: 1 addition & 1 deletion packages/canvas-renderer/src/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const parentCreate = Renderer.create;
*/
Renderer.create = function create(options: IRendererOptionsAuto): IRenderer
{
const forceCanvas = options && options.forceCanvas;
const forceCanvas = options?.forceCanvas;

if (!forceCanvas)
{
Expand Down
2 changes: 1 addition & 1 deletion packages/compressed-textures/src/resources/BlobResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export abstract class BlobResource extends BufferResource
{
this.load();
}
if (data && data.length)
if (data?.length)
{
this.loaded = true;
this.onBlobLoaded(this.buffer.rawBinaryData);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/filters/FilterSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ export class FilterSystem implements ISystem
this.renderer.projection.transform = null;
}

if (filterTexture && filterTexture.filterFrame)
if (filterTexture?.filterFrame)
{
const destinationFrame = this.tempRect;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/render/ObjectRendererSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class ObjectRendererSystem implements ISystem
renderer.renderTexture.bind(renderTexture);
renderer.batch.currentRenderer.start();

if (clear !== undefined ? clear : renderer.background.clearBeforeRender)
if (clear ?? renderer.background.clearBeforeRender)
{
renderer.renderTexture.clear();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/renderTexture/BaseRenderTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class BaseRenderTexture extends BaseTexture

options.width = options.width || 100;
options.height = options.height || 100;
options.multisample = options.multisample !== undefined ? options.multisample : MSAA_QUALITY.NONE;
options.multisample ??= MSAA_QUALITY.NONE;

super(null, options);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shader/utils/getTestContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let context: WebGLRenderingContext | WebGL2RenderingContext = unknownContext as
*/
export function getTestContext(): WebGLRenderingContext | WebGL2RenderingContext
{
if (context === unknownContext || (context && context.isContextLost()))
if (context === unknownContext || context?.isContextLost())
{
const canvas = document.createElement('canvas');

Expand Down
12 changes: 6 additions & 6 deletions packages/core/src/textures/BaseTexture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ export class BaseTexture<R extends Resource = Resource, RO = IAutoDetectOptions>
this.resolution = resolution || settings.RESOLUTION;
this.width = Math.round((width || 0) * this.resolution) / this.resolution;
this.height = Math.round((height || 0) * this.resolution) / this.resolution;
this._mipmap = mipmap !== undefined ? mipmap : settings.MIPMAP_TEXTURES;
this.anisotropicLevel = anisotropicLevel !== undefined ? anisotropicLevel : settings.ANISOTROPIC_LEVEL;
this._mipmap = mipmap ?? settings.MIPMAP_TEXTURES;
this.anisotropicLevel = anisotropicLevel ?? settings.ANISOTROPIC_LEVEL;
this._wrapMode = wrapMode || settings.WRAP_MODE;
this._scaleMode = scaleMode !== undefined ? scaleMode : settings.SCALE_MODE;
this._scaleMode = scaleMode ?? settings.SCALE_MODE;
this.format = format || FORMATS.RGBA;
this.type = type || TYPES.UNSIGNED_BYTE;
this.target = target || TARGETS.TEXTURE_2D;
this.alphaMode = alphaMode !== undefined ? alphaMode : ALPHA_MODES.UNPACK;
this.alphaMode = alphaMode ?? ALPHA_MODES.UNPACK;

this.uid = uid();
this.touched = 0;
Expand Down Expand Up @@ -603,7 +603,7 @@ export class BaseTexture<R extends Resource = Resource, RO = IAutoDetectOptions>
{
if (!(source as any)._pixiId)
{
const prefix = (options && options.pixiIdPrefix) || 'pixiid';
const prefix = options?.pixiIdPrefix || 'pixiid';

(source as any)._pixiId = `${prefix}_${uid()}`;
}
Expand Down Expand Up @@ -703,7 +703,7 @@ export class BaseTexture<R extends Resource = Resource, RO = IAutoDetectOptions>
return baseTextureFromCache;
}
}
else if (baseTexture && baseTexture.textureCacheIds)
else if (baseTexture?.textureCacheIds)
{
for (let i = 0; i < baseTexture.textureCacheIds.length; ++i)
{
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/textures/Texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export class Texture<R extends Resource = Resource> extends EventEmitter

// delete the texture if it exists in the texture cache..
// this only needs to be removed if the base texture is actually destroyed too..
if (resource && resource.url && TextureCache[resource.url])
if (resource?.url && TextureCache[resource.url])
{
Texture.removeFromCache(resource.url);
}
Expand Down Expand Up @@ -299,7 +299,7 @@ export class Texture<R extends Resource = Resource> extends EventEmitter
const clonedTexture = new Texture(this.baseTexture,
!this.noFrame && clonedFrame,
clonedOrig,
this.trim && this.trim.clone(),
this.trim?.clone(),
this.rotate,
this.defaultAnchor
);
Expand Down Expand Up @@ -353,7 +353,7 @@ export class Texture<R extends Resource = Resource> extends EventEmitter
{
if (!source.cacheId)
{
const prefix = (options && options.pixiIdPrefix) || 'pixiid';
const prefix = options?.pixiIdPrefix || 'pixiid';

source.cacheId = `${prefix}-${uid()}`;
BaseTexture.addToCache(source, source.cacheId);
Expand All @@ -365,7 +365,7 @@ export class Texture<R extends Resource = Resource> extends EventEmitter
{
if (!(source as any)._pixiId)
{
const prefix = (options && options.pixiIdPrefix) || 'pixiid';
const prefix = options?.pixiIdPrefix || 'pixiid';

(source as any)._pixiId = `${prefix}_${uid()}`;
}
Expand Down Expand Up @@ -551,7 +551,7 @@ export class Texture<R extends Resource = Resource> extends EventEmitter
return textureFromCache;
}
}
else if (texture && texture.textureCacheIds)
else if (texture?.textureCacheIds)
{
for (let i = 0; i < texture.textureCacheIds.length; ++i)
{
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/textures/TextureSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class TextureSystem implements ISystem

// cannot bind partial texture
// TODO: report a warning
if (texture && texture.valid && !texture.parentTextureArray)
if (texture?.valid && !texture.parentTextureArray)
{
texture.touched = this.renderer.textureGC.count;

Expand Down Expand Up @@ -312,7 +312,7 @@ export class TextureSystem implements ISystem

this.initTextureType(texture, glTexture);

if (texture.resource && texture.resource.upload(renderer, texture, glTexture))
if (texture.resource?.upload(renderer, texture, glTexture))
{
// texture is uploaded, dont do anything!
if (glTexture.samplerType !== SAMPLER_TYPES.FLOAT)
Expand Down Expand Up @@ -418,7 +418,7 @@ export class TextureSystem implements ISystem
glTexture.wrapMode = texture.wrapMode;
}

if (texture.resource && texture.resource.style(this.renderer, texture, glTexture))
if (texture.resource?.style(this.renderer, texture, glTexture))
{
// style is set, dont do anything!
}
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/textures/resources/ImageResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ export class ImageResource extends BaseImageResource
this._process = null;

this.preserveBitmap = false;
this.createBitmap = (options.createBitmap !== undefined
? options.createBitmap : settings.CREATE_IMAGE_BITMAP) && !!globalThis.createImageBitmap;
this.createBitmap = (options.createBitmap ?? settings.CREATE_IMAGE_BITMAP) && !!globalThis.createImageBitmap;
this.alphaMode = typeof options.alphaMode === 'number' ? options.alphaMode : null;
this.bitmap = null;

Expand Down
10 changes: 5 additions & 5 deletions packages/display/src/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ export class Container extends DisplayObject
}

// do a quick check to see if this element has a mask or a filter.
if (this._mask || (this.filters && this.filters.length))
if (this._mask || this.filters?.length)
{
this.renderAdvanced(renderer);
}
Expand Down Expand Up @@ -669,7 +669,7 @@ export class Container extends DisplayObject
}
}

const flush = (filters && this._enabledFilters && this._enabledFilters.length)
const flush = (filters && this._enabledFilters?.length)
|| (mask && (!mask.isMaskData
|| (mask.enabled && (mask.autoDetect || mask.type !== MASK_TYPES.NONE))));

Expand All @@ -678,7 +678,7 @@ export class Container extends DisplayObject
renderer.batch.flush();
}

if (filters && this._enabledFilters && this._enabledFilters.length)
if (filters && this._enabledFilters?.length)
{
renderer.filter.push(this, this._enabledFilters);
}
Expand Down Expand Up @@ -712,7 +712,7 @@ export class Container extends DisplayObject
renderer.mask.pop(this);
}

if (filters && this._enabledFilters && this._enabledFilters.length)
if (filters && this._enabledFilters?.length)
{
renderer.filter.pop();
}
Expand Down Expand Up @@ -745,7 +745,7 @@ export class Container extends DisplayObject

this.sortDirty = false;

const destroyChildren = typeof options === 'boolean' ? options : options && options.children;
const destroyChildren = typeof options === 'boolean' ? options : options?.children;

const oldChildren = this.removeChildren(0, this.children.length);

Expand Down
2 changes: 1 addition & 1 deletion packages/graphics/src/Graphics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ export class Graphics extends Container
options = Object.assign({
width: 0,
texture: Texture.WHITE,
color: (options && options.texture) ? 0xFFFFFF : 0x0,
color: options?.texture ? 0xFFFFFF : 0x0,
alpha: 1,
matrix: null,
alignment: 0.5,
Expand Down
6 changes: 3 additions & 3 deletions packages/interaction/src/InteractionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ export class InteractionManager extends EventEmitter
options = options || {};

this.renderer = renderer;
this.autoPreventDefault = options.autoPreventDefault !== undefined ? options.autoPreventDefault : true;
this.autoPreventDefault = options.autoPreventDefault ?? true;
this.interactionFrequency = options.interactionFrequency || 10;
this.mouse = new InteractionData();
this.mouse.identifier = MOUSE_POINTER_ID;
Expand Down Expand Up @@ -651,7 +651,7 @@ export class InteractionManager extends EventEmitter
* @param {PIXI.InteractionEvent} event - Interaction event
*/

this._useSystemTicker = options.useSystemTicker !== undefined ? options.useSystemTicker : true;
this._useSystemTicker = options.useSystemTicker ?? true;

this.setTargetElement(this.renderer.view, this.renderer.resolution);
}
Expand Down Expand Up @@ -1407,7 +1407,7 @@ export class InteractionManager extends EventEmitter
if (isTouch) this.dispatchEvent(displayObject, 'touchendoutside', interactionEvent);
}
// Only remove the tracking data if there is no over/down state still associated with it
if (trackingData && trackingData.none)
if (trackingData?.none)
{
delete displayObject.trackedPointers[id];
}
Expand Down
6 changes: 3 additions & 3 deletions packages/loaders/src/LoaderResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function _noop(): void { /* empty */ }
*/
function setExtMap(map: Dict<any>, extname: string, val: number)
{
if (extname && extname.startsWith('.'))
if (extname?.startsWith('.'))
{
extname = extname.substring(1);
}
Expand Down Expand Up @@ -626,7 +626,7 @@ class LoaderResource
{
clearTimeout(this._elementTimer);

if (this.data && this.data.removeEventListener)
if (this.data?.removeEventListener)
{
this.data.removeEventListener('error', this._boundOnError, false);
this.data.removeEventListener('load', this._boundComplete, false);
Expand Down Expand Up @@ -892,7 +892,7 @@ class LoaderResource
*/
private _onProgress(event: ProgressEvent): void
{
if (event && event.lengthComputable)
if (event?.lengthComputable)
{
this.onProgress.dispatch(this, event.loaded / event.total);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/loaders/src/middleware/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function parsing(resource: LoaderResource, next: (...args: any) => void):
const type = resource.xhr.getResponseHeader('content-type');

// this is an image, convert the binary string into a data url
if (type && type.startsWith('image'))
if (type?.startsWith('image'))
{
resource.data = new Image();
resource.data.src = `data:${type};base64,${encodeBinary(resource.xhr.responseText)}`;
Expand Down
6 changes: 3 additions & 3 deletions packages/mixin-cache-as-bitmap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ DisplayObject.prototype._renderCached = function _renderCached(renderer: Rendere
*/
DisplayObject.prototype._initCachedDisplayObject = function _initCachedDisplayObject(renderer: Renderer): void
{
if (this._cacheData && this._cacheData.sprite)
if (this._cacheData?.sprite)
{
return;
}
Expand All @@ -258,7 +258,7 @@ DisplayObject.prototype._initCachedDisplayObject = function _initCachedDisplayOb
const bounds = (this as Container).getLocalBounds(null, true).clone();

// add some padding!
if (this.filters && this.filters.length)
if (this.filters?.length)
{
const padding = this.filters[0].padding;

Expand Down Expand Up @@ -377,7 +377,7 @@ DisplayObject.prototype._initCachedDisplayObjectCanvas = function _initCachedDis
renderer: CanvasRenderer
): void
{
if (this._cacheData && this._cacheData.sprite)
if (this._cacheData?.sprite)
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/prepare/src/BasePrepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function findMultipleBaseTextures(item: IDisplayObjectExtended, queue: Array<any
let result = false;

// Objects with multiple textures
if (item && item._textures && item._textures.length)
if (item?._textures?.length)
{
for (let i = 0; i < item._textures.length; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions packages/sprite/src/Sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,11 @@ export class Sprite extends Container

this._anchor = null;

const destroyTexture = typeof options === 'boolean' ? options : options && options.texture;
const destroyTexture = typeof options === 'boolean' ? options : options?.texture;

if (destroyTexture)
{
const destroyBaseTexture = typeof options === 'boolean' ? options : options && options.baseTexture;
const destroyBaseTexture = typeof options === 'boolean' ? options : options?.baseTexture;

this._texture.destroy(!!destroyBaseTexture);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/spritesheet/src/Spritesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class Spritesheet
if (resolution === null)
{
// Use the scale value or default to 1
resolution = scale !== undefined ? parseFloat(scale) : 1;
resolution = parseFloat(scale ?? '1');
}

// For non-1 resolutions, update baseTexture
Expand Down
2 changes: 1 addition & 1 deletion packages/text/src/TextMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ export class TextMetrics
const c = new OffscreenCanvas(0, 0);
const context = c.getContext('2d');

if (context && context.measureText)
if (context?.measureText)
{
TextMetrics.__canvas = c;

Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/network/getResolutionOfUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { settings } from '../settings';
* @param {number} [defaultValue=1] - the defaultValue if no filename prefix is set.
* @returns {number} resolution / device pixel ratio of an asset
*/
export function getResolutionOfUrl(url: string, defaultValue?: number): number
export function getResolutionOfUrl(url: string, defaultValue = 1): number
{
const resolution = settings.RETINA_PREFIX.exec(url);

Expand All @@ -18,5 +18,5 @@ export function getResolutionOfUrl(url: string, defaultValue?: number): number
return parseFloat(resolution[1]);
}

return defaultValue !== undefined ? defaultValue : 1;
return defaultValue;
}

0 comments on commit b841857

Please sign in to comment.