-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentifier.ts
216 lines (206 loc) · 4.97 KB
/
identifier.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
export type TsIdentifier = string & { _tsIdentifier: never };
const tsIdentifierFromString = (value: string): TsIdentifier => {
return value as TsIdentifier;
};
/**
* 識別子を文字列から無理矢理でも生成する.
* 空文字だった場合は $00
* 識別子に使えない文字が含まれていた場合, 末尾に_がつくか, $マークでエンコードされる
* @param text
*/
export const identifierFromString = (word: string): TsIdentifier => {
const [firstChar] = word;
if (firstChar === undefined) {
return tsIdentifierFromString("$00");
}
let result =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_".includes(firstChar)
? firstChar
: escapeChar(firstChar);
const slicedWord = word.slice(1);
for (const char of slicedWord) {
result +=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"
.includes(
char,
)
? char
: escapeChar(char);
}
if (reservedByLanguageWordSet.has(word)) {
return tsIdentifierFromString(result + "_");
}
return tsIdentifierFromString(result);
};
const escapeChar = (char: string): string =>
"$" + char.charCodeAt(0).toString(16);
/**
* JavaScriptやTypeScriptによって決められた予約語と、できるだけ使いたくない語
*/
const reservedByLanguageWordSet: ReadonlySet<string> = new Set([
"await",
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"export",
"extends",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"return",
"super",
"switch",
"this",
"throw",
"try",
"typeof",
"var",
"void",
"while",
"with",
"yield",
"let",
"static",
"enum",
"implements",
"package",
"protected",
"interface",
"private",
"public",
"null",
"true",
"false",
"any",
"boolean",
"constructor",
"declare",
"get",
"module",
"require",
"number",
"set",
"string",
"symbol",
"from",
"of",
"as",
"unknown",
"Infinity",
"NaN",
"undefined",
"top",
"closed",
"self",
]);
/**
* 識別子のID
*/
export type IdentifierIndex = number & { _identifierIndex: never };
/** 初期インデックス */
export const initialIdentifierIndex = 0 as IdentifierIndex;
/**
* 識別子を生成する
* @param identifierIndex 識別子を生成するインデックス
* @param reserved 言語の予約語と別に使わない識別子
*/
export const createIdentifier = (
identifierIndex: IdentifierIndex,
reserved: ReadonlySet<string>,
): { identifier: TsIdentifier; nextIdentifierIndex: IdentifierIndex } => {
let index: number = identifierIndex;
while (true) {
const result = createIdentifierByIndex(index);
if (!reserved.has(result) && !reservedByLanguageWordSet.has(result)) {
return {
identifier: tsIdentifierFromString(result),
nextIdentifierIndex: (index + 1) as IdentifierIndex,
};
}
index += 1;
}
};
/**
* indexから識別子を生成する (予約語を考慮しない)
* @param index
*/
const createIdentifierByIndex = (index: number): string => {
const headIdentifierCharTable =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const noHeadIdentifierCharTable = headIdentifierCharTable + "0123456789";
const char = headIdentifierCharTable[index];
if (typeof char === "string") {
return char;
}
let result = "";
let offsetIndex = index - headIdentifierCharTable.length;
while (true) {
const quotient = Math.floor(offsetIndex / noHeadIdentifierCharTable.length);
const first = headIdentifierCharTable[quotient];
const second = noHeadIdentifierCharTable[
offsetIndex % noHeadIdentifierCharTable.length
] as string;
if (typeof first === "string") {
return first + second + result;
}
result = second + result;
offsetIndex = quotient;
}
};
/**
* 識別子として使える文字かどうか調べる。日本語の識別子は使えないものとする
* @param word 識別子として使えるかどうか調べるワード
*/
export const isIdentifier = (word: string): boolean => {
if (!isSafePropertyName(word)) {
return false;
}
return !reservedByLanguageWordSet.has(word);
};
/**
* ```ts
* ({ await: 32 }.await)
* ({ "": "empty"}[""])
* ```
*
* プロパティ名として直接コードで指定できるかどうか
* {@link isIdentifier} とは予約語を指定できるかの面で違う
*/
export const isSafePropertyName = (word: string): boolean => {
const [firstChar] = word;
if (firstChar === undefined) {
return false;
}
if (
!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_".includes(
firstChar,
)
) {
return false;
}
const slicedWord = word.slice(1);
for (const char of slicedWord) {
if (
!"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"
.includes(
char,
)
) {
return false;
}
}
return true;
};