Skip to content

Commit

Permalink
fix(core): remove duplicated EMPTY_ARRAY constant (#40991)
Browse files Browse the repository at this point in the history
The codebase currently contains several `EMPTY_ARRAY` constants,
and they can end up in the bundle of an application.
A recent commit 6fbe219 tipped us off
as it introduced several `noop` occurrences in the golden symbol files.
After investigating with @petebacondarwin,
we decided to remove the duplicated symbols.

This probably shaves only a few bytes,
but this commit removes the duplicated functions,
by always using the one in `core/src/utils/empty`.

PR Close #40991
  • Loading branch information
cexbrayat authored and zarend committed Mar 1, 2021
1 parent be8893f commit e12d9de
Show file tree
Hide file tree
Showing 14 changed files with 13 additions and 27 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/di/r3_injector.ts
Expand Up @@ -13,6 +13,7 @@ import {AbstractType, Type} from '../interface/type';
import {FactoryFn, getFactoryDef} from '../render3/definition_factory';
import {throwCyclicDependencyError, throwInvalidProviderError, throwMixedMultiProviderError} from '../render3/errors_di';
import {deepForEach, newArray} from '../util/array_utils';
import {EMPTY_ARRAY} from '../util/empty';
import {stringify} from '../util/stringify';

import {resolveForwardRef} from './forward_ref';
Expand Down Expand Up @@ -48,8 +49,6 @@ const NOT_YET = {};
*/
const CIRCULAR = {};

const EMPTY_ARRAY = [] as any[];

/**
* A lazily initialized NullInjector.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/di/util.ts
Expand Up @@ -8,6 +8,7 @@

import {Type} from '../interface/type';
import {ReflectionCapabilities} from '../reflection/reflection_capabilities';
import {EMPTY_ARRAY} from '../util/empty';
import {getClosureSafeProperty} from '../util/property';

import {resolveForwardRef} from './forward_ref';
Expand All @@ -16,7 +17,6 @@ import {ClassSansProvider, ConstructorSansProvider, ExistingSansProvider, Factor

const USE_VALUE =
getClosureSafeProperty<ValueProvider>({provide: String, useValue: getClosureSafeProperty});
const EMPTY_ARRAY: any[] = [];

export function convertInjectableProviderToFactory(
type: Type<any>,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/render3/context_discovery.ts
Expand Up @@ -9,7 +9,7 @@ import '../util/ng_dev_mode';

import {assertDomNode} from '../util/assert';

import {EMPTY_ARRAY} from './empty';
import {EMPTY_ARRAY} from '../util/empty';
import {LContext, MONKEY_PATCH_KEY_NAME} from './interfaces/context';
import {TNode, TNodeFlags} from './interfaces/node';
import {RElement, RNode} from './interfaces/renderer_dom';
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/render3/definition.ts
Expand Up @@ -12,9 +12,10 @@ import {NgModuleDef, NgModuleType} from '../metadata/ng_module_def';
import {SchemaMetadata} from '../metadata/schema';
import {ViewEncapsulation} from '../metadata/view';
import {noSideEffects} from '../util/closure';
import {EMPTY_ARRAY} from '../util/empty';
import {initNgDevMode} from '../util/ng_dev_mode';
import {stringify} from '../util/stringify';
import {EMPTY_ARRAY, EMPTY_OBJ} from './empty';
import {EMPTY_OBJ} from './empty';
import {NG_COMP_DEF, NG_DIR_DEF, NG_LOC_ID_DEF, NG_MOD_DEF, NG_PIPE_DEF} from './fields';
import {ComponentDef, ComponentDefFeature, ComponentTemplate, ComponentType, ContentQueriesFunction, DirectiveDef, DirectiveDefFeature, DirectiveTypesOrFactory, HostBindingsFunction, PipeDef, PipeTypesOrFactory, ViewQueriesFunction} from './interfaces/definition';
import {AttributeMarker, TAttributes, TConstantsOrFactory} from './interfaces/node';
Expand Down
3 changes: 0 additions & 3 deletions packages/core/src/render3/empty.ts
Expand Up @@ -15,14 +15,11 @@ import {initNgDevMode} from '../util/ng_dev_mode';
*/

export const EMPTY_OBJ: {} = {};
export const EMPTY_ARRAY: any[] = [];

// freezing the values prevents any code from accidentally inserting new values in
if ((typeof ngDevMode === 'undefined' || ngDevMode) && initNgDevMode()) {
// These property accesses can be ignored because ngDevMode will be set to false
// when optimizing code and the whole if statement will be dropped.
// tslint:disable-next-line:no-toplevel-property-access
Object.freeze(EMPTY_OBJ);
// tslint:disable-next-line:no-toplevel-property-access
Object.freeze(EMPTY_ARRAY);
}
Expand Up @@ -7,8 +7,9 @@
*/

import {Type, Writable} from '../../interface/type';
import {EMPTY_ARRAY} from '../../util/empty';
import {fillProperties} from '../../util/property';
import {EMPTY_ARRAY, EMPTY_OBJ} from '../empty';
import {EMPTY_OBJ} from '../empty';
import {ComponentDef, ContentQueriesFunction, DirectiveDef, DirectiveDefFeature, HostBindingsFunction, RenderFlags, ViewQueriesFunction} from '../interfaces/definition';
import {TAttributes} from '../interfaces/node';
import {isComponentDef} from '../interfaces/type_checks';
Expand Down
Expand Up @@ -7,8 +7,8 @@
*/

import {assertDomNode, assertNumber, assertNumberInRange} from '../../util/assert';
import {EMPTY_ARRAY} from '../../util/empty';
import {assertTIcu, assertTNodeForLView} from '../assert';
import {EMPTY_ARRAY} from '../empty';
import {getCurrentICUCaseIndex} from '../i18n/i18n_util';
import {I18nRemoveOpCodes, TIcu} from '../interfaces/i18n';
import {TIcuContainerNode} from '../interfaces/node';
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/render3/jit/directive.ts
Expand Up @@ -15,9 +15,10 @@ import {Query} from '../../metadata/di';
import {Component, Directive, Input} from '../../metadata/directives';
import {componentNeedsResolution, maybeQueueResolutionOfComponentResources} from '../../metadata/resource_loading';
import {ViewEncapsulation} from '../../metadata/view';
import {EMPTY_ARRAY} from '../../util/empty';
import {initNgDevMode} from '../../util/ng_dev_mode';
import {getComponentDef, getDirectiveDef} from '../definition';
import {EMPTY_ARRAY, EMPTY_OBJ} from '../empty';
import {EMPTY_OBJ} from '../empty';
import {NG_COMP_DEF, NG_DIR_DEF, NG_FACTORY_DEF} from '../fields';
import {ComponentType} from '../interfaces/definition';
import {stringifyForError} from '../util/stringify_utils';
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/render3/jit/module.ts
Expand Up @@ -16,6 +16,7 @@ import {ModuleWithProviders, NgModule} from '../../metadata/ng_module';
import {NgModuleDef, NgModuleTransitiveScopes, NgModuleType} from '../../metadata/ng_module_def';
import {deepForEach, flatten} from '../../util/array_utils';
import {assertDefined} from '../../util/assert';
import {EMPTY_ARRAY} from '../../util/empty';
import {getComponentDef, getDirectiveDef, getNgModuleDef, getPipeDef} from '../definition';
import {NG_COMP_DEF, NG_DIR_DEF, NG_MOD_DEF, NG_PIPE_DEF} from '../fields';
import {ComponentDef} from '../interfaces/definition';
Expand All @@ -24,8 +25,6 @@ import {stringifyForError} from '../util/stringify_utils';

import {angularCoreEnv} from './environment';

const EMPTY_ARRAY: Type<any>[] = [];

interface ModuleQueueItem {
moduleType: Type<any>;
ngModule: NgModule;
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/view/index.ts
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

export {EMPTY_ARRAY} from '../util/empty';
export {anchorDef, elementDef} from './element';
export {clearOverrides, createNgModuleFactory, overrideComponentView, overrideProvider} from './entrypoint';
export {ngContentDef} from './ng_content';
Expand All @@ -16,7 +17,7 @@ export {queryDef} from './query';
export {createComponentFactory, getComponentViewDefinitionFactory, nodeValue, ViewRef_} from './refs';
export {initServicesIfNeeded} from './services';
export {textDef} from './text';
export {createRendererType2, elementEventFullName, EMPTY_ARRAY, EMPTY_MAP, inlineInterpolate, interpolate, rootRenderNodes, tokenKey, unwrapValue} from './util';
export {createRendererType2, elementEventFullName, EMPTY_MAP, inlineInterpolate, interpolate, rootRenderNodes, tokenKey, unwrapValue} from './util';
export {viewDef} from './view';
export {attachEmbeddedView, detachEmbeddedView, moveEmbeddedView} from './view_attach';

Expand Down
1 change: 0 additions & 1 deletion packages/core/src/view/util.ts
Expand Up @@ -444,5 +444,4 @@ function _toStringWithNull(v: any): string {
return v != null ? v.toString() : '';
}

export const EMPTY_ARRAY: any[] = [];
export const EMPTY_MAP: {[key: string]: any} = {};
6 changes: 0 additions & 6 deletions packages/core/test/bundling/forms/bundle.golden_symbols.json
Expand Up @@ -173,12 +173,6 @@
{
"name": "EMPTY_ARRAY"
},
{
"name": "EMPTY_ARRAY"
},
{
"name": "EMPTY_ARRAY"
},
{
"name": "EMPTY_OBJ"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/core/test/bundling/router/bundle.golden_symbols.json
Expand Up @@ -236,9 +236,6 @@
{
"name": "EMPTY_ARRAY"
},
{
"name": "EMPTY_ARRAY"
},
{
"name": "EMPTY_OBJ"
},
Expand Down
3 changes: 0 additions & 3 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Expand Up @@ -14,9 +14,6 @@
{
"name": "EMPTY_ARRAY"
},
{
"name": "EMPTY_ARRAY"
},
{
"name": "EMPTY_OBJ"
},
Expand Down

0 comments on commit e12d9de

Please sign in to comment.