From 7f9035e9a6a554865b385e48d454d34ee5ffb83e Mon Sep 17 00:00:00 2001 From: Julius Walther Date: Mon, 12 Mar 2018 16:23:18 +0100 Subject: [PATCH] refactor(store): make PatternType an enum again --- src/component/container/pattern-list.tsx | 2 +- src/component/presentation/react/preview.tsx | 10 +++++----- src/component/preview.ts | 5 +++-- src/store/styleguide/pattern-type.ts | 10 ++++------ src/store/styleguide/pattern.ts | 7 ++++--- src/styleguide-analyzer/styleguide-analyzer.ts | 7 +++++++ .../typescript-react-analyzer.ts | 9 ++++++++- 7 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/component/container/pattern-list.tsx b/src/component/container/pattern-list.tsx index 01a46cc05..13140fe7b 100644 --- a/src/component/container/pattern-list.tsx +++ b/src/component/container/pattern-list.tsx @@ -91,7 +91,7 @@ export class PatternListContainer extends React.Component { } const patternId: string = pattern.getId(); - const patternType: string = pattern.getType(); + const patternType: PatternType = pattern.getType(); - if (patternType === 'synthetic') { + if (patternType === PatternType.Synthetic) { switch (patternId) { case 'text': return pageElement.getPropertyValue('text'); diff --git a/src/component/preview.ts b/src/component/preview.ts index 4c14efe27..9c5ff8988 100644 --- a/src/component/preview.ts +++ b/src/component/preview.ts @@ -1,5 +1,6 @@ import { ipcRenderer } from 'electron'; import { JsonObject } from '../store/json'; +import { PatternType } from '../store/styleguide/pattern-type'; import { renderReact } from './presentation/react/render'; import * as SmoothscrollPolyfill from 'smoothscroll-polyfill'; import { Store } from '../store/store'; @@ -80,12 +81,12 @@ window.onload = () => { } switch (analyzer.getPatternType()) { - case 'react': + case PatternType.React: renderReact(store, highlightElement); break; default: - console.log('No matching rederer found'); + console.log('No matching renderer found'); } }; diff --git a/src/store/styleguide/pattern-type.ts b/src/store/styleguide/pattern-type.ts index cf569cd5a..d7e804ff8 100644 --- a/src/store/styleguide/pattern-type.ts +++ b/src/store/styleguide/pattern-type.ts @@ -1,6 +1,4 @@ -const patternType: { [name: string]: string } = { - SYNTHETIC: 'synthetic', - REACT: 'react' -}; - -export { patternType as PatternType }; +export enum PatternType { + Synthetic = 'synthetic', + React = 'react' +} diff --git a/src/store/styleguide/pattern.ts b/src/store/styleguide/pattern.ts index 463211bf6..1b52d5b07 100644 --- a/src/store/styleguide/pattern.ts +++ b/src/store/styleguide/pattern.ts @@ -1,4 +1,5 @@ import { PatternFolder } from './folder'; +import { PatternType } from './pattern-type'; import { Property } from './property/property'; import { Store } from '../store'; @@ -54,7 +55,7 @@ export class Pattern { /** * The type of the pattern (e.g. react, angular, vue). */ - protected type: string; + protected type: PatternType; /** * Creates a new pattern. @@ -70,7 +71,7 @@ export class Pattern { public constructor( id: string, name: string, - type: string, + type: PatternType, implementationPath: string, exportName?: string ) { @@ -165,7 +166,7 @@ export class Pattern { * Returns the type of the pattern (e.g. react, angular, vue). * @return The type of the pattern (e.g. react, angular, vue). */ - public getType(): string { + public getType(): PatternType { return this.type; } diff --git a/src/styleguide-analyzer/styleguide-analyzer.ts b/src/styleguide-analyzer/styleguide-analyzer.ts index 8cb901bdb..7c1ef3d73 100644 --- a/src/styleguide-analyzer/styleguide-analyzer.ts +++ b/src/styleguide-analyzer/styleguide-analyzer.ts @@ -1,3 +1,4 @@ +import { PatternType } from '../store/styleguide/pattern-type'; import { Styleguide } from '../store/styleguide/styleguide'; /** @@ -12,4 +13,10 @@ export abstract class StyleguideAnalyzer { * @param styleGuide The styleguide to analyze its implementations. */ public abstract analyze(styleGuide: Styleguide): void; + + /** + * Returns the pattern type this analyzer creates. + * @return The pattern type this analyzer creates. + */ + public abstract getPatternType(): PatternType; } diff --git a/src/styleguide-analyzer/typescript-react-analyzer/typescript-react-analyzer.ts b/src/styleguide-analyzer/typescript-react-analyzer/typescript-react-analyzer.ts index 54730957f..2cc2fe106 100644 --- a/src/styleguide-analyzer/typescript-react-analyzer/typescript-react-analyzer.ts +++ b/src/styleguide-analyzer/typescript-react-analyzer/typescript-react-analyzer.ts @@ -83,7 +83,7 @@ export class TypescriptReactAnalyzer extends StyleguideAnalyzer { const pattern = new Pattern( id, name, - PatternType.REACT, + PatternType.React, patternInfo.implementationPath, exportInfo.name ); @@ -197,4 +197,11 @@ export class TypescriptReactAnalyzer extends StyleguideAnalyzer { const directoryName = PathUtils.basename(fileInfo.directory); return exportInfo.name || (baseName !== 'index' ? baseName : directoryName); } + + /** + * @inheritdoc + */ + public getPatternType(): PatternType { + return PatternType.React; + } }