Skip to content

Commit a22a9fa

Browse files
devversionjelbourn
authored andcommitted
fix: workaround for es2015 inheritance not always working (#13834)
Since the inherited `ctorParameters` can be either a static array or a function that returns an array, the workaround that landed with e9103a6 does not fully solve the issue, which developers experience when using Angular Material with ES2015. This improves the workaround and ensures that it handles both scenarios properly. Also the workaround has been moved into a separate function in order to remove code duplication. This should also make it easier to delete the workaround if we need to.
1 parent 0589881 commit a22a9fa

15 files changed

+51
-23
lines changed

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
### Bug Fixes
66

7-
* **build:** material not working with ES2015 ([#13709](https://github.com/angular/material2/issues/13709)) ([e9103a6](https://github.com/angular/material2/commit/e9103a6)), closes [#12760](https://github.com/angular/material2/issues/12760) [#13695](https://github.com/angular/material2/issues/13695)
87
* **button-toggle:** webkit tap highlight conflicting with ripples ([#13721](https://github.com/angular/material2/issues/13721)) ([abd0278](https://github.com/angular/material2/commit/abd0278))
98
* **cdk-platform:** pass `{}` to `@NgModule` since passing nothing breaks ([#13792](https://github.com/angular/material2/issues/13792)) ([5abb644](https://github.com/angular/material2/commit/5abb644))
109
* **checkbox:** disabled property not being coerced ([#13755](https://github.com/angular/material2/issues/13755)) ([cee8c65](https://github.com/angular/material2/commit/cee8c65)), closes [#13739](https://github.com/angular/material2/issues/13739)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {Type} from '@angular/core';
9+
10+
/**
11+
* Workaround for https://github.com/angular/material2/issues/12760. In order to work around
12+
* the issue where classes which extend external classes do not have the proper metadata in
13+
* ES2015, we just manually inherit the metadata for the constructor parameters.
14+
* TODO(devversion): check if we can remove the workaround after ivy landed.
15+
*/
16+
export function _inheritCtorParametersMetadata(target: Type<any>, base: Type<any>) {
17+
(target as any)['ctorParameters'] = () => {
18+
const baseParameters = (base as any)['ctorParameters'];
19+
return (typeof baseParameters === 'function' ? baseParameters() : baseParameters) || [];
20+
};
21+
}

src/lib/core/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export * from './selection/index';
2020

2121
// TODO: don't have this
2222
export * from './testing/month-constants';
23+
export * from './constructor-metadata-inherit';

src/lib/input/autosize.ts

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

99
import {CdkTextareaAutosize} from '@angular/cdk/text-field';
1010
import {Directive, Input} from '@angular/core';
11+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
1112

1213
/**
1314
* Directive to automatically resize a textarea to fit its content.
@@ -45,5 +46,4 @@ export class MatTextareaAutosize extends CdkTextareaAutosize {
4546
}
4647

4748
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
48-
(MatTextareaAutosize as any)['ctorParameters'] = () =>
49-
(CdkTextareaAutosize as any)['ctorParameters'];
49+
_inheritCtorParametersMetadata(MatTextareaAutosize, CdkTextareaAutosize);

src/lib/stepper/step-label.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88

99
import {Directive} from '@angular/core';
1010
import {CdkStepLabel} from '@angular/cdk/stepper';
11+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
1112

1213
@Directive({
1314
selector: '[matStepLabel]',
1415
})
1516
export class MatStepLabel extends CdkStepLabel {}
1617

1718
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
18-
(MatStepLabel as any)['ctorParameters'] = () => (CdkStepLabel as any)['ctorParameters'];
19+
_inheritCtorParametersMetadata(MatStepLabel, CdkStepLabel);

src/lib/stepper/stepper-button.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {Directive} from '@angular/core';
1010
import {CdkStepper, CdkStepperNext, CdkStepperPrevious} from '@angular/cdk/stepper';
1111
import {MatStepper} from './stepper';
12+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
1213

1314
/** Button that moves to the next step in a stepper workflow. */
1415
@Directive({
@@ -35,6 +36,5 @@ export class MatStepperNext extends CdkStepperNext {}
3536
export class MatStepperPrevious extends CdkStepperPrevious {}
3637

3738
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
38-
(MatStepperNext as any)['ctorParameters'] = () => (CdkStepperNext as any)['ctorParameters'];
39-
(MatStepperPrevious as any)['ctorParameters'] = () =>
40-
(CdkStepperPrevious as any)['ctorParameters'];
39+
_inheritCtorParametersMetadata(MatStepperNext, CdkStepperNext);
40+
_inheritCtorParametersMetadata(MatStepperPrevious, CdkStepperPrevious);

src/lib/stepper/stepper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
} from '@angular/core';
3939
import {FormControl, FormGroupDirective, NgForm} from '@angular/forms';
4040
import {DOCUMENT} from '@angular/common';
41-
import {ErrorStateMatcher} from '@angular/material/core';
41+
import {_inheritCtorParametersMetadata, ErrorStateMatcher} from '@angular/material/core';
4242
import {Subject} from 'rxjs';
4343
import {takeUntil, distinctUntilChanged} from 'rxjs/operators';
4444

@@ -124,7 +124,7 @@ export class MatStepper extends CdkStepper implements AfterContentInit {
124124
}
125125

126126
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
127-
(MatStepper as any)['ctorParameters'] = () => (CdkStepper as any)['ctorParameters'];
127+
_inheritCtorParametersMetadata(MatStepper, CdkStepper);
128128

129129
@Component({
130130
moduleId: module.id,

src/lib/table/cell.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
CdkHeaderCell,
1515
CdkHeaderCellDef,
1616
} from '@angular/cdk/table';
17+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
1718

1819
/**
1920
* Cell definition for the mat-table.
@@ -46,10 +47,9 @@ export class MatHeaderCellDef extends CdkHeaderCellDef {}
4647
export class MatFooterCellDef extends CdkFooterCellDef {}
4748

4849
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
49-
(MatCellDef as any)['ctorParameters'] = () => (CdkCellDef as any)['ctorParameters'];
50-
(MatHeaderCellDef as any)['ctorParameters'] = () => (CdkHeaderCellDef as any)['ctorParameters'];
51-
(MatFooterCellDef as any)['ctorParameters'] = () => (MatFooterCellDef as any)['ctorParameters'];
52-
50+
_inheritCtorParametersMetadata(MatCellDef, CdkCellDef);
51+
_inheritCtorParametersMetadata(MatHeaderCellDef, CdkHeaderCellDef);
52+
_inheritCtorParametersMetadata(MatFooterCellDef, CdkFooterCellDef);
5353
/**
5454
* Column definition for the mat-table.
5555
* Defines a set of cells available for a table column.

src/lib/table/row.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
CdkRow,
2020
CdkRowDef,
2121
} from '@angular/cdk/table';
22+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
2223

2324
/**
2425
* Header row definition for the mat-table.
@@ -55,9 +56,9 @@ export class MatFooterRowDef extends CdkFooterRowDef {}
5556
export class MatRowDef<T> extends CdkRowDef<T> {}
5657

5758
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
58-
(MatHeaderRowDef as any)['ctorParameters'] = () => (CdkHeaderRowDef as any)['ctorParameters'];
59-
(MatFooterRowDef as any)['ctorParameters'] = () => (CdkFooterRowDef as any)['ctorParameters'];
60-
(MatRowDef as any)['ctorParameters'] = () => (CdkRowDef as any)['ctorParameters'];
59+
_inheritCtorParametersMetadata(MatHeaderRowDef, CdkHeaderRowDef);
60+
_inheritCtorParametersMetadata(MatFooterRowDef, CdkFooterRowDef);
61+
_inheritCtorParametersMetadata(MatRowDef, CdkRowDef);
6162

6263
/** Footer template container that contains the cell outlet. Adds the right class and role. */
6364
@Component({

src/lib/table/table.ts

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

99
import {CDK_TABLE_TEMPLATE, CdkTable} from '@angular/cdk/table';
1010
import {ChangeDetectionStrategy, Component, ViewEncapsulation} from '@angular/core';
11+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
1112

1213
/**
1314
* Wrapper for the CdkTable with Material design styles.
@@ -30,4 +31,4 @@ export class MatTable<T> extends CdkTable<T> {
3031
}
3132

3233
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
33-
(MatTable as any)['ctorParameters'] = () => (CdkTable as any)['ctorParameters'];
34+
_inheritCtorParametersMetadata(MatTable, CdkTable);

src/lib/tabs/tab-label.ts

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

99
import {Directive} from '@angular/core';
1010
import {CdkPortal} from '@angular/cdk/portal';
11+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
1112

1213
/** Used to flag tab labels for use with the portal directive */
1314
@Directive({
@@ -16,4 +17,4 @@ import {CdkPortal} from '@angular/cdk/portal';
1617
export class MatTabLabel extends CdkPortal {}
1718

1819
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
19-
(MatTabLabel as any)['ctorParameters'] = () => (CdkPortal as any)['ctorParameters'];
20+
_inheritCtorParametersMetadata(MatTabLabel, CdkPortal);

src/lib/tree/node.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
QueryList,
2020
} from '@angular/core';
2121
import {
22+
_inheritCtorParametersMetadata,
2223
CanDisable, CanDisableCtor,
2324
HasTabIndex,
2425
HasTabIndexCtor,
@@ -77,7 +78,7 @@ export class MatTreeNodeDef<T> extends CdkTreeNodeDef<T> {
7778
}
7879

7980
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
80-
(MatTreeNodeDef as any)['ctorParameters'] = () => (CdkTreeNodeDef as any)['ctorParameters'];
81+
_inheritCtorParametersMetadata(MatTreeNodeDef, CdkTreeNodeDef);
8182

8283
/**
8384
* Wrapper for the CdkTree nested node with Material design styles.

src/lib/tree/padding.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import {CdkTreeNodePadding} from '@angular/cdk/tree';
99
import {Directive, Input} from '@angular/core';
10+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
1011

1112
/**
1213
* Wrapper for the CdkTree padding with Material design styles.
@@ -25,5 +26,4 @@ export class MatTreeNodePadding<T> extends CdkTreeNodePadding<T> {
2526
}
2627

2728
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
28-
(MatTreeNodePadding as any)['ctorParameters'] = () =>
29-
(CdkTreeNodePadding as any)['ctorParameters'];
29+
_inheritCtorParametersMetadata(MatTreeNodePadding, CdkTreeNodePadding);

src/lib/tree/toggle.ts

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

99
import {CdkTreeNodeToggle} from '@angular/cdk/tree';
1010
import {Directive, Input} from '@angular/core';
11+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
1112

1213
/**
1314
* Wrapper for the CdkTree's toggle with Material design styles.
@@ -24,4 +25,4 @@ export class MatTreeNodeToggle<T> extends CdkTreeNodeToggle<T> {
2425
}
2526

2627
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
27-
(MatTreeNodeToggle as any)['ctorParameters'] = () => (CdkTreeNodeToggle as any)['ctorParameters'];
28+
_inheritCtorParametersMetadata(MatTreeNodeToggle, CdkTreeNodeToggle);

src/lib/tree/tree.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import {CdkTree} from '@angular/cdk/tree';
1010
import {ChangeDetectionStrategy, Component, ViewChild, ViewEncapsulation} from '@angular/core';
1111
import {MatTreeNodeOutlet} from './outlet';
12+
import {_inheritCtorParametersMetadata} from '@angular/material/core';
1213

1314
/**
1415
* Wrapper for the CdkTable with Material design styles.
@@ -33,4 +34,4 @@ export class MatTree<T> extends CdkTree<T> {
3334
}
3435

3536
// TODO(devversion): workaround for https://github.com/angular/material2/issues/12760
36-
(MatTree as any)['ctorParameters'] = () => (CdkTree as any)['ctorParameters'];
37+
_inheritCtorParametersMetadata(MatTree, CdkTree);

0 commit comments

Comments
 (0)