From 4969c6046b21682d7e52184c60f65e1c6faf5804 Mon Sep 17 00:00:00 2001 From: Bilawal Hameed Date: Thu, 26 Mar 2020 16:34:12 +0100 Subject: [PATCH] [core/apis] moved away from ts decorators --- packages/core/src/api/app/FeatureFlags.tsx | 24 ++++++------ packages/core/src/testUtils/decorators.ts | 45 ---------------------- packages/core/tsconfig.json | 3 +- 3 files changed, 12 insertions(+), 60 deletions(-) delete mode 100644 packages/core/src/testUtils/decorators.ts diff --git a/packages/core/src/api/app/FeatureFlags.tsx b/packages/core/src/api/app/FeatureFlags.tsx index 55558bfc1a..6459b38f0d 100644 --- a/packages/core/src/api/app/FeatureFlags.tsx +++ b/packages/core/src/api/app/FeatureFlags.tsx @@ -17,15 +17,13 @@ import React, { ComponentType, createContext, FC } from 'react'; import { FeatureFlagName } from '../plugin/types'; import { FeatureFlagsApi } from '../apis/definitions/featureFlags'; -import { staticImplements } from '../../testUtils'; // TODO: figure out where to put implementations of APIs, both inside apps // but also in core/separate package. -@staticImplements() -export class FeatureFlags { - private static readonly localStorageKey = 'featureFlags'; +class FeatureFlagsImpl implements FeatureFlagsApi { + private readonly localStorageKey = 'featureFlags'; - private static getUserEnabledFeatureFlags(): Set { + private getUserEnabledFeatureFlags(): Set { if (!('localStorage' in window)) { throw new Error( 'Feature Flags are not supported on browsers without the Local Storage API', @@ -42,9 +40,7 @@ export class FeatureFlags { } } - private static saveUserEnabledFeatureFlags( - flags: Set, - ): void { + private saveUserEnabledFeatureFlags(flags: Set): void { if (!('localStorage' in window)) { throw new Error( 'Feature Flags are not supported on browsers without the Local Storage API', @@ -59,32 +55,34 @@ export class FeatureFlags { ); } - static getItem(name: FeatureFlagName): boolean { + getItem(name: FeatureFlagName): boolean { return this.getUserEnabledFeatureFlags().has(name); } - static enable(name: FeatureFlagName): void { + enable(name: FeatureFlagName): void { const flags = this.getUserEnabledFeatureFlags(); flags.add(name); this.saveUserEnabledFeatureFlags(flags); } - static disable(name: FeatureFlagName): void { + disable(name: FeatureFlagName): void { const flags = this.getUserEnabledFeatureFlags(); flags.delete(name); this.saveUserEnabledFeatureFlags(flags); } - static toggle(name: FeatureFlagName): void { + toggle(name: FeatureFlagName): void { (this.getItem(name) ? this.disable : this.enable).bind(this)(name); } } +export const FeatureFlags = new FeatureFlagsImpl(); + /** * Create a shared React context for Feature Flags. * * This will be used to propagate all available feature flags to - * Backstage components. This enables viewing all of the components. + * Backstage components. This enables viewing all of the available flags. */ export interface FeatureFlagsEntry { name: FeatureFlagName; diff --git a/packages/core/src/testUtils/decorators.ts b/packages/core/src/testUtils/decorators.ts deleted file mode 100644 index 2cea8901b4..0000000000 --- a/packages/core/src/testUtils/decorators.ts +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * This utilizes an experimental TypeScript feature called decorators. - * This was originally added to allow us to expose a FeatureFlags API - * with static methods (for backwards compatibility primarily). It takes - * an existing interface and applies the types to static methods in classes. - * - * @see https://www.typescriptlang.org/docs/handbook/decorators.html - * @example - * interface StaticProps { - * append(name: string, extra: string): string; - * reverse(name: string): string; - * } - * - * @staticImplements() - * class StaticPropsClass { - * static append(name, extra) { - * return `${name}${extra}`; - * } - * - * static reverse(name) { - * return name.reverse(); - * } - * } - */ -export function staticImplements() { - return (constructor: U) => { - constructor; - }; -} diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 4e024e548f..ad43a0b986 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -2,7 +2,6 @@ "extends": "../../tsconfig.json", "include": ["src"], "compilerOptions": { - "noImplicitAny": false, - "experimentalDecorators": true + "noImplicitAny": false } }