Skip to content

Commit

Permalink
feat(module:core): add universal logger funcs and deprecation warnings (
Browse files Browse the repository at this point in the history
#3538)

* chore: add universal logger service

* chore: rename to NzLoggerService to avoid conflication with users'

* chore: use funcs instead of service

* fix: fix record not working

* chore: show error all the time

* fix: unused

* fix: simply usage

* fix: remove namespace

* revert: tslint

* revert: lint

* fix: tree

* fix: self import

* fix: tests

* fix: jump over bad test

* fix: test

* fix: test
  • Loading branch information
Wendell authored and vthinkxie committed Jun 18, 2019
1 parent 5033083 commit b893520
Show file tree
Hide file tree
Showing 23 changed files with 171 additions and 166 deletions.
6 changes: 6 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"fileReplacements": [
{
"replace": "components/core/environments/environment.ts",
"with": "components/core/environments/environment.test.ts"
}
],
"main": "components/test.ts",
"karmaConfig": "components/karma.conf.js",
"polyfills": "components/polyfills.ts",
Expand Down
4 changes: 2 additions & 2 deletions components/breadcrumb/nz-breadcrumb.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { Subject } from 'rxjs';
import { startWith } from 'rxjs/internal/operators/startWith';
import { filter, takeUntil } from 'rxjs/operators';

import { InputBoolean } from 'ng-zorro-antd/core';
import { InputBoolean, PREFIX } from 'ng-zorro-antd/core';

export const NZ_ROUTE_DATA_BREADCRUMB = 'breadcrumb';

Expand Down Expand Up @@ -107,7 +107,7 @@ export class NzBreadCrumbComponent implements OnInit, OnDestroy {
this.cdr.markForCheck();
});
} catch (e) {
throw new Error('[NG-ZORRO] You should import RouterModule if you want to use `NzAutoGenerate`');
throw new Error(`${PREFIX} You should import RouterModule if you want to use 'NzAutoGenerate'.`);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

// tslint:disable-next-line:no-any
export function getEmptyContentTypeError(content: any): Error {
return TypeError(`[NG-ZORRO]: useDefaultContent expect 'string', 'templateRef' or 'component' but get ${content}`);
}
export const environment = {
isTestMode: true
};
11 changes: 11 additions & 0 deletions components/core/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @license
* Copyright Alibaba.com 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://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

export const environment = {
isTestMode: false
};
16 changes: 0 additions & 16 deletions components/core/logger/logger.module.ts

This file was deleted.

61 changes: 0 additions & 61 deletions components/core/logger/logger.service.ts

This file was deleted.

46 changes: 46 additions & 0 deletions components/core/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license
* Copyright Alibaba.com 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://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

// tslint:disable:no-any
import { environment } from '../environments/environment';

import { isDevMode } from '@angular/core';

const record: Record<string, boolean> = {};

export const PREFIX = '[NG-ZORRO]:';

function notRecorded(...args: any[]): boolean {
const asRecord = args.reduce((acc, c) => acc + c.toString(), '');

if (record[asRecord]) {
return false;
} else {
record[asRecord] = true;
return true;
}
}

function consoleCommonBehavior(consoleFunc: (...args: any) => void, ...args: any[]): void {
if (environment.isTestMode || (isDevMode() && notRecorded(...args))) {
consoleFunc(...args);
}
}

// Warning should only be printed in dev mode and only once.
export const warn = (...args: any[]) => consoleCommonBehavior((...arg: any[]) => console.warn(PREFIX, ...arg), ...args);

export const warnDeprecation = (...args: any[]) =>
consoleCommonBehavior((...arg: any[]) => console.error(PREFIX, 'deprecated:', ...arg), ...args);

// Log should only be printed in dev mode.
export const log = (...args: any[]) => {
if (isDevMode()) {
console.log(PREFIX, ...args);
}
};
8 changes: 1 addition & 7 deletions components/core/logger/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,4 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

export { LoggerModule } from './logger.module';
export {
LoggerService,
NZ_LOGGER_STATE,
LOGGER_SERVICE_PROVIDER,
LOGGER_SERVICE_PROVIDER_FACTORY
} from './logger.service';
export * from './logger';
13 changes: 9 additions & 4 deletions components/core/tree/nz-tree-base-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { warnDeprecation } from '../logger/logger';
import { NzTreeNodeBaseComponent } from './nz-tree-base.definitions';
import { NzTreeBaseService } from './nz-tree-base.service';

Expand Down Expand Up @@ -167,9 +168,10 @@ export class NzTreeNode {
}

/**
* @deprecated Maybe removed in next major version, use isChecked instead
* @deprecated Maybe removed in next major version, use `isChecked` instead.
*/
set isAllChecked(value: boolean) {
warnDeprecation(`'isAllChecked' is going to be removed in 9.0.0. Please use 'isChecked' instead.`);
this._isAllChecked = value;
}

Expand Down Expand Up @@ -246,26 +248,29 @@ export class NzTreeNode {
}

/**
* @deprecated Maybe removed in next major version, use isChecked instead
* @deprecated Maybe removed in next major version, use `isChecked` instead.
*/
public setChecked(checked: boolean = false, halfChecked: boolean = false): void {
warnDeprecation(`'setChecked' is going to be removed in 9.0.0. Please use 'isChecked' instead.`);
this.origin.checked = checked;
this.isChecked = checked;
this.isAllChecked = checked;
this.isHalfChecked = halfChecked;
}

/**
* @deprecated Maybe removed in next major version, use isExpanded instead
* @deprecated Maybe removed in next major version, use `isExpanded` instead.
*/
public setExpanded(value: boolean): void {
warnDeprecation(`'setExpanded' is going to be removed in 9.0.0. Please use 'isExpanded' instead.`);
this.isExpanded = value;
}

/**
* @deprecated Maybe removed in next major version, use isSelected instead
* @deprecated Maybe removed in next major version, use `isSelected` instead.
*/
public setSelected(value: boolean): void {
warnDeprecation(`'setSelected' is going to be removed in 9.0.0. Please use 'isExpanded' isSelected.`);
if (this.isDisabled) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion components/core/util/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { coerceBooleanProperty, coerceCssPixelValue, _isNumberValue } from '@angular/cdk/coercion';

import { warn } from '../logger/logger';
import { FunctionProp } from '../types/common-wrap';

export function toBoolean(value: boolean | string): boolean {
Expand Down Expand Up @@ -39,7 +40,7 @@ function propDecoratorFactory<T, D>(name: string, fallback: (v: T) => D): (targe
const privatePropName = `$$__${propName}`;

if (Object.prototype.hasOwnProperty.call(target, privatePropName)) {
console.warn(`The prop "${privatePropName}" is already exist, it will be overrided by ${name} decorator.`);
warn(`The prop "${privatePropName}" is already exist, it will be overrided by ${name} decorator.`);
}

Object.defineProperty(target, privatePropName, {
Expand Down
2 changes: 0 additions & 2 deletions components/core/util/textarea-caret-position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,4 @@ export function createDebugEle(element: HTMLInputElement | HTMLTextAreaElement,
element.scrollLeft +
window.pageXOffset +
coordinates.left}px`;
console.log(rect.style.top);
console.log(rect.style.left);
}
8 changes: 6 additions & 2 deletions components/empty/nz-empty.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import { Inject, Injectable, Optional, TemplateRef, Type } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

import { PREFIX } from 'ng-zorro-antd/core';

import { NzEmptyCustomContent, NZ_DEFAULT_EMPTY_CONTENT } from './nz-empty-config';
import { getEmptyContentTypeError } from './nz-empty-error';

@Injectable({
providedIn: 'root'
Expand All @@ -34,7 +36,9 @@ export class NzEmptyService<T = any> {
) {
this.userDefaultContent$.next(content);
} else {
throw getEmptyContentTypeError(content);
throw new Error(
`${PREFIX} 'useDefaultContent' expect 'string', 'templateRef' or 'component' but get ${content}.`
);
}
}

Expand Down
13 changes: 11 additions & 2 deletions components/i18n/date-helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import { Inject, Injectable, Injector, Optional } from '@angular/core';
import fnsFormat from 'date-fns/format';
import fnsGetISOWeek from 'date-fns/get_iso_week';
import fnsParse from 'date-fns/parse';

import { warnDeprecation } from 'ng-zorro-antd/core';

import { mergeDateConfig, NzDateConfig, NZ_DATE_CONFIG } from './date-config';
import { NzI18nService } from './nz-i18n.service';

Expand Down Expand Up @@ -81,13 +84,19 @@ export class DateHelperByDateFns extends DateHelperService {

/**
* DateHelper that handles date formats with angular's date-pipe
* [BUG] Use DatePipe may cause non-standard week bug, see: https://github.com/NG-ZORRO/ng-zorro-antd/issues/2406
*
* @deprecated Maybe removed in next major version due to this serious bug
* @see https://github.com/NG-ZORRO/ng-zorro-antd/issues/2406 - DatePipe may cause non-standard week bug, see:
*
* @deprecated 9.0.0 - `DateHelperByDatePipe` would be removed in 9.0.0 due to the serious bug above, unless it's get fixed.
* Please use `DateHelperByDateFns` instead.
*/
export class DateHelperByDatePipe extends DateHelperService {
constructor(i18n: NzI18nService, @Optional() @Inject(NZ_DATE_CONFIG) config: NzDateConfig) {
super(i18n, config);

warnDeprecation(
`'DateHelperByDatePipe' would be removed in 9.0.0 due to this serious bug of Angular: https://github.com/NG-ZORRO/ng-zorro-antd/issues/2406. Please use 'DateHelperByDateFns' instead.`
);
}

getISOWeek(date: Date): number {
Expand Down
3 changes: 1 addition & 2 deletions components/i18n/nz-i18n.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
*/

import { NgModule } from '@angular/core';
import { LoggerModule } from 'ng-zorro-antd/core';

import { NzI18nPipe } from './nz-i18n.pipe';

@NgModule({
imports: [LoggerModule],
declarations: [NzI18nPipe],
exports: [NzI18nPipe]
})
Expand Down
21 changes: 8 additions & 13 deletions components/icon/nz-icon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
UploadOutline,
UpOutline
} from '@ant-design/icons-angular/icons';
import { warn } from 'ng-zorro-antd/core';

export interface NzIconfontOption {
scriptUrl: string;
Expand Down Expand Up @@ -101,22 +102,16 @@ export const NZ_ICONS_USED_BY_ZORRO: IconDefinition[] = [
})
export class NzIconService extends IconService {
private iconfontCache = new Set<string>();
private warnedAboutAPI = false;
private warnedAboutCross = false;
private warnedAboutVertical = false;

warnAPI(type: 'old' | 'cross' | 'vertical'): void {
if (type === 'old' && !this.warnedAboutAPI) {
console.warn(`<i class="anticon"></i> would be deprecated soon. Please use <i nz-icon type=""></i> API.`);
this.warnedAboutAPI = true;
if (type === 'old') {
warn(`<i class="anticon"></i> would be deprecated in 9.0.0. Please use <i nz-icon nzType=""></i> API.`);
}
if (type === 'cross' && !this.warnedAboutCross) {
console.warn(`'cross' icon is replaced by 'close' icon.`);
this.warnedAboutCross = true;
if (type === 'cross') {
warn(`'cross' icon is replaced by 'close' icon.`);
}
if (type === 'vertical' && !this.warnedAboutVertical) {
console.warn(`'verticle' is misspelled, would be corrected in the next major version.`);
this.warnedAboutVertical = true;
if (type === 'vertical') {
warn(`'verticle' is misspelled, would be corrected in the next major version.`);
}
}

Expand Down Expand Up @@ -166,7 +161,7 @@ export class NzIconService extends IconService {
if (this.defaultColor.startsWith('#')) {
primaryColor = this.defaultColor;
} else {
console.warn('[NG-ZORRO]: twotone color must be a hex color!');
warn('Twotone color must be a hex color!');
}
}
this.twoToneColor = { primaryColor };
Expand Down
Loading

0 comments on commit b893520

Please sign in to comment.