From 071ee64d914e9272b8495563f3ad14f70bf12691 Mon Sep 17 00:00:00 2001 From: Jason Aden Date: Wed, 24 Apr 2019 10:21:19 -0700 Subject: [PATCH] refactor(common): rename LocationUpgradeService and remove angular/upgrade dependency (#30055) PR Close #30055 --- packages/common/upgrade/package.json | 5 +- .../src/{$location.ts => $location_shim.ts} | 6 +- .../common/upgrade/src/angular_js_module.ts | 25 ------- packages/common/upgrade/src/index.ts | 3 +- .../upgrade/src/location_upgrade_module.ts | 9 +-- packages/common/upgrade/test/upgrade.spec.ts | 29 ++++---- .../test/upgrade_location_test_module.ts | 8 +-- packages/common/upgrade/tsconfig-build.json | 3 - tools/public_api_guard/common/upgrade.d.ts | 72 +++++++++---------- 9 files changed, 60 insertions(+), 100 deletions(-) rename packages/common/upgrade/src/{$location.ts => $location_shim.ts} (99%) delete mode 100644 packages/common/upgrade/src/angular_js_module.ts diff --git a/packages/common/upgrade/package.json b/packages/common/upgrade/package.json index 1321a88448..769ba43d5d 100644 --- a/packages/common/upgrade/package.json +++ b/packages/common/upgrade/package.json @@ -8,8 +8,5 @@ "esm2015": "../esm2015/upgrade/upgrade.js", "fesm5": "../fesm5/upgrade.js", "fesm2015": "../fesm2015/upgrade.js", - "sideEffects": false, - "peerDependencies": { - "@angular/upgrade": "0.0.0-PLACEHOLDER" - } + "sideEffects": false } \ No newline at end of file diff --git a/packages/common/upgrade/src/$location.ts b/packages/common/upgrade/src/$location_shim.ts similarity index 99% rename from packages/common/upgrade/src/$location.ts rename to packages/common/upgrade/src/$location_shim.ts index a21b0c5977..49eb8d4c4b 100644 --- a/packages/common/upgrade/src/$location.ts +++ b/packages/common/upgrade/src/$location_shim.ts @@ -26,7 +26,7 @@ const DEFAULT_PORTS: {[key: string]: number} = { * * @publicApi */ -export class LocationUpgradeService { +export class $locationShim { private initalizing = true; private updateBrowser = false; private $$absUrl: string = ''; @@ -664,14 +664,14 @@ export class LocationUpgradeService { * * @publicApi */ -export class LocationUpgradeProvider { +export class $locationShimProvider { constructor( private ngUpgrade: UpgradeModule, private location: Location, private platformLocation: PlatformLocation, private urlCodec: UrlCodec, private locationStrategy: LocationStrategy) {} $get() { - return new LocationUpgradeService( + return new $locationShim( this.ngUpgrade.$injector, this.location, this.platformLocation, this.urlCodec, this.locationStrategy); } diff --git a/packages/common/upgrade/src/angular_js_module.ts b/packages/common/upgrade/src/angular_js_module.ts deleted file mode 100644 index e5ef85bd14..0000000000 --- a/packages/common/upgrade/src/angular_js_module.ts +++ /dev/null @@ -1,25 +0,0 @@ -/** - * @license - * Copyright Google Inc. All Rights Reserved. - * - * Use of this source code is governed by an MIT-style license that can be - * found in the LICENSE file at https://angular.io/license - */ - -import {downgradeInjectable} from '@angular/upgrade/static'; -import {LocationUpgradeProvider} from './$location'; - -/** - * Name of AngularJS module under which $location upgrade services are exported. - * - * @publicApi - */ -export const LOCATION_UPGRADE_MODULE = 'LOCATION_UPGRADE_MODULE'; - -/** - * Downgraded $location provider. API should match AngularJS $location and should be a drop-in - * replacement. - * - * @publicApi - */ -export const $locationProvider = downgradeInjectable(LocationUpgradeProvider); diff --git a/packages/common/upgrade/src/index.ts b/packages/common/upgrade/src/index.ts index b82f517449..0ed428fdf4 100644 --- a/packages/common/upgrade/src/index.ts +++ b/packages/common/upgrade/src/index.ts @@ -7,6 +7,5 @@ */ export * from './location_upgrade_module'; -export * from './angular_js_module'; -export * from './$location'; +export * from './$location_shim'; export * from './params'; \ No newline at end of file diff --git a/packages/common/upgrade/src/location_upgrade_module.ts b/packages/common/upgrade/src/location_upgrade_module.ts index 4a6b877743..ae8bd84ec4 100644 --- a/packages/common/upgrade/src/location_upgrade_module.ts +++ b/packages/common/upgrade/src/location_upgrade_module.ts @@ -10,9 +10,10 @@ import {APP_BASE_HREF, CommonModule, HashLocationStrategy, Location, LocationStr import {Inject, InjectionToken, ModuleWithProviders, NgModule, Optional} from '@angular/core'; import {UpgradeModule} from '@angular/upgrade/static'; -import {LocationUpgradeProvider, LocationUpgradeService} from './$location'; +import {$locationShim, $locationShimProvider} from './$location_shim'; import {AngularJSUrlCodec, UrlCodec} from './params'; + /** * Configuration options for LocationUpgrade. * @@ -49,7 +50,7 @@ export class LocationUpgradeModule { providers: [ Location, { - provide: LocationUpgradeService, + provide: $locationShim, useFactory: provide$location, deps: [UpgradeModule, Location, PlatformLocation, UrlCodec, LocationStrategy] }, @@ -101,8 +102,8 @@ export function provideLocationStrategy( export function provide$location( ngUpgrade: UpgradeModule, location: Location, platformLocation: PlatformLocation, urlCodec: UrlCodec, locationStrategy: LocationStrategy) { - const $locationProvider = new LocationUpgradeProvider( - ngUpgrade, location, platformLocation, urlCodec, locationStrategy); + const $locationProvider = + new $locationShimProvider(ngUpgrade, location, platformLocation, urlCodec, locationStrategy); return $locationProvider.$get(); } \ No newline at end of file diff --git a/packages/common/upgrade/test/upgrade.spec.ts b/packages/common/upgrade/test/upgrade.spec.ts index 0df73c9519..2851a7d9d5 100644 --- a/packages/common/upgrade/test/upgrade.spec.ts +++ b/packages/common/upgrade/test/upgrade.spec.ts @@ -10,7 +10,7 @@ import {CommonModule, PathLocationStrategy} from '@angular/common'; import {TestBed, inject} from '@angular/core/testing'; import {UpgradeModule} from '@angular/upgrade/static'; -import {LocationUpgradeService} from '../src/$location'; +import {$locationShim} from '../src/$location_shim'; import {LocationUpgradeTestModule} from './upgrade_location_test_module'; @@ -79,17 +79,16 @@ describe('LocationProvider', () => { upgradeModule.$injector = {get: injectorFactory()}; }); - it('should instantiate LocationProvider', - inject([LocationUpgradeService], ($location: LocationUpgradeService) => { + it('should instantiate LocationProvider', inject([$locationShim], ($location: $locationShim) => { expect($location).toBeDefined(); - expect($location instanceof LocationUpgradeService).toBe(true); + expect($location instanceof $locationShim).toBe(true); })); }); describe('LocationHtml5Url', function() { - let $location: LocationUpgradeService; + let $location: $locationShim; let upgradeModule: UpgradeModule; beforeEach(() => { @@ -106,8 +105,7 @@ describe('LocationHtml5Url', function() { upgradeModule.$injector = {get: injectorFactory()}; }); - beforeEach( - inject([LocationUpgradeService], (loc: LocationUpgradeService) => { $location = loc; })); + beforeEach(inject([$locationShim], (loc: $locationShim) => { $location = loc; })); it('should set the URL', () => { @@ -166,7 +164,7 @@ describe('LocationHtml5Url', function() { describe('NewUrl', function() { - let $location: LocationUpgradeService; + let $location: $locationShim; let upgradeModule: UpgradeModule; beforeEach(() => { @@ -182,8 +180,7 @@ describe('NewUrl', function() { upgradeModule.$injector = {get: injectorFactory()}; }); - beforeEach( - inject([LocationUpgradeService], (loc: LocationUpgradeService) => { $location = loc; })); + beforeEach(inject([$locationShim], (loc: $locationShim) => { $location = loc; })); // Sets the default most of these tests rely on function setupUrl(url = '/path/b?search=a&b=c&d#hash') { $location.url(url); } @@ -472,7 +469,7 @@ describe('NewUrl', function() { }); describe('New URL Parsing', () => { - let $location: LocationUpgradeService; + let $location: $locationShim; let upgradeModule: UpgradeModule; beforeEach(() => { @@ -489,8 +486,7 @@ describe('New URL Parsing', () => { upgradeModule.$injector = {get: injectorFactory()}; }); - beforeEach( - inject([LocationUpgradeService], (loc: LocationUpgradeService) => { $location = loc; })); + beforeEach(inject([$locationShim], (loc: $locationShim) => { $location = loc; })); it('should prepend path with basePath', function() { $location.$$parse('http://server/base/abc?a'); @@ -504,7 +500,7 @@ describe('New URL Parsing', () => { }); describe('New URL Parsing', () => { - let $location: LocationUpgradeService; + let $location: $locationShim; let upgradeModule: UpgradeModule; beforeEach(() => { @@ -520,8 +516,7 @@ describe('New URL Parsing', () => { upgradeModule.$injector = {get: injectorFactory()}; }); - beforeEach( - inject([LocationUpgradeService], (loc: LocationUpgradeService) => { $location = loc; })); + beforeEach(inject([$locationShim], (loc: $locationShim) => { $location = loc; })); it('should parse new url', function() { $location.$$parse('http://host.com/base'); @@ -629,7 +624,7 @@ describe('New URL Parsing', () => { }); }); -function parseLinkAndReturn(location: LocationUpgradeService, toUrl: string, relHref?: string) { +function parseLinkAndReturn(location: $locationShim, toUrl: string, relHref?: string) { const resetUrl = location.$$parseLinkUrl(toUrl, relHref); return resetUrl && location.absUrl() || undefined; } diff --git a/packages/common/upgrade/test/upgrade_location_test_module.ts b/packages/common/upgrade/test/upgrade_location_test_module.ts index 14c52c1116..10c982e1eb 100644 --- a/packages/common/upgrade/test/upgrade_location_test_module.ts +++ b/packages/common/upgrade/test/upgrade_location_test_module.ts @@ -11,7 +11,7 @@ import {MockPlatformLocation} from '@angular/common/testing'; import {Inject, InjectionToken, ModuleWithProviders, NgModule, Optional} from '@angular/core'; import {UpgradeModule} from '@angular/upgrade/static'; -import {LocationUpgradeProvider, LocationUpgradeService} from '../src/$location'; +import {$locationShim, $locationShimProvider} from '../src/$location_shim'; import {LocationUpgradeModule} from '../src/location_upgrade_module'; import {UrlCodec} from '../src/params'; @@ -60,7 +60,7 @@ export class LocationUpgradeTestModule { deps: [[new Inject(APP_BASE_HREF), new Optional()]] }, { - provide: LocationUpgradeService, + provide: $locationShim, useFactory: provide$location, deps: [ UpgradeModule, Location, PlatformLocation, UrlCodec, LocationStrategy, @@ -81,8 +81,8 @@ export class LocationUpgradeTestModule { export function provide$location( ngUpgrade: UpgradeModule, location: Location, platformLocation: PlatformLocation, urlCodec: UrlCodec, locationStrategy: LocationStrategy, config?: LocationUpgradeTestingConfig) { - const $locationProvider = new LocationUpgradeProvider( - ngUpgrade, location, platformLocation, urlCodec, locationStrategy); + const $locationProvider = + new $locationShimProvider(ngUpgrade, location, platformLocation, urlCodec, locationStrategy); $locationProvider.hashPrefix(config && config.hashPrefix); $locationProvider.html5Mode(config && !config.useHash); diff --git a/packages/common/upgrade/tsconfig-build.json b/packages/common/upgrade/tsconfig-build.json index 70e507ee6c..0ea78796f2 100644 --- a/packages/common/upgrade/tsconfig-build.json +++ b/packages/common/upgrade/tsconfig-build.json @@ -12,9 +12,6 @@ ], "@angular/platform-browser": [ "../../../dist/packages/platform-browser" - ], - "@angular/upgrade/static": [ - "../../../dist/packages/upgrade/static" ] }, "outDir": "../../../dist/packages/common" diff --git a/tools/public_api_guard/common/upgrade.d.ts b/tools/public_api_guard/common/upgrade.d.ts index 72b47abf3d..3f50d4970f 100644 --- a/tools/public_api_guard/common/upgrade.d.ts +++ b/tools/public_api_guard/common/upgrade.d.ts @@ -1,4 +1,37 @@ -export declare const $locationProvider: Function; +export declare class $locationShim { + constructor($injector: any, location: Location, platformLocation: PlatformLocation, urlCodec: UrlCodec, locationStrategy: LocationStrategy); + $$parse(url: string): void; + $$parseLinkUrl(url: string, relHref?: string | null): boolean; + absUrl(): string; + hash(hash: string | number | null): this; + hash(): string; + host(): string; + path(): string; + path(path: string | number | null): this; + port(): number | null; + protocol(): string; + replace(): this; + search(): { + [key: string]: unknown; + }; + search(search: string | number | { + [key: string]: unknown; + }): this; + search(search: string | number | { + [key: string]: unknown; + }, paramValue: null | undefined | string | number | boolean | string[]): this; + state(state: unknown): this; + state(): unknown; + url(): string; + url(url: string): this; +} + +export declare class $locationShimProvider { + constructor(ngUpgrade: UpgradeModule, location: Location, platformLocation: PlatformLocation, urlCodec: UrlCodec, locationStrategy: LocationStrategy); + $get(): $locationShim; + hashPrefix(prefix?: string): void; + html5Mode(mode?: any): void; +} export declare class AngularJSUrlCodec implements UrlCodec { areEqual(a: string, b: string): boolean; @@ -30,8 +63,6 @@ export declare class AngularJSUrlCodec implements UrlCodec { export declare const LOCATION_UPGRADE_CONFIGURATION: InjectionToken; -export declare const LOCATION_UPGRADE_MODULE = "LOCATION_UPGRADE_MODULE"; - export interface LocationUpgradeConfig { appBaseHref?: string; hashPrefix?: string; @@ -44,41 +75,6 @@ export declare class LocationUpgradeModule { static config(config?: LocationUpgradeConfig): ModuleWithProviders; } -export declare class LocationUpgradeProvider { - constructor(ngUpgrade: UpgradeModule, location: Location, platformLocation: PlatformLocation, urlCodec: UrlCodec, locationStrategy: LocationStrategy); - $get(): LocationUpgradeService; - hashPrefix(prefix?: string): void; - html5Mode(mode?: any): void; -} - -export declare class LocationUpgradeService { - constructor($injector: any, location: Location, platformLocation: PlatformLocation, urlCodec: UrlCodec, locationStrategy: LocationStrategy); - $$parse(url: string): void; - $$parseLinkUrl(url: string, relHref?: string | null): boolean; - absUrl(): string; - hash(hash: string | number | null): this; - hash(): string; - host(): string; - path(): string; - path(path: string | number | null): this; - port(): number | null; - protocol(): string; - replace(): this; - search(): { - [key: string]: unknown; - }; - search(search: string | number | { - [key: string]: unknown; - }): this; - search(search: string | number | { - [key: string]: unknown; - }, paramValue: null | undefined | string | number | boolean | string[]): this; - state(state: unknown): this; - state(): unknown; - url(): string; - url(url: string): this; -} - export declare abstract class UrlCodec { abstract areEqual(a: string, b: string): boolean; abstract decodeHash(hash: string): string;