@@ -12,6 +12,14 @@ import * as Operation from "./Operation";
12
12
import * as State from "./State" ;
13
13
import * as Structure from "./structure" ;
14
14
15
+ export interface AddStatementOption {
16
+ /**
17
+ * pathに対して強制的にSchemaを上書きするフラグ
18
+ * TypeAliasが先に登録され、Primitiveな型定義が登録されない問題を解決する
19
+ */
20
+ override ?: boolean ;
21
+ }
22
+
15
23
class Store {
16
24
private state : State . Type ;
17
25
private operator : Structure . OperatorType ;
@@ -49,12 +57,16 @@ class Store {
49
57
statements,
50
58
} ) ;
51
59
}
60
+ private capitalizeFirstLetter ( text : string ) : string {
61
+ return text . charAt ( 0 ) . toUpperCase ( ) + text . slice ( 1 ) ;
62
+ }
52
63
public getRootStatements ( ) : ts . Statement [ ] {
53
64
// Debug Point: 抽象的なデータ構造全体を把握するために出力すると良い
54
65
// fs.writeFileSync("debug/tree.json", JSON.stringify(this.operator.getHierarchy(), null, 2), { encoding: "utf-8" });
55
66
const statements = Def . componentNames . reduce < ts . Statement [ ] > ( ( statements , componentName ) => {
56
67
const treeOfNamespace = this . getChildByPaths ( componentName , "namespace" ) ;
57
68
if ( treeOfNamespace ) {
69
+ treeOfNamespace . name = this . capitalizeFirstLetter ( treeOfNamespace . name ) ;
58
70
return statements . concat ( this . convertNamespace ( treeOfNamespace ) ) ;
59
71
}
60
72
return statements ;
@@ -74,23 +86,32 @@ class Store {
74
86
/**
75
87
* @params path: "components/headers/hoge"
76
88
*/
77
- public addStatement ( path : string , statement : Structure . ComponentParams ) : void {
89
+ public addStatement ( path : string , statement : Structure . ComponentParams , options ?: AddStatementOption ) : void {
78
90
if ( ! path . startsWith ( "components" ) ) {
79
91
throw new UnSupportError ( `componentsから始まっていません。path=${ path } ` ) ;
80
92
}
81
93
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" ] ) ) {
84
96
return ;
85
97
}
86
98
// もしTypeAliasが同じスコープに登録されているかつ、interfaceが新しく追加しようとしている場合、既存のstatementを削除する
87
- if ( this . hasStatement ( targetPath , [ "typeAlias" ] ) && statement . kind === "interface" ) {
99
+ if ( ! ! options ?. override || ( this . hasStatement ( targetPath , [ "typeAlias" ] ) && statement . kind === "interface" ) ) {
88
100
this . operator . remove ( targetPath , "typeAlias" ) ;
89
101
}
90
102
this . operator . set ( targetPath , Structure . createInstance ( statement ) ) ;
91
103
}
92
104
public getStatement < T extends Structure . DataStructure . Kind > ( path : string , kind : T ) : Structure . DataStructure . GetChild < T > | undefined {
93
105
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
+ }
94
115
return this . getChildByPaths ( targetPath , kind ) ;
95
116
}
96
117
public addComponent ( componentName : Def . ComponentName , statement : Structure . ComponentParams ) : void {
0 commit comments