From 90f8a2ceb0966d448545dc6a57ce3d3947e03bc5 Mon Sep 17 00:00:00 2001 From: Julius Walther Date: Thu, 8 Mar 2018 17:52:44 +0100 Subject: [PATCH] refactor(store): allow only one analyzer per styleguide --- src/store/store.ts | 10 ++++------ src/store/styleguide/styleguide.ts | 29 +++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/store/store.ts b/src/store/store.ts index f284cf34a..701cad871 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -1,6 +1,5 @@ import * as FileUtils from 'fs'; import { TypescriptReactAnalyzer } from './styleguide/typescript-react-analyzer/index'; -import { SyntheticAnalyzer } from './styleguide/synthetic-analyzer/index'; import { JsonArray, JsonObject, Persister } from './json'; import * as MobX from 'mobx'; import { IObservableArray } from 'mobx/lib/types/observablearray'; @@ -366,12 +365,11 @@ export class Store { this.currentPage = undefined; const typescriptReactAnalyzer = new TypescriptReactAnalyzer('react'); - const syntheticAnalyzer = new SyntheticAnalyzer('synthetic'); - const styleguide = new Styleguide(`${styleguidePath}/lib/patterns`, [ - typescriptReactAnalyzer, - syntheticAnalyzer - ]); + const styleguide = new Styleguide( + `${styleguidePath}/lib/patterns`, + typescriptReactAnalyzer + ); this.styleguide = styleguide; this.styleguide.load(); diff --git a/src/store/styleguide/styleguide.ts b/src/store/styleguide/styleguide.ts index 1ba1971be..925f3283c 100644 --- a/src/store/styleguide/styleguide.ts +++ b/src/store/styleguide/styleguide.ts @@ -1,15 +1,26 @@ import { Folder } from './utils/folder'; import { Pattern } from '../pattern/pattern'; import { StyleguideAnalyzer } from './styleguide-analyzer'; +import { SyntheticAnalyzer } from './synthetic-analyzer'; + +export interface Analyzers { + default?: StyleguideAnalyzer; + synthetic: StyleguideAnalyzer; + [id: string]: StyleguideAnalyzer | undefined; +} export class Styleguide { - private readonly analyzers: StyleguideAnalyzer[]; + private readonly analyzers: Analyzers; private readonly path: string; private patterns: Map = new Map(); private folders: Folder[] = []; - public constructor(path: string, analyzers?: StyleguideAnalyzer[]) { - this.analyzers = analyzers || []; + public constructor(path: string, analyzer?: StyleguideAnalyzer) { + this.analyzers = { + default: analyzer, + synthetic: new SyntheticAnalyzer('synthetic') + }; + this.path = path; } @@ -17,8 +28,8 @@ export class Styleguide { return this.path; } - public getAnalyzers(): ReadonlyArray { - return this.analyzers; + public getDefaultAnalyzer(): StyleguideAnalyzer | undefined { + return this.analyzers.default; } public getPatterns(): ReadonlyMap { @@ -33,7 +44,13 @@ export class Styleguide { this.patterns = new Map(); this.folders = []; - this.analyzers.forEach(analyzer => { + Object.keys(this.analyzers).forEach(analyzerId => { + const analyzer = this.analyzers[analyzerId]; + + if (analyzer === undefined) { + return; + } + const groupedPatterns = analyzer.analyze(this.path); const patterns = groupedPatterns.flatten();