Skip to content

Commit 8ae7b18

Browse files
authored
fix: consistently use const enum (#18390)
Plain `enum` declarations generate a bunch of code that can't be minified very well and is included even if the enum isn't used. In most places we use `const enum` which inlines the value on consumption, but we have a few leftovers that are still using plain enums. These changes switch all of the remaining usages (except for one) and add a lint rule to enforce consistency in the future.
1 parent 5e34de2 commit 8ae7b18

File tree

11 files changed

+33
-7
lines changed

11 files changed

+33
-7
lines changed

src/cdk/platform/features/scrolling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
/** The possible ways the browser may handle the horizontal scroll axis in RTL languages. */
10-
export enum RtlScrollAxisType {
10+
export const enum RtlScrollAxisType {
1111
/**
1212
* scrollLeft is 0 when scrolled all the way left and (scrollWidth - clientWidth) when scrolled
1313
* all the way right.

src/cdk/schematics/update-tool/target-version.ts

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

99
/** Possible versions that can be automatically migrated by `ng update`. */
10+
// Used in an `Object.keys` call below so it can't be `const enum`.
11+
// tslint:disable-next-line:prefer-const-enum
1012
export enum TargetVersion {
1113
V6 = 'version 6',
1214
V7 = 'version 7',

src/cdk/testing/test-element.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface ModifierKeys {
2222
// dependency on any particular testing framework here. Instead we'll just maintain this supported
2323
// list of keys and let individual concrete `HarnessEnvironment` classes map them to whatever key
2424
// representation is used in its respective testing framework.
25+
// tslint:disable-next-line:prefer-const-enum Seems like this causes some issues with System.js
2526
export enum TestKey {
2627
BACKSPACE,
2728
TAB,

src/material/checkbox/checkbox.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const MAT_CHECKBOX_CONTROL_VALUE_ACCESSOR: any = {
7070
* Represents the different states that require custom transitions between them.
7171
* @docs-private
7272
*/
73-
export enum TransitionCheckState {
73+
export const enum TransitionCheckState {
7474
/** The initial state of the component before any user interaction. */
7575
Init,
7676
/** The state representing the component when it's becoming checked. */

src/material/core/ripple/ripple-ref.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import {RippleConfig, RippleRenderer} from './ripple-renderer';
1010

1111
/** Possible states for a ripple element. */
12-
export enum RippleState {
12+
export const enum RippleState {
1313
FADING_IN, VISIBLE, FADING_OUT, HIDDEN
1414
}
1515

src/material/schematics/ng-update/upgrade-rules/hammer-gestures-v9/import-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as ts from 'typescript';
1313
// tslint:disable:no-bitwise
1414

1515
/** Enum describing the possible states of an analyzed import. */
16-
enum ImportState {
16+
const enum ImportState {
1717
UNMODIFIED = 0b0,
1818
MODIFIED = 0b10,
1919
ADDED = 0b100,

tools/public_api_guard/cdk/platform.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export declare class PlatformModule {
2626
static ɵmod: i0.ɵɵNgModuleDefWithMeta<PlatformModule, never, never, never>;
2727
}
2828

29-
export declare enum RtlScrollAxisType {
29+
export declare const enum RtlScrollAxisType {
3030
NORMAL = 0,
3131
NEGATED = 1,
3232
INVERTED = 2

tools/public_api_guard/material/checkbox.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export declare class MatCheckboxRequiredValidator extends CheckboxRequiredValida
8181
static ɵfac: i0.ɵɵFactoryDef<MatCheckboxRequiredValidator>;
8282
}
8383

84-
export declare enum TransitionCheckState {
84+
export declare const enum TransitionCheckState {
8585
Init = 0,
8686
Checked = 1,
8787
Unchecked = 2,

tools/public_api_guard/material/core.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ export declare class RippleRenderer {
441441
setupTriggerEvents(elementOrElementRef: HTMLElement | ElementRef<HTMLElement>): void;
442442
}
443443

444-
export declare enum RippleState {
444+
export declare const enum RippleState {
445445
FADING_IN = 0,
446446
VISIBLE = 1,
447447
FADING_OUT = 2,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import * as ts from 'typescript';
2+
import * as Lint from 'tslint';
3+
import * as tsutils from 'tsutils';
4+
5+
/**
6+
* Rule that enforces that we use `const enum` rather than a plain `enum`.
7+
*/
8+
export class Rule extends Lint.Rules.AbstractRule {
9+
apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
10+
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
11+
}
12+
}
13+
14+
class Walker extends Lint.RuleWalker {
15+
visitEnumDeclaration(node: ts.EnumDeclaration) {
16+
if (!tsutils.hasModifier(node.modifiers, ts.SyntaxKind.ConstKeyword)) {
17+
this.addFailureAtNode(node.name, 'Enums should be declared as `const enum`.');
18+
}
19+
20+
super.visitEnumDeclaration(node);
21+
}
22+
}

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"require-breaking-change-version": true,
118118
"class-list-signatures": true,
119119
"no-nested-ternary": true,
120+
"prefer-const-enum": true,
120121
"coercion-types": [true,
121122
["coerceBooleanProperty", "coerceCssPixelValue", "coerceNumberProperty"],
122123
{

0 commit comments

Comments
 (0)