Skip to content

Commit c33fda2

Browse files
committed
perf: Don’t subclass Error; resulting in smaller binary (angular#14160)
Subclassing errors is problematic since Error returns a new instance. All of the patching which we do than prevent proper application of source maps. PR Close angular#14160
1 parent 3c2842b commit c33fda2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+407
-500
lines changed

modules/@angular/common/src/pipes/async_pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {ChangeDetectorRef, OnDestroy, Pipe, PipeTransform, WrappedValue} from '@angular/core';
1010
import {EventEmitter, Observable} from '../facade/async';
1111
import {isObservable, isPromise} from '../private_import_core';
12-
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
12+
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
1313

1414
interface SubscriptionStrategy {
1515
createSubscription(async: any, updateLatestValue: any): any;
@@ -120,7 +120,7 @@ export class AsyncPipe implements OnDestroy, PipeTransform {
120120
return _observableStrategy;
121121
}
122122

123-
throw new InvalidPipeArgumentError(AsyncPipe, obj);
123+
throw invalidPipeArgumentError(AsyncPipe, obj);
124124
}
125125

126126
private _dispose(): void {

modules/@angular/common/src/pipes/case_conversion_pipes.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Pipe, PipeTransform} from '@angular/core';
10-
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
10+
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
1111

1212
/**
1313
* Transforms text to lowercase.
@@ -21,7 +21,7 @@ export class LowerCasePipe implements PipeTransform {
2121
transform(value: string): string {
2222
if (!value) return value;
2323
if (typeof value !== 'string') {
24-
throw new InvalidPipeArgumentError(LowerCasePipe, value);
24+
throw invalidPipeArgumentError(LowerCasePipe, value);
2525
}
2626
return value.toLowerCase();
2727
}
@@ -48,7 +48,7 @@ export class TitleCasePipe implements PipeTransform {
4848
transform(value: string): string {
4949
if (!value) return value;
5050
if (typeof value !== 'string') {
51-
throw new InvalidPipeArgumentError(TitleCasePipe, value);
51+
throw invalidPipeArgumentError(TitleCasePipe, value);
5252
}
5353

5454
return value.split(/\b/g).map(word => titleCaseWord(word)).join('');
@@ -65,7 +65,7 @@ export class UpperCasePipe implements PipeTransform {
6565
transform(value: string): string {
6666
if (!value) return value;
6767
if (typeof value !== 'string') {
68-
throw new InvalidPipeArgumentError(UpperCasePipe, value);
68+
throw invalidPipeArgumentError(UpperCasePipe, value);
6969
}
7070
return value.toUpperCase();
7171
}

modules/@angular/common/src/pipes/date_pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {Inject, LOCALE_ID, Pipe, PipeTransform} from '@angular/core';
1010
import {NumberWrapper} from '../facade/lang';
1111
import {DateFormatter} from './intl';
12-
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
12+
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
1313

1414
const ISO8601_DATE_REGEX =
1515
/^(\d{4})-?(\d\d)-?(\d\d)(?:T(\d\d)(?::?(\d\d)(?::?(\d\d)(?:\.(\d+))?)?)?(Z|([+-])(\d\d):?(\d\d))?)?$/;
@@ -134,7 +134,7 @@ export class DatePipe implements PipeTransform {
134134
if ((typeof value === 'string') && (match = value.match(ISO8601_DATE_REGEX))) {
135135
date = isoStringToDate(match);
136136
} else {
137-
throw new InvalidPipeArgumentError(DatePipe, value);
137+
throw invalidPipeArgumentError(DatePipe, value);
138138
}
139139
}
140140

modules/@angular/common/src/pipes/i18n_plural_pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import {Pipe, PipeTransform} from '@angular/core';
1010
import {NgLocalization, getPluralCategory} from '../localization';
11-
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
11+
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
1212

1313
const _INTERPOLATION_REGEXP: RegExp = /#/g;
1414

@@ -37,7 +37,7 @@ export class I18nPluralPipe implements PipeTransform {
3737
if (value == null) return '';
3838

3939
if (typeof pluralMap !== 'object' || pluralMap === null) {
40-
throw new InvalidPipeArgumentError(I18nPluralPipe, pluralMap);
40+
throw invalidPipeArgumentError(I18nPluralPipe, pluralMap);
4141
}
4242

4343
const key = getPluralCategory(value, Object.keys(pluralMap), this._localization);

modules/@angular/common/src/pipes/i18n_select_pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Pipe, PipeTransform} from '@angular/core';
10-
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
10+
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
1111

1212
/**
1313
* @ngModule CommonModule
@@ -32,7 +32,7 @@ export class I18nSelectPipe implements PipeTransform {
3232
if (value == null) return '';
3333

3434
if (typeof mapping !== 'object' || typeof value !== 'string') {
35-
throw new InvalidPipeArgumentError(I18nSelectPipe, mapping);
35+
throw invalidPipeArgumentError(I18nSelectPipe, mapping);
3636
}
3737

3838
if (mapping.hasOwnProperty(value)) {

modules/@angular/common/src/pipes/invalid_pipe_argument_error.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88

99
import {Type} from '@angular/core';
1010

11-
import {BaseError} from '../facade/errors';
1211
import {stringify} from '../facade/lang';
1312

14-
export class InvalidPipeArgumentError extends BaseError {
15-
constructor(type: Type<any>, value: Object) {
16-
super(`Invalid argument '${value}' for pipe '${stringify(type)}'`);
17-
}
13+
export function invalidPipeArgumentError(type: Type<any>, value: Object) {
14+
return Error(`InvalidPipeArgument: '${value}' for pipe '${stringify(type)}'`);
1815
}

modules/@angular/common/src/pipes/number_pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {Inject, LOCALE_ID, Pipe, PipeTransform, Type} from '@angular/core';
1111
import {NumberWrapper} from '../facade/lang';
1212

1313
import {NumberFormatStyle, NumberFormatter} from './intl';
14-
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
14+
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
1515

1616
const _NUMBER_FORMAT_REGEXP = /^(\d+)?\.((\d+)(-(\d+))?)?$/;
1717

@@ -23,7 +23,7 @@ function formatNumber(
2323
// Convert strings to numbers
2424
value = typeof value === 'string' && NumberWrapper.isNumeric(value) ? +value : value;
2525
if (typeof value !== 'number') {
26-
throw new InvalidPipeArgumentError(pipe, value);
26+
throw invalidPipeArgumentError(pipe, value);
2727
}
2828

2929
let minInt: number;

modules/@angular/common/src/pipes/slice_pipe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {Pipe, PipeTransform} from '@angular/core';
10-
import {InvalidPipeArgumentError} from './invalid_pipe_argument_error';
10+
import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
1111

1212
/**
1313
* @ngModule CommonModule
@@ -60,7 +60,7 @@ export class SlicePipe implements PipeTransform {
6060
if (value == null) return value;
6161

6262
if (!this.supports(value)) {
63-
throw new InvalidPipeArgumentError(SlicePipe, value);
63+
throw invalidPipeArgumentError(SlicePipe, value);
6464
}
6565

6666
return value.slice(start, end);

modules/@angular/common/test/pipes/date_pipe_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function main() {
5959
() => { expect(() => pipe.transform(isoStringWithoutTime)).not.toThrow(); });
6060

6161
it('should not support other objects',
62-
() => expect(() => pipe.transform({})).toThrowError(/Invalid argument/));
62+
() => expect(() => pipe.transform({})).toThrowError(/InvalidPipeArgument/));
6363
});
6464

6565
describe('transform', () => {

modules/@angular/compiler-cli/src/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import 'reflect-metadata';
1313

1414
import * as ts from 'typescript';
1515
import * as tsc from '@angular/tsc-wrapped';
16+
import {isSyntaxError} from '@angular/compiler';
1617

17-
import {SyntaxError} from '@angular/compiler';
1818
import {CodeGenerator} from './codegen';
1919

2020
function codegen(
@@ -29,7 +29,7 @@ export function main(
2929
const cliOptions = new tsc.NgcCliOptions(args);
3030

3131
return tsc.main(project, cliOptions, codegen).then(() => 0).catch(e => {
32-
if (e instanceof tsc.UserError || e instanceof SyntaxError) {
32+
if (e instanceof tsc.UserError || isSyntaxError(e)) {
3333
consoleError(e.message);
3434
return Promise.resolve(1);
3535
} else {

0 commit comments

Comments
 (0)