refactor: replace local namespaced imports with named (#28642)

At the moment, the API extractor doesn't support local namespaced imports, this will break the generation of flat dts files. When we turn on dts bundling for this package it will break. Hence this is the ground work needed for making this package compatable with the API extractor.

See: https://github.com/Microsoft/web-build-tools/issues/1029

Relates to #28588

PR Close #28642
This commit is contained in:
Alan
2019-02-11 11:12:57 +01:00
committed by Igor Minar
parent 599e2e22bc
commit 7c1b9ff5ec
11 changed files with 114 additions and 115 deletions

View File

@ -8,7 +8,7 @@
import {ComponentFactory, ComponentFactoryResolver, Injector, NgZone, Type} from '@angular/core';
import * as angular from './angular1';
import {IAnnotatedFunction, IAttributes, IAugmentedJQuery, ICompileService, IDirective, IInjectorService, INgModelController, IParseService, IScope} from './angular1';
import {$COMPILE, $INJECTOR, $PARSE, INJECTOR_KEY, LAZY_MODULE_REF, REQUIRE_INJECTOR, REQUIRE_NG_MODEL} from './constants';
import {DowngradeComponentAdapter} from './downgrade_component_adapter';
import {LazyModuleRef, UpgradeAppType, controllerKey, getDowngradedModuleCount, getTypeName, getUpgradeAppType, isFunction, validateInjectionKey} from './util';
@ -73,11 +73,8 @@ export function downgradeComponent(info: {
/** @deprecated since v4. This parameter is no longer used */
selectors?: string[];
}): any /* angular.IInjectable */ {
const directiveFactory:
angular.IAnnotatedFunction = function(
$compile: angular.ICompileService,
$injector: angular.IInjectorService,
$parse: angular.IParseService): angular.IDirective {
const directiveFactory: IAnnotatedFunction = function(
$compile: ICompileService, $injector: IInjectorService, $parse: IParseService): IDirective {
// When using `downgradeModule()`, we need to handle certain things specially. For example:
// - We always need to attach the component view to the `ApplicationRef` for it to be
// dirty-checked.
@ -99,13 +96,12 @@ export function downgradeComponent(info: {
restrict: 'E',
terminal: true,
require: [REQUIRE_INJECTOR, REQUIRE_NG_MODEL],
link: (scope: angular.IScope, element: angular.IAugmentedJQuery, attrs: angular.IAttributes,
required: any[]) => {
link: (scope: IScope, element: IAugmentedJQuery, attrs: IAttributes, required: any[]) => {
// We might have to compile the contents asynchronously, because this might have been
// triggered by `UpgradeNg1ComponentAdapterBuilder`, before the Angular templates have
// been compiled.
const ngModel: angular.INgModelController = required[1];
const ngModel: INgModelController = required[1];
const parentInjector: Injector|Thenable<Injector>|undefined = required[0];
let moduleInjector: Injector|Thenable<Injector>|undefined = undefined;
let ranAsync = false;
@ -230,7 +226,7 @@ class ParentInjectorPromise {
private injectorKey: string = controllerKey(INJECTOR_KEY);
private callbacks: ((injector: Injector) => any)[] = [];
constructor(private element: angular.IAugmentedJQuery) {
constructor(private element: IAugmentedJQuery) {
// Store the promise on the element.
element.data !(this.injectorKey, this);
}

View File

@ -8,7 +8,7 @@
import {ApplicationRef, ChangeDetectorRef, ComponentFactory, ComponentRef, EventEmitter, Injector, OnChanges, SimpleChange, SimpleChanges, StaticProvider, Testability, TestabilityRegistry, Type} from '@angular/core';
import * as angular from './angular1';
import {IAttributes, IAugmentedJQuery, ICompileService, IInjectorService, INgModelController, IParseService, IScope} from './angular1';
import {PropertyBinding} from './component_info';
import {$SCOPE} from './constants';
import {getTypeName, hookupNgModel, strictEquals} from './util';
@ -21,7 +21,7 @@ export class DowngradeComponentAdapter {
private implementsOnChanges = false;
private inputChangeCount: number = 0;
private inputChanges: SimpleChanges = {};
private componentScope: angular.IScope;
private componentScope: IScope;
// TODO(issue/24571): remove '!'.
private componentRef !: ComponentRef<any>;
private component: any;
@ -31,11 +31,10 @@ export class DowngradeComponentAdapter {
private viewChangeDetector !: ChangeDetectorRef;
constructor(
private element: angular.IAugmentedJQuery, private attrs: angular.IAttributes,
private scope: angular.IScope, private ngModel: angular.INgModelController,
private parentInjector: Injector, private $injector: angular.IInjectorService,
private $compile: angular.ICompileService, private $parse: angular.IParseService,
private componentFactory: ComponentFactory<any>,
private element: IAugmentedJQuery, private attrs: IAttributes, private scope: IScope,
private ngModel: INgModelController, private parentInjector: Injector,
private $injector: IInjectorService, private $compile: ICompileService,
private $parse: IParseService, private componentFactory: ComponentFactory<any>,
private wrapCallback: <T>(cb: () => T) => () => T) {
this.componentScope = scope.$new();
}

View File

@ -7,7 +7,7 @@
*/
import {Injector} from '@angular/core';
import * as angular from './angular1';
import {IInjectorService} from './angular1';
import {$INJECTOR, INJECTOR_KEY} from './constants';
import {getTypeName, isFunction, validateInjectionKey} from './util';
@ -72,7 +72,7 @@ import {getTypeName, isFunction, validateInjectionKey} from './util';
* @publicApi
*/
export function downgradeInjectable(token: any, downgradedModule: string = ''): Function {
const factory = function($injector: angular.IInjectorService) {
const factory = function($injector: IInjectorService) {
const injectorKey = `${INJECTOR_KEY}${downgradedModule}`;
const injectableName = isFunction(token) ? getTypeName(token) : String(token);
const attemptedAction = `instantiating injectable '${injectableName}'`;

View File

@ -8,11 +8,12 @@
import {ElementRef, Injector, SimpleChanges} from '@angular/core';
import * as angular from './angular1';
import {DirectiveRequireProperty, IAugmentedJQuery, ICloneAttachFunction, ICompileService, IController, IControllerService, IDirective, IHttpBackendService, IInjectorService, ILinkFn, IScope, ITemplateCacheService, SingleOrListOrMap, element as angularElement} from './angular1';
import {$COMPILE, $CONTROLLER, $HTTP_BACKEND, $INJECTOR, $TEMPLATE_CACHE} from './constants';
import {controllerKey, directiveNormalize, isFunction} from './util';
// Constants
const REQUIRE_PREFIX_RE = /^(\^\^?)?(\?)?(\^\^?)?/;
@ -31,29 +32,29 @@ export interface IControllerInstance extends IBindingDestination {
// Classes
export class UpgradeHelper {
public readonly $injector: angular.IInjectorService;
public readonly $injector: IInjectorService;
public readonly element: Element;
public readonly $element: angular.IAugmentedJQuery;
public readonly directive: angular.IDirective;
public readonly $element: IAugmentedJQuery;
public readonly directive: IDirective;
private readonly $compile: angular.ICompileService;
private readonly $controller: angular.IControllerService;
private readonly $compile: ICompileService;
private readonly $controller: IControllerService;
constructor(
private injector: Injector, private name: string, elementRef: ElementRef,
directive?: angular.IDirective) {
directive?: IDirective) {
this.$injector = injector.get($INJECTOR);
this.$compile = this.$injector.get($COMPILE);
this.$controller = this.$injector.get($CONTROLLER);
this.element = elementRef.nativeElement;
this.$element = angular.element(this.element);
this.$element = angularElement(this.element);
this.directive = directive || UpgradeHelper.getDirective(this.$injector, name);
}
static getDirective($injector: angular.IInjectorService, name: string): angular.IDirective {
const directives: angular.IDirective[] = $injector.get(name + 'Directive');
static getDirective($injector: IInjectorService, name: string): IDirective {
const directives: IDirective[] = $injector.get(name + 'Directive');
if (directives.length > 1) {
throw new Error(`Only support single directive definition for: ${name}`);
}
@ -70,12 +71,12 @@ export class UpgradeHelper {
}
static getTemplate(
$injector: angular.IInjectorService, directive: angular.IDirective,
fetchRemoteTemplate = false): string|Promise<string> {
$injector: IInjectorService, directive: IDirective, fetchRemoteTemplate = false): string
|Promise<string> {
if (directive.template !== undefined) {
return getOrCall<string>(directive.template);
} else if (directive.templateUrl) {
const $templateCache = $injector.get($TEMPLATE_CACHE) as angular.ITemplateCacheService;
const $templateCache = $injector.get($TEMPLATE_CACHE) as ITemplateCacheService;
const url = getOrCall<string>(directive.templateUrl);
const template = $templateCache.get(url);
@ -86,7 +87,7 @@ export class UpgradeHelper {
}
return new Promise((resolve, reject) => {
const $httpBackend = $injector.get($HTTP_BACKEND) as angular.IHttpBackendService;
const $httpBackend = $injector.get($HTTP_BACKEND) as IHttpBackendService;
$httpBackend('GET', url, null, (status: number, response: string) => {
if (status === 200) {
resolve($templateCache.put(url, response));
@ -100,7 +101,7 @@ export class UpgradeHelper {
}
}
buildController(controllerType: angular.IController, $scope: angular.IScope) {
buildController(controllerType: IController, $scope: IScope) {
// TODO: Document that we do not pre-assign bindings on the controller instance.
// Quoted properties below so that this code can be optimized with Closure Compiler.
const locals = {'$scope': $scope, '$element': this.$element};
@ -111,7 +112,7 @@ export class UpgradeHelper {
return controller;
}
compileTemplate(template?: string): angular.ILinkFn {
compileTemplate(template?: string): ILinkFn {
if (template === undefined) {
template = UpgradeHelper.getTemplate(this.$injector, this.directive) as string;
}
@ -119,7 +120,7 @@ export class UpgradeHelper {
return this.compileHtml(template);
}
onDestroy($scope: angular.IScope, controllerInstance?: any) {
onDestroy($scope: IScope, controllerInstance?: any) {
if (controllerInstance && isFunction(controllerInstance.$onDestroy)) {
controllerInstance.$onDestroy();
}
@ -131,14 +132,14 @@ export class UpgradeHelper {
// https://github.com/angular/angular.js/blob/26ddc5f830f902a3d22f4b2aab70d86d4d688c82/src/jqLite.js#L306-L312
// `cleanData` will invoke the AngularJS `$destroy` DOM event
// https://github.com/angular/angular.js/blob/26ddc5f830f902a3d22f4b2aab70d86d4d688c82/src/Angular.js#L1911-L1924
angular.element.cleanData([this.element]);
angular.element.cleanData(this.element.querySelectorAll('*'));
angularElement.cleanData([this.element]);
angularElement.cleanData(this.element.querySelectorAll('*'));
}
prepareTransclusion(): angular.ILinkFn|undefined {
prepareTransclusion(): ILinkFn|undefined {
const transclude = this.directive.transclude;
const contentChildNodes = this.extractChildNodes();
const attachChildrenFn: angular.ILinkFn = (scope, cloneAttachFn) => {
const attachChildrenFn: ILinkFn = (scope, cloneAttachFn) => {
// Since AngularJS v1.5.8, `cloneAttachFn` will try to destroy the transclusion scope if
// `$template` is empty. Since the transcluded content comes from Angular, not AngularJS,
// there will be no transclusion scope here.
@ -189,8 +190,9 @@ export class UpgradeHelper {
Object.keys(slots).filter(slotName => slots[slotName]).forEach(slotName => {
const nodes = slots[slotName];
slots[slotName] = (scope: angular.IScope, cloneAttach: angular.ICloneAttachFunction) =>
cloneAttach !(nodes, scope);
slots[slotName] = (scope: IScope, cloneAttach: ICloneAttachFunction) => {
return cloneAttach !(nodes, scope);
};
});
}
@ -231,7 +233,7 @@ export class UpgradeHelper {
return requiredControllers;
}
private compileHtml(html: string): angular.ILinkFn {
private compileHtml(html: string): ILinkFn {
this.element.innerHTML = html;
return this.$compile(this.element.childNodes);
}
@ -248,7 +250,7 @@ export class UpgradeHelper {
return childNodes;
}
private getDirectiveRequire(): angular.DirectiveRequireProperty {
private getDirectiveRequire(): DirectiveRequireProperty {
const require = this.directive.require || (this.directive.controller && this.directive.name) !;
if (isMap(require)) {
@ -266,8 +268,8 @@ export class UpgradeHelper {
return require;
}
private resolveRequire(require: angular.DirectiveRequireProperty, controllerInstance?: any):
angular.SingleOrListOrMap<IControllerInstance>|null {
private resolveRequire(require: DirectiveRequireProperty, controllerInstance?: any):
SingleOrListOrMap<IControllerInstance>|null {
if (!require) {
return null;
} else if (Array.isArray(require)) {
@ -307,7 +309,7 @@ function getOrCall<T>(property: T | Function): T {
}
// NOTE: Only works for `typeof T !== 'object'`.
function isMap<T>(value: angular.SingleOrListOrMap<T>): value is {[key: string]: T} {
function isMap<T>(value: SingleOrListOrMap<T>): value is {[key: string]: T} {
return value && !Array.isArray(value) && typeof value === 'object';
}

View File

@ -7,7 +7,8 @@
*/
import {Injector, Type} from '@angular/core';
import * as angular from './angular1';
import {IInjectorService, INgModelController} from './angular1';
import {DOWNGRADED_MODULE_COUNT_KEY, UPGRADE_APP_TYPE_KEY} from './constants';
const DIRECTIVE_PREFIX_REGEXP = /^(?:x|data)[:\-_]/i;
@ -38,12 +39,12 @@ export function getTypeName(type: Type<any>): string {
return (type as any).overriddenName || type.name || type.toString().split('\n')[0];
}
export function getDowngradedModuleCount($injector: angular.IInjectorService): number {
export function getDowngradedModuleCount($injector: IInjectorService): number {
return $injector.has(DOWNGRADED_MODULE_COUNT_KEY) ? $injector.get(DOWNGRADED_MODULE_COUNT_KEY) :
0;
}
export function getUpgradeAppType($injector: angular.IInjectorService): UpgradeAppType {
export function getUpgradeAppType($injector: IInjectorService): UpgradeAppType {
return $injector.has(UPGRADE_APP_TYPE_KEY) ? $injector.get(UPGRADE_APP_TYPE_KEY) :
UpgradeAppType.None;
}
@ -53,7 +54,7 @@ export function isFunction(value: any): value is Function {
}
export function validateInjectionKey(
$injector: angular.IInjectorService, downgradedModule: string, injectionKey: string,
$injector: IInjectorService, downgradedModule: string, injectionKey: string,
attemptedAction: string): void {
const upgradeAppType = getUpgradeAppType($injector);
const downgradedModuleCount = getDowngradedModuleCount($injector);
@ -141,7 +142,7 @@ function supportsNgModel(component: any) {
* Glue the AngularJS `NgModelController` (if it exists) to the component
* (if it implements the needed subset of the `ControlValueAccessor` interface).
*/
export function hookupNgModel(ngModel: angular.INgModelController, component: any) {
export function hookupNgModel(ngModel: INgModelController, component: any) {
if (ngModel && supportsNgModel(component)) {
ngModel.$render = () => { component.writeValue(ngModel.$viewValue); };
component.registerOnChange(ngModel.$setViewValue.bind(ngModel));