Skip to content

Commit f1cc0c3

Browse files
authored
Merge pull request #427 from narumincho/419-reduce-globalthis
419 reduce globalthis
2 parents 90f5133 + 58ce47a commit f1cc0c3

File tree

13 files changed

+266
-98
lines changed

13 files changed

+266
-98
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import * as expr from "jsr:@narumincho/js-ts-code-generator/expr";
3333
const serverModule: Module = {
3434
definitionList: [
3535
exportDefinitionFunction({
36+
export: true,
3637
isAsync: false,
3738
name: identifierFromString("middleware"),
3839
document: "ミドルウェア",
@@ -122,7 +123,7 @@ console.log(codeAsString);
122123

123124
```ts
124125
/** generated by
125-
* - https://jsr.io/@narumincho/js-ts-code-generator@0.5.0
126+
* - https://jsr.io/@narumincho/js-ts-code-generator@0.6.0
126127
* Do not edit!
127128
*
128129
* @module

collect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const collectInTypeAlias = (
117117
);
118118

119119
const collectInFunctionDefinition = (
120-
function_: d.Function,
120+
function_: d.FunctionDefinition,
121121
rootScopeIdentifierSet: RootScopeIdentifierSet,
122122
): UsedNameAndModulePathSet => {
123123
const parameterNameSet = new Set(
@@ -163,7 +163,7 @@ const collectInFunctionDefinition = (
163163
};
164164

165165
const collectInVariableDefinition = (
166-
variable: d.Variable,
166+
variable: d.VariableDefinition,
167167
rootScopeIdentifierSet: RootScopeIdentifierSet,
168168
): UsedNameAndModulePathSet =>
169169
concatCollectData(

data.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export type Module = {
2323

2424
export type Definition =
2525
| { readonly type: "typeAlias"; readonly typeAlias: TypeAlias }
26-
| { readonly type: "function"; readonly function: Function }
27-
| { readonly type: "variable"; readonly variable: Variable };
26+
| { readonly type: "function"; readonly function: FunctionDefinition }
27+
| { readonly type: "variable"; readonly variable: VariableDefinition };
2828

2929
/**
3030
* JavaScript の 文
@@ -62,6 +62,9 @@ export type Statement =
6262
* TypeAlias. `export type T = {}`
6363
*/
6464
export type TypeAlias = {
65+
/** 外部に公開するか */
66+
readonly export: boolean;
67+
6568
/**
6669
* 名前空間
6770
*/
@@ -87,7 +90,10 @@ export type TypeAlias = {
8790
/**
8891
* 外部に公開する関数
8992
*/
90-
export type Function = {
93+
export type FunctionDefinition = {
94+
/** 外部に公開するか */
95+
readonly export: boolean;
96+
9197
readonly isAsync: boolean;
9298
/**
9399
* 外部に公開する関数の名前
@@ -160,7 +166,7 @@ export type TsExpr =
160166
readonly withTypeArguments: WithTypeArguments;
161167
};
162168

163-
export type Variable = {
169+
export type VariableDefinition = {
164170
/**
165171
* 変数の名前
166172
*/
@@ -177,8 +183,9 @@ export type Variable = {
177183
* 変数の式
178184
*/
179185
readonly expr: TsExpr;
180-
/** 外部に公開しないか */
181-
readonly private?: boolean;
186+
187+
/** 外部に公開するか */
188+
readonly export: boolean;
182189
};
183190

184191
export type TsType =
@@ -512,6 +519,7 @@ export type KeyValue = {
512519
* ラムダ式
513520
*/
514521
export type LambdaExpr = {
522+
readonly isAsync: boolean;
515523
/**
516524
* パラメーターのリスト
517525
*/

deno.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@narumincho/js-ts-code-generator",
3-
"version": "0.5.0",
3+
"version": "0.6.0",
44
"license": "MIT",
55
"exports": {
66
".": "./mod.ts",
@@ -10,6 +10,7 @@
1010
},
1111
"compilerOptions": {
1212
"noUncheckedIndexedAccess": true,
13-
"exactOptionalPropertyTypes": true
13+
"exactOptionalPropertyTypes": true,
14+
"erasableSyntaxOnly": true
1415
}
1516
}

identifier.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ export const identifierFromString = (word: string): TsIdentifier => {
2121
: escapeChar(firstChar);
2222
const slicedWord = word.slice(1);
2323
for (const char of slicedWord) {
24-
result +=
25-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"
26-
.includes(
27-
char,
28-
)
29-
? char
30-
: escapeChar(char);
24+
result += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"
25+
.includes(
26+
char,
27+
)
28+
? char
29+
: escapeChar(char);
3130
}
3231
if (reservedByLanguageWordSet.has(word)) {
3332
return tsIdentifierFromString(result + "_");
@@ -109,6 +108,7 @@ const reservedByLanguageWordSet: ReadonlySet<string> = new Set([
109108
"top",
110109
"closed",
111110
"self",
111+
"globalThis",
112112
]);
113113

114114
/**

mod.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {
44
type TsIdentifier,
55
} from "./identifier.ts";
66
import { collectInCode, type UsedNameAndModulePathSet } from "./collect.ts";
7-
import { toString } from "./toString.ts";
7+
import { moduleToString } from "./toString.ts";
88
import type {
99
CodeType,
1010
Definition,
11-
Function,
11+
FunctionDefinition,
1212
Module,
1313
TypeAlias,
14-
Variable,
14+
VariableDefinition,
1515
} from "./data.ts";
1616
export * from "./identifier.ts";
1717
export * from "./data.ts";
@@ -38,11 +38,10 @@ export const generateCodeAsString = (
3838
// グローバル空間にある名前とimportしたモジュールのパスを集める
3939
const usedNameAndModulePath = collectInCode(code);
4040

41-
return toString(
41+
return moduleToString(
4242
code,
4343
{
4444
moduleMap: createImportedModuleName(usedNameAndModulePath),
45-
usedNameSet: usedNameAndModulePath.usedNameSet,
4645
codeType,
4746
},
4847
generatedByLinks,
@@ -73,13 +72,13 @@ const createImportedModuleName = (
7372
};
7473

7574
export const definitionFunction = (
76-
func: Function,
75+
func: FunctionDefinition,
7776
): Definition => ({ type: "function", function: func });
7877

7978
export const definitionTypeAlias = (
8079
typeAlias: TypeAlias,
8180
): Definition => ({ type: "typeAlias", typeAlias });
8281

8382
export const definitionVariable = (
84-
variable: Variable,
83+
variable: VariableDefinition,
8584
): Definition => ({ type: "variable", variable });

sample.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as expr from "./expr.ts";
1111
const serverModule: Module = {
1212
definitionList: [
1313
definitionFunction({
14+
export: true,
1415
isAsync: false,
1516
name: identifierFromString("middleware"),
1617
document: "ミドルウェア",

test.ts

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const expressResponse: jsTs.TsType = {
3535
const sampleCode: jsTs.Module = {
3636
definitionList: [
3737
jsTs.definitionFunction({
38+
export: true,
3839
isAsync: false,
3940
name: identifierFromString("middleware"),
4041
typeParameterList: [],
@@ -79,6 +80,7 @@ Deno.test("not include revered word", () => {
7980
code: {
8081
definitionList: [
8182
jsTs.definitionFunction({
83+
export: true,
8284
isAsync: false,
8385
name: identifierFromString("new"),
8486
document: "newという名前の関数",
@@ -102,6 +104,7 @@ Deno.test("識別子として使えない文字は, 変更される", () => {
102104
code: {
103105
definitionList: [
104106
jsTs.definitionFunction({
107+
export: true,
105108
isAsync: false,
106109
name: identifierFromString("0name"),
107110
document: "0から始まる識別子",
@@ -140,6 +143,7 @@ Deno.test("escape string literal", () => {
140143
{
141144
type: "variable",
142145
variable: {
146+
export: true,
143147
name: identifierFromString("stringValue"),
144148
document: "文字列リテラルでエスケープしているか調べる",
145149
type: { type: "String" },
@@ -166,6 +170,7 @@ Deno.test("include function parameter name", () => {
166170
const nodeJsCode: jsTs.Module = {
167171
definitionList: [
168172
jsTs.definitionFunction({
173+
export: true,
169174
isAsync: false,
170175
name: identifierFromString("middleware"),
171176
document: "ミドルウェア",
@@ -263,6 +268,7 @@ Deno.test("get array index", () => {
263268
code: {
264269
definitionList: [
265270
jsTs.definitionFunction({
271+
export: true,
266272
isAsync: false,
267273
name: identifierFromString("getZeroIndexElement"),
268274
document: "Uint8Arrayの0番目の要素を取得する",
@@ -327,6 +333,7 @@ Deno.test("type parameter", () => {
327333
code: {
328334
definitionList: [
329335
jsTs.definitionFunction({
336+
export: true,
330337
isAsync: false,
331338
name: identifierFromString("sample"),
332339
document: "",
@@ -407,6 +414,7 @@ Deno.test("object literal return need parenthesis", () => {
407414
code: {
408415
definitionList: [
409416
jsTs.definitionFunction({
417+
export: true,
410418
isAsync: false,
411419
name: identifierFromString("returnObject"),
412420
document: "",
@@ -532,6 +540,7 @@ Deno.test("switch", () => {
532540
{
533541
type: "typeAlias",
534542
typeAlias: {
543+
export: true,
535544
name: identifierFromString("Result"),
536545
document: "Result型",
537546
namespace: [],
@@ -576,6 +585,7 @@ Deno.test("switch", () => {
576585
},
577586
},
578587
jsTs.definitionFunction({
588+
export: true,
579589
isAsync: false,
580590
name: identifierFromString("switchSample"),
581591
document: "switch文のテスト",
@@ -677,6 +687,36 @@ Deno.test("Type Assertion", () => {
677687
codeType: "TypeScript",
678688
});
679689
console.log(codeAsString);
690+
assertMatch(codeAsString, /as Date/u);
691+
});
692+
693+
Deno.test("Type Assertion With globalThis", () => {
694+
const code: jsTs.Module = {
695+
definitionList: [
696+
jsTs.definitionTypeAlias({
697+
export: true,
698+
name: identifierFromString("Date"),
699+
document: "",
700+
namespace: [],
701+
type: { type: "String" },
702+
typeParameterList: [],
703+
}),
704+
],
705+
statementList: [
706+
statement.evaluateExpr({
707+
type: "TypeAssertion",
708+
typeAssertion: {
709+
expr: expr.objectLiteral([]),
710+
type: type.Date,
711+
},
712+
}),
713+
],
714+
};
715+
const codeAsString = jsTs.generateCodeAsString({
716+
code,
717+
codeType: "TypeScript",
718+
});
719+
console.log(codeAsString);
680720
assertMatch(codeAsString, /as globalThis.Date/u);
681721
});
682722

@@ -686,6 +726,7 @@ Deno.test("Type Intersection", () => {
686726
{
687727
type: "typeAlias",
688728
typeAlias: {
729+
export: true,
689730
name: identifierFromString("SampleIntersectionType"),
690731
document: "",
691732
namespace: [],
@@ -707,7 +748,7 @@ Deno.test("Type Intersection", () => {
707748
codeType: "TypeScript",
708749
});
709750
console.log(codeAsString);
710-
assertMatch(codeAsString, /globalThis.Date & globalThis.Uint8Array/u);
751+
assertMatch(codeAsString, /Date & Uint8Array/u);
711752
});
712753

713754
Deno.test("object literal spread syntax", () => {
@@ -764,6 +805,7 @@ Deno.test("type property document", () => {
764805
{
765806
type: "typeAlias",
766807
typeAlias: {
808+
export: true,
767809
name: identifierFromString("Time"),
768810
document: "初期のdefinyで使う時間の内部表現",
769811
namespace: [],
@@ -842,6 +884,7 @@ Deno.test("output lambda type parameter", () => {
842884
expr: {
843885
type: "Lambda",
844886
lambdaExpr: {
887+
isAsync: false,
845888
parameterList: [
846889
{
847890
name: identifierFromString("input"),
@@ -894,6 +937,7 @@ Deno.test("output optional type member", () => {
894937
{
895938
type: "variable",
896939
variable: {
940+
export: true,
897941
name: identifierFromString("value"),
898942
document: "年齢があってもなくてもいいやつ",
899943
type: type.object([
@@ -930,6 +974,7 @@ Deno.test("read me code", () => {
930974
const serverCode: jsTs.Module = {
931975
definitionList: [
932976
jsTs.definitionFunction({
977+
export: true,
933978
isAsync: false,
934979
name: identifierFromString("middleware"),
935980
document: "ミドルウェア",
@@ -1020,7 +1065,7 @@ Deno.test("read me code", () => {
10201065
codeType: "TypeScript",
10211066
}),
10221067
`/** generated by
1023-
* - https://jsr.io/@narumincho/js-ts-code-generator@0.5.0
1068+
* - https://jsr.io/@narumincho/js-ts-code-generator@0.6.0
10241069
* Do not edit!
10251070
*
10261071
* @module
@@ -1051,6 +1096,7 @@ Deno.test("import name", () => {
10511096
{
10521097
type: "variable",
10531098
variable: {
1099+
export: true,
10541100
name: identifierFromString("a"),
10551101
document: "a",
10561102
expr: expr.stringLiteral("aaa"),
@@ -1077,7 +1123,7 @@ Deno.test("import name", () => {
10771123
assertEquals(
10781124
codeAsString,
10791125
`/** generated by
1080-
* - https://jsr.io/@narumincho/js-ts-code-generator@0.5.0
1126+
* - https://jsr.io/@narumincho/js-ts-code-generator@0.6.0
10811127
* Do not edit!
10821128
*
10831129
* @module

0 commit comments

Comments
 (0)