From c60d7563a812e1f02707603564a1e0eef801d5b0 Mon Sep 17 00:00:00 2001 From: Danny Skoog Date: Thu, 17 Oct 2019 02:46:50 +0200 Subject: [PATCH] style: enforce disallowance of object constructor (#33211) Applying the `prefer-literal` tslint rule to object enforces the style guide rule https://google.github.io/styleguide/jsguide.html#features-objects-ctor PR Close #33211 --- packages/common/test/pipes/async_pipe_spec.ts | 2 +- .../core/src/di/injector_compatibility.ts | 2 +- packages/core/src/di/reflective_injector.ts | 2 +- packages/core/src/view/ng_module.ts | 2 +- packages/core/src/view/refs.ts | 2 +- .../change_detector_util_spec.ts | 22 +++++++++---------- packages/core/test/view/anchor_spec.ts | 2 +- packages/core/test/view/element_spec.ts | 2 +- packages/core/test/view/embedded_view_spec.ts | 4 ++-- packages/core/test/view/text_spec.ts | 2 +- tools/public_api_guard/core/core.d.ts | 2 +- tslint.json | 4 ++++ 12 files changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/common/test/pipes/async_pipe_spec.ts b/packages/common/test/pipes/async_pipe_spec.ts index fed5236e47..29f945ef9b 100644 --- a/packages/common/test/pipes/async_pipe_spec.ts +++ b/packages/common/test/pipes/async_pipe_spec.ts @@ -114,7 +114,7 @@ import {SpyChangeDetectorRef} from '../spies'; }); describe('Promise', () => { - const message = new Object(); + const message = {}; let pipe: AsyncPipe; let resolve: (result: any) => void; let reject: (error: any) => void; diff --git a/packages/core/src/di/injector_compatibility.ts b/packages/core/src/di/injector_compatibility.ts index e8ddaa7d33..04abc56919 100644 --- a/packages/core/src/di/injector_compatibility.ts +++ b/packages/core/src/di/injector_compatibility.ts @@ -33,7 +33,7 @@ export const INJECTOR = new InjectionToken( -1 as any // `-1` is used by Ivy DI system as special value to recognize it as `Injector`. ); -const _THROW_IF_NOT_FOUND = new Object(); +const _THROW_IF_NOT_FOUND = {}; export const THROW_IF_NOT_FOUND = _THROW_IF_NOT_FOUND; export const NG_TEMP_TOKEN_PATH = 'ngTempTokenPath'; diff --git a/packages/core/src/di/reflective_injector.ts b/packages/core/src/di/reflective_injector.ts index ee7aaab217..6b979b8f4d 100644 --- a/packages/core/src/di/reflective_injector.ts +++ b/packages/core/src/di/reflective_injector.ts @@ -16,7 +16,7 @@ import {ReflectiveDependency, ResolvedReflectiveFactory, ResolvedReflectiveProvi // Threshold for the dynamic version -const UNDEFINED = new Object(); +const UNDEFINED = {}; /** * A ReflectiveDependency injection container used for instantiating objects and resolving diff --git a/packages/core/src/view/ng_module.ts b/packages/core/src/view/ng_module.ts index e0d5d17458..4700396465 100644 --- a/packages/core/src/view/ng_module.ts +++ b/packages/core/src/view/ng_module.ts @@ -18,7 +18,7 @@ import {stringify} from '../util/stringify'; import {DepDef, DepFlags, NgModuleData, NgModuleDefinition, NgModuleProviderDef, NodeFlags} from './types'; import {splitDepsDsl, tokenKey} from './util'; -const UNDEFINED_VALUE = new Object(); +const UNDEFINED_VALUE = {}; const InjectorRefTokenKey = tokenKey(Injector); const INJECTORRefTokenKey = tokenKey(INJECTOR); diff --git a/packages/core/src/view/refs.ts b/packages/core/src/view/refs.ts index 198f56df50..4fe9a5d731 100644 --- a/packages/core/src/view/refs.ts +++ b/packages/core/src/view/refs.ts @@ -26,7 +26,7 @@ import {DepFlags, ElementData, NgModuleData, NgModuleDefinition, NodeDef, NodeFl import {markParentViewsForCheck, resolveDefinition, rootRenderNodes, splitNamespace, tokenKey, viewParentEl} from './util'; import {attachEmbeddedView, detachEmbeddedView, moveEmbeddedView, renderDetachView} from './view_attach'; -const EMPTY_CONTEXT = new Object(); +const EMPTY_CONTEXT = {}; // Attention: this function is called as top level function. // Putting any logic in here will destroy closure tree shaking! diff --git a/packages/core/test/change_detection/change_detector_util_spec.ts b/packages/core/test/change_detection/change_detector_util_spec.ts index 8d8cbb7786..f6a58a1797 100644 --- a/packages/core/test/change_detection/change_detector_util_spec.ts +++ b/packages/core/test/change_detection/change_detector_util_spec.ts @@ -16,41 +16,41 @@ import {devModeEqual} from '@angular/core/src/change_detection/change_detection_ expect(devModeEqual(['one'], ['one', 'two'])).toBe(false); expect(devModeEqual(['one', 'two'], ['one'])).toBe(false); expect(devModeEqual(['one'], 'one')).toBe(false); - expect(devModeEqual(['one'], new Object())).toBe(false); + expect(devModeEqual(['one'], {})).toBe(false); expect(devModeEqual('one', ['one'])).toBe(false); - expect(devModeEqual(new Object(), ['one'])).toBe(false); + expect(devModeEqual({}, ['one'])).toBe(false); }); it('should compare primitive numbers', () => { expect(devModeEqual(1, 1)).toBe(true); expect(devModeEqual(1, 2)).toBe(false); - expect(devModeEqual(new Object(), 2)).toBe(false); - expect(devModeEqual(1, new Object())).toBe(false); + expect(devModeEqual({}, 2)).toBe(false); + expect(devModeEqual(1, {})).toBe(false); }); it('should compare primitive strings', () => { expect(devModeEqual('one', 'one')).toBe(true); expect(devModeEqual('one', 'two')).toBe(false); - expect(devModeEqual(new Object(), 'one')).toBe(false); - expect(devModeEqual('one', new Object())).toBe(false); + expect(devModeEqual({}, 'one')).toBe(false); + expect(devModeEqual('one', {})).toBe(false); }); it('should compare primitive booleans', () => { expect(devModeEqual(true, true)).toBe(true); expect(devModeEqual(true, false)).toBe(false); - expect(devModeEqual(new Object(), true)).toBe(false); - expect(devModeEqual(true, new Object())).toBe(false); + expect(devModeEqual({}, true)).toBe(false); + expect(devModeEqual(true, {})).toBe(false); }); it('should compare null', () => { expect(devModeEqual(null, null)).toBe(true); expect(devModeEqual(null, 1)).toBe(false); - expect(devModeEqual(new Object(), null)).toBe(false); - expect(devModeEqual(null, new Object())).toBe(false); + expect(devModeEqual({}, null)).toBe(false); + expect(devModeEqual(null, {})).toBe(false); }); it('should return true for other objects', - () => { expect(devModeEqual(new Object(), new Object())).toBe(true); }); + () => { expect(devModeEqual({}, {})).toBe(true); }); }); }); } diff --git a/packages/core/test/view/anchor_spec.ts b/packages/core/test/view/anchor_spec.ts index 3b3c93a496..d19a5a3d3f 100644 --- a/packages/core/test/view/anchor_spec.ts +++ b/packages/core/test/view/anchor_spec.ts @@ -40,7 +40,7 @@ import {compViewDef, createAndGetRootNodes} from './helper'; }); it('should add debug information to the renderer', () => { - const someContext = new Object(); + const someContext = {}; const {view, rootNodes} = createAndGetRootNodes( compViewDef([anchorDef(NodeFlags.None, null, null, 0)]), someContext); expect(getDebugNode(rootNodes[0]) !.nativeNode).toBe(asElementData(view, 0).renderElement); diff --git a/packages/core/test/view/element_spec.ts b/packages/core/test/view/element_spec.ts index d7d95b0cf1..967cf83884 100644 --- a/packages/core/test/view/element_spec.ts +++ b/packages/core/test/view/element_spec.ts @@ -62,7 +62,7 @@ const removeEventListener = 'removeEventListener'; }); it('should add debug information to the renderer', () => { - const someContext = new Object(); + const someContext = {}; const {view, rootNodes} = createAndGetRootNodes( compViewDef([elementDef(0, NodeFlags.None, null, null, 0, 'div')]), someContext); expect(getDebugNode(rootNodes[0]) !.nativeNode).toBe(asElementData(view, 0).renderElement); diff --git a/packages/core/test/view/embedded_view_spec.ts b/packages/core/test/view/embedded_view_spec.ts index 3b9cbcd2f4..f327981582 100644 --- a/packages/core/test/view/embedded_view_spec.ts +++ b/packages/core/test/view/embedded_view_spec.ts @@ -16,8 +16,8 @@ import {compViewDef, compViewDefFactory, createAndGetRootNodes, createEmbeddedVi describe(`Embedded Views`, () => { it('should create embedded views with the right context', () => { - const parentContext = new Object(); - const childContext = new Object(); + const parentContext = {}; + const childContext = {}; const {view: parentView} = createAndGetRootNodes( compViewDef([ diff --git a/packages/core/test/view/text_spec.ts b/packages/core/test/view/text_spec.ts index e2f41d5ea6..ad169872e1 100644 --- a/packages/core/test/view/text_spec.ts +++ b/packages/core/test/view/text_spec.ts @@ -41,7 +41,7 @@ import {ARG_TYPE_VALUES, checkNodeInlineOrDynamic, compViewDef, createAndGetRoot }); it('should add debug information to the renderer', () => { - const someContext = new Object(); + const someContext = {}; const {view, rootNodes} = createAndGetRootNodes(compViewDef([textDef(0, null, ['a'])]), someContext); expect(getDebugNode(rootNodes[0]) !.nativeNode).toBe(asTextData(view, 0).renderText); diff --git a/tools/public_api_guard/core/core.d.ts b/tools/public_api_guard/core/core.d.ts index f5b9fd870b..b31ed025c7 100644 --- a/tools/public_api_guard/core/core.d.ts +++ b/tools/public_api_guard/core/core.d.ts @@ -459,7 +459,7 @@ export declare abstract class Injector { abstract get(token: Type | InjectionToken | AbstractType, notFoundValue?: T, flags?: InjectFlags): T; /** @deprecated */ abstract get(token: any, notFoundValue?: any): any; static NULL: Injector; - static THROW_IF_NOT_FOUND: Object; + static THROW_IF_NOT_FOUND: {}; static ɵprov: never; /** @deprecated */ static create(providers: StaticProvider[], parent?: Injector): Injector; static create(options: { diff --git a/tslint.json b/tslint.json index 6a87bfb3c8..a8c2deb355 100644 --- a/tslint.json +++ b/tslint.json @@ -19,6 +19,10 @@ "no-duplicate-variable": true, "no-jasmine-focus": true, "no-var-keyword": true, + "prefer-literal": [ + true, + "object" + ], "require-internal-with-underscore": true, "no-toplevel-property-access": [ true,