@@ -12,6 +12,14 @@ import * as Operation from "./Operation";
1212import * as State from "./State" ;
1313import * as Structure from "./structure" ;
1414
15+ export interface AddStatementOption {
16+ /**
17+ * pathに対して強制的にSchemaを上書きするフラグ
18+ * TypeAliasが先に登録され、Primitiveな型定義が登録されない問題を解決する
19+ */
20+ override ?: boolean ;
21+ }
22+
1523class Store {
1624 private state : State . Type ;
1725 private operator : Structure . OperatorType ;
@@ -49,12 +57,16 @@ class Store {
4957 statements,
5058 } ) ;
5159 }
60+ private capitalizeFirstLetter ( text : string ) : string {
61+ return text . charAt ( 0 ) . toUpperCase ( ) + text . slice ( 1 ) ;
62+ }
5263 public getRootStatements ( ) : ts . Statement [ ] {
5364 // Debug Point: 抽象的なデータ構造全体を把握するために出力すると良い
5465 // fs.writeFileSync("debug/tree.json", JSON.stringify(this.operator.getHierarchy(), null, 2), { encoding: "utf-8" });
5566 const statements = Def . componentNames . reduce < ts . Statement [ ] > ( ( statements , componentName ) => {
5667 const treeOfNamespace = this . getChildByPaths ( componentName , "namespace" ) ;
5768 if ( treeOfNamespace ) {
69+ treeOfNamespace . name = this . capitalizeFirstLetter ( treeOfNamespace . name ) ;
5870 return statements . concat ( this . convertNamespace ( treeOfNamespace ) ) ;
5971 }
6072 return statements ;
@@ -74,23 +86,32 @@ class Store {
7486 /**
7587 * @params path: "components/headers/hoge"
7688 */
77- public addStatement ( path : string , statement : Structure . ComponentParams ) : void {
89+ public addStatement ( path : string , statement : Structure . ComponentParams , options ?: AddStatementOption ) : void {
7890 if ( ! path . startsWith ( "components" ) ) {
7991 throw new UnSupportError ( `componentsから始まっていません。path=${ path } ` ) ;
8092 }
8193 const targetPath = Path . posix . relative ( "components" , path ) ;
82- // すでにinterfaceとして登録がある場合はスキップ
83- if ( this . hasStatement ( targetPath , [ "interface" ] ) ) {
94+ // すでにinterfaceまたはNAMESPACEとして登録がある場合はスキップ
95+ if ( this . hasStatement ( targetPath , [ "interface" , "namespace" ] ) ) {
8496 return ;
8597 }
8698 // もしTypeAliasが同じスコープに登録されているかつ、interfaceが新しく追加しようとしている場合、既存のstatementを削除する
87- if ( this . hasStatement ( targetPath , [ "typeAlias" ] ) && statement . kind === "interface" ) {
99+ if ( ! ! options ?. override || ( this . hasStatement ( targetPath , [ "typeAlias" ] ) && statement . kind === "interface" ) ) {
88100 this . operator . remove ( targetPath , "typeAlias" ) ;
89101 }
90102 this . operator . set ( targetPath , Structure . createInstance ( statement ) ) ;
91103 }
92104 public getStatement < T extends Structure . DataStructure . Kind > ( path : string , kind : T ) : Structure . DataStructure . GetChild < T > | undefined {
93105 const targetPath = Path . posix . relative ( "components" , path ) ;
106+ // components/schemasの場合
107+ if ( path . split ( "/" ) . length === 2 && kind === "namespace" ) {
108+ const child = this . getChildByPaths ( targetPath , kind ) ;
109+ if ( child ) {
110+ // FIXME Side Effect
111+ child . name = this . capitalizeFirstLetter ( child . name ) ;
112+ }
113+ return child ;
114+ }
94115 return this . getChildByPaths ( targetPath , kind ) ;
95116 }
96117 public addComponent ( componentName : Def . ComponentName , statement : Structure . ComponentParams ) : void {
0 commit comments